Skip to content

Add test for classAttributes matching variable names in JS(X)/TS(X) #1315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 110 additions & 1 deletion packages/tailwindcss-language-service/src/util/find.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'vitest'
import { findClassListsInHtmlRange } from './find'
import { js, html, createDocument } from './test-utils'
import { js, html, pug, createDocument } from './test-utils'

test('class regex works in astro', async ({ expect }) => {
let file = createDocument({
Expand Down Expand Up @@ -682,3 +682,112 @@ test('classFunctions should only match in JS-like contexts', async ({ expect })
},
])
})

test('classAttributes find class lists inside variables in JS(X)/TS(X)', async ({ expect }) => {
let file = createDocument({
name: 'file.html',
lang: 'javascript',
settings: {
tailwindCSS: {
classAttributes: ['className'],
},
},
content: js`
let className = {
a: "relative flex",
nested: {
b: "relative flex",
},
inFn: fn({
c: "relative flex",
})
}

let other = {
a: "relative flex",
}
`,
})

let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')

expect(classLists).toEqual([
{
classList: 'relative flex',
range: {
start: { line: 1, character: 6 },
end: { line: 1, character: 19 },
},
},
{
classList: 'relative flex',
range: {
start: { line: 3, character: 8 },
end: { line: 3, character: 21 },
},
},
{
classList: 'relative flex',
range: {
start: { line: 6, character: 8 },
end: { line: 6, character: 21 },
},
},
])
})

test('classAttributes find class lists inside pug', async ({ expect }) => {
let file = createDocument({
name: 'file.pug',
lang: 'pug',
settings: {
tailwindCSS: {
classAttributes: ['className'],
},
},
content: pug`
div(classNAme="relative flex")
`,
})

let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')

expect(classLists).toEqual([
{
classList: 'relative flex',
range: {
start: { line: 0, character: 15 },
end: { line: 0, character: 28 },
},
},
])
})

test('classAttributes find class lists inside Vue bindings', async ({ expect }) => {
let file = createDocument({
name: 'file.pug',
lang: 'vue',
settings: {
tailwindCSS: {
classAttributes: ['class'],
},
},
content: html`
<template>
<div :class="{'relative flex': true}"></div>
</template>
`,
})

let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')

expect(classLists).toEqual([
{
classList: 'relative flex',
range: {
start: { line: 1, character: 17 },
end: { line: 1, character: 30 },
},
},
])
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ts = dedent
export const tsx = dedent
export const css = dedent
export const html = dedent
export const pug = dedent

export function createDocument({
name,
Expand Down