Skip to content

Commit 111d39d

Browse files
Don't match helper functions when part of a larger function name (#1429)
If we see a function call like `from-config(…)` it shouldn’t match the `config(…)` part as a helper function
1 parent 5d8b5dc commit 111d39d

File tree

3 files changed

+88
-36
lines changed

3 files changed

+88
-36
lines changed

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

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,9 @@ test('Can find helper functions in CSS', async ({ expect }) => {
914914
.a { color: from-config(theme(foo / 0.5, default)); }
915915
.a { color: from-config(theme("foo" / 0.5)); }
916916
.a { color: from-config(theme("foo" / 0.5, default)); }
917+
918+
/* nested helpers */
919+
.a { color: config(theme(foo)); }
917920
`,
918921
})
919922

@@ -962,89 +965,132 @@ test('Can find helper functions in CSS', async ({ expect }) => {
962965
},
963966

964967
// Nested
965-
{
966-
helper: 'config',
967-
path: 'theme(foo)',
968-
ranges: { full: range(11, 30, 11, 40), path: range(11, 30, 11, 40) },
969-
},
970968
{
971969
helper: 'theme',
972970
path: 'foo',
973971
ranges: { full: range(11, 36, 11, 39), path: range(11, 36, 11, 39) },
974972
},
975-
{
976-
helper: 'config',
977-
path: 'theme(foo, default)',
978-
ranges: { full: range(12, 30, 12, 49), path: range(12, 30, 12, 49) },
979-
},
980973
{
981974
helper: 'theme',
982975
path: 'foo',
983976
ranges: { full: range(12, 36, 12, 48), path: range(12, 36, 12, 39) },
984977
},
985978
{
986-
helper: 'config',
987-
path: 'theme("foo")',
988-
ranges: { full: range(13, 30, 13, 42), path: range(13, 30, 13, 42) },
979+
helper: 'theme',
980+
path: 'foo',
981+
ranges: { full: range(13, 36, 13, 41), path: range(13, 37, 13, 40) },
989982
},
990983
{
991984
helper: 'theme',
992985
path: 'foo',
993-
ranges: { full: range(13, 36, 13, 41), path: range(13, 37, 13, 40) },
986+
ranges: { full: range(14, 36, 14, 50), path: range(14, 37, 14, 40) },
994987
},
995988
{
996-
helper: 'config',
997-
path: 'theme("foo", default)',
998-
ranges: { full: range(14, 30, 14, 51), path: range(14, 30, 14, 51) },
989+
helper: 'theme',
990+
path: 'foo',
991+
ranges: { full: range(15, 36, 15, 45), path: range(15, 36, 15, 39) },
999992
},
1000993
{
1001994
helper: 'theme',
1002995
path: 'foo',
1003-
ranges: { full: range(14, 36, 14, 50), path: range(14, 37, 14, 40) },
996+
ranges: { full: range(16, 36, 16, 54), path: range(16, 36, 16, 39) },
1004997
},
1005998
{
1006-
helper: 'config',
1007-
path: 'theme(foo / 0.5)',
1008-
ranges: { full: range(15, 30, 15, 46), path: range(15, 30, 15, 46) },
999+
helper: 'theme',
1000+
path: 'foo',
1001+
ranges: { full: range(17, 36, 17, 47), path: range(17, 37, 17, 40) },
10091002
},
10101003
{
10111004
helper: 'theme',
10121005
path: 'foo',
1013-
ranges: { full: range(15, 36, 15, 45), path: range(15, 36, 15, 39) },
1006+
ranges: { full: range(18, 36, 18, 56), path: range(18, 37, 18, 40) },
10141007
},
1008+
1009+
// Nested helpers
10151010
{
10161011
helper: 'config',
1017-
path: 'theme(foo / 0.5, default)',
1018-
ranges: { full: range(16, 30, 16, 55), path: range(16, 30, 16, 55) },
1012+
path: 'theme(foo)',
1013+
ranges: { full: range(21, 25, 21, 35), path: range(21, 25, 21, 35) },
10191014
},
10201015
{
10211016
helper: 'theme',
10221017
path: 'foo',
1023-
ranges: { full: range(16, 36, 16, 54), path: range(16, 36, 16, 39) },
1018+
ranges: { full: range(21, 31, 21, 34), path: range(21, 31, 21, 34) },
10241019
},
1025-
{
1026-
helper: 'config',
1027-
path: 'theme("foo" / 0.5)',
1028-
ranges: { full: range(17, 30, 17, 48), path: range(17, 30, 17, 48) },
1020+
])
1021+
})
1022+
1023+
test('Helper functions can start with --', async ({ expect }) => {
1024+
let file = createDocument({
1025+
name: 'file.css',
1026+
lang: 'css',
1027+
settings: {
1028+
tailwindCSS: {
1029+
classFunctions: ['clsx'],
1030+
},
10291031
},
1032+
content: `
1033+
.a { color: --theme(foo); }
1034+
.a { color: --theme(--theme(foo)); }
1035+
`,
1036+
})
1037+
1038+
let fns = findHelperFunctionsInDocument(file.state, file.doc)
1039+
1040+
expect(fns).toEqual([
10301041
{
10311042
helper: 'theme',
10321043
path: 'foo',
1033-
ranges: { full: range(17, 36, 17, 47), path: range(17, 37, 17, 40) },
1044+
ranges: { full: range(1, 26, 1, 29), path: range(1, 26, 1, 29) },
10341045
},
10351046
{
1036-
helper: 'config',
1037-
path: 'theme("foo" / 0.5, default)',
1038-
ranges: { full: range(18, 30, 18, 57), path: range(18, 30, 18, 57) },
1047+
helper: 'theme',
1048+
path: '--theme(foo)',
1049+
ranges: { full: range(2, 26, 2, 38), path: range(2, 26, 2, 38) },
10391050
},
10401051
{
10411052
helper: 'theme',
10421053
path: 'foo',
1043-
ranges: { full: range(18, 36, 18, 56), path: range(18, 37, 18, 40) },
1054+
ranges: { full: range(2, 34, 2, 37), path: range(2, 34, 2, 37) },
10441055
},
10451056
])
10461057
})
10471058

1059+
test('Can find helper functions in SCSS', async ({ expect }) => {
1060+
let file = createDocument({
1061+
name: 'file.scss',
1062+
lang: 'scss',
1063+
settings: {
1064+
tailwindCSS: {
1065+
classFunctions: ['clsx'],
1066+
},
1067+
},
1068+
content: `
1069+
.foo {
1070+
color: config(foo);
1071+
@include my-config($foo: bar);
1072+
@include .my-config($foo: bar);
1073+
@include $my-config($foo: bar);
1074+
@include %my-config($foo: bar);
1075+
@include #my-config($foo: bar);
1076+
}
1077+
`,
1078+
})
1079+
1080+
let fns = findHelperFunctionsInDocument(file.state, file.doc)
1081+
1082+
expect(fns).toEqual([
1083+
// The first function matches
1084+
{
1085+
helper: 'config',
1086+
path: 'foo',
1087+
ranges: { full: range(2, 22, 2, 25), path: range(2, 22, 2, 25) },
1088+
},
1089+
1090+
// The rest don't
1091+
])
1092+
})
1093+
10481094
test('class functions work inside astro code fences', async ({ expect }) => {
10491095
let file = createDocument({
10501096
name: 'file.astro',
@@ -1082,7 +1128,9 @@ test('class functions work inside astro code fences', async ({ expect }) => {
10821128
])
10831129
})
10841130

1085-
test('classFunctions are detected inside of arrays in javascript just after opening bracket', async ({ expect }) => {
1131+
test('classFunctions are detected inside of arrays in javascript just after opening bracket', async ({
1132+
expect,
1133+
}) => {
10861134
let file = createDocument({
10871135
name: 'file.js',
10881136
lang: 'javascript',

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,10 @@ export function findHelperFunctionsInRange(
406406
let text = getTextWithoutComments(doc, 'css', range)
407407

408408
// Find every instance of a helper function
409-
let matches = findAll(/\b(?<helper>config|theme|--theme|var)\(/g, text)
409+
let matches = findAll(
410+
/(?:\b|(?<=[\s\W]))(?<![-$%#.])(?<helper>config|theme|--theme|var)\(/g,
411+
text,
412+
)
410413

411414
// Eliminate matches that are attached to an `@import`
412415
matches = matches.filter((match) => {

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Prerelease
44

55
- Match class functions that appear after an opening square bracket ([#1428](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1428))
6+
- Don't match helper functions when part of a larger function name ([#1429](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1429))
67

78
## 0.14.25
89

0 commit comments

Comments
 (0)