Skip to content

Commit b10aba1

Browse files
Fix errors around using @import with layer() and @layer in Tailwind CSS Language mode (#1097)
Fixes #1095 Fixes #1096 Fixes #1098
1 parent cc8dd1c commit b10aba1

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

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

+24-15
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,29 @@ function replace(delta = 0) {
337337

338338
function createVirtualCssDocument(textDocument: TextDocument): TextDocument {
339339
let content = textDocument
340-
.getText()
341-
.replace(/@screen(\s+[^{]+){/g, replace(-2))
342-
.replace(/@variants(\s+[^{]+){/g, replace())
343-
.replace(/@responsive(\s*){/g, replace())
344-
.replace(/@layer(\s+[^{]{2,}){/g, replace(-3))
345-
.replace(
346-
/@media(\s+screen\s*\([^)]+\))/g,
347-
(_match, screen) => `@media (${MEDIA_MARKER})${' '.repeat(screen.length - 4)}`,
348-
)
340+
.getText()
341+
342+
// Remove inline `@layer` directives
343+
// TODO: This should be unnecessary once we have updated the bundled CSS
344+
// language service
345+
.replace(/@layer\s+[^;{]+(;|$)/g, '')
346+
347+
.replace(/@screen(\s+[^{]+){/g, replace(-2))
348+
.replace(/@variants(\s+[^{]+){/g, replace())
349+
.replace(/@responsive(\s*){/g, replace())
350+
.replace(/@layer(\s+[^{]{2,}){/g, replace(-3))
351+
.replace(
352+
/@media(\s+screen\s*\([^)]+\))/g,
353+
(_match, screen) => `@media (${MEDIA_MARKER})${' '.repeat(screen.length - 4)}`,
354+
)
355+
.replace(/@import\s*("(?:[^"]+)"|'(?:[^']+)')\s*(.*?)(?=;|$)/g, (_match, url, other) => {
349356
// Remove`source(…)`, `theme(…)`, and `prefix(…)` from `@import`s
350357
// otherwise we'll show syntax-error diagnostics which we don't want
351-
.replace(
352-
/@import\s*("(?:[^"]+)"|'(?:[^']+)')\s*((source|theme|prefix)\([^)]+\)\s*)+/g,
353-
(_match, url) => `@import "${url.slice(1, -1)}"`,
354-
)
355-
.replace(/(?<=\b(?:theme|config)\([^)]*)[.[\]]/g, '_')
358+
other = other.replace(/((source|theme|prefix)\([^)]+\)\s*)+?/g, '')
359+
360+
return `@import "${url.slice(1, -1)}" ${other}`
361+
})
362+
.replace(/(?<=\b(?:theme|config)\([^)]*)[.[\]]/g, '_')
356363

357364
return TextDocument.create(
358365
textDocument.uri,
@@ -389,7 +396,9 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
389396
.filter((diagnostic) => {
390397
if (
391398
diagnostic.code === 'unknownAtRules' &&
392-
/Unknown at rule @(tailwind|apply|config|theme|plugin|source|utility|variant)/.test(diagnostic.message)
399+
/Unknown at rule @(tailwind|apply|config|theme|plugin|source|utility|variant)/.test(
400+
diagnostic.message,
401+
)
393402
) {
394403
return false
395404
}

packages/vscode-tailwindcss/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
## Prerelease
44

55
- Reload variants when editing the theme in v4 ([#1094](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1094))
6+
- Don't show syntax errors when imports contain `layer(…)` and `source(…)` ([#1095](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1095))
7+
- Don't show syntax errors when document contains an `@layer` statement ([#1095](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1095))
8+
- Correct syntax highlighting when imports contain `layer(…)` ([#1095](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1095))
69

710
## 0.12.14
811

packages/vscode-tailwindcss/syntaxes/at-rules.tmLanguage.json

+31-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
{
3838
"include": "source.css#url"
3939
},
40+
{
41+
"include": "#layer-fn"
42+
},
4043
{
4144
"include": "#source-fn"
4245
},
@@ -522,7 +525,7 @@
522525
"patterns": [
523526
{
524527
"match": "none(?=[)])",
525-
"name": "variable.other.css"
528+
"name": "support.constant.none.css"
526529
},
527530
{
528531
"include": "source.css#string"
@@ -531,6 +534,33 @@
531534
}
532535
]
533536
},
537+
"layer-fn": {
538+
"patterns": [
539+
{
540+
"begin": "(?i)(?:\\s*)(?<![\\w@-])(layer)([(])",
541+
"beginCaptures": {
542+
"1": {
543+
"name": "support.function.layer.css"
544+
},
545+
"2": {
546+
"name": "punctuation.section.function.begin.bracket.round.css"
547+
}
548+
},
549+
"end": "[)]",
550+
"endCaptures": {
551+
"0": {
552+
"name": "punctuation.section.function.end.bracket.round.css"
553+
}
554+
},
555+
"patterns": [
556+
{
557+
"match": "\\s*([a-zA-Z][a-zA-Z0-9.]+|\\\\(?:[0-9a-fA-F]{1,6}))?\\s*",
558+
"name": "support.constant.layer-name.css"
559+
}
560+
]
561+
}
562+
]
563+
},
534564
"theme-meta-fn": {
535565
"patterns": [
536566
{

0 commit comments

Comments
 (0)