oxlint-tailwindcss 1.9.0 → 1.10.1
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 +58 -13
- package/dist/index.cjs +58 -13
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +58 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# oxlint-tailwindcss
|
|
2
2
|
|
|
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
6
|
> **v1.0.0** — Upgrading from v0.x? See the
|
|
@@ -30,7 +30,7 @@ Read the story behind this plugin:
|
|
|
30
30
|
- **Typo suggestions** — `itms-center` → "Did you mean `items-center`?"
|
|
31
31
|
- **Conflict detection** — Shows exactly which CSS properties conflict and which class wins.
|
|
32
32
|
- **Lightweight** — Only 2 runtime dependencies: `@tailwindcss/node` and `tailwindcss`.
|
|
33
|
-
- **
|
|
33
|
+
- **24 rules** — Correctness, style, complexity, and restriction rules with autofixes where
|
|
34
34
|
possible.
|
|
35
35
|
- **Variable detection** — Lints variables matching `/^classNames?$/`, `/^classes$/`, `/^styles?$/`
|
|
36
36
|
(e.g. `className`, `classNames`, `classes`, `styles`) automatically.
|
|
@@ -61,6 +61,7 @@ Add the plugin to your `.oxlintrc.json`:
|
|
|
61
61
|
"tailwindcss/no-conflicting-classes": "error",
|
|
62
62
|
"tailwindcss/no-deprecated-classes": "error",
|
|
63
63
|
"tailwindcss/no-unnecessary-whitespace": "error",
|
|
64
|
+
// These two only inspect literal class lists on native elements; cn()/twMerge() fragments and custom-component classNames are skipped (issue #117).
|
|
64
65
|
"tailwindcss/no-dark-without-light": "warn",
|
|
65
66
|
"tailwindcss/no-contradicting-variants": "warn",
|
|
66
67
|
// Style
|
|
@@ -246,7 +247,7 @@ entries are appended to the built-in defaults:
|
|
|
246
247
|
}
|
|
247
248
|
```
|
|
248
249
|
|
|
249
|
-
This applies to all
|
|
250
|
+
This applies to all 24 rules at once. For example, adding `"classNames"` to `attributes` makes every
|
|
250
251
|
rule lint `<Input classNames={{ root: "..." }} />`.
|
|
251
252
|
|
|
252
253
|
To **remove** specific items from the built-in defaults, use `exclude`:
|
|
@@ -476,6 +477,11 @@ Requires a base (light) utility when using the `dark:` variant on the same eleme
|
|
|
476
477
|
Groups by utility prefix (`bg-`, `text-`, `border-`, etc.) — only checks that a base utility of the
|
|
477
478
|
same type exists.
|
|
478
479
|
|
|
480
|
+
**Scope (issue #117):** only inspects literal class lists on **native host elements** (`<div>`,
|
|
481
|
+
`<input>`, …). Strings passed to `cn`/`twMerge`/`cva` or set on a custom component's `className` are
|
|
482
|
+
override fragments whose light base commonly lives in another module, so they are skipped to avoid
|
|
483
|
+
false positives.
|
|
484
|
+
|
|
479
485
|
**Options:**
|
|
480
486
|
|
|
481
487
|
| Option | Type | Default | Description |
|
|
@@ -507,6 +513,11 @@ Only flags when the exact same utility exists both as base and with a conditiona
|
|
|
507
513
|
that change the selector target (pseudo-elements, child/descendant selectors, arbitrary selectors)
|
|
508
514
|
are not flagged.
|
|
509
515
|
|
|
516
|
+
**Scope (issue #117):** only inspects literal class lists on **native host elements** (`<div>`,
|
|
517
|
+
`<button>`, …). Strings passed to `cn`/`twMerge`/`cva` or set on a custom component's `className`
|
|
518
|
+
are override fragments where the "redundant" variant is often load-bearing (it beats a same-group
|
|
519
|
+
class the component merges in), so they are skipped to avoid false positives.
|
|
520
|
+
|
|
510
521
|
**No options.** **No autofix.**
|
|
511
522
|
|
|
512
523
|
---
|
|
@@ -517,18 +528,22 @@ are not flagged.
|
|
|
517
528
|
|
|
518
529
|
Enforces canonical Tailwind CSS class names. Uses `canonicalizeCandidates()` from the Tailwind CSS
|
|
519
530
|
engine dynamically — the same API that powers Tailwind CSS IntelliSense's `suggestCanonicalClasses`.
|
|
531
|
+
It only rewrites when the canonical form emits **byte-identical CSS** (a value-preserving rename or
|
|
532
|
+
syntax normalization), so `--fix` never changes how your design renders.
|
|
520
533
|
|
|
521
534
|
```tsx
|
|
522
535
|
// ❌ Bad → ✅ Fixed
|
|
523
|
-
"-m-0"
|
|
524
|
-
"-mt-0"
|
|
525
|
-
"
|
|
526
|
-
"
|
|
527
|
-
"text-[var(--color-text)]/90" → "text-(--color-text)/90"
|
|
528
|
-
"[--w-padding:theme(spacing.1)]" → "[--w-padding:--spacing(1)]"
|
|
536
|
+
"-m-0" → "m-0"
|
|
537
|
+
"-mt-0" → "mt-0"
|
|
538
|
+
"flex-grow-[2]" → "grow-2"
|
|
539
|
+
"text-[var(--color-text)]/90" → "text-(--color-text)/90"
|
|
529
540
|
```
|
|
530
541
|
|
|
531
|
-
|
|
542
|
+
It does **not** rewrite an arbitrary length into a spacing-scale step (`pl-[15px]` → `pl-3.75`),
|
|
543
|
+
even though IntelliSense suggests it: `pl-3.75` compiles to `calc(var(--spacing) * 3.75)`, which is
|
|
544
|
+
not byte-identical to `15px`, so the two can diverge under a `:root` override or a different root
|
|
545
|
+
font size. Those value-changing suggestions live in [`prefer-scale-token`](#prefer-scale-token),
|
|
546
|
+
report-only.
|
|
532
547
|
|
|
533
548
|
**Requires design system.** **Autofix:** Replaces with canonical form.
|
|
534
549
|
|
|
@@ -924,6 +939,35 @@ CSS in those setups. (When the theme exposes the variable directly with no wrapp
|
|
|
924
939
|
|
|
925
940
|
---
|
|
926
941
|
|
|
942
|
+
#### `prefer-scale-token`
|
|
943
|
+
|
|
944
|
+
Reports a hardcoded value that is **numerically equal** to a spacing-scale step or a named theme
|
|
945
|
+
token, and suggests the token. This is the rule that matches Tailwind CSS IntelliSense's
|
|
946
|
+
`suggestCanonicalClasses` suggestion for arbitrary lengths (e.g. `pl-[15px]` → `pl-3.75`) — the
|
|
947
|
+
conversion `enforce-canonical` deliberately leaves alone because the CSS isn't byte-identical (see
|
|
948
|
+
[#78](https://github.com/sergioazoc/oxlint-tailwindcss/issues/78)).
|
|
949
|
+
|
|
950
|
+
**Report-only, on purpose.** There is no autofix: the token resolves through `var(--spacing)`, so a
|
|
951
|
+
`:root` override or a different root font size can make the two diverge. It only ever suggests, so
|
|
952
|
+
`oxlint --fix` skips it while `oxlint --fix-suggestions` applies it in bulk.
|
|
953
|
+
|
|
954
|
+
```tsx
|
|
955
|
+
// ⚠️ Suggested (not auto-fixed)
|
|
956
|
+
"p-[10px]" → "p-2.5"
|
|
957
|
+
"rounded-[0.5rem]" → "rounded-lg"
|
|
958
|
+
```
|
|
959
|
+
|
|
960
|
+
By default it reports values on the granularity Tailwind's own scale enumerates (multiples of
|
|
961
|
+
`0.5`). To match IntelliSense exactly — including dynamic steps like `pl-3.75` — set a finer `step`:
|
|
962
|
+
|
|
963
|
+
```jsonc
|
|
964
|
+
{ "tailwindcss/prefer-scale-token": ["warn", { "step": 0.25 }] }
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
**Off by default.** **Requires design system.** **No autofix — suggestions only.**
|
|
968
|
+
|
|
969
|
+
---
|
|
970
|
+
|
|
927
971
|
## Edge cases
|
|
928
972
|
|
|
929
973
|
The class parser correctly handles:
|
|
@@ -941,9 +985,10 @@ The class parser correctly handles:
|
|
|
941
985
|
- **`enforce-canonical`**: Named classes are canonicalized via the precomputed map (covers
|
|
942
986
|
everything in `getClassList()` plus a curated list of legacy v3 spellings like `break-words`,
|
|
943
987
|
`flex-grow`, `start-N`, `bg-gradient-to-*`, `bg-left-top` → `bg-top-left`). Arbitrary/CSS-var
|
|
944
|
-
forms (`
|
|
945
|
-
|
|
946
|
-
|
|
988
|
+
forms (`bg-(--c)`, `text-[var(--x)]`) are canonicalized dynamically via the worker when the result
|
|
989
|
+
is byte-identical; a value-changing form like `p-[2px]` → `p-0.5` is left to `prefer-scale-token`.
|
|
990
|
+
Some valid v4 classes that don't appear in `getClassList()` and have no canonical rewrite (e.g.
|
|
991
|
+
`border-1` is valid as a dynamic numeric value but isn't enumerated) are left untouched.
|
|
947
992
|
- **`no-conflicting-classes`**: compares the emitted declarations (property, value, the custom
|
|
948
993
|
properties each value reads, and which box it applies to), so compositions are derived rather than
|
|
949
994
|
enumerated. Two exceptions cannot be derived and stay declared in the source: `prose` variants and
|
package/dist/index.cjs
CHANGED
|
@@ -3,7 +3,7 @@ let node_crypto = require("node:crypto");
|
|
|
3
3
|
let node_fs = require("node:fs");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
let node_os = require("node:os");
|
|
6
|
-
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.
|
|
6
|
+
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.80.0/node_modules/@oxlint/plugins/index.js
|
|
7
7
|
/**
|
|
8
8
|
* Define a plugin.
|
|
9
9
|
*
|
|
@@ -90,6 +90,21 @@ function matchesAny(value, list) {
|
|
|
90
90
|
//#endregion
|
|
91
91
|
//#region src/utils/extractors.ts
|
|
92
92
|
/**
|
|
93
|
+
* Whether this string is the element's FINAL, self-contained class list — the
|
|
94
|
+
* only position where a RELATIONAL rule (one whose verdict for a class depends
|
|
95
|
+
* on the OTHER classes in the string) can decide safely. Everywhere else the
|
|
96
|
+
* string may be an override FRAGMENT merged at runtime — by `cn`/`twMerge`/
|
|
97
|
+
* `cva`, or inside a custom component — with classes contributed in another
|
|
98
|
+
* argument or another module, where claims like "the base already applies
|
|
99
|
+
* unconditionally" or "there is no light base" are undecidable without
|
|
100
|
+
* emulating tailwind-merge (issue #117). The two relational rules
|
|
101
|
+
* (`no-contradicting-variants`, `no-dark-without-light`) gate on this; it is
|
|
102
|
+
* the single home for that decision so it can't drift between them.
|
|
103
|
+
*/
|
|
104
|
+
function isFinalClassList(loc) {
|
|
105
|
+
return loc.origin === "jsx-native";
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
93
108
|
* Preserves leading/trailing spaces in fixed class strings for template literals.
|
|
94
109
|
* Without this, `h-3 w-3 ${x}` fixed to `size-3${x}` would break classes.
|
|
95
110
|
*/
|
|
@@ -191,6 +206,32 @@ function createExtractorVisitors(context, check) {
|
|
|
191
206
|
};
|
|
192
207
|
}
|
|
193
208
|
/**
|
|
209
|
+
* Stamps every location with its extraction origin and returns the array.
|
|
210
|
+
* Origin is set once at each of the 4 entry points, so the recursive
|
|
211
|
+
* `extractFromExpression` helpers stay origin-agnostic.
|
|
212
|
+
*/
|
|
213
|
+
function withOrigin(locations, origin) {
|
|
214
|
+
for (const loc of locations) loc.origin = origin;
|
|
215
|
+
return locations;
|
|
216
|
+
}
|
|
217
|
+
const NATIVE_JSX_TAG = /^[a-z]/;
|
|
218
|
+
/**
|
|
219
|
+
* Classifies the JSX host element of an attribute as a native (lowercase) host
|
|
220
|
+
* — where the `className` literal IS the element's final class list — or a
|
|
221
|
+
* custom component, where the string is opaque (it may be re-merged inside the
|
|
222
|
+
* component with `cva`/`twMerge`). Conservative: unknown parent shapes →
|
|
223
|
+
* component, so a relational rule under-reports rather than emitting a false
|
|
224
|
+
* positive.
|
|
225
|
+
*/
|
|
226
|
+
function jsxHostOrigin(node) {
|
|
227
|
+
const parent = node.parent;
|
|
228
|
+
if (parent && parent.type === "JSXOpeningElement") {
|
|
229
|
+
const elementName = parent.name;
|
|
230
|
+
if (elementName.type === "JSXIdentifier" && NATIVE_JSX_TAG.test(elementName.name)) return "jsx-native";
|
|
231
|
+
}
|
|
232
|
+
return "jsx-component";
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
194
235
|
* Extracts class locations from a JSXAttribute node.
|
|
195
236
|
* Handles: className="...", className={`...`}, className={cond ? "..." : "..."}
|
|
196
237
|
*/
|
|
@@ -198,15 +239,17 @@ function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
198
239
|
const name = node.name.type === "JSXIdentifier" ? node.name.name : void 0;
|
|
199
240
|
if (!name || !config.attributes.includes(name)) return [];
|
|
200
241
|
if (!node.value) return [];
|
|
242
|
+
const hostOrigin = jsxHostOrigin(node);
|
|
201
243
|
if (node.value.type === "Literal" && typeof node.value.value === "string") return [{
|
|
202
244
|
value: node.value.value,
|
|
203
245
|
node: node.value,
|
|
204
|
-
range: [node.value.range[0] + 1, node.value.range[1] - 1]
|
|
246
|
+
range: [node.value.range[0] + 1, node.value.range[1] - 1],
|
|
247
|
+
origin: hostOrigin
|
|
205
248
|
}];
|
|
206
249
|
if (node.value.type === "JSXExpressionContainer") {
|
|
207
250
|
const expr = node.value.expression;
|
|
208
|
-
if (expr.type === "ObjectExpression") return extractObjectValues(expr);
|
|
209
|
-
return extractFromExpression(expr);
|
|
251
|
+
if (expr.type === "ObjectExpression") return withOrigin(extractObjectValues(expr), "jsx-object");
|
|
252
|
+
return withOrigin(extractFromExpression(expr), hostOrigin);
|
|
210
253
|
}
|
|
211
254
|
return [];
|
|
212
255
|
}
|
|
@@ -217,12 +260,12 @@ function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
217
260
|
function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
218
261
|
const calleeName = getCalleeName(node.callee);
|
|
219
262
|
if (!calleeName || !config.callees.includes(calleeName)) return [];
|
|
220
|
-
if (calleeName === "cva") return extractFromCvaCall(node);
|
|
221
|
-
if (calleeName === "tv") return extractFromTvCall(node);
|
|
222
|
-
if (calleeName === "classed") return extractFromClassedCall(node);
|
|
263
|
+
if (calleeName === "cva") return withOrigin(extractFromCvaCall(node), "callee");
|
|
264
|
+
if (calleeName === "tv") return withOrigin(extractFromTvCall(node), "callee");
|
|
265
|
+
if (calleeName === "classed") return withOrigin(extractFromClassedCall(node), "callee");
|
|
223
266
|
const results = [];
|
|
224
267
|
for (const arg of node.arguments) extractFromExpression(arg, results);
|
|
225
|
-
return results;
|
|
268
|
+
return withOrigin(results, "callee");
|
|
226
269
|
}
|
|
227
270
|
/**
|
|
228
271
|
* Extracts class locations from a TaggedTemplateExpression (tw`...`).
|
|
@@ -230,7 +273,7 @@ function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
230
273
|
function extractFromTaggedTemplate(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
231
274
|
const tagName = getCalleeName(node.tag);
|
|
232
275
|
if (!tagName || !config.tags.includes(tagName)) return [];
|
|
233
|
-
return extractFromTemplateLiteral(node.quasi);
|
|
276
|
+
return withOrigin(extractFromTemplateLiteral(node.quasi), "template-tag");
|
|
234
277
|
}
|
|
235
278
|
/**
|
|
236
279
|
* Extracts class string values from an ObjectExpression in a JSX attribute.
|
|
@@ -367,7 +410,7 @@ function extractFromVariableDeclarator(node, config = DEFAULT_EXTRACTOR_CONFIG)
|
|
|
367
410
|
if (node.id.type !== "Identifier") return [];
|
|
368
411
|
if (!config.variablePatterns.some((p) => p.test(node.id.name))) return [];
|
|
369
412
|
if (!node.init) return [];
|
|
370
|
-
return extractFromExpression(node.init);
|
|
413
|
+
return withOrigin(extractFromExpression(node.init), "variable");
|
|
371
414
|
}
|
|
372
415
|
/**
|
|
373
416
|
* Walk an expression and append every class-bearing location to `out`.
|
|
@@ -6319,6 +6362,7 @@ const noDarkWithoutLight = defineRule({
|
|
|
6319
6362
|
const ds = softGetDS(getDS);
|
|
6320
6363
|
const cache = ds ? ds.cache : null;
|
|
6321
6364
|
for (const loc of locations) {
|
|
6365
|
+
if (!isFinalClassList(loc)) continue;
|
|
6322
6366
|
const classes = splitClasses(loc.value);
|
|
6323
6367
|
if (cache && ds) {
|
|
6324
6368
|
let unresolved = null;
|
|
@@ -6579,6 +6623,7 @@ const noContradictingVariants = defineRule({
|
|
|
6579
6623
|
function check(locations) {
|
|
6580
6624
|
const factsFor = variantFactsLookup();
|
|
6581
6625
|
for (const loc of locations) {
|
|
6626
|
+
if (!isFinalClassList(loc)) continue;
|
|
6582
6627
|
const classes = splitClasses(loc.value);
|
|
6583
6628
|
const baseUtilities = /* @__PURE__ */ new Set();
|
|
6584
6629
|
for (const cls of classes) if (extractVariants(cls).length === 0) baseUtilities.add(cls);
|
|
@@ -7052,10 +7097,10 @@ const preferThemeTokens = defineRule({
|
|
|
7052
7097
|
const { bare: bareUtility, position } = splitImportant(utility);
|
|
7053
7098
|
const match = detectRawVariable(bareUtility);
|
|
7054
7099
|
if (!match) return [];
|
|
7055
|
-
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
7056
|
-
if (!cache.isValid(candidate)) return [];
|
|
7057
|
-
const target = `--${match.varName}`;
|
|
7058
7100
|
const baseCandidate = `${match.prefix}-${match.varName}`;
|
|
7101
|
+
if (!cache.isValid(baseCandidate)) return [];
|
|
7102
|
+
const candidate = `${baseCandidate}${match.modifier}`;
|
|
7103
|
+
const target = `--${match.varName}`;
|
|
7059
7104
|
if (!cache.getCssDeclarations(baseCandidate).some((decl) => [...decl.readsVars, ...decl.readsFallbackVars].some((read) => cache.themeVarResolvesTo(read, target))) && cache.definesVar(target)) return [];
|
|
7060
7105
|
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
7061
7106
|
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.
|
|
1
|
+
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.80.0/node_modules/@oxlint/plugins/index.d.ts
|
|
2
2
|
//#endregion
|
|
3
3
|
//#region src-js/plugins/tokens.d.ts
|
|
4
4
|
/**
|
|
@@ -969,7 +969,7 @@ type FunctionType = "FunctionDeclaration" | "FunctionExpression" | "TSDeclareFun
|
|
|
969
969
|
interface FormalParameterRest extends Span {
|
|
970
970
|
type: "RestElement";
|
|
971
971
|
argument: BindingPattern;
|
|
972
|
-
decorators?:
|
|
972
|
+
decorators?: Array<Decorator>;
|
|
973
973
|
optional?: boolean;
|
|
974
974
|
typeAnnotation?: TSTypeAnnotation | null;
|
|
975
975
|
value?: null;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.
|
|
1
|
+
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.80.0/node_modules/@oxlint/plugins/index.d.ts
|
|
2
2
|
//#endregion
|
|
3
3
|
//#region src-js/plugins/tokens.d.ts
|
|
4
4
|
/**
|
|
@@ -969,7 +969,7 @@ type FunctionType = "FunctionDeclaration" | "FunctionExpression" | "TSDeclareFun
|
|
|
969
969
|
interface FormalParameterRest extends Span {
|
|
970
970
|
type: "RestElement";
|
|
971
971
|
argument: BindingPattern;
|
|
972
|
-
decorators?:
|
|
972
|
+
decorators?: Array<Decorator>;
|
|
973
973
|
optional?: boolean;
|
|
974
974
|
typeAnnotation?: TSTypeAnnotation | null;
|
|
975
975
|
value?: null;
|
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import { tmpdir, userInfo } from "node:os";
|
|
|
7
7
|
//#region \0rolldown/runtime.js
|
|
8
8
|
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
9
9
|
//#endregion
|
|
10
|
-
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.
|
|
10
|
+
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.80.0/node_modules/@oxlint/plugins/index.js
|
|
11
11
|
/**
|
|
12
12
|
* Define a plugin.
|
|
13
13
|
*
|
|
@@ -94,6 +94,21 @@ function matchesAny(value, list) {
|
|
|
94
94
|
//#endregion
|
|
95
95
|
//#region src/utils/extractors.ts
|
|
96
96
|
/**
|
|
97
|
+
* Whether this string is the element's FINAL, self-contained class list — the
|
|
98
|
+
* only position where a RELATIONAL rule (one whose verdict for a class depends
|
|
99
|
+
* on the OTHER classes in the string) can decide safely. Everywhere else the
|
|
100
|
+
* string may be an override FRAGMENT merged at runtime — by `cn`/`twMerge`/
|
|
101
|
+
* `cva`, or inside a custom component — with classes contributed in another
|
|
102
|
+
* argument or another module, where claims like "the base already applies
|
|
103
|
+
* unconditionally" or "there is no light base" are undecidable without
|
|
104
|
+
* emulating tailwind-merge (issue #117). The two relational rules
|
|
105
|
+
* (`no-contradicting-variants`, `no-dark-without-light`) gate on this; it is
|
|
106
|
+
* the single home for that decision so it can't drift between them.
|
|
107
|
+
*/
|
|
108
|
+
function isFinalClassList(loc) {
|
|
109
|
+
return loc.origin === "jsx-native";
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
97
112
|
* Preserves leading/trailing spaces in fixed class strings for template literals.
|
|
98
113
|
* Without this, `h-3 w-3 ${x}` fixed to `size-3${x}` would break classes.
|
|
99
114
|
*/
|
|
@@ -195,6 +210,32 @@ function createExtractorVisitors(context, check) {
|
|
|
195
210
|
};
|
|
196
211
|
}
|
|
197
212
|
/**
|
|
213
|
+
* Stamps every location with its extraction origin and returns the array.
|
|
214
|
+
* Origin is set once at each of the 4 entry points, so the recursive
|
|
215
|
+
* `extractFromExpression` helpers stay origin-agnostic.
|
|
216
|
+
*/
|
|
217
|
+
function withOrigin(locations, origin) {
|
|
218
|
+
for (const loc of locations) loc.origin = origin;
|
|
219
|
+
return locations;
|
|
220
|
+
}
|
|
221
|
+
const NATIVE_JSX_TAG = /^[a-z]/;
|
|
222
|
+
/**
|
|
223
|
+
* Classifies the JSX host element of an attribute as a native (lowercase) host
|
|
224
|
+
* — where the `className` literal IS the element's final class list — or a
|
|
225
|
+
* custom component, where the string is opaque (it may be re-merged inside the
|
|
226
|
+
* component with `cva`/`twMerge`). Conservative: unknown parent shapes →
|
|
227
|
+
* component, so a relational rule under-reports rather than emitting a false
|
|
228
|
+
* positive.
|
|
229
|
+
*/
|
|
230
|
+
function jsxHostOrigin(node) {
|
|
231
|
+
const parent = node.parent;
|
|
232
|
+
if (parent && parent.type === "JSXOpeningElement") {
|
|
233
|
+
const elementName = parent.name;
|
|
234
|
+
if (elementName.type === "JSXIdentifier" && NATIVE_JSX_TAG.test(elementName.name)) return "jsx-native";
|
|
235
|
+
}
|
|
236
|
+
return "jsx-component";
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
198
239
|
* Extracts class locations from a JSXAttribute node.
|
|
199
240
|
* Handles: className="...", className={`...`}, className={cond ? "..." : "..."}
|
|
200
241
|
*/
|
|
@@ -202,15 +243,17 @@ function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
202
243
|
const name = node.name.type === "JSXIdentifier" ? node.name.name : void 0;
|
|
203
244
|
if (!name || !config.attributes.includes(name)) return [];
|
|
204
245
|
if (!node.value) return [];
|
|
246
|
+
const hostOrigin = jsxHostOrigin(node);
|
|
205
247
|
if (node.value.type === "Literal" && typeof node.value.value === "string") return [{
|
|
206
248
|
value: node.value.value,
|
|
207
249
|
node: node.value,
|
|
208
|
-
range: [node.value.range[0] + 1, node.value.range[1] - 1]
|
|
250
|
+
range: [node.value.range[0] + 1, node.value.range[1] - 1],
|
|
251
|
+
origin: hostOrigin
|
|
209
252
|
}];
|
|
210
253
|
if (node.value.type === "JSXExpressionContainer") {
|
|
211
254
|
const expr = node.value.expression;
|
|
212
|
-
if (expr.type === "ObjectExpression") return extractObjectValues(expr);
|
|
213
|
-
return extractFromExpression(expr);
|
|
255
|
+
if (expr.type === "ObjectExpression") return withOrigin(extractObjectValues(expr), "jsx-object");
|
|
256
|
+
return withOrigin(extractFromExpression(expr), hostOrigin);
|
|
214
257
|
}
|
|
215
258
|
return [];
|
|
216
259
|
}
|
|
@@ -221,12 +264,12 @@ function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
221
264
|
function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
222
265
|
const calleeName = getCalleeName(node.callee);
|
|
223
266
|
if (!calleeName || !config.callees.includes(calleeName)) return [];
|
|
224
|
-
if (calleeName === "cva") return extractFromCvaCall(node);
|
|
225
|
-
if (calleeName === "tv") return extractFromTvCall(node);
|
|
226
|
-
if (calleeName === "classed") return extractFromClassedCall(node);
|
|
267
|
+
if (calleeName === "cva") return withOrigin(extractFromCvaCall(node), "callee");
|
|
268
|
+
if (calleeName === "tv") return withOrigin(extractFromTvCall(node), "callee");
|
|
269
|
+
if (calleeName === "classed") return withOrigin(extractFromClassedCall(node), "callee");
|
|
227
270
|
const results = [];
|
|
228
271
|
for (const arg of node.arguments) extractFromExpression(arg, results);
|
|
229
|
-
return results;
|
|
272
|
+
return withOrigin(results, "callee");
|
|
230
273
|
}
|
|
231
274
|
/**
|
|
232
275
|
* Extracts class locations from a TaggedTemplateExpression (tw`...`).
|
|
@@ -234,7 +277,7 @@ function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
234
277
|
function extractFromTaggedTemplate(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
235
278
|
const tagName = getCalleeName(node.tag);
|
|
236
279
|
if (!tagName || !config.tags.includes(tagName)) return [];
|
|
237
|
-
return extractFromTemplateLiteral(node.quasi);
|
|
280
|
+
return withOrigin(extractFromTemplateLiteral(node.quasi), "template-tag");
|
|
238
281
|
}
|
|
239
282
|
/**
|
|
240
283
|
* Extracts class string values from an ObjectExpression in a JSX attribute.
|
|
@@ -371,7 +414,7 @@ function extractFromVariableDeclarator(node, config = DEFAULT_EXTRACTOR_CONFIG)
|
|
|
371
414
|
if (node.id.type !== "Identifier") return [];
|
|
372
415
|
if (!config.variablePatterns.some((p) => p.test(node.id.name))) return [];
|
|
373
416
|
if (!node.init) return [];
|
|
374
|
-
return extractFromExpression(node.init);
|
|
417
|
+
return withOrigin(extractFromExpression(node.init), "variable");
|
|
375
418
|
}
|
|
376
419
|
/**
|
|
377
420
|
* Walk an expression and append every class-bearing location to `out`.
|
|
@@ -6323,6 +6366,7 @@ const noDarkWithoutLight = defineRule({
|
|
|
6323
6366
|
const ds = softGetDS(getDS);
|
|
6324
6367
|
const cache = ds ? ds.cache : null;
|
|
6325
6368
|
for (const loc of locations) {
|
|
6369
|
+
if (!isFinalClassList(loc)) continue;
|
|
6326
6370
|
const classes = splitClasses(loc.value);
|
|
6327
6371
|
if (cache && ds) {
|
|
6328
6372
|
let unresolved = null;
|
|
@@ -6583,6 +6627,7 @@ const noContradictingVariants = defineRule({
|
|
|
6583
6627
|
function check(locations) {
|
|
6584
6628
|
const factsFor = variantFactsLookup();
|
|
6585
6629
|
for (const loc of locations) {
|
|
6630
|
+
if (!isFinalClassList(loc)) continue;
|
|
6586
6631
|
const classes = splitClasses(loc.value);
|
|
6587
6632
|
const baseUtilities = /* @__PURE__ */ new Set();
|
|
6588
6633
|
for (const cls of classes) if (extractVariants(cls).length === 0) baseUtilities.add(cls);
|
|
@@ -7056,10 +7101,10 @@ const preferThemeTokens = defineRule({
|
|
|
7056
7101
|
const { bare: bareUtility, position } = splitImportant(utility);
|
|
7057
7102
|
const match = detectRawVariable(bareUtility);
|
|
7058
7103
|
if (!match) return [];
|
|
7059
|
-
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
7060
|
-
if (!cache.isValid(candidate)) return [];
|
|
7061
|
-
const target = `--${match.varName}`;
|
|
7062
7104
|
const baseCandidate = `${match.prefix}-${match.varName}`;
|
|
7105
|
+
if (!cache.isValid(baseCandidate)) return [];
|
|
7106
|
+
const candidate = `${baseCandidate}${match.modifier}`;
|
|
7107
|
+
const target = `--${match.varName}`;
|
|
7063
7108
|
if (!cache.getCssDeclarations(baseCandidate).some((decl) => [...decl.readsVars, ...decl.readsFallbackVars].some((read) => cache.themeVarResolvesTo(read, target))) && cache.definesVar(target)) return [];
|
|
7064
7109
|
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
7065
7110
|
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|