Skip to content

Commit 906d90f

Browse files
committed
Don’t match helper function suffixes
If we see a function call like `from-config(…)` it shouldn’t match the `config(…)` part as a helper function
1 parent 5d8b5dc commit 906d90f

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

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

Lines changed: 50 additions & 38 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,96 @@ 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
},
985-
{
986-
helper: 'config',
987-
path: 'theme("foo")',
988-
ranges: { full: range(13, 30, 13, 42), path: range(13, 30, 13, 42) },
989-
},
990978
{
991979
helper: 'theme',
992980
path: 'foo',
993981
ranges: { full: range(13, 36, 13, 41), path: range(13, 37, 13, 40) },
994982
},
995-
{
996-
helper: 'config',
997-
path: 'theme("foo", default)',
998-
ranges: { full: range(14, 30, 14, 51), path: range(14, 30, 14, 51) },
999-
},
1000983
{
1001984
helper: 'theme',
1002985
path: 'foo',
1003986
ranges: { full: range(14, 36, 14, 50), path: range(14, 37, 14, 40) },
1004987
},
1005-
{
1006-
helper: 'config',
1007-
path: 'theme(foo / 0.5)',
1008-
ranges: { full: range(15, 30, 15, 46), path: range(15, 30, 15, 46) },
1009-
},
1010988
{
1011989
helper: 'theme',
1012990
path: 'foo',
1013991
ranges: { full: range(15, 36, 15, 45), path: range(15, 36, 15, 39) },
1014992
},
1015-
{
1016-
helper: 'config',
1017-
path: 'theme(foo / 0.5, default)',
1018-
ranges: { full: range(16, 30, 16, 55), path: range(16, 30, 16, 55) },
1019-
},
1020993
{
1021994
helper: 'theme',
1022995
path: 'foo',
1023996
ranges: { full: range(16, 36, 16, 54), path: range(16, 36, 16, 39) },
1024997
},
1025998
{
1026-
helper: 'config',
1027-
path: 'theme("foo" / 0.5)',
1028-
ranges: { full: range(17, 30, 17, 48), path: range(17, 30, 17, 48) },
999+
helper: 'theme',
1000+
path: 'foo',
1001+
ranges: { full: range(17, 36, 17, 47), path: range(17, 37, 17, 40) },
10291002
},
10301003
{
10311004
helper: 'theme',
10321005
path: 'foo',
1033-
ranges: { full: range(17, 36, 17, 47), path: range(17, 37, 17, 40) },
1006+
ranges: { full: range(18, 36, 18, 56), path: range(18, 37, 18, 40) },
10341007
},
1008+
1009+
// Nested helpers
10351010
{
10361011
helper: 'config',
1037-
path: 'theme("foo" / 0.5, default)',
1038-
ranges: { full: range(18, 30, 18, 57), path: range(18, 30, 18, 57) },
1012+
path: 'theme(foo)',
1013+
ranges: { full: range(21, 25, 21, 35), path: range(21, 25, 21, 35) },
10391014
},
10401015
{
10411016
helper: 'theme',
10421017
path: 'foo',
1043-
ranges: { full: range(18, 36, 18, 56), path: range(18, 37, 18, 40) },
1018+
ranges: { full: range(21, 31, 21, 34), path: range(21, 31, 21, 34) },
10441019
},
10451020
])
10461021
})
10471022

1023+
test('Can find helper functions in SCSS', async ({ expect }) => {
1024+
let file = createDocument({
1025+
name: 'file.scss',
1026+
lang: 'scss',
1027+
settings: {
1028+
tailwindCSS: {
1029+
classFunctions: ['clsx'],
1030+
},
1031+
},
1032+
content: `
1033+
.foo {
1034+
color: config(foo);
1035+
@include my-config($foo: bar);
1036+
@include .my-config($foo: bar);
1037+
@include $my-config($foo: bar);
1038+
@include %my-config($foo: bar);
1039+
@include #my-config($foo: bar);
1040+
}
1041+
`,
1042+
})
1043+
1044+
let fns = findHelperFunctionsInDocument(file.state, file.doc)
1045+
1046+
expect(fns).toEqual([
1047+
// The first function matches
1048+
{
1049+
helper: 'config',
1050+
path: 'foo',
1051+
ranges: { full: range(2, 22, 2, 25), path: range(2, 22, 2, 25) },
1052+
},
1053+
1054+
// The rest don't
1055+
])
1056+
})
1057+
10481058
test('class functions work inside astro code fences', async ({ expect }) => {
10491059
let file = createDocument({
10501060
name: 'file.astro',
@@ -1082,7 +1092,9 @@ test('class functions work inside astro code fences', async ({ expect }) => {
10821092
])
10831093
})
10841094

1085-
test('classFunctions are detected inside of arrays in javascript just after opening bracket', async ({ expect }) => {
1095+
test('classFunctions are detected inside of arrays in javascript just after opening bracket', async ({
1096+
expect,
1097+
}) => {
10861098
let file = createDocument({
10871099
name: 'file.js',
10881100
lang: 'javascript',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ 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(/\b(?<![-$%#.])(?<helper>config|theme|--theme|var)\(/g, text)
410410

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

0 commit comments

Comments
 (0)