Skip to content

Commit bd1fc8c

Browse files
Add test for classAttributes matching variable names in JS(X)/TS(X) (#1315)
Welp… this was 100% a bug that this behavior got introduced but at this point it’s shipped and people might rely on it or otherwise want this behavior. As such I’m explicitly encoding this in a test so it doesn’t break later when the big parsing refactor lands.
1 parent 92ea52f commit bd1fc8c

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

packages/tailwindcss-language-service/src/util/find.test.ts

+110-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from 'vitest'
22
import { findClassListsInHtmlRange } from './find'
3-
import { js, html, createDocument } from './test-utils'
3+
import { js, html, pug, createDocument } from './test-utils'
44

55
test('class regex works in astro', async ({ expect }) => {
66
let file = createDocument({
@@ -682,3 +682,112 @@ test('classFunctions should only match in JS-like contexts', async ({ expect })
682682
},
683683
])
684684
})
685+
686+
test('classAttributes find class lists inside variables in JS(X)/TS(X)', async ({ expect }) => {
687+
let file = createDocument({
688+
name: 'file.html',
689+
lang: 'javascript',
690+
settings: {
691+
tailwindCSS: {
692+
classAttributes: ['className'],
693+
},
694+
},
695+
content: js`
696+
let className = {
697+
a: "relative flex",
698+
nested: {
699+
b: "relative flex",
700+
},
701+
inFn: fn({
702+
c: "relative flex",
703+
})
704+
}
705+
706+
let other = {
707+
a: "relative flex",
708+
}
709+
`,
710+
})
711+
712+
let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')
713+
714+
expect(classLists).toEqual([
715+
{
716+
classList: 'relative flex',
717+
range: {
718+
start: { line: 1, character: 6 },
719+
end: { line: 1, character: 19 },
720+
},
721+
},
722+
{
723+
classList: 'relative flex',
724+
range: {
725+
start: { line: 3, character: 8 },
726+
end: { line: 3, character: 21 },
727+
},
728+
},
729+
{
730+
classList: 'relative flex',
731+
range: {
732+
start: { line: 6, character: 8 },
733+
end: { line: 6, character: 21 },
734+
},
735+
},
736+
])
737+
})
738+
739+
test('classAttributes find class lists inside pug', async ({ expect }) => {
740+
let file = createDocument({
741+
name: 'file.pug',
742+
lang: 'pug',
743+
settings: {
744+
tailwindCSS: {
745+
classAttributes: ['className'],
746+
},
747+
},
748+
content: pug`
749+
div(classNAme="relative flex")
750+
`,
751+
})
752+
753+
let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')
754+
755+
expect(classLists).toEqual([
756+
{
757+
classList: 'relative flex',
758+
range: {
759+
start: { line: 0, character: 15 },
760+
end: { line: 0, character: 28 },
761+
},
762+
},
763+
])
764+
})
765+
766+
test('classAttributes find class lists inside Vue bindings', async ({ expect }) => {
767+
let file = createDocument({
768+
name: 'file.pug',
769+
lang: 'vue',
770+
settings: {
771+
tailwindCSS: {
772+
classAttributes: ['class'],
773+
},
774+
},
775+
content: html`
776+
<template>
777+
<div :class="{'relative flex': true}"></div>
778+
</template>
779+
`,
780+
})
781+
782+
let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')
783+
784+
expect(classLists).toEqual([
785+
{
786+
classList: 'relative flex',
787+
range: {
788+
start: { line: 1, character: 17 },
789+
end: { line: 1, character: 30 },
790+
},
791+
},
792+
])
793+
})

packages/tailwindcss-language-service/src/util/test-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const ts = dedent
99
export const tsx = dedent
1010
export const css = dedent
1111
export const html = dedent
12+
export const pug = dedent
1213

1314
export function createDocument({
1415
name,

0 commit comments

Comments
 (0)