Skip to content

Commit 15ece2d

Browse files
committed
wip
1 parent a1375d8 commit 15ece2d

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

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

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test('class regex works in astro', async ({ expect }) => {
5959
test('find class lists in functions', async ({ expect }) => {
6060
let fileA = createDocument({
6161
name: 'file.jsx',
62-
lang: 'javascript',
62+
lang: 'javascriptreact',
6363
settings: {
6464
tailwindCSS: {
6565
classFunctions: ['clsx', 'cva'],
@@ -84,7 +84,7 @@ test('find class lists in functions', async ({ expect }) => {
8484

8585
let fileB = createDocument({
8686
name: 'file.jsx',
87-
lang: 'javascript',
87+
lang: 'javascriptreact',
8888
settings: {
8989
tailwindCSS: {
9090
classFunctions: ['clsx', 'cva'],
@@ -171,7 +171,7 @@ test('find class lists in functions', async ({ expect }) => {
171171
test('find class lists in nested fn calls', async ({ expect }) => {
172172
let fileA = createDocument({
173173
name: 'file.jsx',
174-
lang: 'javascript',
174+
lang: 'javascriptreact',
175175
settings: {
176176
tailwindCSS: {
177177
classFunctions: ['clsx', 'cva'],
@@ -303,7 +303,7 @@ test('find class lists in nested fn calls', async ({ expect }) => {
303303
test('find class lists in nested fn calls (only nested matches)', async ({ expect }) => {
304304
let fileA = createDocument({
305305
name: 'file.jsx',
306-
lang: 'javascript',
306+
lang: 'javascriptreact',
307307
settings: {
308308
tailwindCSS: {
309309
classFunctions: ['clsx', 'cva'],
@@ -350,7 +350,7 @@ test('find class lists in nested fn calls (only nested matches)', async ({ expec
350350
test('find class lists in tagged template literals', async ({ expect }) => {
351351
let fileA = createDocument({
352352
name: 'file.jsx',
353-
lang: 'javascript',
353+
lang: 'javascriptreact',
354354
settings: {
355355
tailwindCSS: {
356356
classFunctions: ['clsx', 'cva'],
@@ -375,7 +375,7 @@ test('find class lists in tagged template literals', async ({ expect }) => {
375375

376376
let fileB = createDocument({
377377
name: 'file.jsx',
378-
lang: 'javascript',
378+
lang: 'javascriptreact',
379379
settings: {
380380
tailwindCSS: {
381381
classFunctions: ['clsx', 'cva'],
@@ -445,6 +445,71 @@ test('find class lists in tagged template literals', async ({ expect }) => {
445445
expect(classListsB).toEqual([])
446446
})
447447

448+
test('classFunctions can be a regex', async ({ expect }) => {
449+
let fileA = createDocument({
450+
name: 'file.jsx',
451+
lang: 'javascriptreact',
452+
settings: {
453+
tailwindCSS: {
454+
classFunctions: ['tw\\.[a-z]+'],
455+
},
456+
},
457+
content: js`
458+
let classes = tw.div('flex p-4')
459+
`,
460+
})
461+
462+
let fileB = createDocument({
463+
name: 'file.jsx',
464+
lang: 'javascriptreact',
465+
settings: {
466+
tailwindCSS: {
467+
classFunctions: ['tw\\.[a-z]+'],
468+
},
469+
},
470+
content: js`
471+
let classes = tw.div.foo('flex p-4')
472+
`,
473+
})
474+
475+
let classListsA = await findClassListsInHtmlRange(fileA.state, fileA.doc, 'js')
476+
let classListsB = await findClassListsInHtmlRange(fileB.state, fileB.doc, 'js')
477+
478+
expect(classListsA).toEqual([
479+
{
480+
classList: 'flex p-4',
481+
range: {
482+
start: { line: 0, character: 22 },
483+
end: { line: 0, character: 30 },
484+
},
485+
},
486+
])
487+
488+
// none from tw.div.foo(`…`) since it does not match a class function
489+
expect(classListsB).toEqual([])
490+
})
491+
492+
test('classFunctions regexes only match on function names', async ({ expect }) => {
493+
let fileA = createDocument({
494+
name: 'file.jsx',
495+
lang: 'javascriptreact',
496+
settings: {
497+
tailwindCSS: {
498+
// A function name itself cannot contain a `:`
499+
classFunctions: [':\\s*tw\\.[a-z]+'],
500+
},
501+
},
502+
content: js`
503+
let classes = tw.div('flex p-4')
504+
let classes = { foo: tw.div('flex p-4') }
505+
`,
506+
})
507+
508+
let classListsA = await findClassListsInHtmlRange(fileA.state, fileA.doc, 'js')
509+
510+
expect(classListsA).toEqual([])
511+
})
512+
448513
function createDocument({
449514
name,
450515
lang,

0 commit comments

Comments
 (0)