Skip to content

Commit dc441bc

Browse files
author
Laurynas Grigutis
committed
Added support for tagged template literals
1 parent 7d059da commit dc441bc

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

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

+51
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,57 @@ test('test nested classFunctions', async ({ expect }) => {
276276
expect(classLists).toMatchObject(expectedResult)
277277
})
278278

279+
test('test classFunctions with tagged template literals', async ({ expect }) => {
280+
const state = getTailwindSettingsForClassFunctions()
281+
const classList = `pointer-events-auto relative flex bg-red-500
282+
items-center justify-between overflow-hidden
283+
md:min-w-[20rem] md:max-w-[37.5rem] md:py-sm pl-md py-xs pr-xs gap-sm w-full
284+
data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)]
285+
md:h-[calc(100%-2rem)]
286+
data-[swipe=move]:transition-none`
287+
288+
const expectedResult: DocumentClassList[] = [
289+
{
290+
classList,
291+
range: {
292+
start: { line: 2, character: 6 },
293+
end: { line: 7, character: 37 },
294+
},
295+
},
296+
]
297+
298+
const cnContent = `
299+
const tagged = cn\`
300+
${classList}\`
301+
`
302+
const cnDoc = TextDocument.create('file://file.html', 'html', 1, cnContent)
303+
const cnClassLists = await findClassListsInHtmlRange(state, cnDoc, 'html')
304+
305+
console.log('cnClassLists', JSON.stringify(cnClassLists, null, 2))
306+
307+
expect(cnClassLists).toMatchObject(expectedResult)
308+
309+
const cvaContent = `
310+
const tagged = cva\`
311+
${classList}\`
312+
`
313+
const cvaDoc = TextDocument.create('file://file.html', 'html', 1, cvaContent)
314+
const cvaClassLists = await findClassListsInHtmlRange(state, cvaDoc, 'html')
315+
316+
expect(cvaClassLists).toMatchObject(expectedResult)
317+
318+
// Ensure another tag name with the same layout doesn't match
319+
const cmaContent = `
320+
const tagged = cma\`
321+
${classList}\`
322+
`
323+
324+
const cmaDoc = TextDocument.create('file://file.html', 'html', 1, cmaContent)
325+
const cmaClassLists = await findClassListsInHtmlRange(state, cmaDoc, 'html')
326+
327+
expect(cmaClassLists).toMatchObject([])
328+
})
329+
279330
function getTailwindSettingsForClassFunctions(): Parameters<typeof findClassListsInHtmlRange>[0] {
280331
const defaultSettings = getDefaultTailwindSettings()
281332
return {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function matchClassAttributes(text: string, attributes: string[]): RegExp
166166

167167
export function matchClassFunctions(text: string, fnNames: string[]): RegExpMatchArray[] {
168168
const names = fnNames.filter((x) => typeof x === 'string')
169-
const re = /\b(F_NAMES)\(/
169+
const re = /\b(F_NAMES)(\(|`)/
170170
return findAll(new RegExp(re.source.replace('F_NAMES', names.join('|')), 'gi'), text)
171171
}
172172

packages/vscode-tailwindcss/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
"type": "string"
191191
},
192192
"default": [],
193-
"markdownDescription": "The function names for which to provide class completions, hover previews, linting etc."
193+
"markdownDescription": "The function or tagged template literal names for which to provide class completions, hover previews, linting etc."
194194
},
195195
"tailwindCSS.suggestions": {
196196
"type": "boolean",

0 commit comments

Comments
 (0)