Skip to content

Commit 5d8b5dc

Browse files
javoscriptJavier Ugartethecrypticace
authored
Match class functions that appear after an opening square bracket (#1428)
**The problem:** The lookbehind pattern `(?<=^|[:=,;\s{()])` doesn't include `[` in its character class, so these cases would be missed: ```javascript const classes = [clsx('btn', 'primary')] // ❌ "clsx" not matched const arr = [cn('text-red-500')] // ❌ "cn" not matched ``` But these would work: ```javascript const classes = [ clsx('btn', 'primary')] // ✅ matches (whitespace after [) const arr = [, cn('text-red-500')] // ✅ matches (comma before cn) ``` **Why this matters:** Array contexts are common in modern JavaScript/React for combining classes: ```javascript const buttonClasses = [clsx(conditionalClasses), baseClasses] const styles = [cn(variantStyle), defaultStyle] ``` **The Fix:** The regex should include `[` in the lookbehind character class: ```javascript /(?<=^|[:=,;\s{()\[])([\p{ID_Start}$_][\p{ID_Continue}$_.]*)[(`]/dgiu // ^ add \[ here ``` This would ensure we properly detect class utility functions at the beginning of arrays. --------- Co-authored-by: Javier Ugarte <[email protected]> Co-authored-by: Jordan Pittman <[email protected]>
1 parent 3d3f866 commit 5d8b5dc

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,30 @@ test('class functions work inside astro code fences', async ({ expect }) => {
10811081
},
10821082
])
10831083
})
1084+
1085+
test('classFunctions are detected inside of arrays in javascript just after opening bracket', async ({ expect }) => {
1086+
let file = createDocument({
1087+
name: 'file.js',
1088+
lang: 'javascript',
1089+
settings: {
1090+
tailwindCSS: {
1091+
classFunctions: ['cn'],
1092+
},
1093+
},
1094+
content: js`
1095+
const list = [cn('relative')]
1096+
`,
1097+
})
1098+
1099+
let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'js')
1100+
1101+
expect(classLists).toEqual([
1102+
{
1103+
classList: 'relative',
1104+
range: {
1105+
start: { line: 0, character: 18 },
1106+
end: { line: 0, character: 26 },
1107+
},
1108+
},
1109+
])
1110+
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export function matchClassFunctions(text: string, fnNames: string[]): RegExpMatc
179179
// - It needs to be in an expression position — so it must be preceded by
180180
// whitespace, parens, curlies, commas, whitespace, etc…
181181
// - It must look like a fn call or a tagged template literal
182-
let FN_NAMES = /(?<=^|[:=,;\s{()])([\p{ID_Start}$_][\p{ID_Continue}$_.]*)[(`]/dgiu
182+
let FN_NAMES = /(?<=^|[:=,;\s{()\[])([\p{ID_Start}$_][\p{ID_Continue}$_.]*)[(`]/dgiu
183183
let foundFns = findAll(FN_NAMES, text)
184184

185185
// 3. Match against the function names in the document

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Prerelease
44

5-
- Nothing yet!
5+
- Match class functions that appear after an opening square bracket ([#1428](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1428))
66

77
## 0.14.25
88

0 commit comments

Comments
 (0)