Skip to content

Commit 2e92af0

Browse files
committed
Handle projects with a configured prefix
1 parent c5f2652 commit 2e92af0

File tree

6 files changed

+136
-2
lines changed

6 files changed

+136
-2
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,102 @@ withFixture('v4/basic', (c) => {
155155
expected: [],
156156
})
157157
})
158+
159+
withFixture('v4/with-prefix', (c) => {
160+
function testInline(fixture, { code, expected, language = 'html' }) {
161+
test(fixture, async () => {
162+
let promise = new Promise((resolve) => {
163+
c.onNotification('textDocument/publishDiagnostics', ({ diagnostics }) => {
164+
resolve(diagnostics)
165+
})
166+
})
167+
168+
let doc = await c.openDocument({ text: code, lang: language })
169+
let diagnostics = await promise
170+
171+
expected = JSON.parse(JSON.stringify(expected).replaceAll('{{URI}}', doc.uri))
172+
173+
expect(diagnostics).toEqual(expected)
174+
})
175+
}
176+
177+
// testFixture('css-conflict/simple')
178+
// testFixture('css-conflict/variants-negative')
179+
// testFixture('css-conflict/variants-positive')
180+
// testFixture('css-conflict/jsx-concat-negative')
181+
// testFixture('css-conflict/jsx-concat-positive')
182+
// testFixture('css-conflict/vue-style-lang-sass')
183+
184+
// testFixture('css-conflict/css')
185+
// testFixture('css-conflict/css-multi-rule')
186+
// testFixture('css-conflict/css-multi-prop')
187+
// testFixture('invalid-screen/simple')
188+
189+
testInline('simple typos in theme keys (in key)', {
190+
code: '.test { color: theme(--color-red-901) }',
191+
language: 'css',
192+
expected: [
193+
{
194+
code: 'invalidConfigPath',
195+
range: { start: { line: 0, character: 21 }, end: { line: 0, character: 36 } },
196+
severity: 1,
197+
message: "'--color-red-901' does not exist in your theme. Did you mean '--color-red-900'?",
198+
suggestions: ['--color-red-900'],
199+
},
200+
],
201+
})
202+
203+
testInline('simple typos in theme keys (in namespace)', {
204+
code: '.test { color: theme(--colors-red-901) }',
205+
language: 'css',
206+
expected: [
207+
{
208+
code: 'invalidConfigPath',
209+
range: { start: { line: 0, character: 21 }, end: { line: 0, character: 37 } },
210+
severity: 1,
211+
message: "'--colors-red-901' does not exist in your theme. Did you mean '--color-red-900'?",
212+
suggestions: ['--color-red-900'],
213+
},
214+
],
215+
})
216+
217+
testInline('No similar theme key exists', {
218+
code: '.test { color: theme(--font-obliqueness-90) }',
219+
language: 'css',
220+
expected: [
221+
{
222+
code: 'invalidConfigPath',
223+
range: { start: { line: 0, character: 21 }, end: { line: 0, character: 42 } },
224+
severity: 1,
225+
message: "'--font-obliqueness-90' does not exist in your theme.",
226+
suggestions: [],
227+
},
228+
],
229+
})
230+
231+
testInline('valid theme keys dont issue diagnostics', {
232+
code: '.test { color: theme(--color-red-900) }',
233+
language: 'css',
234+
expected: [],
235+
})
236+
237+
testInline('types in legacy theme config paths', {
238+
code: '.test { color: theme(colors.red.901) }',
239+
language: 'css',
240+
expected: [
241+
{
242+
code: 'invalidConfigPath',
243+
range: { start: { line: 0, character: 21 }, end: { line: 0, character: 35 } },
244+
severity: 1,
245+
message: "'colors.red.901' does not exist in your theme config.",
246+
suggestions: [],
247+
},
248+
],
249+
})
250+
251+
testInline('valid legacy theme config paths', {
252+
code: '.test { color: theme(colors.red.900) }',
253+
language: 'css',
254+
expected: [],
255+
})
256+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import 'tailwindcss';
2+
3+
@theme prefix(tw) {
4+
--color-potato: #907a70;
5+
}

packages/tailwindcss-language-server/tests/fixtures/v4/with-prefix/package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"tailwindcss": "^4.0.0-alpha.30"
4+
}
5+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ export function getInvalidConfigPathDiagnostics(
207207
}
208208

209209
function resolveThemeValue(design: DesignSystem, path: string) {
210-
let candidate = `[--custom:theme(${path})]`
210+
let prefix = design.theme.prefix ?? null
211+
let candidate = prefix ? `${prefix}:[--custom:theme(${path})]` : `[--custom:theme(${path})]`
211212

212213
// Compile a dummy candidate that uses the theme function with the given path.
213214
//
@@ -230,7 +231,12 @@ function resolveThemeValue(design: DesignSystem, path: string) {
230231
function resolveKnownThemeKeys(design: DesignSystem): string[] {
231232
let validThemeKeys = Array.from(design.theme.entries(), ([key]) => key)
232233

233-
return validThemeKeys
234+
let prefixLength = design.theme.prefix?.length ?? 0
235+
236+
return prefixLength > 0
237+
? // Strip the configured prefix from the list of valid theme keys
238+
validThemeKeys.map((key) => `--${key.slice(prefixLength + 3)}`)
239+
: validThemeKeys
234240
}
235241

236242
function validateV4ThemePath(state: State, path: string): ValidationResult {

packages/tailwindcss-language-service/src/util/v4/design-system.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { Rule } from './ast'
33
import type { NamedVariant } from './candidate'
44

55
export interface Theme {
6+
// Prefix didn't exist on
7+
prefix?: string
68
entries(): [string, any][]
79
}
810

0 commit comments

Comments
 (0)