oxlint-tailwindcss 0.6.3 → 0.7.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 +22 -4
- package/dist/index.cjs +231 -32
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +317 -115
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -367,6 +367,68 @@ function splitClasses(classString) {
|
|
|
367
367
|
if (current.length > 0) classes.push(current);
|
|
368
368
|
return classes;
|
|
369
369
|
}
|
|
370
|
+
function splitClassesWithSeparators(classString) {
|
|
371
|
+
const classes = [];
|
|
372
|
+
const separators = [];
|
|
373
|
+
let current = "";
|
|
374
|
+
let currentSep = "";
|
|
375
|
+
let bracketDepth = 0;
|
|
376
|
+
let inSingleQuote = false;
|
|
377
|
+
let inDoubleQuote = false;
|
|
378
|
+
for (let i = 0; i < classString.length; i++) {
|
|
379
|
+
const char = classString[i];
|
|
380
|
+
const prev = classString[i - 1];
|
|
381
|
+
if (bracketDepth > 0 && prev !== "\\") {
|
|
382
|
+
if (char === "'" && !inDoubleQuote) inSingleQuote = !inSingleQuote;
|
|
383
|
+
if (char === "\"" && !inSingleQuote) inDoubleQuote = !inDoubleQuote;
|
|
384
|
+
}
|
|
385
|
+
if (!inSingleQuote && !inDoubleQuote) {
|
|
386
|
+
if (char === "[") bracketDepth++;
|
|
387
|
+
if (char === "]") bracketDepth--;
|
|
388
|
+
}
|
|
389
|
+
if (/\s/.test(char) && bracketDepth === 0) {
|
|
390
|
+
if (current.length > 0) {
|
|
391
|
+
classes.push(current);
|
|
392
|
+
separators.push(currentSep);
|
|
393
|
+
current = "";
|
|
394
|
+
currentSep = "";
|
|
395
|
+
}
|
|
396
|
+
currentSep += char;
|
|
397
|
+
} else current += char;
|
|
398
|
+
}
|
|
399
|
+
if (current.length > 0) {
|
|
400
|
+
classes.push(current);
|
|
401
|
+
separators.push(currentSep);
|
|
402
|
+
currentSep = "";
|
|
403
|
+
}
|
|
404
|
+
separators.push(currentSep);
|
|
405
|
+
return {
|
|
406
|
+
classes,
|
|
407
|
+
separators
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Reconstruct a class string from a `ClassSplit` and a (possibly transformed)
|
|
412
|
+
* array of classes.
|
|
413
|
+
*
|
|
414
|
+
* - If `newClasses.length === split.classes.length` (1-to-1 transformation:
|
|
415
|
+
* canonicalize, sort, variant-order, etc.) every separator is preserved
|
|
416
|
+
* verbatim. This keeps multiline indentation intact.
|
|
417
|
+
* - If lengths differ (shorthand collapses pairs, no-duplicate removes dupes)
|
|
418
|
+
* the helper degrades gracefully: leading and trailing whitespace are
|
|
419
|
+
* preserved; classes are joined by the first internal separator that
|
|
420
|
+
* contains a newline, or a single space if none does.
|
|
421
|
+
*/
|
|
422
|
+
function rebuildClassString(split, newClasses) {
|
|
423
|
+
const { classes, separators } = split;
|
|
424
|
+
if (newClasses.length === classes.length) {
|
|
425
|
+
let result = separators[0];
|
|
426
|
+
for (let i = 0; i < newClasses.length; i++) result += newClasses[i] + separators[i + 1];
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
const join = separators.slice(1, -1).find((s) => s.includes("\n")) ?? " ";
|
|
430
|
+
return separators[0] + newClasses.join(join) + separators[separators.length - 1];
|
|
431
|
+
}
|
|
370
432
|
//#endregion
|
|
371
433
|
//#region src/rules/no-duplicate-classes.ts
|
|
372
434
|
const noDuplicateClasses = defineRule({
|
|
@@ -380,13 +442,14 @@ const noDuplicateClasses = defineRule({
|
|
|
380
442
|
createOnce(context) {
|
|
381
443
|
function check(locations) {
|
|
382
444
|
for (const loc of locations) {
|
|
383
|
-
const
|
|
445
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
446
|
+
const classes = split.classes;
|
|
384
447
|
const seen = /* @__PURE__ */ new Set();
|
|
385
448
|
const duplicates = [];
|
|
386
449
|
for (const cls of classes) if (seen.has(cls)) duplicates.push(cls);
|
|
387
450
|
else seen.add(cls);
|
|
388
451
|
if (duplicates.length > 0) {
|
|
389
|
-
const fixed = [...new Set(classes)]
|
|
452
|
+
const fixed = rebuildClassString(split, [...new Set(classes)]);
|
|
390
453
|
for (const dup of duplicates) context.report({
|
|
391
454
|
node: loc.node,
|
|
392
455
|
messageId: "duplicate",
|
|
@@ -414,7 +477,12 @@ const noUnnecessaryWhitespace = defineRule({
|
|
|
414
477
|
createOnce(context) {
|
|
415
478
|
function check(locations) {
|
|
416
479
|
for (const loc of locations) {
|
|
417
|
-
let normalized = loc.value.
|
|
480
|
+
let normalized = loc.value.split("\n").map((line, i) => {
|
|
481
|
+
if (i === 0) return line.replace(/[ \t]+/g, " ");
|
|
482
|
+
const m = /^([ \t]*)([\s\S]*)$/.exec(line);
|
|
483
|
+
if (!m) return line.replace(/[ \t]+/g, " ");
|
|
484
|
+
return m[1] + m[2].replace(/[ \t]+/g, " ");
|
|
485
|
+
}).join("\n");
|
|
418
486
|
if (normalized.startsWith(" ") && !loc.preserveLeadingSpace) normalized = normalized.slice(1);
|
|
419
487
|
if (normalized.endsWith(" ") && !loc.preserveTrailingSpace) normalized = normalized.slice(0, -1);
|
|
420
488
|
if (normalized !== loc.value) context.report({
|
|
@@ -808,7 +876,11 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
808
876
|
* For arbitrary values (bg-[#123]) that aren't in the class list, we use heuristics.
|
|
809
877
|
*/
|
|
810
878
|
const PRECOMPUTE_SCRIPT = `
|
|
811
|
-
|
|
879
|
+
// Resolve @tailwindcss/node via an absolute path passed in by the parent
|
|
880
|
+
// process — bare-specifier lookup from this child's cwd fails under pnpm
|
|
881
|
+
// strict workspaces (the module lives under node_modules/.pnpm/... which
|
|
882
|
+
// is not on the resolution path from the consumer's project root).
|
|
883
|
+
const { __unstable__loadDesignSystem } = require(process.env.TAILWIND_NODE_PATH);
|
|
812
884
|
const { readFileSync } = require('fs');
|
|
813
885
|
const { dirname, resolve } = require('path');
|
|
814
886
|
|
|
@@ -1175,20 +1247,24 @@ function loadDesignSystemSync(cssPath, timeout) {
|
|
|
1175
1247
|
} catch {}
|
|
1176
1248
|
return cached;
|
|
1177
1249
|
}
|
|
1250
|
+
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
1178
1251
|
const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
1179
1252
|
encoding: "utf-8",
|
|
1180
1253
|
timeout: timeout ?? 3e4,
|
|
1181
1254
|
maxBuffer: 50 * 1024 * 1024,
|
|
1182
1255
|
env: {
|
|
1183
1256
|
...process.env,
|
|
1184
|
-
TAILWIND_CSS_PATH: resolvedPath
|
|
1257
|
+
TAILWIND_CSS_PATH: resolvedPath,
|
|
1258
|
+
TAILWIND_NODE_PATH: tailwindNodePath
|
|
1185
1259
|
},
|
|
1186
1260
|
cwd: dirname(resolvedPath)
|
|
1187
1261
|
});
|
|
1188
1262
|
writeCacheFiles(contentCachePath, mtimeIndexPath, contentHash, stdout);
|
|
1189
1263
|
return JSON.parse(stdout);
|
|
1190
1264
|
} catch (error) {
|
|
1191
|
-
|
|
1265
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1266
|
+
if (msg.includes("Cannot find module '@tailwindcss/node'")) console.error(`[oxlint-tailwindcss] Could not resolve '@tailwindcss/node' for "${resolvedPath}". If you are using pnpm with strict hoisting, add '@tailwindcss/node' as a direct devDependency, or upgrade oxlint-tailwindcss to >= 0.7.0 which resolves it from the plugin's own install location. DS-dependent rules will be skipped for this file.`);
|
|
1267
|
+
else console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, msg);
|
|
1192
1268
|
return null;
|
|
1193
1269
|
}
|
|
1194
1270
|
}
|
|
@@ -1560,7 +1636,8 @@ const noDeprecatedClasses = defineRule({
|
|
|
1560
1636
|
function check(locations) {
|
|
1561
1637
|
const dsResult = getDS();
|
|
1562
1638
|
for (const loc of locations) {
|
|
1563
|
-
const
|
|
1639
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
1640
|
+
const classes = split.classes;
|
|
1564
1641
|
const offending = [];
|
|
1565
1642
|
for (const cls of classes) {
|
|
1566
1643
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
@@ -1581,7 +1658,7 @@ const noDeprecatedClasses = defineRule({
|
|
|
1581
1658
|
}
|
|
1582
1659
|
if (offending.length === 0) continue;
|
|
1583
1660
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1584
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
1661
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
1585
1662
|
for (let i = 0; i < offending.length; i++) {
|
|
1586
1663
|
const { cls, replacement } = offending[i];
|
|
1587
1664
|
if (i === 0) context.report({
|
|
@@ -1682,7 +1759,8 @@ const noUnknownClasses = defineRule({
|
|
|
1682
1759
|
if (!ds) return;
|
|
1683
1760
|
const { cache } = ds;
|
|
1684
1761
|
for (const loc of locations) {
|
|
1685
|
-
const
|
|
1762
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
1763
|
+
const classes = split.classes;
|
|
1686
1764
|
for (const cls of classes) {
|
|
1687
1765
|
if (shouldIgnore(cls)) continue;
|
|
1688
1766
|
const stripped = stripModifiers(cls);
|
|
@@ -1690,7 +1768,7 @@ const noUnknownClasses = defineRule({
|
|
|
1690
1768
|
if (DEPRECATED_MAP[stripped]) continue;
|
|
1691
1769
|
const suggestion = findBestSuggestion(stripped, cache.validClasses);
|
|
1692
1770
|
if (suggestion) {
|
|
1693
|
-
const fixedValue = classes.map((c) => c === cls ? suggestion : c)
|
|
1771
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? suggestion : c));
|
|
1694
1772
|
context.report({
|
|
1695
1773
|
node: loc.node,
|
|
1696
1774
|
messageId: "unknownWithSuggestion",
|
|
@@ -2083,7 +2161,8 @@ const enforceCanonical = defineRule({
|
|
|
2083
2161
|
if (!ds) return;
|
|
2084
2162
|
const { cache, entryPoint } = ds;
|
|
2085
2163
|
for (const loc of locations) {
|
|
2086
|
-
const
|
|
2164
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2165
|
+
const classes = split.classes;
|
|
2087
2166
|
const canonicals = Array.from({ length: classes.length });
|
|
2088
2167
|
const arbitraryIdx = [];
|
|
2089
2168
|
const arbitrary = [];
|
|
@@ -2103,7 +2182,7 @@ const enforceCanonical = defineRule({
|
|
|
2103
2182
|
if (canonical === cls) continue;
|
|
2104
2183
|
if (firstNonCanonical) {
|
|
2105
2184
|
firstNonCanonical = false;
|
|
2106
|
-
const fixedValue =
|
|
2185
|
+
const fixedValue = rebuildClassString(split, canonicals);
|
|
2107
2186
|
context.report({
|
|
2108
2187
|
node: loc.node,
|
|
2109
2188
|
messageId: "nonCanonical",
|
|
@@ -2116,7 +2195,7 @@ const enforceCanonical = defineRule({
|
|
|
2116
2195
|
}
|
|
2117
2196
|
});
|
|
2118
2197
|
} else {
|
|
2119
|
-
const fixedValue =
|
|
2198
|
+
const fixedValue = rebuildClassString(split, canonicals);
|
|
2120
2199
|
context.report({
|
|
2121
2200
|
node: loc.node,
|
|
2122
2201
|
messageId: "nonCanonical",
|
|
@@ -2395,7 +2474,8 @@ const enforceSortOrder = defineRule({
|
|
|
2395
2474
|
return result;
|
|
2396
2475
|
}
|
|
2397
2476
|
for (const loc of locations) {
|
|
2398
|
-
const
|
|
2477
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2478
|
+
const classes = split.classes;
|
|
2399
2479
|
if (classes.length < 2) continue;
|
|
2400
2480
|
const sortedNames = mode === "strict" ? sortStrict(classes) : sortDefault(classes);
|
|
2401
2481
|
if (classes.every((name, i) => name === sortedNames[i])) continue;
|
|
@@ -2403,7 +2483,7 @@ const enforceSortOrder = defineRule({
|
|
|
2403
2483
|
node: loc.node,
|
|
2404
2484
|
messageId: "unsorted",
|
|
2405
2485
|
fix(fixer) {
|
|
2406
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc,
|
|
2486
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, sortedNames)));
|
|
2407
2487
|
}
|
|
2408
2488
|
});
|
|
2409
2489
|
}
|
|
@@ -2489,7 +2569,8 @@ const enforceShorthand = defineRule({
|
|
|
2489
2569
|
createOnce(context) {
|
|
2490
2570
|
function check(locations) {
|
|
2491
2571
|
for (const loc of locations) {
|
|
2492
|
-
const
|
|
2572
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2573
|
+
const classes = split.classes;
|
|
2493
2574
|
if (classes.length < 2) continue;
|
|
2494
2575
|
const classSet = new Set(classes);
|
|
2495
2576
|
const values = /* @__PURE__ */ new Set();
|
|
@@ -2518,7 +2599,7 @@ const enforceShorthand = defineRule({
|
|
|
2518
2599
|
replacement: `${importantStart}${rule.replacement}${importantEnd}`
|
|
2519
2600
|
},
|
|
2520
2601
|
fix(fixer) {
|
|
2521
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc,
|
|
2602
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
|
|
2522
2603
|
}
|
|
2523
2604
|
});
|
|
2524
2605
|
}
|
|
@@ -2576,7 +2657,8 @@ const enforceLogical = defineRule({
|
|
|
2576
2657
|
}
|
|
2577
2658
|
function check(locations) {
|
|
2578
2659
|
for (const loc of locations) {
|
|
2579
|
-
const
|
|
2660
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2661
|
+
const classes = split.classes;
|
|
2580
2662
|
const offending = [];
|
|
2581
2663
|
for (const cls of classes) {
|
|
2582
2664
|
const converted = convertClass(cls);
|
|
@@ -2587,7 +2669,7 @@ const enforceLogical = defineRule({
|
|
|
2587
2669
|
}
|
|
2588
2670
|
if (offending.length === 0) continue;
|
|
2589
2671
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2590
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
2672
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2591
2673
|
for (let i = 0; i < offending.length; i++) {
|
|
2592
2674
|
const { cls, replacement } = offending[i];
|
|
2593
2675
|
if (i === 0) context.report({
|
|
@@ -2655,7 +2737,8 @@ const enforcePhysical = defineRule({
|
|
|
2655
2737
|
}
|
|
2656
2738
|
function check(locations) {
|
|
2657
2739
|
for (const loc of locations) {
|
|
2658
|
-
const
|
|
2740
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2741
|
+
const classes = split.classes;
|
|
2659
2742
|
const offending = [];
|
|
2660
2743
|
for (const cls of classes) {
|
|
2661
2744
|
const converted = convertClass(cls);
|
|
@@ -2666,7 +2749,7 @@ const enforcePhysical = defineRule({
|
|
|
2666
2749
|
}
|
|
2667
2750
|
if (offending.length === 0) continue;
|
|
2668
2751
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2669
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
2752
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2670
2753
|
for (let i = 0; i < offending.length; i++) {
|
|
2671
2754
|
const { cls, replacement } = offending[i];
|
|
2672
2755
|
if (i === 0) context.report({
|
|
@@ -2909,7 +2992,8 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
2909
2992
|
function check(locations) {
|
|
2910
2993
|
const position = getPosition();
|
|
2911
2994
|
for (const loc of locations) {
|
|
2912
|
-
const
|
|
2995
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2996
|
+
const classes = split.classes;
|
|
2913
2997
|
const offending = [];
|
|
2914
2998
|
for (const cls of classes) {
|
|
2915
2999
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
@@ -2921,7 +3005,7 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
2921
3005
|
}
|
|
2922
3006
|
if (offending.length === 0) continue;
|
|
2923
3007
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2924
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3008
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2925
3009
|
for (let i = 0; i < offending.length; i++) {
|
|
2926
3010
|
const { cls, replacement } = offending[i];
|
|
2927
3011
|
if (i === 0) context.report({
|
|
@@ -2995,7 +3079,8 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
2995
3079
|
createOnce(context) {
|
|
2996
3080
|
function check(locations) {
|
|
2997
3081
|
for (const loc of locations) {
|
|
2998
|
-
const
|
|
3082
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3083
|
+
const classes = split.classes;
|
|
2999
3084
|
const offending = [];
|
|
3000
3085
|
for (const cls of classes) {
|
|
3001
3086
|
const fixed = fixClass(cls);
|
|
@@ -3006,7 +3091,7 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
3006
3091
|
}
|
|
3007
3092
|
if (offending.length === 0) continue;
|
|
3008
3093
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3009
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3094
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3010
3095
|
for (let i = 0; i < offending.length; i++) {
|
|
3011
3096
|
const { cls, replacement } = offending[i];
|
|
3012
3097
|
if (i === 0) context.report({
|
|
@@ -3100,7 +3185,8 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
3100
3185
|
function check(locations) {
|
|
3101
3186
|
const syntax = getSyntax();
|
|
3102
3187
|
for (const loc of locations) {
|
|
3103
|
-
const
|
|
3188
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3189
|
+
const classes = split.classes;
|
|
3104
3190
|
const offending = [];
|
|
3105
3191
|
for (const cls of classes) {
|
|
3106
3192
|
const converted = convertClass(cls, syntax);
|
|
@@ -3111,7 +3197,7 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
3111
3197
|
}
|
|
3112
3198
|
if (offending.length === 0) continue;
|
|
3113
3199
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3114
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3200
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3115
3201
|
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
3116
3202
|
for (let i = 0; i < offending.length; i++) {
|
|
3117
3203
|
const { cls, replacement } = offending[i];
|
|
@@ -3572,7 +3658,8 @@ const consistentVariantOrder = defineRule({
|
|
|
3572
3658
|
}
|
|
3573
3659
|
function check(locations) {
|
|
3574
3660
|
for (const loc of locations) {
|
|
3575
|
-
const
|
|
3661
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3662
|
+
const classes = split.classes;
|
|
3576
3663
|
const offending = [];
|
|
3577
3664
|
for (const cls of classes) {
|
|
3578
3665
|
const fixed = reorderClass(cls);
|
|
@@ -3583,7 +3670,7 @@ const consistentVariantOrder = defineRule({
|
|
|
3583
3670
|
}
|
|
3584
3671
|
if (offending.length === 0) continue;
|
|
3585
3672
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3586
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3673
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3587
3674
|
for (let i = 0; i < offending.length; i++) {
|
|
3588
3675
|
const { cls, replacement } = offending[i];
|
|
3589
3676
|
if (i === 0) context.report({
|
|
@@ -3624,6 +3711,189 @@ const consistentVariantOrder = defineRule({
|
|
|
3624
3711
|
//#endregion
|
|
3625
3712
|
//#region src/rules/enforce-consistent-line-wrapping.ts
|
|
3626
3713
|
const DEFAULT_PRINT_WIDTH = 80;
|
|
3714
|
+
const enforceConsistentLineWrapping = defineRule({
|
|
3715
|
+
meta: {
|
|
3716
|
+
type: "suggestion",
|
|
3717
|
+
docs: { description: "Warn when a class string exceeds the configured print width" },
|
|
3718
|
+
fixable: "whitespace",
|
|
3719
|
+
schema: [{
|
|
3720
|
+
type: "object",
|
|
3721
|
+
properties: {
|
|
3722
|
+
printWidth: { type: "number" },
|
|
3723
|
+
classesPerLine: { type: "number" }
|
|
3724
|
+
},
|
|
3725
|
+
additionalProperties: false
|
|
3726
|
+
}],
|
|
3727
|
+
defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
|
|
3728
|
+
messages: {
|
|
3729
|
+
tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
|
|
3730
|
+
tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
|
|
3731
|
+
}
|
|
3732
|
+
},
|
|
3733
|
+
createOnce(context) {
|
|
3734
|
+
let _printWidth = null;
|
|
3735
|
+
function getPrintWidth() {
|
|
3736
|
+
if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
|
|
3737
|
+
return _printWidth;
|
|
3738
|
+
}
|
|
3739
|
+
let _classesPerLine = null;
|
|
3740
|
+
function getClassesPerLine() {
|
|
3741
|
+
if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
|
|
3742
|
+
return _classesPerLine;
|
|
3743
|
+
}
|
|
3744
|
+
function check(locations) {
|
|
3745
|
+
const printWidth = getPrintWidth();
|
|
3746
|
+
const classesPerLine = getClassesPerLine();
|
|
3747
|
+
for (const loc of locations) {
|
|
3748
|
+
if (loc.value.length > printWidth) context.report({
|
|
3749
|
+
node: loc.node,
|
|
3750
|
+
messageId: "tooLong",
|
|
3751
|
+
data: {
|
|
3752
|
+
length: String(loc.value.length),
|
|
3753
|
+
printWidth: String(printWidth)
|
|
3754
|
+
}
|
|
3755
|
+
});
|
|
3756
|
+
if (classesPerLine !== void 0) {
|
|
3757
|
+
const classes = splitClasses(loc.value);
|
|
3758
|
+
if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
|
|
3759
|
+
const chunks = [];
|
|
3760
|
+
for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
|
|
3761
|
+
const startCol = loc.node.loc?.start.column ?? 0;
|
|
3762
|
+
const indent = " ".repeat(startCol);
|
|
3763
|
+
const fixedValue = chunks.join("\n" + indent);
|
|
3764
|
+
context.report({
|
|
3765
|
+
node: loc.node,
|
|
3766
|
+
messageId: "tooManyPerLine",
|
|
3767
|
+
data: {
|
|
3768
|
+
count: String(classes.length),
|
|
3769
|
+
max: String(classesPerLine)
|
|
3770
|
+
},
|
|
3771
|
+
fix(fixer) {
|
|
3772
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3773
|
+
}
|
|
3774
|
+
});
|
|
3775
|
+
} else context.report({
|
|
3776
|
+
node: loc.node,
|
|
3777
|
+
messageId: "tooManyPerLine",
|
|
3778
|
+
data: {
|
|
3779
|
+
count: String(classes.length),
|
|
3780
|
+
max: String(classesPerLine)
|
|
3781
|
+
}
|
|
3782
|
+
});
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
}
|
|
3786
|
+
return createExtractorVisitors(context, check);
|
|
3787
|
+
}
|
|
3788
|
+
});
|
|
3789
|
+
//#endregion
|
|
3790
|
+
//#region src/rules/no-unnecessary-arbitrary-value.ts
|
|
3791
|
+
const noUnnecessaryArbitraryValue = defineRule({
|
|
3792
|
+
meta: {
|
|
3793
|
+
type: "suggestion",
|
|
3794
|
+
docs: { description: "Disallow arbitrary values when a named Tailwind class produces the same CSS" },
|
|
3795
|
+
fixable: "code",
|
|
3796
|
+
schema: [{
|
|
3797
|
+
type: "object",
|
|
3798
|
+
properties: { entryPoint: { type: "string" } },
|
|
3799
|
+
additionalProperties: false
|
|
3800
|
+
}],
|
|
3801
|
+
hasSuggestions: true,
|
|
3802
|
+
messages: {
|
|
3803
|
+
unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead.",
|
|
3804
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3805
|
+
}
|
|
3806
|
+
},
|
|
3807
|
+
createOnce(context) {
|
|
3808
|
+
const getDS = createLazyLoader(context);
|
|
3809
|
+
function check(locations) {
|
|
3810
|
+
const ds = getDS();
|
|
3811
|
+
if (!ds) return;
|
|
3812
|
+
const { cache } = ds;
|
|
3813
|
+
for (const loc of locations) {
|
|
3814
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3815
|
+
const classes = split.classes;
|
|
3816
|
+
const offending = [];
|
|
3817
|
+
for (const cls of classes) {
|
|
3818
|
+
if (!hasArbitraryValue(cls)) continue;
|
|
3819
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3820
|
+
const hasImportantPrefix = utility.startsWith("!");
|
|
3821
|
+
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3822
|
+
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3823
|
+
const named = cache.getNamedEquivalent(bareUtility);
|
|
3824
|
+
if (!named) continue;
|
|
3825
|
+
offending.push({
|
|
3826
|
+
cls,
|
|
3827
|
+
replacement: variant + (hasImportantPrefix ? "!" : "") + named + (hasImportantSuffix ? "!" : "")
|
|
3828
|
+
});
|
|
3829
|
+
}
|
|
3830
|
+
if (offending.length === 0) continue;
|
|
3831
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3832
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3833
|
+
for (let i = 0; i < offending.length; i++) {
|
|
3834
|
+
const { cls, replacement } = offending[i];
|
|
3835
|
+
if (i === 0) context.report({
|
|
3836
|
+
node: loc.node,
|
|
3837
|
+
messageId: "unnecessaryArbitrary",
|
|
3838
|
+
data: {
|
|
3839
|
+
className: cls,
|
|
3840
|
+
replacement
|
|
3841
|
+
},
|
|
3842
|
+
fix(fixer) {
|
|
3843
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3844
|
+
}
|
|
3845
|
+
});
|
|
3846
|
+
else context.report({
|
|
3847
|
+
node: loc.node,
|
|
3848
|
+
messageId: "unnecessaryArbitrary",
|
|
3849
|
+
data: {
|
|
3850
|
+
className: cls,
|
|
3851
|
+
replacement
|
|
3852
|
+
},
|
|
3853
|
+
suggest: [{
|
|
3854
|
+
messageId: "suggestReplace",
|
|
3855
|
+
data: {
|
|
3856
|
+
className: cls,
|
|
3857
|
+
replacement
|
|
3858
|
+
},
|
|
3859
|
+
fix(fixer) {
|
|
3860
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3861
|
+
}
|
|
3862
|
+
}]
|
|
3863
|
+
});
|
|
3864
|
+
}
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3867
|
+
return createExtractorVisitors(context, check);
|
|
3868
|
+
}
|
|
3869
|
+
});
|
|
3870
|
+
//#endregion
|
|
3871
|
+
//#region src/rules/prefer-theme-tokens.ts
|
|
3872
|
+
/**
|
|
3873
|
+
* Match `prefix-(--name)` or `prefix-(--name)/modifier`.
|
|
3874
|
+
* Captures: 1=prefix, 2=variable name, 3=optional `/modifier` (kept verbatim).
|
|
3875
|
+
*/
|
|
3876
|
+
const PAREN_VAR_RE = /^(.+?)-\(--([\w-]+)\)(\/[^/\s]+)?$/;
|
|
3877
|
+
/**
|
|
3878
|
+
* Match `prefix-[var(--name)]` or `prefix-[var(--name)]/modifier`.
|
|
3879
|
+
* Captures: 1=prefix, 2=variable name, 3=optional `/modifier`.
|
|
3880
|
+
*/
|
|
3881
|
+
const BRACKET_VAR_RE = /^(.+?)-\[var\(--([\w-]+)\)\](\/[^/\s]+)?$/;
|
|
3882
|
+
function detectRawVariable(utility) {
|
|
3883
|
+
const paren = PAREN_VAR_RE.exec(utility);
|
|
3884
|
+
if (paren) return {
|
|
3885
|
+
prefix: paren[1],
|
|
3886
|
+
varName: paren[2],
|
|
3887
|
+
modifier: paren[3] ?? ""
|
|
3888
|
+
};
|
|
3889
|
+
const bracket = BRACKET_VAR_RE.exec(utility);
|
|
3890
|
+
if (bracket) return {
|
|
3891
|
+
prefix: bracket[1],
|
|
3892
|
+
varName: bracket[2],
|
|
3893
|
+
modifier: bracket[3] ?? ""
|
|
3894
|
+
};
|
|
3895
|
+
return null;
|
|
3896
|
+
}
|
|
3627
3897
|
//#endregion
|
|
3628
3898
|
//#region src/index.ts
|
|
3629
3899
|
const plugin = definePlugin({
|
|
@@ -3649,85 +3919,12 @@ const plugin = definePlugin({
|
|
|
3649
3919
|
"no-hardcoded-colors": noHardcodedColors,
|
|
3650
3920
|
"no-contradicting-variants": noContradictingVariants,
|
|
3651
3921
|
"consistent-variant-order": consistentVariantOrder,
|
|
3652
|
-
"enforce-consistent-line-wrapping":
|
|
3922
|
+
"enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
|
|
3923
|
+
"no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue,
|
|
3924
|
+
"prefer-theme-tokens": defineRule({
|
|
3653
3925
|
meta: {
|
|
3654
3926
|
type: "suggestion",
|
|
3655
|
-
docs: { description: "
|
|
3656
|
-
fixable: "whitespace",
|
|
3657
|
-
schema: [{
|
|
3658
|
-
type: "object",
|
|
3659
|
-
properties: {
|
|
3660
|
-
printWidth: { type: "number" },
|
|
3661
|
-
classesPerLine: { type: "number" }
|
|
3662
|
-
},
|
|
3663
|
-
additionalProperties: false
|
|
3664
|
-
}],
|
|
3665
|
-
defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
|
|
3666
|
-
messages: {
|
|
3667
|
-
tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
|
|
3668
|
-
tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
|
|
3669
|
-
}
|
|
3670
|
-
},
|
|
3671
|
-
createOnce(context) {
|
|
3672
|
-
let _printWidth = null;
|
|
3673
|
-
function getPrintWidth() {
|
|
3674
|
-
if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
|
|
3675
|
-
return _printWidth;
|
|
3676
|
-
}
|
|
3677
|
-
let _classesPerLine = null;
|
|
3678
|
-
function getClassesPerLine() {
|
|
3679
|
-
if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
|
|
3680
|
-
return _classesPerLine;
|
|
3681
|
-
}
|
|
3682
|
-
function check(locations) {
|
|
3683
|
-
const printWidth = getPrintWidth();
|
|
3684
|
-
const classesPerLine = getClassesPerLine();
|
|
3685
|
-
for (const loc of locations) {
|
|
3686
|
-
if (loc.value.length > printWidth) context.report({
|
|
3687
|
-
node: loc.node,
|
|
3688
|
-
messageId: "tooLong",
|
|
3689
|
-
data: {
|
|
3690
|
-
length: String(loc.value.length),
|
|
3691
|
-
printWidth: String(printWidth)
|
|
3692
|
-
}
|
|
3693
|
-
});
|
|
3694
|
-
if (classesPerLine !== void 0) {
|
|
3695
|
-
const classes = splitClasses(loc.value);
|
|
3696
|
-
if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
|
|
3697
|
-
const chunks = [];
|
|
3698
|
-
for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
|
|
3699
|
-
const startCol = loc.node.loc?.start.column ?? 0;
|
|
3700
|
-
const indent = " ".repeat(startCol);
|
|
3701
|
-
const fixedValue = chunks.join("\n" + indent);
|
|
3702
|
-
context.report({
|
|
3703
|
-
node: loc.node,
|
|
3704
|
-
messageId: "tooManyPerLine",
|
|
3705
|
-
data: {
|
|
3706
|
-
count: String(classes.length),
|
|
3707
|
-
max: String(classesPerLine)
|
|
3708
|
-
},
|
|
3709
|
-
fix(fixer) {
|
|
3710
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3711
|
-
}
|
|
3712
|
-
});
|
|
3713
|
-
} else context.report({
|
|
3714
|
-
node: loc.node,
|
|
3715
|
-
messageId: "tooManyPerLine",
|
|
3716
|
-
data: {
|
|
3717
|
-
count: String(classes.length),
|
|
3718
|
-
max: String(classesPerLine)
|
|
3719
|
-
}
|
|
3720
|
-
});
|
|
3721
|
-
}
|
|
3722
|
-
}
|
|
3723
|
-
}
|
|
3724
|
-
return createExtractorVisitors(context, check);
|
|
3725
|
-
}
|
|
3726
|
-
}),
|
|
3727
|
-
"no-unnecessary-arbitrary-value": defineRule({
|
|
3728
|
-
meta: {
|
|
3729
|
-
type: "suggestion",
|
|
3730
|
-
docs: { description: "Disallow arbitrary values when a named Tailwind class produces the same CSS" },
|
|
3927
|
+
docs: { description: "Prefer named theme-token utilities over raw CSS variable references when a matching utility exists" },
|
|
3731
3928
|
fixable: "code",
|
|
3732
3929
|
schema: [{
|
|
3733
3930
|
type: "object",
|
|
@@ -3736,7 +3933,7 @@ const plugin = definePlugin({
|
|
|
3736
3933
|
}],
|
|
3737
3934
|
hasSuggestions: true,
|
|
3738
3935
|
messages: {
|
|
3739
|
-
|
|
3936
|
+
preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
|
|
3740
3937
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3741
3938
|
}
|
|
3742
3939
|
},
|
|
@@ -3747,29 +3944,34 @@ const plugin = definePlugin({
|
|
|
3747
3944
|
if (!ds) return;
|
|
3748
3945
|
const { cache } = ds;
|
|
3749
3946
|
for (const loc of locations) {
|
|
3750
|
-
const
|
|
3947
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3948
|
+
const classes = split.classes;
|
|
3751
3949
|
const offending = [];
|
|
3752
3950
|
for (const cls of classes) {
|
|
3753
|
-
if (!hasArbitraryValue(cls)) continue;
|
|
3754
3951
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3755
3952
|
const hasImportantPrefix = utility.startsWith("!");
|
|
3756
3953
|
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3757
3954
|
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3758
|
-
const
|
|
3759
|
-
if (!
|
|
3955
|
+
const match = detectRawVariable(bareUtility);
|
|
3956
|
+
if (!match) continue;
|
|
3957
|
+
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
3958
|
+
if (!cache.isValid(candidate)) continue;
|
|
3959
|
+
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
3960
|
+
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) continue;
|
|
3961
|
+
const replacement = variant + (hasImportantPrefix ? "!" : "") + candidate + (hasImportantSuffix ? "!" : "");
|
|
3760
3962
|
offending.push({
|
|
3761
3963
|
cls,
|
|
3762
|
-
replacement
|
|
3964
|
+
replacement
|
|
3763
3965
|
});
|
|
3764
3966
|
}
|
|
3765
3967
|
if (offending.length === 0) continue;
|
|
3766
3968
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3767
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3969
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3768
3970
|
for (let i = 0; i < offending.length; i++) {
|
|
3769
3971
|
const { cls, replacement } = offending[i];
|
|
3770
3972
|
if (i === 0) context.report({
|
|
3771
3973
|
node: loc.node,
|
|
3772
|
-
messageId: "
|
|
3974
|
+
messageId: "preferNamed",
|
|
3773
3975
|
data: {
|
|
3774
3976
|
className: cls,
|
|
3775
3977
|
replacement
|
|
@@ -3780,7 +3982,7 @@ const plugin = definePlugin({
|
|
|
3780
3982
|
});
|
|
3781
3983
|
else context.report({
|
|
3782
3984
|
node: loc.node,
|
|
3783
|
-
messageId: "
|
|
3985
|
+
messageId: "preferNamed",
|
|
3784
3986
|
data: {
|
|
3785
3987
|
className: cls,
|
|
3786
3988
|
replacement
|