Skip to content

Commit 0ab91c4

Browse files
committed
wip
1 parent a4aeab8 commit 0ab91c4

File tree

14 files changed

+269
-319
lines changed

14 files changed

+269
-319
lines changed

packages/tailwindcss-language-service/src/util/color.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as jit from './jit'
88
import * as culori from 'culori'
99
import namedColors from 'color-name'
1010
import postcss from 'postcss'
11-
import { replaceCssVarsWithFallbacks } from './css-vars'
11+
import { replaceCssVarsWithFallbacks } from './rewriting'
1212

1313
const COLOR_PROPS = [
1414
'accent-color',
@@ -100,9 +100,11 @@ function getColorFromDecls(
100100

101101
const propsToCheck = areAllCustom ? props : nonCustomProps
102102

103-
const colors = propsToCheck.flatMap((prop) => ensureArray(decls[prop]).flatMap((str) => {
104-
return getColorsInString(state, str)
105-
}))
103+
const colors = propsToCheck.flatMap((prop) =>
104+
ensureArray(decls[prop]).flatMap((str) => {
105+
return getColorsInString(state, str)
106+
}),
107+
)
106108

107109
// check that all of the values are valid colors
108110
// if (colors.some((color) => color instanceof TinyColor && !color.isValid)) {

packages/tailwindcss-language-service/src/util/css-calc.test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/tailwindcss-language-service/src/util/css-calc.ts

Lines changed: 0 additions & 116 deletions
This file was deleted.

packages/tailwindcss-language-service/src/util/css-replacements.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/tailwindcss-language-service/src/util/css-vars.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

packages/tailwindcss-language-service/src/util/equivalents.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,60 +28,3 @@ export function addEquivalents(css: string, settings: TailwindCssSettings): stri
2828

2929
return applyComments(css, comments)
3030
}
31-
32-
import type { Plugin } from 'postcss'
33-
import { inlineCalc } from './css-calc'
34-
35-
export function wip(state: State, comments: Comment[], settings: TailwindCssSettings): Plugin {
36-
return {
37-
postcssPlugin: 'plugin',
38-
Declaration(decl) {
39-
let result = inlineCalc(state, decl.value)
40-
if (result === decl.value) return
41-
42-
comments.push({
43-
index: decl.source.end.offset,
44-
value: result,
45-
})
46-
},
47-
}
48-
}
49-
50-
export function addThemeValues(state: State, settings: TailwindCssSettings) {
51-
root.walkDecls((decl) => {
52-
let result = inlineCalc(state, decl.value)
53-
if (result === decl.value) return
54-
55-
comments.push({
56-
index: decl.source.end.offset,
57-
value: result,
58-
})
59-
})
60-
61-
// Add fallbacks to variables with their theme values
62-
// Ideally these would just be commentss like
63-
// `var(--foo) /* 3rem = 48px */` or
64-
// `calc(var(--spacing) * 5) /* 1.25rem = 20px */`
65-
css = replaceCssVars(css, (name) => {
66-
if (!name.startsWith('--')) return null
67-
68-
let value = state.designSystem.resolveThemeValue?.(name) ?? null
69-
if (value === null) return null
70-
71-
let comment = ''
72-
73-
let color = getEquivalentColor(value)
74-
if (color !== value) {
75-
comment = ` /* ${value} = ${color} */`
76-
} else {
77-
let pixels = addPixelEquivalentsToValue(value, settings.rootFontSize, false)
78-
if (pixels !== value) {
79-
comment = ` /* ${value} = ${pixels} */`
80-
}
81-
}
82-
83-
return `var(${name})${comment}`
84-
})
85-
86-
return css
87-
}

packages/tailwindcss-language-service/src/util/jit.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import type { State, TailwindCssSettings } from './state'
22
import type { Container, Document, Root, Rule, Node, AtRule } from 'postcss'
33
import { addPixelEquivalentsToValue } from './pixelEquivalents'
44
import { addEquivalents } from './equivalents'
5-
import { replaceCssVars, replaceCssVarsWithFallbacks } from './css-vars'
6-
import { addColorEquivalentToValue, getEquivalentColor } from './colorEquivalents'
7-
import { evaluateExpression, inlineCalc, replaceCssCalc } from './css-calc'
8-
import { applyComments, Comment } from './comments'
9-
import { addThemeValues } from './add-theme-values'
5+
import { addThemeValues } from './rewriting'
106

117
export function bigSign(bigIntValue) {
128
// @ts-ignore

packages/tailwindcss-language-service/src/util/add-theme-values.test.ts renamed to packages/tailwindcss-language-service/src/util/rewriting/add-theme-values.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from 'vitest'
2-
import { State, TailwindCssSettings } from './state'
3-
import { DesignSystem } from './v4'
2+
import { State, TailwindCssSettings } from '../state'
3+
import { DesignSystem } from '../v4'
44
import { addThemeValues } from './add-theme-values'
55

66
test('Inlicing calc expressions using the design system', () => {

packages/tailwindcss-language-service/src/util/add-theme-values.ts renamed to packages/tailwindcss-language-service/src/util/rewriting/add-theme-values.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { State, TailwindCssSettings } from './state'
1+
import type { State, TailwindCssSettings } from '../state'
22

3-
import { evaluateExpression, replaceCssCalc } from './css-calc'
4-
import { replaceCssVars } from './css-vars'
5-
import { addPixelEquivalentsToValue } from './pixelEquivalents'
3+
import { evaluateExpression } from './calc'
4+
import { replaceCssVars, replaceCssCalc } from './replacements'
5+
import { addPixelEquivalentsToValue } from '../pixelEquivalents'
66

77
export function addThemeValues(css: string, state: State, settings: TailwindCssSettings) {
88
// TODO: Add fallbacks to variables with their theme values
@@ -11,7 +11,7 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
1111
// `calc(var(--spacing) * 5) /* 1.25rem = 20px */`
1212

1313
css = replaceCssCalc(css, (expr) => {
14-
let inlined = replaceCssVars(expr, (name) => {
14+
let inlined = replaceCssVars(expr.value, ({ name }) => {
1515
if (!name.startsWith('--')) return null
1616

1717
let value = state.designSystem.resolveThemeValue?.(name) ?? null
@@ -23,7 +23,7 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
2323
let evaluated = evaluateExpression(inlined)
2424

2525
// No changes were made so we can just return the original expression
26-
if (expr === evaluated) return expr
26+
if (expr.value === evaluated) return expr.value
2727

2828
let equiv = addPixelEquivalentsToValue(evaluated, settings.rootFontSize, false)
2929
if (equiv !== evaluated) {
@@ -33,7 +33,7 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
3333
return `calc(${expr}) /* ${evaluated} */`
3434
})
3535

36-
css = replaceCssVars(css, (name) => {
36+
css = replaceCssVars(css, ({ name }) => {
3737
if (!name.startsWith('--')) return null
3838

3939
let value = state.designSystem.resolveThemeValue?.(name) ?? null

0 commit comments

Comments
 (0)