oxlint-tailwindcss 0.6.3 → 0.7.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 +23 -5
- package/dist/index.cjs +273 -33
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +359 -116
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
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
|
|
|
@@ -956,6 +1028,47 @@ async function main() {
|
|
|
956
1028
|
}
|
|
957
1029
|
}
|
|
958
1030
|
|
|
1031
|
+
// Legacy v3 classes that produce valid CSS in v4 but are not enumerated by
|
|
1032
|
+
// getClassList(). Tailwind's canonicalizeCandidates() still rewrites them to
|
|
1033
|
+
// their v4 equivalent, so we feed them in explicitly. Without this pass,
|
|
1034
|
+
// classes like \`break-words\` would be invisible to enforce-canonical
|
|
1035
|
+
// (issue #16).
|
|
1036
|
+
// - Fixed renames mirror the internal v3->v4 map in tailwindcss/canonicalize.
|
|
1037
|
+
// - Pattern renames (start-* -> inset-s-*, end-* -> inset-e-*) are derived
|
|
1038
|
+
// from the inset-{s,e}-* utilities present in validClasses so we cover
|
|
1039
|
+
// every numeric/named value the design system exposes.
|
|
1040
|
+
const legacyCandidates = [
|
|
1041
|
+
'order-none',
|
|
1042
|
+
'break-words',
|
|
1043
|
+
'overflow-ellipsis',
|
|
1044
|
+
'flex-grow', 'flex-grow-0', 'flex-grow-1',
|
|
1045
|
+
'flex-shrink', 'flex-shrink-0', 'flex-shrink-1',
|
|
1046
|
+
'decoration-clone', 'decoration-slice',
|
|
1047
|
+
'bg-gradient-to-t', 'bg-gradient-to-tr', 'bg-gradient-to-r', 'bg-gradient-to-br',
|
|
1048
|
+
'bg-gradient-to-b', 'bg-gradient-to-bl', 'bg-gradient-to-l', 'bg-gradient-to-tl',
|
|
1049
|
+
];
|
|
1050
|
+
for (const cls of validClasses) {
|
|
1051
|
+
if (cls.startsWith('inset-s-')) legacyCandidates.push('start-' + cls.slice(8));
|
|
1052
|
+
else if (cls.startsWith('-inset-s-')) legacyCandidates.push('-start-' + cls.slice(9));
|
|
1053
|
+
else if (cls.startsWith('inset-e-')) legacyCandidates.push('end-' + cls.slice(8));
|
|
1054
|
+
else if (cls.startsWith('-inset-e-')) legacyCandidates.push('-end-' + cls.slice(9));
|
|
1055
|
+
}
|
|
1056
|
+
const legacyToProcess = legacyCandidates.filter(cls => !validSet.has(cls));
|
|
1057
|
+
if (legacyToProcess.length > 0) {
|
|
1058
|
+
const legacyCssResults = ds.candidatesToCss(legacyToProcess);
|
|
1059
|
+
for (let i = 0; i < legacyToProcess.length; i++) {
|
|
1060
|
+
if (legacyCssResults[i] == null) continue;
|
|
1061
|
+
const cls = legacyToProcess[i];
|
|
1062
|
+
const result = ds.canonicalizeCandidates([cls]);
|
|
1063
|
+
if (result[0] && result[0] !== cls) {
|
|
1064
|
+
canonical[cls] = result[0];
|
|
1065
|
+
}
|
|
1066
|
+
// Mark as valid so no-unknown-classes doesn't flag legacy spellings.
|
|
1067
|
+
validClasses.push(cls);
|
|
1068
|
+
validSet.add(cls);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
|
|
959
1072
|
// Sort order — include extra candidates so bare utilities (rounded, blur, etc.) get order
|
|
960
1073
|
const allForOrder = [...classNames];
|
|
961
1074
|
for (const cls of validClasses) {
|
|
@@ -1123,7 +1236,7 @@ async function main() {
|
|
|
1123
1236
|
main().catch(e => { process.stderr.write(e.message); process.exit(1); });
|
|
1124
1237
|
`;
|
|
1125
1238
|
const CACHE_DIR = join(tmpdir(), "oxlint-tailwindcss");
|
|
1126
|
-
const CACHE_VERSION =
|
|
1239
|
+
const CACHE_VERSION = 13;
|
|
1127
1240
|
/**
|
|
1128
1241
|
* Two-level disk cache for monorepo deduplication:
|
|
1129
1242
|
*
|
|
@@ -1175,20 +1288,24 @@ function loadDesignSystemSync(cssPath, timeout) {
|
|
|
1175
1288
|
} catch {}
|
|
1176
1289
|
return cached;
|
|
1177
1290
|
}
|
|
1291
|
+
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
1178
1292
|
const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
1179
1293
|
encoding: "utf-8",
|
|
1180
1294
|
timeout: timeout ?? 3e4,
|
|
1181
1295
|
maxBuffer: 50 * 1024 * 1024,
|
|
1182
1296
|
env: {
|
|
1183
1297
|
...process.env,
|
|
1184
|
-
TAILWIND_CSS_PATH: resolvedPath
|
|
1298
|
+
TAILWIND_CSS_PATH: resolvedPath,
|
|
1299
|
+
TAILWIND_NODE_PATH: tailwindNodePath
|
|
1185
1300
|
},
|
|
1186
1301
|
cwd: dirname(resolvedPath)
|
|
1187
1302
|
});
|
|
1188
1303
|
writeCacheFiles(contentCachePath, mtimeIndexPath, contentHash, stdout);
|
|
1189
1304
|
return JSON.parse(stdout);
|
|
1190
1305
|
} catch (error) {
|
|
1191
|
-
|
|
1306
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1307
|
+
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.`);
|
|
1308
|
+
else console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, msg);
|
|
1192
1309
|
return null;
|
|
1193
1310
|
}
|
|
1194
1311
|
}
|
|
@@ -1560,7 +1677,8 @@ const noDeprecatedClasses = defineRule({
|
|
|
1560
1677
|
function check(locations) {
|
|
1561
1678
|
const dsResult = getDS();
|
|
1562
1679
|
for (const loc of locations) {
|
|
1563
|
-
const
|
|
1680
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
1681
|
+
const classes = split.classes;
|
|
1564
1682
|
const offending = [];
|
|
1565
1683
|
for (const cls of classes) {
|
|
1566
1684
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
@@ -1581,7 +1699,7 @@ const noDeprecatedClasses = defineRule({
|
|
|
1581
1699
|
}
|
|
1582
1700
|
if (offending.length === 0) continue;
|
|
1583
1701
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1584
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
1702
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
1585
1703
|
for (let i = 0; i < offending.length; i++) {
|
|
1586
1704
|
const { cls, replacement } = offending[i];
|
|
1587
1705
|
if (i === 0) context.report({
|
|
@@ -1682,7 +1800,8 @@ const noUnknownClasses = defineRule({
|
|
|
1682
1800
|
if (!ds) return;
|
|
1683
1801
|
const { cache } = ds;
|
|
1684
1802
|
for (const loc of locations) {
|
|
1685
|
-
const
|
|
1803
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
1804
|
+
const classes = split.classes;
|
|
1686
1805
|
for (const cls of classes) {
|
|
1687
1806
|
if (shouldIgnore(cls)) continue;
|
|
1688
1807
|
const stripped = stripModifiers(cls);
|
|
@@ -1690,7 +1809,7 @@ const noUnknownClasses = defineRule({
|
|
|
1690
1809
|
if (DEPRECATED_MAP[stripped]) continue;
|
|
1691
1810
|
const suggestion = findBestSuggestion(stripped, cache.validClasses);
|
|
1692
1811
|
if (suggestion) {
|
|
1693
|
-
const fixedValue = classes.map((c) => c === cls ? suggestion : c)
|
|
1812
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? suggestion : c));
|
|
1694
1813
|
context.report({
|
|
1695
1814
|
node: loc.node,
|
|
1696
1815
|
messageId: "unknownWithSuggestion",
|
|
@@ -2083,7 +2202,8 @@ const enforceCanonical = defineRule({
|
|
|
2083
2202
|
if (!ds) return;
|
|
2084
2203
|
const { cache, entryPoint } = ds;
|
|
2085
2204
|
for (const loc of locations) {
|
|
2086
|
-
const
|
|
2205
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2206
|
+
const classes = split.classes;
|
|
2087
2207
|
const canonicals = Array.from({ length: classes.length });
|
|
2088
2208
|
const arbitraryIdx = [];
|
|
2089
2209
|
const arbitrary = [];
|
|
@@ -2103,7 +2223,7 @@ const enforceCanonical = defineRule({
|
|
|
2103
2223
|
if (canonical === cls) continue;
|
|
2104
2224
|
if (firstNonCanonical) {
|
|
2105
2225
|
firstNonCanonical = false;
|
|
2106
|
-
const fixedValue =
|
|
2226
|
+
const fixedValue = rebuildClassString(split, canonicals);
|
|
2107
2227
|
context.report({
|
|
2108
2228
|
node: loc.node,
|
|
2109
2229
|
messageId: "nonCanonical",
|
|
@@ -2116,7 +2236,7 @@ const enforceCanonical = defineRule({
|
|
|
2116
2236
|
}
|
|
2117
2237
|
});
|
|
2118
2238
|
} else {
|
|
2119
|
-
const fixedValue =
|
|
2239
|
+
const fixedValue = rebuildClassString(split, canonicals);
|
|
2120
2240
|
context.report({
|
|
2121
2241
|
node: loc.node,
|
|
2122
2242
|
messageId: "nonCanonical",
|
|
@@ -2395,7 +2515,8 @@ const enforceSortOrder = defineRule({
|
|
|
2395
2515
|
return result;
|
|
2396
2516
|
}
|
|
2397
2517
|
for (const loc of locations) {
|
|
2398
|
-
const
|
|
2518
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2519
|
+
const classes = split.classes;
|
|
2399
2520
|
if (classes.length < 2) continue;
|
|
2400
2521
|
const sortedNames = mode === "strict" ? sortStrict(classes) : sortDefault(classes);
|
|
2401
2522
|
if (classes.every((name, i) => name === sortedNames[i])) continue;
|
|
@@ -2403,7 +2524,7 @@ const enforceSortOrder = defineRule({
|
|
|
2403
2524
|
node: loc.node,
|
|
2404
2525
|
messageId: "unsorted",
|
|
2405
2526
|
fix(fixer) {
|
|
2406
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc,
|
|
2527
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, sortedNames)));
|
|
2407
2528
|
}
|
|
2408
2529
|
});
|
|
2409
2530
|
}
|
|
@@ -2489,7 +2610,8 @@ const enforceShorthand = defineRule({
|
|
|
2489
2610
|
createOnce(context) {
|
|
2490
2611
|
function check(locations) {
|
|
2491
2612
|
for (const loc of locations) {
|
|
2492
|
-
const
|
|
2613
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2614
|
+
const classes = split.classes;
|
|
2493
2615
|
if (classes.length < 2) continue;
|
|
2494
2616
|
const classSet = new Set(classes);
|
|
2495
2617
|
const values = /* @__PURE__ */ new Set();
|
|
@@ -2518,7 +2640,7 @@ const enforceShorthand = defineRule({
|
|
|
2518
2640
|
replacement: `${importantStart}${rule.replacement}${importantEnd}`
|
|
2519
2641
|
},
|
|
2520
2642
|
fix(fixer) {
|
|
2521
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc,
|
|
2643
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
|
|
2522
2644
|
}
|
|
2523
2645
|
});
|
|
2524
2646
|
}
|
|
@@ -2576,7 +2698,8 @@ const enforceLogical = defineRule({
|
|
|
2576
2698
|
}
|
|
2577
2699
|
function check(locations) {
|
|
2578
2700
|
for (const loc of locations) {
|
|
2579
|
-
const
|
|
2701
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2702
|
+
const classes = split.classes;
|
|
2580
2703
|
const offending = [];
|
|
2581
2704
|
for (const cls of classes) {
|
|
2582
2705
|
const converted = convertClass(cls);
|
|
@@ -2587,7 +2710,7 @@ const enforceLogical = defineRule({
|
|
|
2587
2710
|
}
|
|
2588
2711
|
if (offending.length === 0) continue;
|
|
2589
2712
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2590
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
2713
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2591
2714
|
for (let i = 0; i < offending.length; i++) {
|
|
2592
2715
|
const { cls, replacement } = offending[i];
|
|
2593
2716
|
if (i === 0) context.report({
|
|
@@ -2655,7 +2778,8 @@ const enforcePhysical = defineRule({
|
|
|
2655
2778
|
}
|
|
2656
2779
|
function check(locations) {
|
|
2657
2780
|
for (const loc of locations) {
|
|
2658
|
-
const
|
|
2781
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
2782
|
+
const classes = split.classes;
|
|
2659
2783
|
const offending = [];
|
|
2660
2784
|
for (const cls of classes) {
|
|
2661
2785
|
const converted = convertClass(cls);
|
|
@@ -2666,7 +2790,7 @@ const enforcePhysical = defineRule({
|
|
|
2666
2790
|
}
|
|
2667
2791
|
if (offending.length === 0) continue;
|
|
2668
2792
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2669
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
2793
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2670
2794
|
for (let i = 0; i < offending.length; i++) {
|
|
2671
2795
|
const { cls, replacement } = offending[i];
|
|
2672
2796
|
if (i === 0) context.report({
|
|
@@ -2909,7 +3033,8 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
2909
3033
|
function check(locations) {
|
|
2910
3034
|
const position = getPosition();
|
|
2911
3035
|
for (const loc of locations) {
|
|
2912
|
-
const
|
|
3036
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3037
|
+
const classes = split.classes;
|
|
2913
3038
|
const offending = [];
|
|
2914
3039
|
for (const cls of classes) {
|
|
2915
3040
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
@@ -2921,7 +3046,7 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
2921
3046
|
}
|
|
2922
3047
|
if (offending.length === 0) continue;
|
|
2923
3048
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2924
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3049
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2925
3050
|
for (let i = 0; i < offending.length; i++) {
|
|
2926
3051
|
const { cls, replacement } = offending[i];
|
|
2927
3052
|
if (i === 0) context.report({
|
|
@@ -2995,7 +3120,8 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
2995
3120
|
createOnce(context) {
|
|
2996
3121
|
function check(locations) {
|
|
2997
3122
|
for (const loc of locations) {
|
|
2998
|
-
const
|
|
3123
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3124
|
+
const classes = split.classes;
|
|
2999
3125
|
const offending = [];
|
|
3000
3126
|
for (const cls of classes) {
|
|
3001
3127
|
const fixed = fixClass(cls);
|
|
@@ -3006,7 +3132,7 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
3006
3132
|
}
|
|
3007
3133
|
if (offending.length === 0) continue;
|
|
3008
3134
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3009
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3135
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3010
3136
|
for (let i = 0; i < offending.length; i++) {
|
|
3011
3137
|
const { cls, replacement } = offending[i];
|
|
3012
3138
|
if (i === 0) context.report({
|
|
@@ -3100,7 +3226,8 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
3100
3226
|
function check(locations) {
|
|
3101
3227
|
const syntax = getSyntax();
|
|
3102
3228
|
for (const loc of locations) {
|
|
3103
|
-
const
|
|
3229
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3230
|
+
const classes = split.classes;
|
|
3104
3231
|
const offending = [];
|
|
3105
3232
|
for (const cls of classes) {
|
|
3106
3233
|
const converted = convertClass(cls, syntax);
|
|
@@ -3111,7 +3238,7 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
3111
3238
|
}
|
|
3112
3239
|
if (offending.length === 0) continue;
|
|
3113
3240
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3114
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3241
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3115
3242
|
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
3116
3243
|
for (let i = 0; i < offending.length; i++) {
|
|
3117
3244
|
const { cls, replacement } = offending[i];
|
|
@@ -3572,7 +3699,8 @@ const consistentVariantOrder = defineRule({
|
|
|
3572
3699
|
}
|
|
3573
3700
|
function check(locations) {
|
|
3574
3701
|
for (const loc of locations) {
|
|
3575
|
-
const
|
|
3702
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3703
|
+
const classes = split.classes;
|
|
3576
3704
|
const offending = [];
|
|
3577
3705
|
for (const cls of classes) {
|
|
3578
3706
|
const fixed = reorderClass(cls);
|
|
@@ -3583,7 +3711,7 @@ const consistentVariantOrder = defineRule({
|
|
|
3583
3711
|
}
|
|
3584
3712
|
if (offending.length === 0) continue;
|
|
3585
3713
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3586
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
3714
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3587
3715
|
for (let i = 0; i < offending.length; i++) {
|
|
3588
3716
|
const { cls, replacement } = offending[i];
|
|
3589
3717
|
if (i === 0) context.report({
|
|
@@ -3624,6 +3752,189 @@ const consistentVariantOrder = defineRule({
|
|
|
3624
3752
|
//#endregion
|
|
3625
3753
|
//#region src/rules/enforce-consistent-line-wrapping.ts
|
|
3626
3754
|
const DEFAULT_PRINT_WIDTH = 80;
|
|
3755
|
+
const enforceConsistentLineWrapping = defineRule({
|
|
3756
|
+
meta: {
|
|
3757
|
+
type: "suggestion",
|
|
3758
|
+
docs: { description: "Warn when a class string exceeds the configured print width" },
|
|
3759
|
+
fixable: "whitespace",
|
|
3760
|
+
schema: [{
|
|
3761
|
+
type: "object",
|
|
3762
|
+
properties: {
|
|
3763
|
+
printWidth: { type: "number" },
|
|
3764
|
+
classesPerLine: { type: "number" }
|
|
3765
|
+
},
|
|
3766
|
+
additionalProperties: false
|
|
3767
|
+
}],
|
|
3768
|
+
defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
|
|
3769
|
+
messages: {
|
|
3770
|
+
tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
|
|
3771
|
+
tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
|
|
3772
|
+
}
|
|
3773
|
+
},
|
|
3774
|
+
createOnce(context) {
|
|
3775
|
+
let _printWidth = null;
|
|
3776
|
+
function getPrintWidth() {
|
|
3777
|
+
if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
|
|
3778
|
+
return _printWidth;
|
|
3779
|
+
}
|
|
3780
|
+
let _classesPerLine = null;
|
|
3781
|
+
function getClassesPerLine() {
|
|
3782
|
+
if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
|
|
3783
|
+
return _classesPerLine;
|
|
3784
|
+
}
|
|
3785
|
+
function check(locations) {
|
|
3786
|
+
const printWidth = getPrintWidth();
|
|
3787
|
+
const classesPerLine = getClassesPerLine();
|
|
3788
|
+
for (const loc of locations) {
|
|
3789
|
+
if (loc.value.length > printWidth) context.report({
|
|
3790
|
+
node: loc.node,
|
|
3791
|
+
messageId: "tooLong",
|
|
3792
|
+
data: {
|
|
3793
|
+
length: String(loc.value.length),
|
|
3794
|
+
printWidth: String(printWidth)
|
|
3795
|
+
}
|
|
3796
|
+
});
|
|
3797
|
+
if (classesPerLine !== void 0) {
|
|
3798
|
+
const classes = splitClasses(loc.value);
|
|
3799
|
+
if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
|
|
3800
|
+
const chunks = [];
|
|
3801
|
+
for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
|
|
3802
|
+
const startCol = loc.node.loc?.start.column ?? 0;
|
|
3803
|
+
const indent = " ".repeat(startCol);
|
|
3804
|
+
const fixedValue = chunks.join("\n" + indent);
|
|
3805
|
+
context.report({
|
|
3806
|
+
node: loc.node,
|
|
3807
|
+
messageId: "tooManyPerLine",
|
|
3808
|
+
data: {
|
|
3809
|
+
count: String(classes.length),
|
|
3810
|
+
max: String(classesPerLine)
|
|
3811
|
+
},
|
|
3812
|
+
fix(fixer) {
|
|
3813
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3814
|
+
}
|
|
3815
|
+
});
|
|
3816
|
+
} else context.report({
|
|
3817
|
+
node: loc.node,
|
|
3818
|
+
messageId: "tooManyPerLine",
|
|
3819
|
+
data: {
|
|
3820
|
+
count: String(classes.length),
|
|
3821
|
+
max: String(classesPerLine)
|
|
3822
|
+
}
|
|
3823
|
+
});
|
|
3824
|
+
}
|
|
3825
|
+
}
|
|
3826
|
+
}
|
|
3827
|
+
return createExtractorVisitors(context, check);
|
|
3828
|
+
}
|
|
3829
|
+
});
|
|
3830
|
+
//#endregion
|
|
3831
|
+
//#region src/rules/no-unnecessary-arbitrary-value.ts
|
|
3832
|
+
const noUnnecessaryArbitraryValue = defineRule({
|
|
3833
|
+
meta: {
|
|
3834
|
+
type: "suggestion",
|
|
3835
|
+
docs: { description: "Disallow arbitrary values when a named Tailwind class produces the same CSS" },
|
|
3836
|
+
fixable: "code",
|
|
3837
|
+
schema: [{
|
|
3838
|
+
type: "object",
|
|
3839
|
+
properties: { entryPoint: { type: "string" } },
|
|
3840
|
+
additionalProperties: false
|
|
3841
|
+
}],
|
|
3842
|
+
hasSuggestions: true,
|
|
3843
|
+
messages: {
|
|
3844
|
+
unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead.",
|
|
3845
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3846
|
+
}
|
|
3847
|
+
},
|
|
3848
|
+
createOnce(context) {
|
|
3849
|
+
const getDS = createLazyLoader(context);
|
|
3850
|
+
function check(locations) {
|
|
3851
|
+
const ds = getDS();
|
|
3852
|
+
if (!ds) return;
|
|
3853
|
+
const { cache } = ds;
|
|
3854
|
+
for (const loc of locations) {
|
|
3855
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3856
|
+
const classes = split.classes;
|
|
3857
|
+
const offending = [];
|
|
3858
|
+
for (const cls of classes) {
|
|
3859
|
+
if (!hasArbitraryValue(cls)) continue;
|
|
3860
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3861
|
+
const hasImportantPrefix = utility.startsWith("!");
|
|
3862
|
+
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3863
|
+
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3864
|
+
const named = cache.getNamedEquivalent(bareUtility);
|
|
3865
|
+
if (!named) continue;
|
|
3866
|
+
offending.push({
|
|
3867
|
+
cls,
|
|
3868
|
+
replacement: variant + (hasImportantPrefix ? "!" : "") + named + (hasImportantSuffix ? "!" : "")
|
|
3869
|
+
});
|
|
3870
|
+
}
|
|
3871
|
+
if (offending.length === 0) continue;
|
|
3872
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3873
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3874
|
+
for (let i = 0; i < offending.length; i++) {
|
|
3875
|
+
const { cls, replacement } = offending[i];
|
|
3876
|
+
if (i === 0) context.report({
|
|
3877
|
+
node: loc.node,
|
|
3878
|
+
messageId: "unnecessaryArbitrary",
|
|
3879
|
+
data: {
|
|
3880
|
+
className: cls,
|
|
3881
|
+
replacement
|
|
3882
|
+
},
|
|
3883
|
+
fix(fixer) {
|
|
3884
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3885
|
+
}
|
|
3886
|
+
});
|
|
3887
|
+
else context.report({
|
|
3888
|
+
node: loc.node,
|
|
3889
|
+
messageId: "unnecessaryArbitrary",
|
|
3890
|
+
data: {
|
|
3891
|
+
className: cls,
|
|
3892
|
+
replacement
|
|
3893
|
+
},
|
|
3894
|
+
suggest: [{
|
|
3895
|
+
messageId: "suggestReplace",
|
|
3896
|
+
data: {
|
|
3897
|
+
className: cls,
|
|
3898
|
+
replacement
|
|
3899
|
+
},
|
|
3900
|
+
fix(fixer) {
|
|
3901
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3902
|
+
}
|
|
3903
|
+
}]
|
|
3904
|
+
});
|
|
3905
|
+
}
|
|
3906
|
+
}
|
|
3907
|
+
}
|
|
3908
|
+
return createExtractorVisitors(context, check);
|
|
3909
|
+
}
|
|
3910
|
+
});
|
|
3911
|
+
//#endregion
|
|
3912
|
+
//#region src/rules/prefer-theme-tokens.ts
|
|
3913
|
+
/**
|
|
3914
|
+
* Match `prefix-(--name)` or `prefix-(--name)/modifier`.
|
|
3915
|
+
* Captures: 1=prefix, 2=variable name, 3=optional `/modifier` (kept verbatim).
|
|
3916
|
+
*/
|
|
3917
|
+
const PAREN_VAR_RE = /^(.+?)-\(--([\w-]+)\)(\/[^/\s]+)?$/;
|
|
3918
|
+
/**
|
|
3919
|
+
* Match `prefix-[var(--name)]` or `prefix-[var(--name)]/modifier`.
|
|
3920
|
+
* Captures: 1=prefix, 2=variable name, 3=optional `/modifier`.
|
|
3921
|
+
*/
|
|
3922
|
+
const BRACKET_VAR_RE = /^(.+?)-\[var\(--([\w-]+)\)\](\/[^/\s]+)?$/;
|
|
3923
|
+
function detectRawVariable(utility) {
|
|
3924
|
+
const paren = PAREN_VAR_RE.exec(utility);
|
|
3925
|
+
if (paren) return {
|
|
3926
|
+
prefix: paren[1],
|
|
3927
|
+
varName: paren[2],
|
|
3928
|
+
modifier: paren[3] ?? ""
|
|
3929
|
+
};
|
|
3930
|
+
const bracket = BRACKET_VAR_RE.exec(utility);
|
|
3931
|
+
if (bracket) return {
|
|
3932
|
+
prefix: bracket[1],
|
|
3933
|
+
varName: bracket[2],
|
|
3934
|
+
modifier: bracket[3] ?? ""
|
|
3935
|
+
};
|
|
3936
|
+
return null;
|
|
3937
|
+
}
|
|
3627
3938
|
//#endregion
|
|
3628
3939
|
//#region src/index.ts
|
|
3629
3940
|
const plugin = definePlugin({
|
|
@@ -3649,85 +3960,12 @@ const plugin = definePlugin({
|
|
|
3649
3960
|
"no-hardcoded-colors": noHardcodedColors,
|
|
3650
3961
|
"no-contradicting-variants": noContradictingVariants,
|
|
3651
3962
|
"consistent-variant-order": consistentVariantOrder,
|
|
3652
|
-
"enforce-consistent-line-wrapping":
|
|
3963
|
+
"enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
|
|
3964
|
+
"no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue,
|
|
3965
|
+
"prefer-theme-tokens": defineRule({
|
|
3653
3966
|
meta: {
|
|
3654
3967
|
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" },
|
|
3968
|
+
docs: { description: "Prefer named theme-token utilities over raw CSS variable references when a matching utility exists" },
|
|
3731
3969
|
fixable: "code",
|
|
3732
3970
|
schema: [{
|
|
3733
3971
|
type: "object",
|
|
@@ -3736,7 +3974,7 @@ const plugin = definePlugin({
|
|
|
3736
3974
|
}],
|
|
3737
3975
|
hasSuggestions: true,
|
|
3738
3976
|
messages: {
|
|
3739
|
-
|
|
3977
|
+
preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
|
|
3740
3978
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3741
3979
|
}
|
|
3742
3980
|
},
|
|
@@ -3747,29 +3985,34 @@ const plugin = definePlugin({
|
|
|
3747
3985
|
if (!ds) return;
|
|
3748
3986
|
const { cache } = ds;
|
|
3749
3987
|
for (const loc of locations) {
|
|
3750
|
-
const
|
|
3988
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3989
|
+
const classes = split.classes;
|
|
3751
3990
|
const offending = [];
|
|
3752
3991
|
for (const cls of classes) {
|
|
3753
|
-
if (!hasArbitraryValue(cls)) continue;
|
|
3754
3992
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3755
3993
|
const hasImportantPrefix = utility.startsWith("!");
|
|
3756
3994
|
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3757
3995
|
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3758
|
-
const
|
|
3759
|
-
if (!
|
|
3996
|
+
const match = detectRawVariable(bareUtility);
|
|
3997
|
+
if (!match) continue;
|
|
3998
|
+
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
3999
|
+
if (!cache.isValid(candidate)) continue;
|
|
4000
|
+
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
4001
|
+
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) continue;
|
|
4002
|
+
const replacement = variant + (hasImportantPrefix ? "!" : "") + candidate + (hasImportantSuffix ? "!" : "");
|
|
3760
4003
|
offending.push({
|
|
3761
4004
|
cls,
|
|
3762
|
-
replacement
|
|
4005
|
+
replacement
|
|
3763
4006
|
});
|
|
3764
4007
|
}
|
|
3765
4008
|
if (offending.length === 0) continue;
|
|
3766
4009
|
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3767
|
-
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls)
|
|
4010
|
+
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3768
4011
|
for (let i = 0; i < offending.length; i++) {
|
|
3769
4012
|
const { cls, replacement } = offending[i];
|
|
3770
4013
|
if (i === 0) context.report({
|
|
3771
4014
|
node: loc.node,
|
|
3772
|
-
messageId: "
|
|
4015
|
+
messageId: "preferNamed",
|
|
3773
4016
|
data: {
|
|
3774
4017
|
className: cls,
|
|
3775
4018
|
replacement
|
|
@@ -3780,7 +4023,7 @@ const plugin = definePlugin({
|
|
|
3780
4023
|
});
|
|
3781
4024
|
else context.report({
|
|
3782
4025
|
node: loc.node,
|
|
3783
|
-
messageId: "
|
|
4026
|
+
messageId: "preferNamed",
|
|
3784
4027
|
data: {
|
|
3785
4028
|
className: cls,
|
|
3786
4029
|
replacement
|