oxlint-tailwindcss 1.11.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -22
- package/dist/index.cjs +101 -37
- package/dist/index.mjs +101 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
24 Tailwind CSS linting rules for [oxlint](https://oxc.rs/docs/guide/usage/linter). Built for
|
|
4
4
|
Tailwind CSS v4 with deterministic config, typo suggestions, and autofixes.
|
|
5
5
|
|
|
6
|
-
> **v1
|
|
6
|
+
> **v1** — Upgrading from v0.x? See the
|
|
7
7
|
> **[migration guide](https://oxlint-tailwindcss.pages.dev/migration/v0-to-v1)**. The headline
|
|
8
8
|
> change: `settings.tailwindcss.entryPoint` is now required.
|
|
9
9
|
|
|
@@ -35,7 +35,8 @@ Read the story behind this plugin:
|
|
|
35
35
|
- **Variable detection** — Lints variables matching `/^classNames?$/`, `/^classes$/`, `/^styles?$/`
|
|
36
36
|
(e.g. `className`, `classNames`, `classes`, `styles`) automatically.
|
|
37
37
|
- **Customizable** — Extend class detection with custom attributes, attribute patterns (regex, for
|
|
38
|
-
`*ClassName` props), callees,
|
|
38
|
+
`*ClassName` props), callees, callee extractors (route a wrapper through the `tv`/`cva`/`classed`
|
|
39
|
+
extractor), tags, and variable patterns.
|
|
39
40
|
- **Component class support** — Recognizes `@layer components { .btn {} }` in your CSS.
|
|
40
41
|
|
|
41
42
|
Full documentation: **https://oxlint-tailwindcss.pages.dev** (English) ·
|
|
@@ -179,8 +180,11 @@ the run fails fast, and the state self-heals once the machine recovers.
|
|
|
179
180
|
|
|
180
181
|
### Root font size
|
|
181
182
|
|
|
182
|
-
|
|
183
|
-
`p-
|
|
183
|
+
Comparing a px value against a rem-based scale depends on the root font size. `prefer-scale-token`
|
|
184
|
+
uses it to suggest `p-[10px]` → `p-2.5`, and `enforce-canonical` passes it to Tailwind's
|
|
185
|
+
canonicalizer. `enforce-canonical` only autofixes rewrites that emit identical CSS, so a px value is
|
|
186
|
+
never rewritten to a token that resolves through `var(--spacing)`. Change it only if your project
|
|
187
|
+
sets a root size other than 16 on `<html>`:
|
|
184
188
|
|
|
185
189
|
```jsonc
|
|
186
190
|
{
|
|
@@ -222,17 +226,18 @@ Output:
|
|
|
222
226
|
|
|
223
227
|
If no entry point is configured, the DS-dependent rules (`no-unknown-classes`,
|
|
224
228
|
`no-conflicting-classes`, `enforce-canonical`, `enforce-sort-order`,
|
|
225
|
-
`no-unnecessary-arbitrary-value`, `prefer-theme-tokens`) emit a single
|
|
226
|
-
diagnostic per file with an actionable hint — no more silent skips.
|
|
227
|
-
tolerates a missing entryPoint (its static fallback is itself
|
|
228
|
-
`no-deprecated-classes`
|
|
229
|
-
map
|
|
229
|
+
`no-unnecessary-arbitrary-value`, `prefer-scale-token`, `prefer-theme-tokens`) emit a single
|
|
230
|
+
`designSystemUnavailable` diagnostic per file with an actionable hint — no more silent skips.
|
|
231
|
+
`consistent-variant-order` tolerates a missing entryPoint (its static fallback is itself
|
|
232
|
+
deterministic), and `no-deprecated-classes` tolerates one too — it falls back to a hardcoded v3→v4
|
|
233
|
+
rename map, and derives a richer map from the design system when an entry point is configured. All
|
|
234
|
+
non-DS rules work without an entry point.
|
|
230
235
|
|
|
231
236
|
## Custom class detection
|
|
232
237
|
|
|
233
238
|
By default the plugin detects Tailwind classes in `className`/`class` attributes, 14 utility
|
|
234
|
-
functions (`cn`, `clsx`, `cva`, `tv`, `classed`, etc.), `tw` tagged templates, and variables
|
|
235
|
-
`className`/`classes`/`style`.
|
|
239
|
+
functions (`cn`, `clsx`, `cva`, `tv`, `classed`, etc.), `tw` tagged templates, and variables whose
|
|
240
|
+
names match `className`/`classNames`/`classes`/`style`/`styles`.
|
|
236
241
|
|
|
237
242
|
You can extend these defaults via `settings.tailwindcss`. All values are **additive** — your custom
|
|
238
243
|
entries are appended to the built-in defaults:
|
|
@@ -249,6 +254,8 @@ entries are appended to the built-in defaults:
|
|
|
249
254
|
"attributePatterns": ["ClassName$"],
|
|
250
255
|
// Additional function names to scan
|
|
251
256
|
"callees": ["myHelper"],
|
|
257
|
+
// Route a custom wrapper through a structured extractor (tv/cva/classed/flat)
|
|
258
|
+
"calleeExtractors": { "defineStyles": "tv" },
|
|
252
259
|
// Additional tagged template tags to scan
|
|
253
260
|
"tags": ["css"],
|
|
254
261
|
// Additional regex patterns for variable names (as strings)
|
|
@@ -265,6 +272,13 @@ entries are appended to the built-in defaults:
|
|
|
265
272
|
This applies to all 24 rules at once. For example, adding `"classNames"` to `attributes` makes every
|
|
266
273
|
rule lint `<Input classNames={{ root: "..." }} />`.
|
|
267
274
|
|
|
275
|
+
> **`calleeExtractors`** — `cva`, `tv`, and `classed` are extracted with logic that understands
|
|
276
|
+
> their config shape (`base`, `slots`, `variants`, …). If a shared package re-exports one under
|
|
277
|
+
> another name (e.g. `defineStyles = createTV(config)`), map it —
|
|
278
|
+
> `"calleeExtractors": { "defineStyles": "tv" }` — so the wrapper gets the same deep extraction
|
|
279
|
+
> without renaming imports. Mapped names are auto-registered as callees; values are one of `tv`,
|
|
280
|
+
> `cva`, `classed`, or `flat` (the reserved names can't be remapped).
|
|
281
|
+
|
|
268
282
|
> **`attributePatterns` vs `variablePatterns`** — both match by regex, but on different things.
|
|
269
283
|
> `attributePatterns` matches **JSX attribute names** (`contentContainerClassName`,
|
|
270
284
|
> `tintColorClassName`, …); `variablePatterns` matches **variable declaration names only**
|
|
@@ -568,8 +582,14 @@ syntax normalization), so `--fix` never changes how your design renders.
|
|
|
568
582
|
"-mt-0" → "mt-0"
|
|
569
583
|
"flex-grow-[2]" → "grow-2"
|
|
570
584
|
"text-[var(--color-text)]/90" → "text-(--color-text)/90"
|
|
585
|
+
"dark:bg-white/[.08]" → "dark:bg-white/8"
|
|
586
|
+
"data-[open]:flex" → "data-open:flex"
|
|
587
|
+
"min-[40rem]:flex" → "sm:flex"
|
|
571
588
|
```
|
|
572
589
|
|
|
590
|
+
Variants and the `!` position are preserved. A variant rewrite that would change the selector
|
|
591
|
+
(`has-[:checked]:` → `has-checked:`, `[&>*]:` → `*:`) is left as written.
|
|
592
|
+
|
|
573
593
|
It does **not** rewrite an arbitrary length into a spacing-scale step (`pl-[15px]` → `pl-3.75`),
|
|
574
594
|
even though IntelliSense suggests it: `pl-3.75` compiles to `calc(var(--spacing) * 3.75)`, which is
|
|
575
595
|
not byte-identical to `15px`, so the two can diverge under a `:root` override or a different root
|
|
@@ -715,8 +735,9 @@ Handles variants correctly — the `!` is placed on the utility, not the variant
|
|
|
715
735
|
| `position` | `"prefix"` \| `"suffix"` | `"suffix"` | Where to place the `!` modifier |
|
|
716
736
|
|
|
717
737
|
> **Note:** The default is `"suffix"` to match Tailwind CSS v4's canonical form. The prefix form
|
|
718
|
-
> (`!flex`) is
|
|
719
|
-
>
|
|
738
|
+
> (`!flex`) is the v3 spelling. `enforce-canonical` preserves whatever `!` position you write (it
|
|
739
|
+
> does not normalize it), so this rule is the single source of truth for `!` placement and the two
|
|
740
|
+
> never conflict.
|
|
720
741
|
|
|
721
742
|
**Autofix:** Moves `!` to the correct position.
|
|
722
743
|
|
|
@@ -837,12 +858,14 @@ Warns when a class string exceeds the configured print width or classes-per-line
|
|
|
837
858
|
|
|
838
859
|
**Options:**
|
|
839
860
|
|
|
840
|
-
| Option | Type
|
|
841
|
-
| ---------------- |
|
|
842
|
-
| `printWidth` | `number`
|
|
843
|
-
| `classesPerLine` | `number`
|
|
861
|
+
| Option | Type | Default | Description |
|
|
862
|
+
| ---------------- | ------------------------------------- | ----------- | ------------------------------------------------------------------- |
|
|
863
|
+
| `printWidth` | `number` | `80` | Max class string length |
|
|
864
|
+
| `classesPerLine` | `number` | | Max classes per line |
|
|
865
|
+
| `wrapLines` | `'overWidth' \| 'all'` | | Re-wrap lines: only over-width, or always into the canonical layout |
|
|
866
|
+
| `group` | `'newLine' \| 'emptyLine' \| 'never'` | `'newLine'` | How the `wrapLines: 'all'` layout separates variant groups |
|
|
844
867
|
|
|
845
|
-
**Autofix:**
|
|
868
|
+
**Autofix:** `classesPerLine` and `wrapLines` re-wrap template literals; `printWidth` is warn-only.
|
|
846
869
|
|
|
847
870
|
---
|
|
848
871
|
|
|
@@ -1016,10 +1039,11 @@ The class parser correctly handles:
|
|
|
1016
1039
|
- **`enforce-canonical`**: Named classes are canonicalized via the precomputed map (covers
|
|
1017
1040
|
everything in `getClassList()` plus a curated list of legacy v3 spellings like `break-words`,
|
|
1018
1041
|
`flex-grow`, `start-N`, `bg-gradient-to-*`, `bg-left-top` → `bg-top-left`). Arbitrary/CSS-var
|
|
1019
|
-
forms (`bg-(--c)`, `text-[var(--x)]`)
|
|
1020
|
-
is byte-identical; a value-changing form
|
|
1021
|
-
Some valid v4 classes that don't appear
|
|
1022
|
-
`border-1` is valid as a dynamic numeric
|
|
1042
|
+
forms (`bg-(--c)`, `text-[var(--x)]`) and arbitrary variants (`data-[open]:`, `min-[40rem]:`) are
|
|
1043
|
+
canonicalized dynamically via the worker when the result is byte-identical; a value-changing form
|
|
1044
|
+
like `p-[2px]` → `p-0.5` is left to `prefer-scale-token`. Some valid v4 classes that don't appear
|
|
1045
|
+
in `getClassList()` and have no canonical rewrite (e.g. `border-1` is valid as a dynamic numeric
|
|
1046
|
+
value but isn't enumerated) are left untouched.
|
|
1023
1047
|
- **`no-conflicting-classes`**: compares the emitted declarations (property, value, the custom
|
|
1024
1048
|
properties each value reads, and which box it applies to), so compositions are derived rather than
|
|
1025
1049
|
enumerated. Two exceptions cannot be derived and stay declared in the source: `prose` variants and
|
package/dist/index.cjs
CHANGED
|
@@ -113,6 +113,13 @@ function preserveSpaces(loc, fixed) {
|
|
|
113
113
|
if (loc.preserveTrailingSpace && !fixed.endsWith(" ")) fixed = `${fixed} `;
|
|
114
114
|
return fixed;
|
|
115
115
|
}
|
|
116
|
+
/** The structured extractor kinds `calleeExtractors` accepts (#155). */
|
|
117
|
+
const CALLEE_EXTRACTOR_KINDS = /* @__PURE__ */ new Set([
|
|
118
|
+
"tv",
|
|
119
|
+
"cva",
|
|
120
|
+
"classed",
|
|
121
|
+
"flat"
|
|
122
|
+
]);
|
|
116
123
|
const DEFAULT_EXTRACTOR_CONFIG = {
|
|
117
124
|
attributes: ["className", "class"],
|
|
118
125
|
attributePatterns: [],
|
|
@@ -137,7 +144,8 @@ const DEFAULT_EXTRACTOR_CONFIG = {
|
|
|
137
144
|
/^classNames?$/,
|
|
138
145
|
/^classes$/,
|
|
139
146
|
/^styles?$/
|
|
140
|
-
]
|
|
147
|
+
],
|
|
148
|
+
calleeExtractors: /* @__PURE__ */ new Map()
|
|
141
149
|
};
|
|
142
150
|
const configCache = /* @__PURE__ */ new WeakMap();
|
|
143
151
|
function mergeUnique(defaults, extras, exclusions) {
|
|
@@ -152,6 +160,25 @@ function mergeUnique(defaults, extras, exclusions) {
|
|
|
152
160
|
return [...set];
|
|
153
161
|
}
|
|
154
162
|
/**
|
|
163
|
+
* Builds the validated `calleeExtractors` map from raw settings (#155).
|
|
164
|
+
* Mirrors the graceful degradation of `compileRegexList`: a malformed shape or
|
|
165
|
+
* an unrecognized kind is skipped rather than thrown, so a typo in the config
|
|
166
|
+
* can never crash the lint from inside a visitor. Names present in
|
|
167
|
+
* `excluded` (from `exclude.callees`) are dropped so exclusion still wins over
|
|
168
|
+
* a callee that would otherwise be auto-registered.
|
|
169
|
+
*/
|
|
170
|
+
function compileCalleeExtractors(raw, excluded) {
|
|
171
|
+
const map = /* @__PURE__ */ new Map();
|
|
172
|
+
if (!raw || typeof raw !== "object") return map;
|
|
173
|
+
for (const [name, kind] of Object.entries(raw)) {
|
|
174
|
+
if (!name || typeof kind !== "string") continue;
|
|
175
|
+
if (!CALLEE_EXTRACTOR_KINDS.has(kind)) continue;
|
|
176
|
+
if (excluded.has(name)) continue;
|
|
177
|
+
map.set(name, kind);
|
|
178
|
+
}
|
|
179
|
+
return map;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
155
182
|
* Returns the extractor config for this rule context, merging defaults with
|
|
156
183
|
* user settings from `settings.tailwindcss`. The result is cached per
|
|
157
184
|
* context (WeakMap-keyed) so settings are read once per rule lifetime.
|
|
@@ -177,12 +204,15 @@ function getExtractorConfig(context) {
|
|
|
177
204
|
const exclude = tw.exclude;
|
|
178
205
|
const excludedPatternSources = new Set(exclude?.variablePatterns ?? []);
|
|
179
206
|
const filteredPatterns = excludedPatternSources.size > 0 ? DEFAULT_EXTRACTOR_CONFIG.variablePatterns.filter((re) => !excludedPatternSources.has(re.source)) : DEFAULT_EXTRACTOR_CONFIG.variablePatterns;
|
|
207
|
+
const excludedCallees = new Set(exclude?.callees ?? []);
|
|
208
|
+
const calleeExtractors = compileCalleeExtractors(tw.calleeExtractors, excludedCallees);
|
|
180
209
|
const resolved = {
|
|
181
210
|
attributes: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.attributes, tw.attributes, exclude?.attributes),
|
|
182
211
|
attributePatterns: compileRegexList(tw.attributePatterns),
|
|
183
|
-
callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, tw.callees, exclude?.callees),
|
|
212
|
+
callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, [...tw.callees ?? [], ...calleeExtractors.keys()], exclude?.callees),
|
|
184
213
|
tags: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.tags, tw.tags, exclude?.tags),
|
|
185
|
-
variablePatterns: [...filteredPatterns, ...compileRegexList(tw.variablePatterns)]
|
|
214
|
+
variablePatterns: [...filteredPatterns, ...compileRegexList(tw.variablePatterns)],
|
|
215
|
+
calleeExtractors
|
|
186
216
|
};
|
|
187
217
|
configCache.set(context, resolved);
|
|
188
218
|
return resolved;
|
|
@@ -262,9 +292,10 @@ function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
262
292
|
function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
263
293
|
const calleeName = getCalleeName(node.callee);
|
|
264
294
|
if (!calleeName || !config.callees.includes(calleeName)) return [];
|
|
265
|
-
|
|
266
|
-
if (
|
|
267
|
-
if (
|
|
295
|
+
const structured = calleeName === "cva" || calleeName === "tv" || calleeName === "classed" ? calleeName : config.calleeExtractors.get(calleeName);
|
|
296
|
+
if (structured === "cva") return withOrigin(extractFromCvaCall(node), "callee");
|
|
297
|
+
if (structured === "tv") return withOrigin(extractFromTvCall(node), "callee");
|
|
298
|
+
if (structured === "classed") return withOrigin(extractFromClassedCall(node), "callee");
|
|
268
299
|
const results = [];
|
|
269
300
|
for (const arg of node.arguments) extractFromExpression(arg, results);
|
|
270
301
|
return withOrigin(results, "callee");
|
|
@@ -830,14 +861,28 @@ function hasArbitraryValue(cls) {
|
|
|
830
861
|
* Unlike `hasArbitraryValue`, this also catches `(--var)` shorthand — both
|
|
831
862
|
* forms are what require the async Tailwind DS to canonicalize, because
|
|
832
863
|
* their value space is unbounded. Simple named classes (`flex`, `px-4`,
|
|
833
|
-
*
|
|
834
|
-
* the
|
|
864
|
+
* `hover:flex`) can be canonicalized from the precomputed cache without
|
|
865
|
+
* touching the worker. See `variantHasArbitraryValue` for the variant side.
|
|
835
866
|
*/
|
|
836
867
|
function utilityHasDynamicValue(cls) {
|
|
837
868
|
const utility = extractUtility(cls);
|
|
838
869
|
return utility.includes("[") || utility.includes("(");
|
|
839
870
|
}
|
|
840
871
|
/**
|
|
872
|
+
* Checks if a class's VARIANT part carries an arbitrary value — an arbitrary
|
|
873
|
+
* variant (`[&>svg]:`) or a bracketed/parenthesized argument (`data-[open]:`,
|
|
874
|
+
* `min-[40rem]:`, `supports-(--x):`).
|
|
875
|
+
*
|
|
876
|
+
* The precompute only knows utilities, so the cache canonicalizes the utility
|
|
877
|
+
* and passes the variant through untouched; Tailwind also canonicalizes
|
|
878
|
+
* variants (`data-[open]:` → `data-open:`, `min-[40rem]:` → `sm:`), which only
|
|
879
|
+
* the design system can answer. Bracket-aware via `splitUtilityAndVariant`.
|
|
880
|
+
*/
|
|
881
|
+
function variantHasArbitraryValue(cls) {
|
|
882
|
+
const { variant } = splitUtilityAndVariant(cls);
|
|
883
|
+
return variant.includes("[") || variant.includes("(");
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
841
886
|
* A utility the precompute cannot have enumerated, because the VALUE comes from
|
|
842
887
|
* the user rather than from the design system's class list: arbitrary brackets,
|
|
843
888
|
* the `(--var)` shorthand, a `/` modifier, or an off-scale number.
|
|
@@ -897,6 +942,39 @@ function reattachImportant(bare, position) {
|
|
|
897
942
|
if (position === "suffix") return `${bare}!`;
|
|
898
943
|
return bare;
|
|
899
944
|
}
|
|
945
|
+
const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-]+)\)\]$/;
|
|
946
|
+
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
947
|
+
/**
|
|
948
|
+
* Convert a class between the two CSS-variable syntaxes:
|
|
949
|
+
* `bg-[var(--x)]` (explicit) ↔ `bg-(--x)` (shorthand). Returns the converted
|
|
950
|
+
* class, or `null` when it isn't a simple single-variable utility.
|
|
951
|
+
*
|
|
952
|
+
* Deliberately narrow — it matches ONLY the bare-bracket form, so compound
|
|
953
|
+
* expressions (`bg-[color-mix(...)]`) and opacity-modifier forms
|
|
954
|
+
* (`text-[var(--c)]/90`, whose trailing `/90` fails the `$` anchor) return
|
|
955
|
+
* `null`. Variants and `!` position round-trip via `splitUtilityAndVariant` /
|
|
956
|
+
* `splitImportant`. This is the single set of classes over which
|
|
957
|
+
* `enforce-consistent-variable-syntax` (which owns the policy) and
|
|
958
|
+
* `enforce-canonical` (which cedes it) overlap, so both consume this one helper.
|
|
959
|
+
*/
|
|
960
|
+
function convertVarSyntax(cls, syntax) {
|
|
961
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
962
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
963
|
+
if (syntax === "shorthand") {
|
|
964
|
+
const match = EXPLICIT_VAR_RE.exec(bareUtility);
|
|
965
|
+
if (match) {
|
|
966
|
+
const [, prefix, varName] = match;
|
|
967
|
+
return `${variant}${reattachImportant(`${prefix}-(${varName})`, position)}`;
|
|
968
|
+
}
|
|
969
|
+
} else {
|
|
970
|
+
const match = SHORTHAND_VAR_RE.exec(bareUtility);
|
|
971
|
+
if (match) {
|
|
972
|
+
const [, prefix, varName] = match;
|
|
973
|
+
return `${variant}${reattachImportant(`${prefix}-[var(${varName})]`, position)}`;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return null;
|
|
977
|
+
}
|
|
900
978
|
/**
|
|
901
979
|
* Pseudo-element variants, as a static fallback for when no design system is
|
|
902
980
|
* configured. They select a generated box (`::before`, `::marker`, …) rather than
|
|
@@ -4951,15 +5029,15 @@ const noDeprecatedClasses = defineRule({
|
|
|
4951
5029
|
const WORKER_SCRIPT$1 = makeWorkerScript(`(ds, request) => {
|
|
4952
5030
|
const { classes, rem } = request;
|
|
4953
5031
|
const options = rem ? { rem } : undefined;
|
|
5032
|
+
const OWN_CLASS = /("(?:\\\\.|[^"\\\\])*"|'(?:\\\\.|[^'\\\\])*')|(^|[{};])(\\s*)\\.(?:\\\\[0-9a-fA-F]{1,6}\\s?|\\\\[^0-9a-fA-F\\s]|[\\w-])+/g;
|
|
4954
5033
|
const declsOf = (cls) => {
|
|
4955
5034
|
let out;
|
|
4956
5035
|
try { out = ds.candidatesToCss([cls]); } catch (e) { return null; }
|
|
4957
5036
|
if (!out || !out[0]) return null;
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
return css.slice(open + 1, close).replace(/\\s+/g, ' ').trim();
|
|
5037
|
+
return out[0]
|
|
5038
|
+
.replace(OWN_CLASS, (m, str, start, ws) => (str !== undefined ? str : start + ws + '.__c'))
|
|
5039
|
+
.replace(/\\s+/g, ' ')
|
|
5040
|
+
.trim();
|
|
4963
5041
|
};
|
|
4964
5042
|
return classes.map((cls) => {
|
|
4965
5043
|
// A malformed/mid-typing arbitrary value (e.g. \`px-[calc(var(--a)+)]\`) can
|
|
@@ -5262,7 +5340,7 @@ const enforceCanonical = defineRule({
|
|
|
5262
5340
|
canonicals[i] = classes[i];
|
|
5263
5341
|
continue;
|
|
5264
5342
|
}
|
|
5265
|
-
if (utilityHasDynamicValue(classes[i])) {
|
|
5343
|
+
if (utilityHasDynamicValue(classes[i]) || variantHasArbitraryValue(classes[i])) {
|
|
5266
5344
|
arbitraryIdx.push(i);
|
|
5267
5345
|
arbitrary.push(classes[i]);
|
|
5268
5346
|
} else canonicals[i] = cache.canonicalize(classes[i]);
|
|
@@ -5273,7 +5351,13 @@ const enforceCanonical = defineRule({
|
|
|
5273
5351
|
if (!dynamic) return;
|
|
5274
5352
|
for (let k = 0; k < arbitrary.length; k++) {
|
|
5275
5353
|
const { canonical, safe } = dynamic[k];
|
|
5276
|
-
|
|
5354
|
+
const idx = arbitraryIdx[k];
|
|
5355
|
+
if (!safe) {
|
|
5356
|
+
canonicals[idx] = arbitrary[k];
|
|
5357
|
+
continue;
|
|
5358
|
+
}
|
|
5359
|
+
const preserved = preserveImportantPosition(arbitrary[k], canonical);
|
|
5360
|
+
canonicals[idx] = convertVarSyntax(arbitrary[k], "shorthand") === preserved ? arbitrary[k] : preserved;
|
|
5277
5361
|
}
|
|
5278
5362
|
}
|
|
5279
5363
|
reportClassReplacements(context, loc, split, classes, classes.flatMap((cls, i) => canonicals[i] === cls ? [] : [{
|
|
@@ -6197,7 +6281,7 @@ const noArbitraryValue = defineRule({
|
|
|
6197
6281
|
const enforceConsistentImportantPosition = defineRule({
|
|
6198
6282
|
meta: {
|
|
6199
6283
|
type: "suggestion",
|
|
6200
|
-
docs: { description: "Enforce consistent position of the important (!) modifier. Default: suffix (Tailwind v4 canonical form).
|
|
6284
|
+
docs: { description: "Enforce consistent position of the important (!) modifier. Default: suffix (Tailwind v4 canonical form). This rule is the single source of truth for ! placement — enforce-canonical preserves the position you wrote, so the two never conflict." },
|
|
6201
6285
|
fixable: "code",
|
|
6202
6286
|
schema: [{
|
|
6203
6287
|
type: "object",
|
|
@@ -6295,26 +6379,6 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
6295
6379
|
});
|
|
6296
6380
|
//#endregion
|
|
6297
6381
|
//#region src/rules/enforce-consistent-variable-syntax.ts
|
|
6298
|
-
const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-]+)\)\]$/;
|
|
6299
|
-
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
6300
|
-
function convertClass(cls, syntax) {
|
|
6301
|
-
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
6302
|
-
const { bare: bareUtility, position } = splitImportant(utility);
|
|
6303
|
-
if (syntax === "shorthand") {
|
|
6304
|
-
const match = EXPLICIT_VAR_RE.exec(bareUtility);
|
|
6305
|
-
if (match) {
|
|
6306
|
-
const [, prefix, varName] = match;
|
|
6307
|
-
return `${variant}${reattachImportant(`${prefix}-(${varName})`, position)}`;
|
|
6308
|
-
}
|
|
6309
|
-
} else {
|
|
6310
|
-
const match = SHORTHAND_VAR_RE.exec(bareUtility);
|
|
6311
|
-
if (match) {
|
|
6312
|
-
const [, prefix, varName] = match;
|
|
6313
|
-
return `${variant}${reattachImportant(`${prefix}-[var(${varName})]`, position)}`;
|
|
6314
|
-
}
|
|
6315
|
-
}
|
|
6316
|
-
return null;
|
|
6317
|
-
}
|
|
6318
6382
|
const enforceConsistentVariableSyntax = defineRule({
|
|
6319
6383
|
meta: {
|
|
6320
6384
|
type: "suggestion",
|
|
@@ -6344,7 +6408,7 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
6344
6408
|
for (const loc of locations) {
|
|
6345
6409
|
const split = splitClassesWithSeparators(loc.value);
|
|
6346
6410
|
const offending = split.classes.flatMap((cls) => {
|
|
6347
|
-
const converted =
|
|
6411
|
+
const converted = convertVarSyntax(cls, syntax);
|
|
6348
6412
|
return converted ? [{
|
|
6349
6413
|
cls,
|
|
6350
6414
|
replacement: converted
|
package/dist/index.mjs
CHANGED
|
@@ -117,6 +117,13 @@ function preserveSpaces(loc, fixed) {
|
|
|
117
117
|
if (loc.preserveTrailingSpace && !fixed.endsWith(" ")) fixed = `${fixed} `;
|
|
118
118
|
return fixed;
|
|
119
119
|
}
|
|
120
|
+
/** The structured extractor kinds `calleeExtractors` accepts (#155). */
|
|
121
|
+
const CALLEE_EXTRACTOR_KINDS = /* @__PURE__ */ new Set([
|
|
122
|
+
"tv",
|
|
123
|
+
"cva",
|
|
124
|
+
"classed",
|
|
125
|
+
"flat"
|
|
126
|
+
]);
|
|
120
127
|
const DEFAULT_EXTRACTOR_CONFIG = {
|
|
121
128
|
attributes: ["className", "class"],
|
|
122
129
|
attributePatterns: [],
|
|
@@ -141,7 +148,8 @@ const DEFAULT_EXTRACTOR_CONFIG = {
|
|
|
141
148
|
/^classNames?$/,
|
|
142
149
|
/^classes$/,
|
|
143
150
|
/^styles?$/
|
|
144
|
-
]
|
|
151
|
+
],
|
|
152
|
+
calleeExtractors: /* @__PURE__ */ new Map()
|
|
145
153
|
};
|
|
146
154
|
const configCache = /* @__PURE__ */ new WeakMap();
|
|
147
155
|
function mergeUnique(defaults, extras, exclusions) {
|
|
@@ -156,6 +164,25 @@ function mergeUnique(defaults, extras, exclusions) {
|
|
|
156
164
|
return [...set];
|
|
157
165
|
}
|
|
158
166
|
/**
|
|
167
|
+
* Builds the validated `calleeExtractors` map from raw settings (#155).
|
|
168
|
+
* Mirrors the graceful degradation of `compileRegexList`: a malformed shape or
|
|
169
|
+
* an unrecognized kind is skipped rather than thrown, so a typo in the config
|
|
170
|
+
* can never crash the lint from inside a visitor. Names present in
|
|
171
|
+
* `excluded` (from `exclude.callees`) are dropped so exclusion still wins over
|
|
172
|
+
* a callee that would otherwise be auto-registered.
|
|
173
|
+
*/
|
|
174
|
+
function compileCalleeExtractors(raw, excluded) {
|
|
175
|
+
const map = /* @__PURE__ */ new Map();
|
|
176
|
+
if (!raw || typeof raw !== "object") return map;
|
|
177
|
+
for (const [name, kind] of Object.entries(raw)) {
|
|
178
|
+
if (!name || typeof kind !== "string") continue;
|
|
179
|
+
if (!CALLEE_EXTRACTOR_KINDS.has(kind)) continue;
|
|
180
|
+
if (excluded.has(name)) continue;
|
|
181
|
+
map.set(name, kind);
|
|
182
|
+
}
|
|
183
|
+
return map;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
159
186
|
* Returns the extractor config for this rule context, merging defaults with
|
|
160
187
|
* user settings from `settings.tailwindcss`. The result is cached per
|
|
161
188
|
* context (WeakMap-keyed) so settings are read once per rule lifetime.
|
|
@@ -181,12 +208,15 @@ function getExtractorConfig(context) {
|
|
|
181
208
|
const exclude = tw.exclude;
|
|
182
209
|
const excludedPatternSources = new Set(exclude?.variablePatterns ?? []);
|
|
183
210
|
const filteredPatterns = excludedPatternSources.size > 0 ? DEFAULT_EXTRACTOR_CONFIG.variablePatterns.filter((re) => !excludedPatternSources.has(re.source)) : DEFAULT_EXTRACTOR_CONFIG.variablePatterns;
|
|
211
|
+
const excludedCallees = new Set(exclude?.callees ?? []);
|
|
212
|
+
const calleeExtractors = compileCalleeExtractors(tw.calleeExtractors, excludedCallees);
|
|
184
213
|
const resolved = {
|
|
185
214
|
attributes: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.attributes, tw.attributes, exclude?.attributes),
|
|
186
215
|
attributePatterns: compileRegexList(tw.attributePatterns),
|
|
187
|
-
callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, tw.callees, exclude?.callees),
|
|
216
|
+
callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, [...tw.callees ?? [], ...calleeExtractors.keys()], exclude?.callees),
|
|
188
217
|
tags: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.tags, tw.tags, exclude?.tags),
|
|
189
|
-
variablePatterns: [...filteredPatterns, ...compileRegexList(tw.variablePatterns)]
|
|
218
|
+
variablePatterns: [...filteredPatterns, ...compileRegexList(tw.variablePatterns)],
|
|
219
|
+
calleeExtractors
|
|
190
220
|
};
|
|
191
221
|
configCache.set(context, resolved);
|
|
192
222
|
return resolved;
|
|
@@ -266,9 +296,10 @@ function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
266
296
|
function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
267
297
|
const calleeName = getCalleeName(node.callee);
|
|
268
298
|
if (!calleeName || !config.callees.includes(calleeName)) return [];
|
|
269
|
-
|
|
270
|
-
if (
|
|
271
|
-
if (
|
|
299
|
+
const structured = calleeName === "cva" || calleeName === "tv" || calleeName === "classed" ? calleeName : config.calleeExtractors.get(calleeName);
|
|
300
|
+
if (structured === "cva") return withOrigin(extractFromCvaCall(node), "callee");
|
|
301
|
+
if (structured === "tv") return withOrigin(extractFromTvCall(node), "callee");
|
|
302
|
+
if (structured === "classed") return withOrigin(extractFromClassedCall(node), "callee");
|
|
272
303
|
const results = [];
|
|
273
304
|
for (const arg of node.arguments) extractFromExpression(arg, results);
|
|
274
305
|
return withOrigin(results, "callee");
|
|
@@ -834,14 +865,28 @@ function hasArbitraryValue(cls) {
|
|
|
834
865
|
* Unlike `hasArbitraryValue`, this also catches `(--var)` shorthand — both
|
|
835
866
|
* forms are what require the async Tailwind DS to canonicalize, because
|
|
836
867
|
* their value space is unbounded. Simple named classes (`flex`, `px-4`,
|
|
837
|
-
*
|
|
838
|
-
* the
|
|
868
|
+
* `hover:flex`) can be canonicalized from the precomputed cache without
|
|
869
|
+
* touching the worker. See `variantHasArbitraryValue` for the variant side.
|
|
839
870
|
*/
|
|
840
871
|
function utilityHasDynamicValue(cls) {
|
|
841
872
|
const utility = extractUtility(cls);
|
|
842
873
|
return utility.includes("[") || utility.includes("(");
|
|
843
874
|
}
|
|
844
875
|
/**
|
|
876
|
+
* Checks if a class's VARIANT part carries an arbitrary value — an arbitrary
|
|
877
|
+
* variant (`[&>svg]:`) or a bracketed/parenthesized argument (`data-[open]:`,
|
|
878
|
+
* `min-[40rem]:`, `supports-(--x):`).
|
|
879
|
+
*
|
|
880
|
+
* The precompute only knows utilities, so the cache canonicalizes the utility
|
|
881
|
+
* and passes the variant through untouched; Tailwind also canonicalizes
|
|
882
|
+
* variants (`data-[open]:` → `data-open:`, `min-[40rem]:` → `sm:`), which only
|
|
883
|
+
* the design system can answer. Bracket-aware via `splitUtilityAndVariant`.
|
|
884
|
+
*/
|
|
885
|
+
function variantHasArbitraryValue(cls) {
|
|
886
|
+
const { variant } = splitUtilityAndVariant(cls);
|
|
887
|
+
return variant.includes("[") || variant.includes("(");
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
845
890
|
* A utility the precompute cannot have enumerated, because the VALUE comes from
|
|
846
891
|
* the user rather than from the design system's class list: arbitrary brackets,
|
|
847
892
|
* the `(--var)` shorthand, a `/` modifier, or an off-scale number.
|
|
@@ -901,6 +946,39 @@ function reattachImportant(bare, position) {
|
|
|
901
946
|
if (position === "suffix") return `${bare}!`;
|
|
902
947
|
return bare;
|
|
903
948
|
}
|
|
949
|
+
const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-]+)\)\]$/;
|
|
950
|
+
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
951
|
+
/**
|
|
952
|
+
* Convert a class between the two CSS-variable syntaxes:
|
|
953
|
+
* `bg-[var(--x)]` (explicit) ↔ `bg-(--x)` (shorthand). Returns the converted
|
|
954
|
+
* class, or `null` when it isn't a simple single-variable utility.
|
|
955
|
+
*
|
|
956
|
+
* Deliberately narrow — it matches ONLY the bare-bracket form, so compound
|
|
957
|
+
* expressions (`bg-[color-mix(...)]`) and opacity-modifier forms
|
|
958
|
+
* (`text-[var(--c)]/90`, whose trailing `/90` fails the `$` anchor) return
|
|
959
|
+
* `null`. Variants and `!` position round-trip via `splitUtilityAndVariant` /
|
|
960
|
+
* `splitImportant`. This is the single set of classes over which
|
|
961
|
+
* `enforce-consistent-variable-syntax` (which owns the policy) and
|
|
962
|
+
* `enforce-canonical` (which cedes it) overlap, so both consume this one helper.
|
|
963
|
+
*/
|
|
964
|
+
function convertVarSyntax(cls, syntax) {
|
|
965
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
966
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
967
|
+
if (syntax === "shorthand") {
|
|
968
|
+
const match = EXPLICIT_VAR_RE.exec(bareUtility);
|
|
969
|
+
if (match) {
|
|
970
|
+
const [, prefix, varName] = match;
|
|
971
|
+
return `${variant}${reattachImportant(`${prefix}-(${varName})`, position)}`;
|
|
972
|
+
}
|
|
973
|
+
} else {
|
|
974
|
+
const match = SHORTHAND_VAR_RE.exec(bareUtility);
|
|
975
|
+
if (match) {
|
|
976
|
+
const [, prefix, varName] = match;
|
|
977
|
+
return `${variant}${reattachImportant(`${prefix}-[var(${varName})]`, position)}`;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
return null;
|
|
981
|
+
}
|
|
904
982
|
/**
|
|
905
983
|
* Pseudo-element variants, as a static fallback for when no design system is
|
|
906
984
|
* configured. They select a generated box (`::before`, `::marker`, …) rather than
|
|
@@ -4955,15 +5033,15 @@ const noDeprecatedClasses = defineRule({
|
|
|
4955
5033
|
const WORKER_SCRIPT$1 = makeWorkerScript(`(ds, request) => {
|
|
4956
5034
|
const { classes, rem } = request;
|
|
4957
5035
|
const options = rem ? { rem } : undefined;
|
|
5036
|
+
const OWN_CLASS = /("(?:\\\\.|[^"\\\\])*"|'(?:\\\\.|[^'\\\\])*')|(^|[{};])(\\s*)\\.(?:\\\\[0-9a-fA-F]{1,6}\\s?|\\\\[^0-9a-fA-F\\s]|[\\w-])+/g;
|
|
4958
5037
|
const declsOf = (cls) => {
|
|
4959
5038
|
let out;
|
|
4960
5039
|
try { out = ds.candidatesToCss([cls]); } catch (e) { return null; }
|
|
4961
5040
|
if (!out || !out[0]) return null;
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
return css.slice(open + 1, close).replace(/\\s+/g, ' ').trim();
|
|
5041
|
+
return out[0]
|
|
5042
|
+
.replace(OWN_CLASS, (m, str, start, ws) => (str !== undefined ? str : start + ws + '.__c'))
|
|
5043
|
+
.replace(/\\s+/g, ' ')
|
|
5044
|
+
.trim();
|
|
4967
5045
|
};
|
|
4968
5046
|
return classes.map((cls) => {
|
|
4969
5047
|
// A malformed/mid-typing arbitrary value (e.g. \`px-[calc(var(--a)+)]\`) can
|
|
@@ -5266,7 +5344,7 @@ const enforceCanonical = defineRule({
|
|
|
5266
5344
|
canonicals[i] = classes[i];
|
|
5267
5345
|
continue;
|
|
5268
5346
|
}
|
|
5269
|
-
if (utilityHasDynamicValue(classes[i])) {
|
|
5347
|
+
if (utilityHasDynamicValue(classes[i]) || variantHasArbitraryValue(classes[i])) {
|
|
5270
5348
|
arbitraryIdx.push(i);
|
|
5271
5349
|
arbitrary.push(classes[i]);
|
|
5272
5350
|
} else canonicals[i] = cache.canonicalize(classes[i]);
|
|
@@ -5277,7 +5355,13 @@ const enforceCanonical = defineRule({
|
|
|
5277
5355
|
if (!dynamic) return;
|
|
5278
5356
|
for (let k = 0; k < arbitrary.length; k++) {
|
|
5279
5357
|
const { canonical, safe } = dynamic[k];
|
|
5280
|
-
|
|
5358
|
+
const idx = arbitraryIdx[k];
|
|
5359
|
+
if (!safe) {
|
|
5360
|
+
canonicals[idx] = arbitrary[k];
|
|
5361
|
+
continue;
|
|
5362
|
+
}
|
|
5363
|
+
const preserved = preserveImportantPosition(arbitrary[k], canonical);
|
|
5364
|
+
canonicals[idx] = convertVarSyntax(arbitrary[k], "shorthand") === preserved ? arbitrary[k] : preserved;
|
|
5281
5365
|
}
|
|
5282
5366
|
}
|
|
5283
5367
|
reportClassReplacements(context, loc, split, classes, classes.flatMap((cls, i) => canonicals[i] === cls ? [] : [{
|
|
@@ -6201,7 +6285,7 @@ const noArbitraryValue = defineRule({
|
|
|
6201
6285
|
const enforceConsistentImportantPosition = defineRule({
|
|
6202
6286
|
meta: {
|
|
6203
6287
|
type: "suggestion",
|
|
6204
|
-
docs: { description: "Enforce consistent position of the important (!) modifier. Default: suffix (Tailwind v4 canonical form).
|
|
6288
|
+
docs: { description: "Enforce consistent position of the important (!) modifier. Default: suffix (Tailwind v4 canonical form). This rule is the single source of truth for ! placement — enforce-canonical preserves the position you wrote, so the two never conflict." },
|
|
6205
6289
|
fixable: "code",
|
|
6206
6290
|
schema: [{
|
|
6207
6291
|
type: "object",
|
|
@@ -6299,26 +6383,6 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
6299
6383
|
});
|
|
6300
6384
|
//#endregion
|
|
6301
6385
|
//#region src/rules/enforce-consistent-variable-syntax.ts
|
|
6302
|
-
const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-]+)\)\]$/;
|
|
6303
|
-
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
6304
|
-
function convertClass(cls, syntax) {
|
|
6305
|
-
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
6306
|
-
const { bare: bareUtility, position } = splitImportant(utility);
|
|
6307
|
-
if (syntax === "shorthand") {
|
|
6308
|
-
const match = EXPLICIT_VAR_RE.exec(bareUtility);
|
|
6309
|
-
if (match) {
|
|
6310
|
-
const [, prefix, varName] = match;
|
|
6311
|
-
return `${variant}${reattachImportant(`${prefix}-(${varName})`, position)}`;
|
|
6312
|
-
}
|
|
6313
|
-
} else {
|
|
6314
|
-
const match = SHORTHAND_VAR_RE.exec(bareUtility);
|
|
6315
|
-
if (match) {
|
|
6316
|
-
const [, prefix, varName] = match;
|
|
6317
|
-
return `${variant}${reattachImportant(`${prefix}-[var(${varName})]`, position)}`;
|
|
6318
|
-
}
|
|
6319
|
-
}
|
|
6320
|
-
return null;
|
|
6321
|
-
}
|
|
6322
6386
|
const enforceConsistentVariableSyntax = defineRule({
|
|
6323
6387
|
meta: {
|
|
6324
6388
|
type: "suggestion",
|
|
@@ -6348,7 +6412,7 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
6348
6412
|
for (const loc of locations) {
|
|
6349
6413
|
const split = splitClassesWithSeparators(loc.value);
|
|
6350
6414
|
const offending = split.classes.flatMap((cls) => {
|
|
6351
|
-
const converted =
|
|
6415
|
+
const converted = convertVarSyntax(cls, syntax);
|
|
6352
6416
|
return converted ? [{
|
|
6353
6417
|
cls,
|
|
6354
6418
|
replacement: converted
|