oxlint-tailwindcss 0.4.1 → 0.6.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
@@ -3,7 +3,7 @@ import { definePlugin, defineRule } from "@oxlint/plugins";
3
3
  import { execFileSync } from "node:child_process";
4
4
  import { createHash } from "node:crypto";
5
5
  import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
6
- import { dirname, join, resolve } from "node:path";
6
+ import { dirname, join, relative, resolve } from "node:path";
7
7
  import { tmpdir } from "node:os";
8
8
  import { Worker } from "node:worker_threads";
9
9
  //#region \0rolldown/runtime.js
@@ -1273,19 +1273,84 @@ function autoDetectEntryPoint(filePath) {
1273
1273
  return null;
1274
1274
  }
1275
1275
  //#endregion
1276
+ //#region src/design-system/debug.ts
1277
+ const PREFIX = "[oxlint-tailwindcss]";
1278
+ let _enabled = null;
1279
+ /**
1280
+ * Check if debug logging is enabled.
1281
+ * Activated by `settings.tailwindcss.debug: true` or `DEBUG=oxlint-tailwindcss` env var.
1282
+ */
1283
+ function isDebugEnabled(settings) {
1284
+ if (process.env.DEBUG === "oxlint-tailwindcss") return true;
1285
+ if (settings !== void 0) {
1286
+ const tw = settings?.tailwindcss;
1287
+ if (tw && typeof tw === "object" && "debug" in tw) return tw.debug === true;
1288
+ }
1289
+ return false;
1290
+ }
1291
+ /**
1292
+ * Enable debug mode (called once when settings become available).
1293
+ */
1294
+ function setDebugEnabled(enabled) {
1295
+ _enabled = enabled;
1296
+ }
1297
+ /**
1298
+ * Log a debug message if debug mode is active.
1299
+ */
1300
+ function debugLog(message) {
1301
+ if (_enabled || process.env.DEBUG === "oxlint-tailwindcss") console.error(`${PREFIX} ${message}`);
1302
+ }
1303
+ //#endregion
1276
1304
  //#region src/design-system/loader.ts
1277
1305
  const dsCache = /* @__PURE__ */ new Map();
1278
1306
  const autoDetectCache = /* @__PURE__ */ new Map();
1279
1307
  let lastLoadedPath = null;
1280
1308
  /**
1281
1309
  * Extracts `entryPoint` from `context.settings.tailwindcss`.
1310
+ * Supports both `string` and `string[]` (for monorepos).
1282
1311
  */
1283
1312
  function entryPointFromSettings(settings) {
1284
1313
  const tw = settings?.tailwindcss;
1285
1314
  if (tw && typeof tw === "object" && "entryPoint" in tw) {
1286
1315
  const ep = tw.entryPoint;
1287
1316
  if (typeof ep === "string") return ep;
1317
+ if (Array.isArray(ep) && ep.length > 0 && ep.every((e) => typeof e === "string")) return ep;
1318
+ }
1319
+ }
1320
+ /**
1321
+ * Given an array of entry points and a file path, pick the one that shares
1322
+ * the longest common directory prefix with the file. Falls back to first entry.
1323
+ */
1324
+ function resolveClosestEntryPoint(entryPoints, filePath) {
1325
+ if (entryPoints.length === 1 || !filePath) return entryPoints[0];
1326
+ const fileDir = dirname(resolve(filePath));
1327
+ let best = entryPoints[0];
1328
+ let bestLen = 0;
1329
+ for (const ep of entryPoints) {
1330
+ const len = commonPrefixLength(fileDir, dirname(resolve(ep)));
1331
+ if (len > bestLen) {
1332
+ bestLen = len;
1333
+ best = ep;
1334
+ }
1335
+ }
1336
+ return best;
1337
+ }
1338
+ function commonPrefixLength(a, b) {
1339
+ const len = Math.min(a.length, b.length);
1340
+ let i = 0;
1341
+ while (i < len && a[i] === b[i]) i++;
1342
+ return i;
1343
+ }
1344
+ /**
1345
+ * Extracts `rootFontSize` from `context.settings.tailwindcss` (default: 16).
1346
+ */
1347
+ function rootFontSizeFromSettings(settings) {
1348
+ const tw = settings?.tailwindcss;
1349
+ if (tw && typeof tw === "object" && "rootFontSize" in tw) {
1350
+ const v = tw.rootFontSize;
1351
+ if (typeof v === "number" && v > 0) return v;
1288
1352
  }
1353
+ return 16;
1289
1354
  }
1290
1355
  /**
1291
1356
  * Extracts `timeout` from `context.settings.tailwindcss`.
@@ -1321,7 +1386,8 @@ function cachedAutoDetect(filePath) {
1321
1386
  * Resolution order: rule option `entryPoint` > `settings.tailwindcss.entryPoint` > auto-detect.
1322
1387
  */
1323
1388
  function getLoadedDesignSystem(entryPoint, settings, filePath) {
1324
- const explicitEntry = entryPoint ?? entryPointFromSettings(settings);
1389
+ const settingsEntry = entryPointFromSettings(settings);
1390
+ const explicitEntry = entryPoint ?? (Array.isArray(settingsEntry) ? resolveClosestEntryPoint(settingsEntry, filePath) : settingsEntry);
1325
1391
  const cssPath = explicitEntry ?? cachedAutoDetect(filePath) ?? lastLoadedPath;
1326
1392
  if (!cssPath) return null;
1327
1393
  const resolvedPath = resolve(cssPath);
@@ -1342,7 +1408,7 @@ function getLoadedDesignSystem(entryPoint, settings, filePath) {
1342
1408
  cache,
1343
1409
  mtime
1344
1410
  });
1345
- console.error(`[oxlint-tailwindcss] Loaded design system from "${resolvedPath}"`);
1411
+ debugLog(`Loaded design system from "${resolvedPath}"`);
1346
1412
  if (explicitEntry) lastLoadedPath = resolvedPath;
1347
1413
  return {
1348
1414
  cache,
@@ -1367,6 +1433,7 @@ function createLazyLoader(context) {
1367
1433
  let lastResult = null;
1368
1434
  let fixedEntryResolved = false;
1369
1435
  let fixedResult = null;
1436
+ let debugInitialized = false;
1370
1437
  return () => {
1371
1438
  let entryPoint;
1372
1439
  try {
@@ -1380,16 +1447,31 @@ function createLazyLoader(context) {
1380
1447
  try {
1381
1448
  filePath = context.filename;
1382
1449
  } catch {}
1383
- const fixedEntry = entryPoint ?? entryPointFromSettings(settings);
1450
+ if (!debugInitialized && settings) {
1451
+ debugInitialized = true;
1452
+ setDebugEnabled(isDebugEnabled(settings));
1453
+ }
1454
+ const settingsEntry = entryPointFromSettings(settings);
1455
+ const fixedEntry = entryPoint ?? (typeof settingsEntry === "string" ? settingsEntry : void 0);
1384
1456
  if (fixedEntry) {
1385
1457
  if (fixedEntryResolved) return fixedResult;
1386
1458
  fixedEntryResolved = true;
1387
1459
  fixedResult = getLoadedDesignSystem(fixedEntry, settings, filePath);
1460
+ if (fixedResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), fixedResult.entryPoint)}`);
1388
1461
  return fixedResult;
1389
1462
  }
1463
+ if (Array.isArray(settingsEntry)) {
1464
+ const closest = resolveClosestEntryPoint(settingsEntry, filePath);
1465
+ if (filePath && filePath === lastFilePath) return lastResult;
1466
+ if (filePath) lastFilePath = filePath;
1467
+ lastResult = getLoadedDesignSystem(closest, settings, filePath);
1468
+ if (lastResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
1469
+ return lastResult;
1470
+ }
1390
1471
  if (filePath && filePath === lastFilePath) return lastResult;
1391
1472
  if (filePath) lastFilePath = filePath;
1392
1473
  lastResult = getLoadedDesignSystem(void 0, settings, filePath);
1474
+ if (lastResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
1393
1475
  return lastResult;
1394
1476
  };
1395
1477
  }
@@ -1411,6 +1493,19 @@ function safeOptions(context) {
1411
1493
  return;
1412
1494
  }
1413
1495
  }
1496
+ /**
1497
+ * Safely read context.settings.
1498
+ *
1499
+ * Like `safeOptions`, `context.settings` may not be accessible in `createOnce()`.
1500
+ * The try/catch handles that gracefully.
1501
+ */
1502
+ function safeSettings(context) {
1503
+ try {
1504
+ return context.settings ?? void 0;
1505
+ } catch {
1506
+ return;
1507
+ }
1508
+ }
1414
1509
  //#endregion
1415
1510
  //#region src/rules/no-deprecated-classes.ts
1416
1511
  const DEPRECATED_MAP = {
@@ -1440,7 +1535,11 @@ const noDeprecatedClasses = defineRule({
1440
1535
  properties: { entryPoint: { type: "string" } },
1441
1536
  additionalProperties: false
1442
1537
  }],
1443
- messages: { deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead." }
1538
+ hasSuggestions: true,
1539
+ messages: {
1540
+ deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead.",
1541
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
1542
+ }
1444
1543
  },
1445
1544
  createOnce(context) {
1446
1545
  const getDS = createLazyLoader(context);
@@ -1488,7 +1587,17 @@ const noDeprecatedClasses = defineRule({
1488
1587
  data: {
1489
1588
  className: cls,
1490
1589
  replacement
1491
- }
1590
+ },
1591
+ suggest: [{
1592
+ messageId: "suggestReplace",
1593
+ data: {
1594
+ className: cls,
1595
+ replacement
1596
+ },
1597
+ fix(fixer) {
1598
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
1599
+ }
1600
+ }]
1492
1601
  });
1493
1602
  }
1494
1603
  }
@@ -1517,9 +1626,15 @@ const noUnknownClasses = defineRule({
1517
1626
  },
1518
1627
  additionalProperties: false
1519
1628
  }],
1629
+ hasSuggestions: true,
1630
+ defaultOptions: [{
1631
+ allowlist: [],
1632
+ ignorePrefixes: []
1633
+ }],
1520
1634
  messages: {
1521
1635
  unknown: "\"{{className}}\" is not a valid Tailwind class.",
1522
- unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?"
1636
+ unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
1637
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
1523
1638
  }
1524
1639
  },
1525
1640
  createOnce(context) {
@@ -1560,15 +1675,27 @@ const noUnknownClasses = defineRule({
1560
1675
  if (cache.isValid(stripped)) continue;
1561
1676
  if (DEPRECATED_MAP[stripped]) continue;
1562
1677
  const suggestion = findBestSuggestion(stripped, cache.validClasses);
1563
- if (suggestion) context.report({
1564
- node: loc.node,
1565
- messageId: "unknownWithSuggestion",
1566
- data: {
1567
- className: cls,
1568
- suggestion
1569
- }
1570
- });
1571
- else context.report({
1678
+ if (suggestion) {
1679
+ const fixedValue = classes.map((c) => c === cls ? suggestion : c).join(" ");
1680
+ context.report({
1681
+ node: loc.node,
1682
+ messageId: "unknownWithSuggestion",
1683
+ data: {
1684
+ className: cls,
1685
+ suggestion
1686
+ },
1687
+ suggest: [{
1688
+ messageId: "suggestReplace",
1689
+ data: {
1690
+ className: cls,
1691
+ replacement: suggestion
1692
+ },
1693
+ fix(fixer) {
1694
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
1695
+ }
1696
+ }]
1697
+ });
1698
+ } else context.report({
1572
1699
  node: loc.node,
1573
1700
  messageId: "unknown",
1574
1701
  data: { className: cls }
@@ -1679,7 +1806,187 @@ const noConflictingClasses = defineRule({
1679
1806
  }
1680
1807
  });
1681
1808
  //#endregion
1809
+ //#region src/design-system/canonicalize-service.ts
1810
+ /**
1811
+ * Persistent canonicalize service using worker_threads + SharedArrayBuffer.
1812
+ *
1813
+ * Calls `ds.canonicalizeCandidates(classes, { rem })` dynamically via a worker
1814
+ * thread. Same pattern as sort-service.ts — loads the DS once, then accepts
1815
+ * requests synchronously via Atomics.wait().
1816
+ *
1817
+ * This enables canonicalization of arbitrary user classes (e.g. `p-[2px]` → `p-0.5`)
1818
+ * which can't be precomputed because the input space is infinite.
1819
+ */
1820
+ const BUFFER_SIZE$1 = 4 * 1024 * 1024;
1821
+ const HEADER_INTS$1 = 4;
1822
+ const DATA_OFFSET$1 = HEADER_INTS$1 * 4 + 4;
1823
+ const INIT_TIMEOUT$1 = 3e4;
1824
+ const REQUEST_TIMEOUT$1 = 1e4;
1825
+ const WORKER_SCRIPT$1 = `
1826
+ const { workerData } = require('worker_threads');
1827
+
1828
+ async function main() {
1829
+ const { sharedBuffer, cssPath } = workerData;
1830
+ const control = new Int32Array(sharedBuffer, 0, ${HEADER_INTS$1});
1831
+ const lengthView = new DataView(sharedBuffer, ${HEADER_INTS$1 * 4}, 4);
1832
+ const dataArea = new Uint8Array(sharedBuffer, ${DATA_OFFSET$1});
1833
+
1834
+ let ds;
1835
+ try {
1836
+ const { __unstable__loadDesignSystem } = require(workerData.tailwindNodePath);
1837
+ const { readFileSync } = require('fs');
1838
+ const { dirname } = require('path');
1839
+ const css = readFileSync(cssPath, 'utf-8');
1840
+ ds = await __unstable__loadDesignSystem(css, { base: dirname(cssPath) });
1841
+ } catch {
1842
+ Atomics.store(control, 2, -1);
1843
+ Atomics.notify(control, 2);
1844
+ return;
1845
+ }
1846
+
1847
+ // Signal ready
1848
+ Atomics.store(control, 2, 1);
1849
+ Atomics.notify(control, 2);
1850
+
1851
+ // Request loop
1852
+ while (true) {
1853
+ Atomics.wait(control, 0, 0);
1854
+
1855
+ const len = lengthView.getUint32(0);
1856
+ const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
1857
+ Atomics.store(control, 0, 0);
1858
+
1859
+ let response;
1860
+ try {
1861
+ const { classes, rem } = JSON.parse(requestStr);
1862
+ const options = rem ? { rem } : undefined;
1863
+ const result = ds.canonicalizeCandidates(classes, options);
1864
+ response = Buffer.from(JSON.stringify(result), 'utf-8');
1865
+ } catch {
1866
+ response = Buffer.from('null', 'utf-8');
1867
+ }
1868
+
1869
+ dataArea.set(response, 0);
1870
+ lengthView.setUint32(0, response.length);
1871
+
1872
+ Atomics.store(control, 1, 1);
1873
+ Atomics.notify(control, 1);
1874
+ }
1875
+ }
1876
+ main();
1877
+ `;
1878
+ let worker$1 = null;
1879
+ let controlArray$1 = null;
1880
+ let lengthView$1 = null;
1881
+ let dataArea$1 = null;
1882
+ let initialized$1 = false;
1883
+ let available$1 = true;
1884
+ let currentCssPath$1 = null;
1885
+ function ensureService$1(cssPath) {
1886
+ if (initialized$1 && currentCssPath$1 === cssPath) return available$1;
1887
+ if (initialized$1) {
1888
+ cleanup$1();
1889
+ initialized$1 = false;
1890
+ available$1 = true;
1891
+ controlArray$1 = null;
1892
+ lengthView$1 = null;
1893
+ dataArea$1 = null;
1894
+ }
1895
+ initialized$1 = true;
1896
+ currentCssPath$1 = cssPath;
1897
+ try {
1898
+ const tailwindNodePath = __require.resolve("@tailwindcss/node");
1899
+ const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE$1);
1900
+ controlArray$1 = new Int32Array(sharedBuffer, 0, HEADER_INTS$1);
1901
+ lengthView$1 = new DataView(sharedBuffer, HEADER_INTS$1 * 4, 4);
1902
+ dataArea$1 = new Uint8Array(sharedBuffer, DATA_OFFSET$1);
1903
+ worker$1 = new Worker(WORKER_SCRIPT$1, {
1904
+ eval: true,
1905
+ workerData: {
1906
+ sharedBuffer,
1907
+ cssPath,
1908
+ tailwindNodePath
1909
+ }
1910
+ });
1911
+ worker$1.unref();
1912
+ worker$1.on("error", () => {
1913
+ available$1 = false;
1914
+ worker$1 = null;
1915
+ });
1916
+ worker$1.on("exit", () => {
1917
+ worker$1 = null;
1918
+ });
1919
+ if (Atomics.wait(controlArray$1, 2, 0, INIT_TIMEOUT$1) === "timed-out" || controlArray$1[2] === -1) {
1920
+ available$1 = false;
1921
+ cleanup$1();
1922
+ return false;
1923
+ }
1924
+ process.on("exit", cleanup$1);
1925
+ return true;
1926
+ } catch {
1927
+ available$1 = false;
1928
+ cleanup$1();
1929
+ return false;
1930
+ }
1931
+ }
1932
+ function cleanup$1() {
1933
+ if (worker$1) {
1934
+ try {
1935
+ worker$1.terminate();
1936
+ } catch {}
1937
+ worker$1 = null;
1938
+ }
1939
+ }
1940
+ /**
1941
+ * Canonicalize classes using the Tailwind CSS design system via worker thread.
1942
+ * Returns the canonicalized class array, or null if the service is unavailable.
1943
+ */
1944
+ function canonicalizeClassesSync(cssPath, classes, rem) {
1945
+ if (!ensureService$1(cssPath)) return null;
1946
+ if (!controlArray$1 || !dataArea$1 || !lengthView$1) return null;
1947
+ try {
1948
+ const request = Buffer.from(JSON.stringify({
1949
+ classes,
1950
+ rem
1951
+ }), "utf-8");
1952
+ if (request.length > BUFFER_SIZE$1 - DATA_OFFSET$1) return null;
1953
+ dataArea$1.set(request, 0);
1954
+ lengthView$1.setUint32(0, request.length);
1955
+ Atomics.store(controlArray$1, 0, 1);
1956
+ Atomics.notify(controlArray$1, 0);
1957
+ if (Atomics.wait(controlArray$1, 1, 0, REQUEST_TIMEOUT$1) === "timed-out") {
1958
+ available$1 = false;
1959
+ cleanup$1();
1960
+ return null;
1961
+ }
1962
+ const responseLen = lengthView$1.getUint32(0);
1963
+ const responseStr = Buffer.from(dataArea$1.slice(0, responseLen)).toString("utf-8");
1964
+ Atomics.store(controlArray$1, 1, 0);
1965
+ return JSON.parse(responseStr);
1966
+ } catch {
1967
+ available$1 = false;
1968
+ cleanup$1();
1969
+ return null;
1970
+ }
1971
+ }
1972
+ //#endregion
1682
1973
  //#region src/rules/enforce-canonical.ts
1974
+ /**
1975
+ * Preserve the user's ! position after canonicalization.
1976
+ * canonicalizeCandidates always normalizes ! to suffix, but
1977
+ * enforce-consistent-important-position handles that separately.
1978
+ */
1979
+ function preserveImportantPosition(original, canonicalized) {
1980
+ const origHasPrefix = original.startsWith("!") || /^[a-z0-9[\]*@-]*:!/.test(original);
1981
+ const origHasSuffix = original.endsWith("!") && !origHasPrefix;
1982
+ if (!origHasPrefix && !origHasSuffix) return canonicalized.replace(/!/g, "");
1983
+ const bare = canonicalized.replace(/!/g, "");
1984
+ if (origHasPrefix) {
1985
+ const variantPrefix = bare.slice(0, bare.length - bare.replace(/^[a-z0-9[\]*@-]*:/g, "").length);
1986
+ return variantPrefix + "!" + bare.slice(variantPrefix.length);
1987
+ }
1988
+ return bare + "!";
1989
+ }
1683
1990
  const enforceCanonical = defineRule({
1684
1991
  meta: {
1685
1992
  type: "suggestion",
@@ -1690,17 +1997,29 @@ const enforceCanonical = defineRule({
1690
1997
  properties: { entryPoint: { type: "string" } },
1691
1998
  additionalProperties: false
1692
1999
  }],
1693
- messages: { nonCanonical: "\"{{className}}\" can be written as \"{{canonical}}\". Use the canonical form." }
2000
+ hasSuggestions: true,
2001
+ messages: {
2002
+ nonCanonical: "\"{{className}}\" can be written as \"{{canonical}}\". Use the canonical form.",
2003
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
2004
+ }
1694
2005
  },
1695
2006
  createOnce(context) {
1696
2007
  const getDS = createLazyLoader(context);
2008
+ let _rem = null;
2009
+ function getRem() {
2010
+ if (_rem === null) _rem = rootFontSizeFromSettings(safeSettings(context));
2011
+ return _rem;
2012
+ }
1697
2013
  function check(locations) {
1698
2014
  const ds = getDS();
1699
2015
  if (!ds) return;
1700
- const { cache } = ds;
2016
+ const { cache, entryPoint } = ds;
1701
2017
  for (const loc of locations) {
1702
2018
  const classes = splitClasses(loc.value);
1703
- const canonicals = classes.map((cls) => cache.canonicalize(cls));
2019
+ const dynamic = canonicalizeClassesSync(entryPoint, classes, getRem());
2020
+ let canonicals;
2021
+ if (dynamic) canonicals = classes.map((cls, i) => preserveImportantPosition(cls, dynamic[i]));
2022
+ else canonicals = classes.map((cls) => cache.canonicalize(cls));
1704
2023
  let firstNonCanonical = true;
1705
2024
  for (let i = 0; i < classes.length; i++) {
1706
2025
  const cls = classes[i];
@@ -1720,14 +2039,27 @@ const enforceCanonical = defineRule({
1720
2039
  return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
1721
2040
  }
1722
2041
  });
1723
- } else context.report({
1724
- node: loc.node,
1725
- messageId: "nonCanonical",
1726
- data: {
1727
- className: cls,
1728
- canonical
1729
- }
1730
- });
2042
+ } else {
2043
+ const fixedValue = canonicals.join(" ");
2044
+ context.report({
2045
+ node: loc.node,
2046
+ messageId: "nonCanonical",
2047
+ data: {
2048
+ className: cls,
2049
+ canonical
2050
+ },
2051
+ suggest: [{
2052
+ messageId: "suggestReplace",
2053
+ data: {
2054
+ className: cls,
2055
+ replacement: canonical
2056
+ },
2057
+ fix(fixer) {
2058
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
2059
+ }
2060
+ }]
2061
+ });
2062
+ }
1731
2063
  }
1732
2064
  }
1733
2065
  }
@@ -2149,7 +2481,11 @@ const enforceLogical = defineRule({
2149
2481
  docs: { description: "Enforce logical (RTL-friendly) Tailwind CSS properties instead of physical ones" },
2150
2482
  fixable: "code",
2151
2483
  schema: [],
2152
- messages: { useLogical: "\"{{className}}\" uses a physical property. Use \"{{replacement}}\" for LTR/RTL support." }
2484
+ hasSuggestions: true,
2485
+ messages: {
2486
+ useLogical: "\"{{className}}\" uses a physical property. Use \"{{replacement}}\" for LTR/RTL support.",
2487
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
2488
+ }
2153
2489
  },
2154
2490
  createOnce(context) {
2155
2491
  function convertClass(cls) {
@@ -2196,7 +2532,17 @@ const enforceLogical = defineRule({
2196
2532
  data: {
2197
2533
  className: cls,
2198
2534
  replacement
2199
- }
2535
+ },
2536
+ suggest: [{
2537
+ messageId: "suggestReplace",
2538
+ data: {
2539
+ className: cls,
2540
+ replacement
2541
+ },
2542
+ fix(fixer) {
2543
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
2544
+ }
2545
+ }]
2200
2546
  });
2201
2547
  }
2202
2548
  }
@@ -2214,7 +2560,11 @@ const enforcePhysical = defineRule({
2214
2560
  docs: { description: "Enforce physical Tailwind CSS properties instead of logical ones for consistency in LTR-only projects" },
2215
2561
  fixable: "code",
2216
2562
  schema: [],
2217
- messages: { usePhysical: "\"{{className}}\" uses a logical property. Use \"{{replacement}}\" for consistency." }
2563
+ hasSuggestions: true,
2564
+ messages: {
2565
+ usePhysical: "\"{{className}}\" uses a logical property. Use \"{{replacement}}\" for consistency.",
2566
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
2567
+ }
2218
2568
  },
2219
2569
  createOnce(context) {
2220
2570
  function convertClass(cls) {
@@ -2261,7 +2611,17 @@ const enforcePhysical = defineRule({
2261
2611
  data: {
2262
2612
  className: cls,
2263
2613
  replacement
2264
- }
2614
+ },
2615
+ suggest: [{
2616
+ messageId: "suggestReplace",
2617
+ data: {
2618
+ className: cls,
2619
+ replacement
2620
+ },
2621
+ fix(fixer) {
2622
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
2623
+ }
2624
+ }]
2265
2625
  });
2266
2626
  }
2267
2627
  }
@@ -2281,6 +2641,7 @@ const maxClassCount = defineRule({
2281
2641
  properties: { max: { type: "number" } },
2282
2642
  additionalProperties: false
2283
2643
  }],
2644
+ defaultOptions: [{ max: DEFAULT_MAX }],
2284
2645
  messages: { tooMany: "Too many Tailwind classes ({{count}}). Maximum allowed is {{max}}. Consider extracting into a component or utility." }
2285
2646
  },
2286
2647
  createOnce(context) {
@@ -2450,9 +2811,12 @@ const enforceConsistentImportantPosition = defineRule({
2450
2811
  } },
2451
2812
  additionalProperties: false
2452
2813
  }],
2814
+ hasSuggestions: true,
2815
+ defaultOptions: [{ position: "suffix" }],
2453
2816
  messages: {
2454
2817
  usePrefix: "\"{{className}}\" uses suffix important. Use \"{{replacement}}\" (prefix) instead.",
2455
- useSuffix: "\"{{className}}\" uses prefix important. Use \"{{replacement}}\" (suffix) instead."
2818
+ useSuffix: "\"{{className}}\" uses prefix important. Use \"{{replacement}}\" (suffix) instead.",
2819
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
2456
2820
  }
2457
2821
  },
2458
2822
  createOnce(context) {
@@ -2502,7 +2866,17 @@ const enforceConsistentImportantPosition = defineRule({
2502
2866
  data: {
2503
2867
  className: cls,
2504
2868
  replacement
2505
- }
2869
+ },
2870
+ suggest: [{
2871
+ messageId: "suggestReplace",
2872
+ data: {
2873
+ className: cls,
2874
+ replacement
2875
+ },
2876
+ fix(fixer) {
2877
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
2878
+ }
2879
+ }]
2506
2880
  });
2507
2881
  }
2508
2882
  }
@@ -2537,7 +2911,11 @@ const enforceNegativeArbitraryValues = defineRule({
2537
2911
  docs: { description: "Enforce moving the negative sign inside arbitrary value brackets: -top-[5px] → top-[-5px]" },
2538
2912
  fixable: "code",
2539
2913
  schema: [],
2540
- messages: { moveNegative: "\"{{className}}\" has the negative outside brackets. Use \"{{replacement}}\" instead." }
2914
+ hasSuggestions: true,
2915
+ messages: {
2916
+ moveNegative: "\"{{className}}\" has the negative outside brackets. Use \"{{replacement}}\" instead.",
2917
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
2918
+ }
2541
2919
  },
2542
2920
  createOnce(context) {
2543
2921
  function check(locations) {
@@ -2573,7 +2951,17 @@ const enforceNegativeArbitraryValues = defineRule({
2573
2951
  data: {
2574
2952
  className: cls,
2575
2953
  replacement
2576
- }
2954
+ },
2955
+ suggest: [{
2956
+ messageId: "suggestReplace",
2957
+ data: {
2958
+ className: cls,
2959
+ replacement
2960
+ },
2961
+ fix(fixer) {
2962
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
2963
+ }
2964
+ }]
2577
2965
  });
2578
2966
  }
2579
2967
  }
@@ -2620,9 +3008,12 @@ const enforceConsistentVariableSyntax = defineRule({
2620
3008
  } },
2621
3009
  additionalProperties: false
2622
3010
  }],
3011
+ hasSuggestions: true,
3012
+ defaultOptions: [{ syntax: "shorthand" }],
2623
3013
  messages: {
2624
3014
  useShorthand: "\"{{className}}\" uses explicit var() syntax. Use \"{{replacement}}\" (shorthand) instead.",
2625
- useExplicit: "\"{{className}}\" uses shorthand syntax. Use \"{{replacement}}\" (explicit) instead."
3015
+ useExplicit: "\"{{className}}\" uses shorthand syntax. Use \"{{replacement}}\" (explicit) instead.",
3016
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
2626
3017
  }
2627
3018
  },
2628
3019
  createOnce(context) {
@@ -2666,7 +3057,17 @@ const enforceConsistentVariableSyntax = defineRule({
2666
3057
  data: {
2667
3058
  className: cls,
2668
3059
  replacement
2669
- }
3060
+ },
3061
+ suggest: [{
3062
+ messageId: "suggestReplace",
3063
+ data: {
3064
+ className: cls,
3065
+ replacement
3066
+ },
3067
+ fix(fixer) {
3068
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3069
+ }
3070
+ }]
2670
3071
  });
2671
3072
  }
2672
3073
  }
@@ -2734,6 +3135,7 @@ const noDarkWithoutLight = defineRule({
2734
3135
  } },
2735
3136
  additionalProperties: false
2736
3137
  }],
3138
+ defaultOptions: [{ variants: DEFAULT_VARIANTS }],
2737
3139
  messages: { missingBase: "\"{{className}}\" uses the {{variant}} variant, but there is no base \"{{prefix}}-*\" class on this element." }
2738
3140
  },
2739
3141
  createOnce(context) {
@@ -3016,7 +3418,11 @@ const consistentVariantOrder = defineRule({
3016
3418
  },
3017
3419
  additionalProperties: false
3018
3420
  }],
3019
- messages: { wrongOrder: "\"{{className}}\" has variants in wrong order. Use \"{{replacement}}\" instead." }
3421
+ hasSuggestions: true,
3422
+ messages: {
3423
+ wrongOrder: "\"{{className}}\" has variants in wrong order. Use \"{{replacement}}\" instead.",
3424
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
3425
+ }
3020
3426
  },
3021
3427
  createOnce(context) {
3022
3428
  const getDS = createLazyLoader(context);
@@ -3105,84 +3511,17 @@ const consistentVariantOrder = defineRule({
3105
3511
  data: {
3106
3512
  className: cls,
3107
3513
  replacement
3108
- }
3109
- });
3110
- }
3111
- }
3112
- }
3113
- return createExtractorVisitors(context, check);
3114
- }
3115
- });
3116
- //#endregion
3117
- //#region src/rules/enforce-consistent-line-wrapping.ts
3118
- const DEFAULT_PRINT_WIDTH = 80;
3119
- const enforceConsistentLineWrapping = defineRule({
3120
- meta: {
3121
- type: "suggestion",
3122
- docs: { description: "Warn when a class string exceeds the configured print width" },
3123
- fixable: "whitespace",
3124
- schema: [{
3125
- type: "object",
3126
- properties: {
3127
- printWidth: { type: "number" },
3128
- classesPerLine: { type: "number" }
3129
- },
3130
- additionalProperties: false
3131
- }],
3132
- messages: {
3133
- tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
3134
- tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
3135
- }
3136
- },
3137
- createOnce(context) {
3138
- let _printWidth = null;
3139
- function getPrintWidth() {
3140
- if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
3141
- return _printWidth;
3142
- }
3143
- let _classesPerLine = null;
3144
- function getClassesPerLine() {
3145
- if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
3146
- return _classesPerLine;
3147
- }
3148
- function check(locations) {
3149
- const printWidth = getPrintWidth();
3150
- const classesPerLine = getClassesPerLine();
3151
- for (const loc of locations) {
3152
- if (loc.value.length > printWidth) context.report({
3153
- node: loc.node,
3154
- messageId: "tooLong",
3155
- data: {
3156
- length: String(loc.value.length),
3157
- printWidth: String(printWidth)
3158
- }
3159
- });
3160
- if (classesPerLine !== void 0) {
3161
- const classes = splitClasses(loc.value);
3162
- if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
3163
- const chunks = [];
3164
- for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
3165
- const startCol = loc.node.loc?.start.column ?? 0;
3166
- const indent = " ".repeat(startCol);
3167
- const fixedValue = chunks.join("\n" + indent);
3168
- context.report({
3169
- node: loc.node,
3170
- messageId: "tooManyPerLine",
3514
+ },
3515
+ suggest: [{
3516
+ messageId: "suggestReplace",
3171
3517
  data: {
3172
- count: String(classes.length),
3173
- max: String(classesPerLine)
3518
+ className: cls,
3519
+ replacement
3174
3520
  },
3175
3521
  fix(fixer) {
3176
3522
  return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3177
3523
  }
3178
- });
3179
- } else context.report({
3180
- node: loc.node,
3181
- messageId: "tooManyPerLine",
3182
- data: {
3183
- count: String(classes.length),
3184
- max: String(classesPerLine)
3185
- }
3524
+ }]
3186
3525
  });
3187
3526
  }
3188
3527
  }
@@ -3191,71 +3530,8 @@ const enforceConsistentLineWrapping = defineRule({
3191
3530
  }
3192
3531
  });
3193
3532
  //#endregion
3194
- //#region src/rules/no-unnecessary-arbitrary-value.ts
3195
- const noUnnecessaryArbitraryValue = defineRule({
3196
- meta: {
3197
- type: "suggestion",
3198
- docs: { description: "Disallow arbitrary values when a named Tailwind class produces the same CSS" },
3199
- fixable: "code",
3200
- schema: [{
3201
- type: "object",
3202
- properties: { entryPoint: { type: "string" } },
3203
- additionalProperties: false
3204
- }],
3205
- messages: { unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead." }
3206
- },
3207
- createOnce(context) {
3208
- const getDS = createLazyLoader(context);
3209
- function check(locations) {
3210
- const ds = getDS();
3211
- if (!ds) return;
3212
- const { cache } = ds;
3213
- for (const loc of locations) {
3214
- const classes = splitClasses(loc.value);
3215
- const offending = [];
3216
- for (const cls of classes) {
3217
- if (!hasArbitraryValue(cls)) continue;
3218
- const { utility, variant } = splitUtilityAndVariant(cls);
3219
- const hasImportantPrefix = utility.startsWith("!");
3220
- const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
3221
- const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
3222
- const named = cache.getNamedEquivalent(bareUtility);
3223
- if (!named) continue;
3224
- offending.push({
3225
- cls,
3226
- replacement: variant + (hasImportantPrefix ? "!" : "") + named + (hasImportantSuffix ? "!" : "")
3227
- });
3228
- }
3229
- if (offending.length === 0) continue;
3230
- const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
3231
- const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3232
- for (let i = 0; i < offending.length; i++) {
3233
- const { cls, replacement } = offending[i];
3234
- if (i === 0) context.report({
3235
- node: loc.node,
3236
- messageId: "unnecessaryArbitrary",
3237
- data: {
3238
- className: cls,
3239
- replacement
3240
- },
3241
- fix(fixer) {
3242
- return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3243
- }
3244
- });
3245
- else context.report({
3246
- node: loc.node,
3247
- messageId: "unnecessaryArbitrary",
3248
- data: {
3249
- className: cls,
3250
- replacement
3251
- }
3252
- });
3253
- }
3254
- }
3255
- }
3256
- return createExtractorVisitors(context, check);
3257
- }
3258
- });
3533
+ //#region src/rules/enforce-consistent-line-wrapping.ts
3534
+ const DEFAULT_PRINT_WIDTH = 80;
3259
3535
  //#endregion
3260
3536
  //#region src/index.ts
3261
3537
  const plugin = definePlugin({
@@ -3281,8 +3557,159 @@ const plugin = definePlugin({
3281
3557
  "no-hardcoded-colors": noHardcodedColors,
3282
3558
  "no-contradicting-variants": noContradictingVariants,
3283
3559
  "consistent-variant-order": consistentVariantOrder,
3284
- "enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
3285
- "no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue
3560
+ "enforce-consistent-line-wrapping": defineRule({
3561
+ meta: {
3562
+ type: "suggestion",
3563
+ docs: { description: "Warn when a class string exceeds the configured print width" },
3564
+ fixable: "whitespace",
3565
+ schema: [{
3566
+ type: "object",
3567
+ properties: {
3568
+ printWidth: { type: "number" },
3569
+ classesPerLine: { type: "number" }
3570
+ },
3571
+ additionalProperties: false
3572
+ }],
3573
+ defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
3574
+ messages: {
3575
+ tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
3576
+ tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
3577
+ }
3578
+ },
3579
+ createOnce(context) {
3580
+ let _printWidth = null;
3581
+ function getPrintWidth() {
3582
+ if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
3583
+ return _printWidth;
3584
+ }
3585
+ let _classesPerLine = null;
3586
+ function getClassesPerLine() {
3587
+ if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
3588
+ return _classesPerLine;
3589
+ }
3590
+ function check(locations) {
3591
+ const printWidth = getPrintWidth();
3592
+ const classesPerLine = getClassesPerLine();
3593
+ for (const loc of locations) {
3594
+ if (loc.value.length > printWidth) context.report({
3595
+ node: loc.node,
3596
+ messageId: "tooLong",
3597
+ data: {
3598
+ length: String(loc.value.length),
3599
+ printWidth: String(printWidth)
3600
+ }
3601
+ });
3602
+ if (classesPerLine !== void 0) {
3603
+ const classes = splitClasses(loc.value);
3604
+ if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
3605
+ const chunks = [];
3606
+ for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
3607
+ const startCol = loc.node.loc?.start.column ?? 0;
3608
+ const indent = " ".repeat(startCol);
3609
+ const fixedValue = chunks.join("\n" + indent);
3610
+ context.report({
3611
+ node: loc.node,
3612
+ messageId: "tooManyPerLine",
3613
+ data: {
3614
+ count: String(classes.length),
3615
+ max: String(classesPerLine)
3616
+ },
3617
+ fix(fixer) {
3618
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3619
+ }
3620
+ });
3621
+ } else context.report({
3622
+ node: loc.node,
3623
+ messageId: "tooManyPerLine",
3624
+ data: {
3625
+ count: String(classes.length),
3626
+ max: String(classesPerLine)
3627
+ }
3628
+ });
3629
+ }
3630
+ }
3631
+ }
3632
+ return createExtractorVisitors(context, check);
3633
+ }
3634
+ }),
3635
+ "no-unnecessary-arbitrary-value": defineRule({
3636
+ meta: {
3637
+ type: "suggestion",
3638
+ docs: { description: "Disallow arbitrary values when a named Tailwind class produces the same CSS" },
3639
+ fixable: "code",
3640
+ schema: [{
3641
+ type: "object",
3642
+ properties: { entryPoint: { type: "string" } },
3643
+ additionalProperties: false
3644
+ }],
3645
+ hasSuggestions: true,
3646
+ messages: {
3647
+ unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead.",
3648
+ suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
3649
+ }
3650
+ },
3651
+ createOnce(context) {
3652
+ const getDS = createLazyLoader(context);
3653
+ function check(locations) {
3654
+ const ds = getDS();
3655
+ if (!ds) return;
3656
+ const { cache } = ds;
3657
+ for (const loc of locations) {
3658
+ const classes = splitClasses(loc.value);
3659
+ const offending = [];
3660
+ for (const cls of classes) {
3661
+ if (!hasArbitraryValue(cls)) continue;
3662
+ const { utility, variant } = splitUtilityAndVariant(cls);
3663
+ const hasImportantPrefix = utility.startsWith("!");
3664
+ const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
3665
+ const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
3666
+ const named = cache.getNamedEquivalent(bareUtility);
3667
+ if (!named) continue;
3668
+ offending.push({
3669
+ cls,
3670
+ replacement: variant + (hasImportantPrefix ? "!" : "") + named + (hasImportantSuffix ? "!" : "")
3671
+ });
3672
+ }
3673
+ if (offending.length === 0) continue;
3674
+ const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
3675
+ const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
3676
+ for (let i = 0; i < offending.length; i++) {
3677
+ const { cls, replacement } = offending[i];
3678
+ if (i === 0) context.report({
3679
+ node: loc.node,
3680
+ messageId: "unnecessaryArbitrary",
3681
+ data: {
3682
+ className: cls,
3683
+ replacement
3684
+ },
3685
+ fix(fixer) {
3686
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3687
+ }
3688
+ });
3689
+ else context.report({
3690
+ node: loc.node,
3691
+ messageId: "unnecessaryArbitrary",
3692
+ data: {
3693
+ className: cls,
3694
+ replacement
3695
+ },
3696
+ suggest: [{
3697
+ messageId: "suggestReplace",
3698
+ data: {
3699
+ className: cls,
3700
+ replacement
3701
+ },
3702
+ fix(fixer) {
3703
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
3704
+ }
3705
+ }]
3706
+ });
3707
+ }
3708
+ }
3709
+ }
3710
+ return createExtractorVisitors(context, check);
3711
+ }
3712
+ })
3286
3713
  }
3287
3714
  });
3288
3715
  //#endregion