Skip to content

Commit 0cddb23

Browse files
Rewrite all occurrences of * in a CSS variable name (#1256)
Fixes #1248
1 parent 07da721 commit 0cddb23

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

packages/tailwindcss-language-server/src/language/css-server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class CssServer {
225225
if (match) {
226226
symbol.name = `${match[1]} ${match[2]?.trim() ?? match[3]?.trim()}`
227227
}
228-
} else if (symbol.name === `.placeholder`) {
228+
} else if (/^\._+$/.test(symbol.name)) {
229229
let doc = documents.get(symbol.location.uri)
230230
let text = doc.getText(symbol.location.range)
231231
let match = text.trim().match(/^(@[^\s]+)(?:([^{]+)[{]|([^;{]+);)/)

packages/tailwindcss-language-server/src/language/rewriting.test.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,22 @@ test('@utility', () => {
5050
'@utility foo-* {',
5151
' color: red;',
5252
'}',
53+
'@utility bar-* {',
54+
' color: --value(--font-*-line-height);',
55+
'}',
5356
]
5457

5558
let output = [
5659
//
57-
'.placeholder {', // wrong
60+
'._______ {',
5861
' color: red;',
5962
'}',
60-
'.placeholder {', // wrong
63+
'._______ {',
6164
' color: red;',
6265
'}',
66+
'._______ {',
67+
' color: --value(--font-_-line-height);',
68+
'}',
6369
]
6470

6571
expect(rewriteCss(input.join('\n'))).toBe(output.join('\n'))
@@ -70,25 +76,33 @@ test('@theme', () => {
7076
//
7177
'@theme {',
7278
' --color: red;',
79+
' --*: initial;',
80+
' --text*: initial;',
7381
' --font-*: initial;',
7482
' --font-weight-*: initial;',
7583
'}',
7684
'@theme inline reference static default {',
7785
' --color: red;',
86+
' --*: initial;',
87+
' --text*: initial;',
7888
' --font-*: initial;',
7989
' --font-weight-*: initial;',
8090
'}',
8191
]
8292

8393
let output = [
8494
//
85-
'.placeholder {', // wrong
95+
'._____ {',
8696
' --color: red;',
97+
' --_: initial;',
98+
' --text_: initial;',
8799
' --font-_: initial;',
88100
' --font-weight-_: initial;',
89101
'}',
90-
'.placeholder {', // wrong
102+
'._____ {',
91103
' --color: red;',
104+
' --_: initial;',
105+
' --text_: initial;',
92106
' --font-_: initial;',
93107
' --font-weight-_: initial;',
94108
'}',
@@ -110,8 +124,8 @@ test('@custom-variant', () => {
110124

111125
let output = [
112126
//
113-
'@media (℘) {}', // wrong
114-
'.placeholder {', // wrong
127+
'@media(℘) {}',
128+
'.______________ {',
115129
' &:hover {',
116130
' @slot;',
117131
' }',
@@ -133,7 +147,7 @@ test('@variant', () => {
133147

134148
let output = [
135149
//
136-
'.placeholder {', // wrong
150+
'._______ {',
137151
' &:hover {',
138152
' @slot;',
139153
' }',

packages/tailwindcss-language-server/src/language/rewriting.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ function replaceWithAtRule(delta = 0) {
1414
}
1515

1616
function replaceWithStyleRule(delta = 0) {
17-
return (_match: string, p1: string) => {
17+
return (_match: string, name: string, p1: string) => {
18+
let className = '_'.repeat(name.length)
1819
let spaces = ' '.repeat(p1.length + delta)
19-
return `.placeholder${spaces}{`
20+
return `.${className}${spaces}{`
2021
}
2122
}
2223

@@ -36,16 +37,16 @@ export function rewriteCss(css: string) {
3637
css = css.replace(/@screen(\s+[^{]+){/g, replaceWithAtRule(-2))
3738
css = css.replace(/@variants(\s+[^{]+){/g, replaceWithAtRule())
3839
css = css.replace(/@responsive(\s*){/g, replaceWithAtRule())
39-
css = css.replace(/@utility(\s+[^{]+){/g, replaceWithStyleRule())
40-
css = css.replace(/@theme(\s+[^{]*){/g, replaceWithStyleRule())
40+
css = css.replace(/@(utility)(\s+[^{]+){/g, replaceWithStyleRule())
41+
css = css.replace(/@(theme)(\s+[^{]*){/g, replaceWithStyleRule())
4142

42-
css = css.replace(/@custom-variant(\s+[^;{]+);/g, (match: string) => {
43-
let spaces = ' '.repeat(match.length - 11)
44-
return `@media (${MEDIA_MARKER})${spaces}{}`
43+
css = css.replace(/@(custom-variant)(\s+[^;{]+);/g, (match: string, name: string) => {
44+
let spaces = ' '.repeat(match.length - name.length + 3)
45+
return `@media(${MEDIA_MARKER})${spaces}{}`
4546
})
4647

47-
css = css.replace(/@custom-variant(\s+[^{]+){/g, replaceWithStyleRule())
48-
css = css.replace(/@variant(\s+[^{]+){/g, replaceWithStyleRule())
48+
css = css.replace(/@(custom-variant)(\s+[^{]+){/g, replaceWithStyleRule())
49+
css = css.replace(/@(variant)(\s+[^{]+){/g, replaceWithStyleRule())
4950
css = css.replace(/@layer(\s+[^{]{2,}){/g, replaceWithAtRule(-3))
5051
css = css.replace(/@reference\s*([^;]{2,})/g, '@import $1')
5152

@@ -73,8 +74,13 @@ export function rewriteCss(css: string) {
7374
return match.replace(/[*]/g, '_')
7475
})
7576

77+
// Replace `--*` with `--_`
78+
// Replace `--some-var*` with `--some-var_`
7679
// Replace `--some-var-*` with `--some-var-_`
77-
css = css.replace(/--([a-zA-Z0-9-]+)-[*]/g, '--$1-_')
80+
// Replace `--text-*-line-height` with `--text-_-line-height`
81+
css = css.replace(/--([a-zA-Z0-9-*]*)/g, (match) => {
82+
return match.replace(/[*]/g, '_')
83+
})
7884

7985
return css
8086
}

packages/tailwindcss-language-server/tests/css/css-server.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defineTest({
3838
uri: '{workspace:default}/file-1.css',
3939
range: {
4040
start: { line: 1, character: 0 },
41-
end: { line: 1, character: 31 },
41+
end: { line: 1, character: 30 },
4242
},
4343
},
4444
},

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- v4: Support loading bundled versions of some first-party plugins ([#1240](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1240))
66
- Cancel initial file search if it takes too long ([#1242](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1242))
77
- LSP: Don’t throw when the client does not provide `textDocument` in capabilities ([#1252](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1252))
8+
- v4: Allow `*` anywhere in a CSS variable name ([#1256](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1256))
89

910
# 0.14.8
1011

0 commit comments

Comments
 (0)