Skip to content

Commit 3742366

Browse files
committed
feat: add tests for invalid class diagnostics and update related logic
1 parent 535429c commit 3742366

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

packages/tailwindcss-language-server/tests/diagnostics/diagnostics.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ withFixture('basic', (c) => {
3737
testFixture('css-conflict/vue-style-lang-sass')
3838
testFixture('invalid-screen/simple')
3939
testFixture('invalid-theme/simple')
40+
testFixture('invalid-class/simple')
4041
})
4142

4243
withFixture('v4/basic', (c) => {
@@ -88,6 +89,7 @@ withFixture('v4/basic', (c) => {
8889
// testFixture('css-conflict/css-multi-rule')
8990
// testFixture('css-conflict/css-multi-prop')
9091
// testFixture('invalid-screen/simple')
92+
testFixture('invalid-class/simple')
9193

9294
testInline('simple typos in theme keys (in key)', {
9395
code: '.test { color: theme(--color-red-901) }',
@@ -324,6 +326,17 @@ withFixture('v4/basic', (c) => {
324326
},
325327
],
326328
},
329+
{
330+
code: 'invalidClass',
331+
source: 'tailwindcss',
332+
message: "Unknown utility class 'foo'.",
333+
className: {
334+
className: 'foo',
335+
classList: {
336+
classList: 'foo max-w-4xl max-w-6xl hover:underline',
337+
},
338+
},
339+
},
327340
],
328341
})
329342

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"code": "<div class=\"px-4 nonexistent-class\"></div>",
3+
"expected": [
4+
{
5+
"code": "invalidClass",
6+
"source": "tailwindcss",
7+
"className": {
8+
"className": "nonexistent-class",
9+
"classList": {
10+
"classList": "px-4 nonexistent-class",
11+
"range": {
12+
"start": { "line": 0, "character": 12 },
13+
"end": { "line": 0, "character": 34 }
14+
}
15+
},
16+
"relativeRange": {
17+
"start": { "line": 0, "character": 5 },
18+
"end": { "line": 0, "character": 22 }
19+
},
20+
"range": { "start": { "line": 0, "character": 17 }, "end": { "line": 0, "character": 34 } }
21+
},
22+
"range": { "start": { "line": 0, "character": 17 }, "end": { "line": 0, "character": 34 } },
23+
"severity": 3,
24+
"message": "Unknown utility class 'nonexistent-class'."
25+
}
26+
]
27+
}

packages/tailwindcss-language-service/src/diagnostics/getInvalidClassDiagnostics.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@ import { getClassNameDecls } from '../util/getClassNameDecls'
66
import * as jit from '../util/jit'
77
import type { TextDocument } from 'vscode-languageserver-textdocument'
88

9+
function isCustomProperty(property: string): boolean {
10+
return property.startsWith('--')
11+
}
12+
913
function isClassValid(state: State, className: string): boolean {
1014
if (state.v4) {
1115
// V4: Use design system compilation
1216
let roots = state.designSystem.compile([className])
1317
let hasDeclarations = false
18+
let hasNonCustomProperties = false
1419

1520
visit([roots[0]], (node) => {
1621
if ((node.type === 'rule' || node.type === 'atrule') && node.nodes) {
1722
for (let child of node.nodes) {
1823
if (child.type === 'decl') {
1924
hasDeclarations = true
20-
break
25+
if (!isCustomProperty(child.prop)) {
26+
hasNonCustomProperties = true
27+
}
2128
}
2229
}
2330
}
2431
})
25-
return hasDeclarations
32+
return hasDeclarations && hasNonCustomProperties
2633
} else if (state.jit) {
2734
// JIT: Try to generate rules
2835
let { rules } = jit.generateRules(state, [className])
@@ -61,7 +68,7 @@ export async function getInvalidClassDiagnostics(
6168
: severity === 'warning'
6269
? 2 /* DiagnosticSeverity.Warning */
6370
: 3 /* DiagnosticSeverity.Information */,
64-
message: `'${className.className}' is not a recognized Tailwind CSS utility class.`,
71+
message: `Unknown utility class '${className.className}'.`,
6572
})
6673
}
6774
})

0 commit comments

Comments
 (0)