Skip to content

Commit 1756fd1

Browse files
authored
fix: parsing @apply in Tailwind 4 (#25)
fix parsing `@apply` prelude in tailwind 4
1 parent e0565da commit 1756fd1

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/tailwind4.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
// Imports
88
//-----------------------------------------------------------------------------
99

10-
import * as TailwindThemeKey from "./node/tailwind-theme-key.js";
1110
import defaultSyntax from "@eslint/css-tree/definition-syntax-data";
11+
import * as TailwindThemeKey from "./node/tailwind-theme-key.js";
12+
import * as TailwindUtilityClass from "./node/tailwind-class.js";
13+
import tailwindApply from "./atrule/tailwind-apply.js";
1214
import theme from "./scope/theme.js";
1315
import { themeTypes } from "./types/theme-types.js";
1416

@@ -23,9 +25,12 @@ import { themeTypes } from "./types/theme-types.js";
2325

2426
/** @type {Partial<SyntaxConfig>} */
2527
export const tailwind4 = {
28+
atrule: {
29+
apply: tailwindApply,
30+
},
2631
atrules: {
2732
apply: {
28-
prelude: "<ident>+",
33+
prelude: "<tw-apply-ident>+",
2934
},
3035
config: {
3136
prelude: "<string>",
@@ -61,10 +66,12 @@ export const tailwind4 = {
6166
"tw-spacing": "--spacing(<number>)",
6267
"tw-any-spacing": "<tw-spacing> | <tw-theme-spacing>",
6368
"tw-any-color": "<tw-alpha> | <tw-theme-color>",
69+
"tw-apply-ident": "<ident> | [ <ident> ':' <ident> ]",
6470
...themeTypes
6571
},
6672
node: {
67-
TailwindThemeKey
73+
TailwindThemeKey,
74+
TailwindUtilityClass
6875
},
6976
scope: {
7077
Value: {

tests/fixtures/tailwind4.css

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

1111
@layer components {
1212
.btn {
13-
@apply px-4 py-2 bg-blue-500 text-white;
13+
@apply px-4 py-2 bg-blue-500 text-white dark:text-black;
1414
}
1515
}
1616

tests/tailwind4.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe("Tailwind 4", function () {
376376

377377
describe("@apply", () => {
378378
it("should parse @apply with valid classes", () => {
379-
const tree = toPlainObject(parse(".example { @apply bg-red-500 text-white; }"));
379+
const tree = toPlainObject(parse(".example { @apply bg-red-500 text-white focus:ring-3; }"));
380380
assert.deepStrictEqual(tree, {
381381
type: "StyleSheet",
382382
loc: null,
@@ -421,6 +421,20 @@ describe("Tailwind 4", function () {
421421
type: "Identifier",
422422
name: "text-white",
423423
loc: null
424+
},
425+
{
426+
type: "TailwindUtilityClass",
427+
loc: null,
428+
variant: {
429+
type: "Identifier",
430+
name: "focus",
431+
loc: null
432+
},
433+
name: {
434+
type: "Identifier",
435+
name: "ring-3",
436+
loc: null
437+
}
424438
}
425439
]
426440
},

0 commit comments

Comments
 (0)