oxlint-tailwindcss 0.6.2 → 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/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 classes = splitClasses(loc.value);
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)].join(" ");
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.replace(/\s+/g, " ");
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({
@@ -558,6 +626,20 @@ function hasArbitraryValue(cls) {
558
626
  return utility.includes("[") && utility.includes("]");
559
627
  }
560
628
  /**
629
+ * Checks if a class's utility part contains dynamic value syntax: either
630
+ * arbitrary brackets (`w-[200px]`) or CSS variable shorthand (`bg-(--color)`).
631
+ *
632
+ * Unlike `hasArbitraryValue`, this also catches `(--var)` shorthand — both
633
+ * forms are what require the async Tailwind DS to canonicalize, because
634
+ * their value space is unbounded. Simple named classes (`flex`, `px-4`,
635
+ * even with variants like `[&>svg]:hover:flex`) can be canonicalized from
636
+ * the precomputed cache without touching the worker.
637
+ */
638
+ function utilityHasDynamicValue(cls) {
639
+ const utility = extractUtility(cls);
640
+ return utility.includes("[") || utility.includes("(");
641
+ }
642
+ /**
561
643
  * Gets the arbitrary value from a class, or null if none.
562
644
  * e.g. "w-[200px]" → "200px"
563
645
  * "bg-[#ff0000]" → "#ff0000"
@@ -794,7 +876,11 @@ var DesignSystemCache = class DesignSystemCache {
794
876
  * For arbitrary values (bg-[#123]) that aren't in the class list, we use heuristics.
795
877
  */
796
878
  const PRECOMPUTE_SCRIPT = `
797
- const { __unstable__loadDesignSystem } = require('@tailwindcss/node');
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);
798
884
  const { readFileSync } = require('fs');
799
885
  const { dirname, resolve } = require('path');
800
886
 
@@ -1161,20 +1247,24 @@ function loadDesignSystemSync(cssPath, timeout) {
1161
1247
  } catch {}
1162
1248
  return cached;
1163
1249
  }
1250
+ const tailwindNodePath = __require.resolve("@tailwindcss/node");
1164
1251
  const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
1165
1252
  encoding: "utf-8",
1166
1253
  timeout: timeout ?? 3e4,
1167
1254
  maxBuffer: 50 * 1024 * 1024,
1168
1255
  env: {
1169
1256
  ...process.env,
1170
- TAILWIND_CSS_PATH: resolvedPath
1257
+ TAILWIND_CSS_PATH: resolvedPath,
1258
+ TAILWIND_NODE_PATH: tailwindNodePath
1171
1259
  },
1172
1260
  cwd: dirname(resolvedPath)
1173
1261
  });
1174
1262
  writeCacheFiles(contentCachePath, mtimeIndexPath, contentHash, stdout);
1175
1263
  return JSON.parse(stdout);
1176
1264
  } catch (error) {
1177
- console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, error instanceof Error ? error.message : error);
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);
1178
1268
  return null;
1179
1269
  }
1180
1270
  }
@@ -1546,7 +1636,8 @@ const noDeprecatedClasses = defineRule({
1546
1636
  function check(locations) {
1547
1637
  const dsResult = getDS();
1548
1638
  for (const loc of locations) {
1549
- const classes = splitClasses(loc.value);
1639
+ const split = splitClassesWithSeparators(loc.value);
1640
+ const classes = split.classes;
1550
1641
  const offending = [];
1551
1642
  for (const cls of classes) {
1552
1643
  const { utility, variant } = splitUtilityAndVariant(cls);
@@ -1567,7 +1658,7 @@ const noDeprecatedClasses = defineRule({
1567
1658
  }
1568
1659
  if (offending.length === 0) continue;
1569
1660
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
1570
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
1661
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
1571
1662
  for (let i = 0; i < offending.length; i++) {
1572
1663
  const { cls, replacement } = offending[i];
1573
1664
  if (i === 0) context.report({
@@ -1668,7 +1759,8 @@ const noUnknownClasses = defineRule({
1668
1759
  if (!ds) return;
1669
1760
  const { cache } = ds;
1670
1761
  for (const loc of locations) {
1671
- const classes = splitClasses(loc.value);
1762
+ const split = splitClassesWithSeparators(loc.value);
1763
+ const classes = split.classes;
1672
1764
  for (const cls of classes) {
1673
1765
  if (shouldIgnore(cls)) continue;
1674
1766
  const stripped = stripModifiers(cls);
@@ -1676,7 +1768,7 @@ const noUnknownClasses = defineRule({
1676
1768
  if (DEPRECATED_MAP[stripped]) continue;
1677
1769
  const suggestion = findBestSuggestion(stripped, cache.validClasses);
1678
1770
  if (suggestion) {
1679
- const fixedValue = classes.map((c) => c === cls ? suggestion : c).join(" ");
1771
+ const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? suggestion : c));
1680
1772
  context.report({
1681
1773
  node: loc.node,
1682
1774
  messageId: "unknownWithSuggestion",
@@ -1810,12 +1902,24 @@ const noConflictingClasses = defineRule({
1810
1902
  /**
1811
1903
  * Persistent canonicalize service using worker_threads + SharedArrayBuffer.
1812
1904
  *
1813
- * Calls `ds.canonicalizeCandidates(classes, { rem })` dynamically via a worker
1905
+ * Calls `ds.canonicalizeCandidates([cls], { rem })` dynamically via a worker
1814
1906
  * thread. Same pattern as sort-service.ts — loads the DS once, then accepts
1815
1907
  * requests synchronously via Atomics.wait().
1816
1908
  *
1817
1909
  * This enables canonicalization of arbitrary user classes (e.g. `p-[2px]` → `p-0.5`)
1818
1910
  * which can't be precomputed because the input space is infinite.
1911
+ *
1912
+ * ## Per-class cache (process-wide)
1913
+ *
1914
+ * Results are cached by `cssPath\0rem\0class`. The worker is invoked only for
1915
+ * cache misses. In practice the cache converges quickly — after the first few
1916
+ * files any given class has been seen, and every subsequent lookup is O(1).
1917
+ *
1918
+ * ## Per-class worker calls
1919
+ *
1920
+ * `ds.canonicalizeCandidates` deduplicates its input (see sync-loader.ts note),
1921
+ * so the worker MUST call it one class at a time to preserve input order/length.
1922
+ * The cache amortizes the per-call overhead.
1819
1923
  */
1820
1924
  const BUFFER_SIZE$1 = 4 * 1024 * 1024;
1821
1925
  const HEADER_INTS$1 = 4;
@@ -1860,7 +1964,12 @@ async function main() {
1860
1964
  try {
1861
1965
  const { classes, rem } = JSON.parse(requestStr);
1862
1966
  const options = rem ? { rem } : undefined;
1863
- const result = ds.canonicalizeCandidates(classes, options);
1967
+ // canonicalizeCandidates deduplicates its input, so we must call it
1968
+ // one class at a time to preserve order/length. See sync-loader.ts.
1969
+ const result = classes.map((cls) => {
1970
+ const r = ds.canonicalizeCandidates([cls], options);
1971
+ return r[0] ?? cls;
1972
+ });
1864
1973
  response = Buffer.from(JSON.stringify(result), 'utf-8');
1865
1974
  } catch {
1866
1975
  response = Buffer.from('null', 'utf-8');
@@ -1882,6 +1991,7 @@ let dataArea$1 = null;
1882
1991
  let initialized$1 = false;
1883
1992
  let available$1 = true;
1884
1993
  let currentCssPath$1 = null;
1994
+ const canonCache = /* @__PURE__ */ new Map();
1885
1995
  function ensureService$1(cssPath) {
1886
1996
  if (initialized$1 && currentCssPath$1 === cssPath) return available$1;
1887
1997
  if (initialized$1) {
@@ -1937,11 +2047,11 @@ function cleanup$1() {
1937
2047
  }
1938
2048
  }
1939
2049
  /**
1940
- * Canonicalize classes using the Tailwind CSS design system via worker thread.
1941
- * Returns the canonicalized class array, or null if the service is unavailable.
2050
+ * Round-trip raw worker call for a batch of classes (no caching).
2051
+ * Returns the canonicalized class array (same length/order as input),
2052
+ * or null if the service is unavailable.
1942
2053
  */
1943
- function canonicalizeClassesSync(cssPath, classes, rem) {
1944
- if (!ensureService$1(cssPath)) return null;
2054
+ function callWorker(classes, rem) {
1945
2055
  if (!controlArray$1 || !dataArea$1 || !lengthView$1) return null;
1946
2056
  try {
1947
2057
  const request = Buffer.from(JSON.stringify({
@@ -1968,6 +2078,43 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
1968
2078
  return null;
1969
2079
  }
1970
2080
  }
2081
+ /**
2082
+ * Canonicalize classes using the Tailwind CSS design system via worker thread.
2083
+ * Returns the canonicalized class array (same length/order as input), or null
2084
+ * if the service is unavailable.
2085
+ *
2086
+ * Uses a process-wide per-class cache: the worker is invoked only for classes
2087
+ * not already seen with this (cssPath, rem) combination.
2088
+ */
2089
+ function canonicalizeClassesSync(cssPath, classes, rem) {
2090
+ if (!ensureService$1(cssPath)) return null;
2091
+ const out = Array.from({ length: classes.length });
2092
+ const missingIdx = [];
2093
+ const missing = [];
2094
+ const cachePrefix = `${cssPath}\0${rem ?? ""}\0`;
2095
+ for (let i = 0; i < classes.length; i++) {
2096
+ const key = cachePrefix + classes[i];
2097
+ const hit = canonCache.get(key);
2098
+ if (hit !== void 0) out[i] = hit;
2099
+ else {
2100
+ missingIdx.push(i);
2101
+ missing.push(classes[i]);
2102
+ }
2103
+ }
2104
+ if (missing.length === 0) return out;
2105
+ const uniqueMissing = [...new Set(missing)];
2106
+ const fresh = callWorker(uniqueMissing, rem);
2107
+ if (!fresh || fresh.length !== uniqueMissing.length) return null;
2108
+ const freshByClass = /* @__PURE__ */ new Map();
2109
+ for (let k = 0; k < uniqueMissing.length; k++) freshByClass.set(uniqueMissing[k], fresh[k]);
2110
+ for (let j = 0; j < missing.length; j++) {
2111
+ const cls = missing[j];
2112
+ const value = freshByClass.get(cls) ?? cls;
2113
+ canonCache.set(cachePrefix + cls, value);
2114
+ out[missingIdx[j]] = value;
2115
+ }
2116
+ return out;
2117
+ }
1971
2118
  //#endregion
1972
2119
  //#region src/rules/enforce-canonical.ts
1973
2120
  /**
@@ -2014,11 +2161,20 @@ const enforceCanonical = defineRule({
2014
2161
  if (!ds) return;
2015
2162
  const { cache, entryPoint } = ds;
2016
2163
  for (const loc of locations) {
2017
- const classes = splitClasses(loc.value);
2018
- const dynamic = canonicalizeClassesSync(entryPoint, classes, getRem());
2019
- let canonicals;
2020
- if (dynamic) canonicals = classes.map((cls, i) => preserveImportantPosition(cls, dynamic[i]));
2021
- else canonicals = classes.map((cls) => cache.canonicalize(cls));
2164
+ const split = splitClassesWithSeparators(loc.value);
2165
+ const classes = split.classes;
2166
+ const canonicals = Array.from({ length: classes.length });
2167
+ const arbitraryIdx = [];
2168
+ const arbitrary = [];
2169
+ for (let i = 0; i < classes.length; i++) if (utilityHasDynamicValue(classes[i])) {
2170
+ arbitraryIdx.push(i);
2171
+ arbitrary.push(classes[i]);
2172
+ } else canonicals[i] = cache.canonicalize(classes[i]);
2173
+ if (arbitrary.length > 0) {
2174
+ const dynamic = canonicalizeClassesSync(entryPoint, arbitrary, getRem());
2175
+ if (dynamic) for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = preserveImportantPosition(arbitrary[k], dynamic[k]);
2176
+ else for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = cache.canonicalize(arbitrary[k]);
2177
+ }
2022
2178
  let firstNonCanonical = true;
2023
2179
  for (let i = 0; i < classes.length; i++) {
2024
2180
  const cls = classes[i];
@@ -2026,7 +2182,7 @@ const enforceCanonical = defineRule({
2026
2182
  if (canonical === cls) continue;
2027
2183
  if (firstNonCanonical) {
2028
2184
  firstNonCanonical = false;
2029
- const fixedValue = canonicals.join(" ");
2185
+ const fixedValue = rebuildClassString(split, canonicals);
2030
2186
  context.report({
2031
2187
  node: loc.node,
2032
2188
  messageId: "nonCanonical",
@@ -2039,7 +2195,7 @@ const enforceCanonical = defineRule({
2039
2195
  }
2040
2196
  });
2041
2197
  } else {
2042
- const fixedValue = canonicals.join(" ");
2198
+ const fixedValue = rebuildClassString(split, canonicals);
2043
2199
  context.report({
2044
2200
  node: loc.node,
2045
2201
  messageId: "nonCanonical",
@@ -2318,7 +2474,8 @@ const enforceSortOrder = defineRule({
2318
2474
  return result;
2319
2475
  }
2320
2476
  for (const loc of locations) {
2321
- const classes = splitClasses(loc.value);
2477
+ const split = splitClassesWithSeparators(loc.value);
2478
+ const classes = split.classes;
2322
2479
  if (classes.length < 2) continue;
2323
2480
  const sortedNames = mode === "strict" ? sortStrict(classes) : sortDefault(classes);
2324
2481
  if (classes.every((name, i) => name === sortedNames[i])) continue;
@@ -2326,7 +2483,7 @@ const enforceSortOrder = defineRule({
2326
2483
  node: loc.node,
2327
2484
  messageId: "unsorted",
2328
2485
  fix(fixer) {
2329
- return fixer.replaceTextRange(loc.range, preserveSpaces(loc, sortedNames.join(" ")));
2486
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, sortedNames)));
2330
2487
  }
2331
2488
  });
2332
2489
  }
@@ -2412,7 +2569,8 @@ const enforceShorthand = defineRule({
2412
2569
  createOnce(context) {
2413
2570
  function check(locations) {
2414
2571
  for (const loc of locations) {
2415
- const classes = splitClasses(loc.value);
2572
+ const split = splitClassesWithSeparators(loc.value);
2573
+ const classes = split.classes;
2416
2574
  if (classes.length < 2) continue;
2417
2575
  const classSet = new Set(classes);
2418
2576
  const values = /* @__PURE__ */ new Set();
@@ -2441,7 +2599,7 @@ const enforceShorthand = defineRule({
2441
2599
  replacement: `${importantStart}${rule.replacement}${importantEnd}`
2442
2600
  },
2443
2601
  fix(fixer) {
2444
- return fixer.replaceTextRange(loc.range, preserveSpaces(loc, remaining.join(" ")));
2602
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
2445
2603
  }
2446
2604
  });
2447
2605
  }
@@ -2499,7 +2657,8 @@ const enforceLogical = defineRule({
2499
2657
  }
2500
2658
  function check(locations) {
2501
2659
  for (const loc of locations) {
2502
- const classes = splitClasses(loc.value);
2660
+ const split = splitClassesWithSeparators(loc.value);
2661
+ const classes = split.classes;
2503
2662
  const offending = [];
2504
2663
  for (const cls of classes) {
2505
2664
  const converted = convertClass(cls);
@@ -2510,7 +2669,7 @@ const enforceLogical = defineRule({
2510
2669
  }
2511
2670
  if (offending.length === 0) continue;
2512
2671
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
2513
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
2672
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
2514
2673
  for (let i = 0; i < offending.length; i++) {
2515
2674
  const { cls, replacement } = offending[i];
2516
2675
  if (i === 0) context.report({
@@ -2578,7 +2737,8 @@ const enforcePhysical = defineRule({
2578
2737
  }
2579
2738
  function check(locations) {
2580
2739
  for (const loc of locations) {
2581
- const classes = splitClasses(loc.value);
2740
+ const split = splitClassesWithSeparators(loc.value);
2741
+ const classes = split.classes;
2582
2742
  const offending = [];
2583
2743
  for (const cls of classes) {
2584
2744
  const converted = convertClass(cls);
@@ -2589,7 +2749,7 @@ const enforcePhysical = defineRule({
2589
2749
  }
2590
2750
  if (offending.length === 0) continue;
2591
2751
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
2592
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
2752
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
2593
2753
  for (let i = 0; i < offending.length; i++) {
2594
2754
  const { cls, replacement } = offending[i];
2595
2755
  if (i === 0) context.report({
@@ -2832,7 +2992,8 @@ const enforceConsistentImportantPosition = defineRule({
2832
2992
  function check(locations) {
2833
2993
  const position = getPosition();
2834
2994
  for (const loc of locations) {
2835
- const classes = splitClasses(loc.value);
2995
+ const split = splitClassesWithSeparators(loc.value);
2996
+ const classes = split.classes;
2836
2997
  const offending = [];
2837
2998
  for (const cls of classes) {
2838
2999
  const { utility, variant } = splitUtilityAndVariant(cls);
@@ -2844,7 +3005,7 @@ const enforceConsistentImportantPosition = defineRule({
2844
3005
  }
2845
3006
  if (offending.length === 0) continue;
2846
3007
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
2847
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3008
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
2848
3009
  for (let i = 0; i < offending.length; i++) {
2849
3010
  const { cls, replacement } = offending[i];
2850
3011
  if (i === 0) context.report({
@@ -2918,7 +3079,8 @@ const enforceNegativeArbitraryValues = defineRule({
2918
3079
  createOnce(context) {
2919
3080
  function check(locations) {
2920
3081
  for (const loc of locations) {
2921
- const classes = splitClasses(loc.value);
3082
+ const split = splitClassesWithSeparators(loc.value);
3083
+ const classes = split.classes;
2922
3084
  const offending = [];
2923
3085
  for (const cls of classes) {
2924
3086
  const fixed = fixClass(cls);
@@ -2929,7 +3091,7 @@ const enforceNegativeArbitraryValues = defineRule({
2929
3091
  }
2930
3092
  if (offending.length === 0) continue;
2931
3093
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
2932
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3094
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
2933
3095
  for (let i = 0; i < offending.length; i++) {
2934
3096
  const { cls, replacement } = offending[i];
2935
3097
  if (i === 0) context.report({
@@ -3023,7 +3185,8 @@ const enforceConsistentVariableSyntax = defineRule({
3023
3185
  function check(locations) {
3024
3186
  const syntax = getSyntax();
3025
3187
  for (const loc of locations) {
3026
- const classes = splitClasses(loc.value);
3188
+ const split = splitClassesWithSeparators(loc.value);
3189
+ const classes = split.classes;
3027
3190
  const offending = [];
3028
3191
  for (const cls of classes) {
3029
3192
  const converted = convertClass(cls, syntax);
@@ -3034,7 +3197,7 @@ const enforceConsistentVariableSyntax = defineRule({
3034
3197
  }
3035
3198
  if (offending.length === 0) continue;
3036
3199
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
3037
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3200
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
3038
3201
  const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
3039
3202
  for (let i = 0; i < offending.length; i++) {
3040
3203
  const { cls, replacement } = offending[i];
@@ -3495,7 +3658,8 @@ const consistentVariantOrder = defineRule({
3495
3658
  }
3496
3659
  function check(locations) {
3497
3660
  for (const loc of locations) {
3498
- const classes = splitClasses(loc.value);
3661
+ const split = splitClassesWithSeparators(loc.value);
3662
+ const classes = split.classes;
3499
3663
  const offending = [];
3500
3664
  for (const cls of classes) {
3501
3665
  const fixed = reorderClass(cls);
@@ -3506,7 +3670,7 @@ const consistentVariantOrder = defineRule({
3506
3670
  }
3507
3671
  if (offending.length === 0) continue;
3508
3672
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
3509
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3673
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
3510
3674
  for (let i = 0; i < offending.length; i++) {
3511
3675
  const { cls, replacement } = offending[i];
3512
3676
  if (i === 0) context.report({
@@ -3547,6 +3711,189 @@ const consistentVariantOrder = defineRule({
3547
3711
  //#endregion
3548
3712
  //#region src/rules/enforce-consistent-line-wrapping.ts
3549
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
+ }
3550
3897
  //#endregion
3551
3898
  //#region src/index.ts
3552
3899
  const plugin = definePlugin({
@@ -3572,85 +3919,12 @@ const plugin = definePlugin({
3572
3919
  "no-hardcoded-colors": noHardcodedColors,
3573
3920
  "no-contradicting-variants": noContradictingVariants,
3574
3921
  "consistent-variant-order": consistentVariantOrder,
3575
- "enforce-consistent-line-wrapping": defineRule({
3576
- meta: {
3577
- type: "suggestion",
3578
- docs: { description: "Warn when a class string exceeds the configured print width" },
3579
- fixable: "whitespace",
3580
- schema: [{
3581
- type: "object",
3582
- properties: {
3583
- printWidth: { type: "number" },
3584
- classesPerLine: { type: "number" }
3585
- },
3586
- additionalProperties: false
3587
- }],
3588
- defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
3589
- messages: {
3590
- tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
3591
- tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
3592
- }
3593
- },
3594
- createOnce(context) {
3595
- let _printWidth = null;
3596
- function getPrintWidth() {
3597
- if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
3598
- return _printWidth;
3599
- }
3600
- let _classesPerLine = null;
3601
- function getClassesPerLine() {
3602
- if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
3603
- return _classesPerLine;
3604
- }
3605
- function check(locations) {
3606
- const printWidth = getPrintWidth();
3607
- const classesPerLine = getClassesPerLine();
3608
- for (const loc of locations) {
3609
- if (loc.value.length > printWidth) context.report({
3610
- node: loc.node,
3611
- messageId: "tooLong",
3612
- data: {
3613
- length: String(loc.value.length),
3614
- printWidth: String(printWidth)
3615
- }
3616
- });
3617
- if (classesPerLine !== void 0) {
3618
- const classes = splitClasses(loc.value);
3619
- if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
3620
- const chunks = [];
3621
- for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
3622
- const startCol = loc.node.loc?.start.column ?? 0;
3623
- const indent = " ".repeat(startCol);
3624
- const fixedValue = chunks.join("\n" + indent);
3625
- context.report({
3626
- node: loc.node,
3627
- messageId: "tooManyPerLine",
3628
- data: {
3629
- count: String(classes.length),
3630
- max: String(classesPerLine)
3631
- },
3632
- fix(fixer) {
3633
- return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3634
- }
3635
- });
3636
- } else context.report({
3637
- node: loc.node,
3638
- messageId: "tooManyPerLine",
3639
- data: {
3640
- count: String(classes.length),
3641
- max: String(classesPerLine)
3642
- }
3643
- });
3644
- }
3645
- }
3646
- }
3647
- return createExtractorVisitors(context, check);
3648
- }
3649
- }),
3650
- "no-unnecessary-arbitrary-value": defineRule({
3922
+ "enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
3923
+ "no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue,
3924
+ "prefer-theme-tokens": defineRule({
3651
3925
  meta: {
3652
3926
  type: "suggestion",
3653
- 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" },
3654
3928
  fixable: "code",
3655
3929
  schema: [{
3656
3930
  type: "object",
@@ -3659,7 +3933,7 @@ const plugin = definePlugin({
3659
3933
  }],
3660
3934
  hasSuggestions: true,
3661
3935
  messages: {
3662
- unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead.",
3936
+ preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
3663
3937
  suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
3664
3938
  }
3665
3939
  },
@@ -3670,29 +3944,34 @@ const plugin = definePlugin({
3670
3944
  if (!ds) return;
3671
3945
  const { cache } = ds;
3672
3946
  for (const loc of locations) {
3673
- const classes = splitClasses(loc.value);
3947
+ const split = splitClassesWithSeparators(loc.value);
3948
+ const classes = split.classes;
3674
3949
  const offending = [];
3675
3950
  for (const cls of classes) {
3676
- if (!hasArbitraryValue(cls)) continue;
3677
3951
  const { utility, variant } = splitUtilityAndVariant(cls);
3678
3952
  const hasImportantPrefix = utility.startsWith("!");
3679
3953
  const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
3680
3954
  const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
3681
- const named = cache.getNamedEquivalent(bareUtility);
3682
- if (!named) continue;
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 ? "!" : "");
3683
3962
  offending.push({
3684
3963
  cls,
3685
- replacement: variant + (hasImportantPrefix ? "!" : "") + named + (hasImportantSuffix ? "!" : "")
3964
+ replacement
3686
3965
  });
3687
3966
  }
3688
3967
  if (offending.length === 0) continue;
3689
3968
  const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
3690
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3969
+ const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
3691
3970
  for (let i = 0; i < offending.length; i++) {
3692
3971
  const { cls, replacement } = offending[i];
3693
3972
  if (i === 0) context.report({
3694
3973
  node: loc.node,
3695
- messageId: "unnecessaryArbitrary",
3974
+ messageId: "preferNamed",
3696
3975
  data: {
3697
3976
  className: cls,
3698
3977
  replacement
@@ -3703,7 +3982,7 @@ const plugin = definePlugin({
3703
3982
  });
3704
3983
  else context.report({
3705
3984
  node: loc.node,
3706
- messageId: "unnecessaryArbitrary",
3985
+ messageId: "preferNamed",
3707
3986
  data: {
3708
3987
  className: cls,
3709
3988
  replacement