oxlint-tailwindcss 1.4.0 → 1.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.cjs +1565 -671
- package/dist/index.mjs +1565 -671
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -731,6 +731,18 @@ function utilityHasDynamicValue(cls) {
|
|
|
731
731
|
return utility.includes("[") || utility.includes("(");
|
|
732
732
|
}
|
|
733
733
|
/**
|
|
734
|
+
* A utility the precompute cannot have enumerated, because the VALUE comes from
|
|
735
|
+
* the user rather than from the design system's class list: arbitrary brackets,
|
|
736
|
+
* the `(--var)` shorthand, a `/` modifier, or an off-scale number.
|
|
737
|
+
*
|
|
738
|
+
* Asking the design system about exactly these is what closes the "looks like
|
|
739
|
+
* coverage" false negatives — `p-4 p-[5px]` used to be accepted silently while
|
|
740
|
+
* `p-4 p-6` reported. Takes the bare utility (no variants, no `!`).
|
|
741
|
+
*/
|
|
742
|
+
function isUserValued(utility) {
|
|
743
|
+
return utility.includes("[") || utility.includes("(") || utility.includes("/") || /-\d/.test(utility);
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
734
746
|
* Gets the arbitrary value from a class, or null if none.
|
|
735
747
|
* e.g. "w-[200px]" → "200px"
|
|
736
748
|
* "bg-[#ff0000]" → "#ff0000"
|
|
@@ -947,8 +959,10 @@ function roundRemValue(className) {
|
|
|
947
959
|
//#endregion
|
|
948
960
|
//#region src/design-system/cache.ts
|
|
949
961
|
const EMPTY_DECLARATIONS = [];
|
|
962
|
+
const EMPTY_TOKEN_VALUES = [];
|
|
950
963
|
var DesignSystemCache = class DesignSystemCache {
|
|
951
964
|
canonicalMap = /* @__PURE__ */ new Map();
|
|
965
|
+
deprecatedMap = /* @__PURE__ */ new Map();
|
|
952
966
|
validitySet = /* @__PURE__ */ new Set();
|
|
953
967
|
orderMap = /* @__PURE__ */ new Map();
|
|
954
968
|
declIndex = null;
|
|
@@ -957,6 +971,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
957
971
|
propsMemo = /* @__PURE__ */ new Map();
|
|
958
972
|
partialSet = /* @__PURE__ */ new Set();
|
|
959
973
|
valueIdByText = null;
|
|
974
|
+
nextValueId = 0;
|
|
960
975
|
variantOrderMap = /* @__PURE__ */ new Map();
|
|
961
976
|
variantFactsMap = /* @__PURE__ */ new Map();
|
|
962
977
|
arbitraryEquivMap = /* @__PURE__ */ new Map();
|
|
@@ -967,11 +982,14 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
967
982
|
componentSet = /* @__PURE__ */ new Set();
|
|
968
983
|
themeRefs = /* @__PURE__ */ new Map();
|
|
969
984
|
definedVarSet = /* @__PURE__ */ new Set();
|
|
985
|
+
tokenValuesMap = /* @__PURE__ */ new Map();
|
|
986
|
+
scaleFacts = null;
|
|
970
987
|
static fromPrecomputed(data) {
|
|
971
988
|
const cache = new DesignSystemCache();
|
|
972
989
|
cache._validClasses = data.validClasses;
|
|
973
990
|
for (const cls of data.validClasses) cache.validitySet.add(cls);
|
|
974
991
|
for (const [from, to] of Object.entries(data.canonical)) cache.canonicalMap.set(from, roundRemValue(to));
|
|
992
|
+
if (data.deprecated) for (const [from, to] of Object.entries(data.deprecated)) cache.deprecatedMap.set(from, to);
|
|
975
993
|
for (const [cls, val] of Object.entries(data.order)) {
|
|
976
994
|
const order = BigInt(val);
|
|
977
995
|
cache.orderMap.set(cls, order);
|
|
@@ -993,6 +1011,12 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
993
1011
|
}
|
|
994
1012
|
for (const name of data.definedVars ?? []) cache.definedVarSet.add(name);
|
|
995
1013
|
if (data.themeRefs) for (const [name, refs] of Object.entries(data.themeRefs)) cache.themeRefs.set(name, refs);
|
|
1014
|
+
for (const [prefix, entries] of Object.entries(data.tokenValues ?? {})) cache.tokenValuesMap.set(prefix, entries);
|
|
1015
|
+
if (data.scale) cache.scaleFacts = {
|
|
1016
|
+
unit: data.scale.unit,
|
|
1017
|
+
step: data.scale.step,
|
|
1018
|
+
prefixes: new Set(data.scale.prefixes)
|
|
1019
|
+
};
|
|
996
1020
|
cache._prefix = data.prefix ?? "";
|
|
997
1021
|
if (data.arbitraryEquivalents) for (const [arb, named] of Object.entries(data.arbitraryEquivalents)) cache.arbitraryEquivMap.set(arb, named);
|
|
998
1022
|
return cache;
|
|
@@ -1026,6 +1050,24 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1026
1050
|
return className;
|
|
1027
1051
|
}
|
|
1028
1052
|
/**
|
|
1053
|
+
* The v4 name of a class Tailwind renamed, or `null` if it isn't a renamed
|
|
1054
|
+
* spelling. Takes the BARE utility (no variants, no `!`) — the caller has
|
|
1055
|
+
* already split those off to rebuild the replacement around them.
|
|
1056
|
+
*
|
|
1057
|
+
* Derived by the precompute from what `canonicalizeCandidates` reports for the
|
|
1058
|
+
* v3 spellings, rather than from a map maintained by hand. Two rules read it:
|
|
1059
|
+
* `no-deprecated-classes`, which owns the diagnostic, and `enforce-canonical`,
|
|
1060
|
+
* which stays quiet about these so one class doesn't produce two identical
|
|
1061
|
+
* rewrites.
|
|
1062
|
+
*/
|
|
1063
|
+
deprecatedReplacement(bareUtility) {
|
|
1064
|
+
return this.deprecatedMap.get(this.stripProjectPrefix(bareUtility)) ?? null;
|
|
1065
|
+
}
|
|
1066
|
+
/** Whether the precompute produced a deprecation map at all (older artifacts didn't). */
|
|
1067
|
+
get hasDeprecatedMap() {
|
|
1068
|
+
return this.deprecatedMap.size > 0;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1029
1071
|
* Resolve a variant name to its priority in the CSS output order.
|
|
1030
1072
|
* Named variants use the precomputed map. Arbitrary variants use pattern-based heuristics
|
|
1031
1073
|
* to approximate their position relative to named variants.
|
|
@@ -1084,6 +1126,44 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1084
1126
|
}
|
|
1085
1127
|
return this._knownPrefixes;
|
|
1086
1128
|
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Is this class name in the precomputed set VERBATIM?
|
|
1131
|
+
*
|
|
1132
|
+
* `isValid` is deliberately tolerant: it accepts anything shaped like a dynamic
|
|
1133
|
+
* value (`w-45`, `bg-red-5000`, `bg-red-500/foo`) because there used to be no
|
|
1134
|
+
* way to ask the design system at lint time. That tolerance is what let
|
|
1135
|
+
* `no-unknown-classes` accept classes Tailwind compiles to nothing. This is the
|
|
1136
|
+
* exact answer, so a rule can tell "known to produce CSS" from "shaped like it
|
|
1137
|
+
* might" and ask the design system about the difference.
|
|
1138
|
+
*/
|
|
1139
|
+
isKnownClass(className) {
|
|
1140
|
+
return this.validitySet.has(this.stripProjectPrefix(className));
|
|
1141
|
+
}
|
|
1142
|
+
/** Variant names the design system reports, for suggesting a typo's neighbour. */
|
|
1143
|
+
variantNames() {
|
|
1144
|
+
return [...this.variantOrderMap.keys()];
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* The numeric theme tokens this utility prefix can be written with, as
|
|
1148
|
+
* `[literal value, class]`. Empty when the prefix has none.
|
|
1149
|
+
*/
|
|
1150
|
+
tokenValuesFor(prefix) {
|
|
1151
|
+
return this.tokenValuesMap.get(this.stripProjectPrefix(prefix)) ?? EMPTY_TOKEN_VALUES;
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* The spacing scale as the design system describes it: the resolved value of
|
|
1155
|
+
* `--spacing`, the granularity Tailwind's own enumerated steps use, and whether
|
|
1156
|
+
* a given prefix reads it. `null` when the theme has no `--spacing`.
|
|
1157
|
+
*/
|
|
1158
|
+
get scale() {
|
|
1159
|
+
return this.scaleFacts ? {
|
|
1160
|
+
unit: this.scaleFacts.unit,
|
|
1161
|
+
step: this.scaleFacts.step
|
|
1162
|
+
} : null;
|
|
1163
|
+
}
|
|
1164
|
+
readsScale(prefix) {
|
|
1165
|
+
return this.scaleFacts?.prefixes.has(this.stripProjectPrefix(prefix)) ?? false;
|
|
1166
|
+
}
|
|
1087
1167
|
isValid(className) {
|
|
1088
1168
|
if (this.validitySet.has(className)) return true;
|
|
1089
1169
|
const utility = extractUtility(className);
|
|
@@ -1228,7 +1308,10 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1228
1308
|
internDeclarations(className, raws, valueFacts) {
|
|
1229
1309
|
const index = this.declIndex;
|
|
1230
1310
|
if (!index) return EMPTY_DECLARATIONS;
|
|
1231
|
-
if (!this.valueIdByText)
|
|
1311
|
+
if (!this.valueIdByText) {
|
|
1312
|
+
this.valueIdByText = new Map(index.values.map((value, id) => [value, id]));
|
|
1313
|
+
this.nextValueId = index.values.length;
|
|
1314
|
+
}
|
|
1232
1315
|
const decls = [];
|
|
1233
1316
|
let hasElement = false;
|
|
1234
1317
|
let hasDescendant = false;
|
|
@@ -1236,8 +1319,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1236
1319
|
for (const [scopeToken, prop, value] of raws) {
|
|
1237
1320
|
let valueId = this.valueIdByText.get(value);
|
|
1238
1321
|
if (valueId === void 0) {
|
|
1239
|
-
valueId =
|
|
1240
|
-
index.values.push(value);
|
|
1322
|
+
valueId = this.nextValueId++;
|
|
1241
1323
|
this.valueIdByText.set(value, valueId);
|
|
1242
1324
|
}
|
|
1243
1325
|
const facts = valueFacts[value];
|
|
@@ -1421,6 +1503,29 @@ function safeGetDS(getDS, context, node) {
|
|
|
1421
1503
|
throw err;
|
|
1422
1504
|
}
|
|
1423
1505
|
}
|
|
1506
|
+
/**
|
|
1507
|
+
* `safeGetDS`'s quiet sibling, for the DS-OPTIONAL rules.
|
|
1508
|
+
*
|
|
1509
|
+
* A handful of rules work without a design system — their static fallback is
|
|
1510
|
+
* itself deterministic (`consistent-variant-order`'s variant table,
|
|
1511
|
+
* `no-dark-without-light`'s prefix grouping) — and an entry point only makes
|
|
1512
|
+
* them more accurate. Those rules must never emit `designSystemUnavailable`:
|
|
1513
|
+
* they were usable with no configuration before the DS lookup existed and have
|
|
1514
|
+
* to stay that way, or adding the lookup would break every project that runs
|
|
1515
|
+
* them without a CSS entry point.
|
|
1516
|
+
*
|
|
1517
|
+
* So: returns the load result, or `null` when the design system is unavailable
|
|
1518
|
+
* for a reason the plugin knows about. Non-fatal errors still propagate, since
|
|
1519
|
+
* those are bugs rather than missing configuration.
|
|
1520
|
+
*/
|
|
1521
|
+
function softGetDS(getDS) {
|
|
1522
|
+
try {
|
|
1523
|
+
return getDS();
|
|
1524
|
+
} catch (err) {
|
|
1525
|
+
if (isFatalError(err)) return null;
|
|
1526
|
+
throw err;
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1424
1529
|
//#endregion
|
|
1425
1530
|
//#region src/design-system/tailwind-node.ts
|
|
1426
1531
|
/**
|
|
@@ -1735,7 +1840,12 @@ function resolveImport(specifier, baseDir) {
|
|
|
1735
1840
|
return null;
|
|
1736
1841
|
}
|
|
1737
1842
|
|
|
1738
|
-
|
|
1843
|
+
// The entry CSS plus every \`@import\` that resolves, one level deep. Shared by
|
|
1844
|
+
// the component-class scan and the \`definedVars\` scan: a custom property declared
|
|
1845
|
+
// in an imported file is as real to the browser as one in the entry, and treating
|
|
1846
|
+
// it as undefined makes prefer-theme-tokens propose a token that can change the
|
|
1847
|
+
// design (the #78 hazard, in miniature).
|
|
1848
|
+
function collectCssSources(cssPath, baseDir) {
|
|
1739
1849
|
let css;
|
|
1740
1850
|
try { css = readFileSync(cssPath, 'utf-8'); } catch { return []; }
|
|
1741
1851
|
const files = [css];
|
|
@@ -1747,6 +1857,11 @@ function extractComponentClasses(cssPath, baseDir) {
|
|
|
1747
1857
|
try { files.push(readFileSync(resolved, 'utf-8')); } catch {}
|
|
1748
1858
|
}
|
|
1749
1859
|
}
|
|
1860
|
+
return files;
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
function extractComponentClasses(cssPath, baseDir) {
|
|
1864
|
+
const files = collectCssSources(cssPath, baseDir);
|
|
1750
1865
|
const result = [];
|
|
1751
1866
|
for (const content of files) {
|
|
1752
1867
|
// Scan both @layer components AND @layer utilities
|
|
@@ -1930,11 +2045,17 @@ async function main() {
|
|
|
1930
2045
|
// their v4 equivalent, so we feed them in explicitly. Without this pass,
|
|
1931
2046
|
// classes like \`break-words\` would be invisible to enforce-canonical
|
|
1932
2047
|
// (issue #16).
|
|
1933
|
-
//
|
|
1934
|
-
//
|
|
1935
|
-
//
|
|
1936
|
-
//
|
|
1937
|
-
|
|
2048
|
+
//
|
|
2049
|
+
// Two kinds, and the difference is what \`deprecated\` records:
|
|
2050
|
+
// - RENAMES: a v3 spelling of a utility v4 calls something else. Mirrors the
|
|
2051
|
+
// internal v3->v4 map in tailwindcss/canonicalize. These ARE deprecated, and
|
|
2052
|
+
// no-deprecated-classes owns them.
|
|
2053
|
+
// - Pattern renames (start-* -> inset-s-*, end-* -> inset-e-*), derived from
|
|
2054
|
+
// the inset-{s,e}-* utilities in validClasses so every numeric/named value is
|
|
2055
|
+
// covered. These are NOT deprecated: \`start-2\` is current Tailwind (it is
|
|
2056
|
+
// what the docs use), it just isn't the canonical spelling. Calling it
|
|
2057
|
+
// deprecated would be a lie, so only enforce-canonical speaks about it.
|
|
2058
|
+
const v3Renames = [
|
|
1938
2059
|
'order-none',
|
|
1939
2060
|
'break-words',
|
|
1940
2061
|
'overflow-ellipsis',
|
|
@@ -1949,12 +2070,15 @@ async function main() {
|
|
|
1949
2070
|
'bg-left-top', 'bg-right-top', 'bg-left-bottom', 'bg-right-bottom',
|
|
1950
2071
|
'object-left-top', 'object-right-top', 'object-left-bottom', 'object-right-bottom',
|
|
1951
2072
|
];
|
|
2073
|
+
const renameSet = new Set(v3Renames);
|
|
2074
|
+
const legacyCandidates = [...v3Renames];
|
|
1952
2075
|
for (const cls of validClasses) {
|
|
1953
2076
|
if (cls.startsWith('inset-s-')) legacyCandidates.push('start-' + cls.slice(8));
|
|
1954
2077
|
else if (cls.startsWith('-inset-s-')) legacyCandidates.push('-start-' + cls.slice(9));
|
|
1955
2078
|
else if (cls.startsWith('inset-e-')) legacyCandidates.push('end-' + cls.slice(8));
|
|
1956
2079
|
else if (cls.startsWith('-inset-e-')) legacyCandidates.push('-end-' + cls.slice(9));
|
|
1957
2080
|
}
|
|
2081
|
+
const deprecated = {};
|
|
1958
2082
|
const legacyToProcess = legacyCandidates.filter(cls => !validSet.has(cls));
|
|
1959
2083
|
if (legacyToProcess.length > 0) {
|
|
1960
2084
|
const legacyCssResults = ds.candidatesToCss(legacyToProcess.map(pfx));
|
|
@@ -1965,6 +2089,10 @@ async function main() {
|
|
|
1965
2089
|
const canon = result[0] ? unpfx(result[0]) : null;
|
|
1966
2090
|
if (canon && canon !== cls) {
|
|
1967
2091
|
canonical[cls] = canon;
|
|
2092
|
+
// Only a v3 rename that this Tailwind still compiles AND still renames.
|
|
2093
|
+
// A release that drops either of those drops the entry, so the rule can
|
|
2094
|
+
// never suggest a replacement for a class that no longer exists.
|
|
2095
|
+
if (renameSet.has(cls)) deprecated[cls] = canon;
|
|
1968
2096
|
}
|
|
1969
2097
|
// Mark as valid so no-unknown-classes doesn't flag legacy spellings.
|
|
1970
2098
|
validClasses.push(cls);
|
|
@@ -2220,20 +2348,21 @@ async function main() {
|
|
|
2220
2348
|
}
|
|
2221
2349
|
}
|
|
2222
2350
|
|
|
2223
|
-
//
|
|
2224
|
-
//
|
|
2225
|
-
//
|
|
2226
|
-
//
|
|
2227
|
-
//
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
const definedVars = [...new Set(css.match(/--[\\w-]+(?=\\s*:)/g) || [])];
|
|
2351
|
+
// Custom properties the project defines, across the entry AND its resolved
|
|
2352
|
+
// @imports — splitting the theme across files is the normal shadcn/ui layout,
|
|
2353
|
+
// and a definition we cannot see reads as "undefined", which is what makes
|
|
2354
|
+
// prefer-theme-tokens propose a token that means something else. One level
|
|
2355
|
+
// deep, same as the component-class scan it shares \`collectCssSources\` with.
|
|
2356
|
+
const definedVars = [...new Set(
|
|
2357
|
+
collectCssSources(cssPath, base).flatMap(src => src.match(/--[\\w-]+(?=\\s*:)/g) || [])
|
|
2358
|
+
)];
|
|
2232
2359
|
|
|
2233
2360
|
const themeRefs = {};
|
|
2361
|
+
const themeValues = new Map();
|
|
2234
2362
|
if (ds.theme && typeof ds.theme.entries === 'function') {
|
|
2235
2363
|
for (const [name, entry] of ds.theme.entries()) {
|
|
2236
2364
|
const value = entry && typeof entry.value === 'string' ? entry.value : '';
|
|
2365
|
+
themeValues.set(name, value);
|
|
2237
2366
|
if (!value.includes('var(')) continue;
|
|
2238
2367
|
const reads = scanVarReads(value);
|
|
2239
2368
|
const all = [...new Set([...reads[0], ...reads[1]])];
|
|
@@ -2241,7 +2370,77 @@ async function main() {
|
|
|
2241
2370
|
}
|
|
2242
2371
|
}
|
|
2243
2372
|
|
|
2244
|
-
|
|
2373
|
+
// ── What a literal value could have been written as (for prefer-scale-token) ──
|
|
2374
|
+
//
|
|
2375
|
+
// Two families, both derived. Neither is a name table: the emitted CSS says
|
|
2376
|
+
// which variable a class reads and the theme says what that variable is worth.
|
|
2377
|
+
|
|
2378
|
+
// A single declaration whose whole value is one \`var(--x)\` that the theme
|
|
2379
|
+
// resolves to a NUMBER. Numeric because that is the only thing the rule can
|
|
2380
|
+
// compare — a colour token could never match a literal the user typed — and
|
|
2381
|
+
// SINGLE because a class that declares more than the original is not an
|
|
2382
|
+
// equivalent: \`text-sm\` sets \`line-height\` too, so \`text-[14px]\` is not it.
|
|
2383
|
+
const NUMERIC_LITERAL = /^-?\\d*\\.?\\d+(rem|px|em|%|s|ms|deg)?$/;
|
|
2384
|
+
const tokenValues = {};
|
|
2385
|
+
for (const cls of validClasses) {
|
|
2386
|
+
if (cls.includes('[') || cls.includes('/')) continue;
|
|
2387
|
+
const css = declCss[cls];
|
|
2388
|
+
if (!css) continue;
|
|
2389
|
+
const open = css.indexOf('{');
|
|
2390
|
+
const close = css.indexOf('}');
|
|
2391
|
+
if (open < 0 || close < 0) continue;
|
|
2392
|
+
const body = css.slice(open + 1, close).replace(/\\s+/g, ' ').replace(/;\\s*$/, '').trim();
|
|
2393
|
+
const m = /^([a-z-]+):\\s*var\\((--[\\w-]+)\\)$/.exec(body);
|
|
2394
|
+
if (!m) continue;
|
|
2395
|
+
const value = themeValues.get(m[2]);
|
|
2396
|
+
if (!value || !NUMERIC_LITERAL.test(value)) continue;
|
|
2397
|
+
const dash = cls.lastIndexOf('-');
|
|
2398
|
+
if (dash <= 0) continue;
|
|
2399
|
+
const prefix = unpfx(cls).slice(0, unpfx(cls).lastIndexOf('-'));
|
|
2400
|
+
if (!prefix) continue;
|
|
2401
|
+
if (!tokenValues[prefix]) tokenValues[prefix] = [];
|
|
2402
|
+
tokenValues[prefix].push([value, unpfx(cls)]);
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
// The spacing scale: its unit, which prefixes read it, and the granularity
|
|
2406
|
+
// Tailwind's own enumerated steps use. Every step getClassList() lists is a
|
|
2407
|
+
// multiple of that granularity, so deriving it is how the rule stays inside
|
|
2408
|
+
// what Tailwind does instead of inventing a threshold.
|
|
2409
|
+
const scaleUnit = themeValues.get('--spacing') || '';
|
|
2410
|
+
const scalePrefixes = [];
|
|
2411
|
+
if (scaleUnit) {
|
|
2412
|
+
const probes = [...knownPrefixes];
|
|
2413
|
+
const probeResults = ds.candidatesToCss(probes.map(p => pfx(p + '-1')));
|
|
2414
|
+
for (let i = 0; i < probes.length; i++) {
|
|
2415
|
+
const css = probeResults[i];
|
|
2416
|
+
if (!css) continue;
|
|
2417
|
+
// \`size-1\` emits width AND height, both reading the unit — one match is enough.
|
|
2418
|
+
if (css.includes('var(--spacing)')) scalePrefixes.push(probes[i]);
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
let scaleStep = 0;
|
|
2422
|
+
if (scalePrefixes.length > 0) {
|
|
2423
|
+
const steps = new Set();
|
|
2424
|
+
const scaleSet = new Set(scalePrefixes);
|
|
2425
|
+
for (const cls of validClasses) {
|
|
2426
|
+
const dash = cls.lastIndexOf('-');
|
|
2427
|
+
if (dash <= 0) continue;
|
|
2428
|
+
if (!scaleSet.has(cls.slice(0, dash))) continue;
|
|
2429
|
+
const suffix = cls.slice(dash + 1);
|
|
2430
|
+
if (!/^\\d*\\.?\\d+$/.test(suffix)) continue;
|
|
2431
|
+
steps.add(parseFloat(suffix));
|
|
2432
|
+
}
|
|
2433
|
+
const sorted = [...steps].sort((a, b) => a - b);
|
|
2434
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
2435
|
+
const gap = Math.round((sorted[i] - sorted[i - 1]) * 1000) / 1000;
|
|
2436
|
+
if (gap > 0 && (scaleStep === 0 || gap < scaleStep)) scaleStep = gap;
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
const scale = scaleUnit && scalePrefixes.length > 0
|
|
2440
|
+
? { unit: scaleUnit, step: scaleStep || 1, prefixes: scalePrefixes.sort() }
|
|
2441
|
+
: undefined;
|
|
2442
|
+
|
|
2443
|
+
const json = JSON.stringify({ validClasses, canonical, deprecated, order, cssDeclarations, variantOrder, variantFacts, componentClasses, arbitraryEquivalents, themeRefs, definedVars, tokenValues, scale, prefix });
|
|
2245
2444
|
// Atomic write: write to a unique temp path then rename, so a peer isolate
|
|
2246
2445
|
// busy-waiting on the cache file never observes a half-written JSON.
|
|
2247
2446
|
writeFileSync(WD_TMP_PATH, json);
|
|
@@ -2945,351 +3144,107 @@ function resolveEntryPointForFile(ruleOptionEntry, settingsEntry, filePath, cwd
|
|
|
2945
3144
|
throw new MissingEntryPointError(`\`settings.tailwindcss.entryPoint\` is required${filePath ? ` (linting \`${filePath}\`)` : ""}.`, "Set it to a string (single-project) or an array of `{ files, use }` mappings (monorepo).");
|
|
2946
3145
|
}
|
|
2947
3146
|
//#endregion
|
|
2948
|
-
//#region src/
|
|
3147
|
+
//#region src/design-system/ds-worker.ts
|
|
2949
3148
|
/**
|
|
2950
|
-
*
|
|
2951
|
-
*
|
|
2952
|
-
* Nine rules (enforce-canonical, enforce-logical, enforce-physical,
|
|
2953
|
-
* enforce-consistent-variable-syntax, enforce-consistent-important-position,
|
|
2954
|
-
* enforce-negative-arbitrary-values, no-deprecated-classes,
|
|
2955
|
-
* no-unnecessary-arbitrary-value, prefer-theme-tokens) all emit
|
|
2956
|
-
* replacements via the same shape:
|
|
3149
|
+
* Generic worker-thread service wrapping `__unstable__loadDesignSystem`.
|
|
2957
3150
|
*
|
|
2958
|
-
*
|
|
2959
|
-
*
|
|
2960
|
-
*
|
|
2961
|
-
*
|
|
2962
|
-
*
|
|
3151
|
+
* `sort-service.ts` and `canonicalize-service.ts` used to be near-byte-identical
|
|
3152
|
+
* (~500 LOC) — both spawned a worker that loaded the design system, then served
|
|
3153
|
+
* synchronous requests over a SharedArrayBuffer using `Atomics.wait`. The only
|
|
3154
|
+
* real difference was the body of the request loop: `getClassOrder` vs
|
|
3155
|
+
* `canonicalizeCandidates`. This class encapsulates the entire shared protocol
|
|
3156
|
+
* — SharedArrayBuffer layout, state machine, lifecycle, fail-loud — and each
|
|
3157
|
+
* service shrinks to ~40 LOC of singleton + public signature.
|
|
2963
3158
|
*
|
|
2964
|
-
*
|
|
2965
|
-
*
|
|
2966
|
-
*
|
|
3159
|
+
* v1: failures throw `SortServiceError`. The error is sticky for the lifetime
|
|
3160
|
+
* of the process (per cssPath) so subsequent calls don't pay another
|
|
3161
|
+
* init-timeout cost. Callers wrap via `safeGetDS` to surface the failure as a
|
|
3162
|
+
* single `designSystemUnavailable` diagnostic.
|
|
2967
3163
|
*/
|
|
3164
|
+
const BUFFER_SIZE = 4 * 1024 * 1024;
|
|
3165
|
+
const HEADER_INTS = 4;
|
|
3166
|
+
const LENGTH_OFFSET = HEADER_INTS * 4;
|
|
3167
|
+
const DATA_OFFSET = 20;
|
|
3168
|
+
const INIT_TIMEOUT = 6e4;
|
|
3169
|
+
const REQUEST_TIMEOUT = 3e4;
|
|
3170
|
+
const MAX_WORKERS = 8;
|
|
2968
3171
|
/**
|
|
2969
|
-
*
|
|
2970
|
-
*
|
|
2971
|
-
*
|
|
2972
|
-
*
|
|
2973
|
-
*
|
|
3172
|
+
* Build the worker script shared by `sort-service` and `canonicalize-service`.
|
|
3173
|
+
* Owns the entire SharedArrayBuffer protocol (offsets derived from the same
|
|
3174
|
+
* constants the host uses, so they can't drift), the design-system load with
|
|
3175
|
+
* error propagation (DS-M4: the real cause is written into the buffer so the
|
|
3176
|
+
* host can surface it, not a generic "failed to load"), the ready signal, and
|
|
3177
|
+
* the request loop. `handlerExpr` is a function expression `(ds, request) =>
|
|
3178
|
+
* result` — the only part that differs between services.
|
|
2974
3179
|
*/
|
|
2975
|
-
function
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
}
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3044
|
-
}
|
|
3045
|
-
},
|
|
3046
|
-
createOnce(context) {
|
|
3047
|
-
function check(locations) {
|
|
3048
|
-
if (locations.length === 0) return;
|
|
3049
|
-
for (const loc of locations) {
|
|
3050
|
-
const split = splitClassesWithSeparators(loc.value);
|
|
3051
|
-
const offending = split.classes.flatMap((cls) => {
|
|
3052
|
-
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3053
|
-
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3054
|
-
const replacement = DEPRECATED_MAP[bareUtility];
|
|
3055
|
-
return replacement ? [{
|
|
3056
|
-
cls,
|
|
3057
|
-
replacement: variant + reattachImportant(replacement, position)
|
|
3058
|
-
}] : [];
|
|
3059
|
-
});
|
|
3060
|
-
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "deprecated" });
|
|
3061
|
-
}
|
|
3062
|
-
}
|
|
3063
|
-
return createExtractorVisitors(context, check);
|
|
3064
|
-
}
|
|
3065
|
-
});
|
|
3066
|
-
//#endregion
|
|
3067
|
-
//#region src/rules/no-unknown-classes.ts
|
|
3068
|
-
const noUnknownClasses = defineRule({
|
|
3069
|
-
meta: {
|
|
3070
|
-
type: "problem",
|
|
3071
|
-
docs: { description: "Disallow classes that are not defined in the Tailwind CSS design system" },
|
|
3072
|
-
schema: [{
|
|
3073
|
-
type: "object",
|
|
3074
|
-
properties: {
|
|
3075
|
-
entryPoint: { type: "string" },
|
|
3076
|
-
allowlist: {
|
|
3077
|
-
type: "array",
|
|
3078
|
-
items: { type: "string" }
|
|
3079
|
-
},
|
|
3080
|
-
ignorePrefixes: {
|
|
3081
|
-
type: "array",
|
|
3082
|
-
items: { type: "string" }
|
|
3083
|
-
}
|
|
3084
|
-
},
|
|
3085
|
-
additionalProperties: false
|
|
3086
|
-
}],
|
|
3087
|
-
hasSuggestions: true,
|
|
3088
|
-
defaultOptions: [{
|
|
3089
|
-
allowlist: [],
|
|
3090
|
-
ignorePrefixes: []
|
|
3091
|
-
}],
|
|
3092
|
-
messages: {
|
|
3093
|
-
unknown: "\"{{className}}\" is not a valid Tailwind class.",
|
|
3094
|
-
unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
|
|
3095
|
-
missingPrefix: "\"{{className}}\" needs the \"{{prefix}}:\" prefix to produce CSS. Did you mean \"{{suggestion}}\"?",
|
|
3096
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
3097
|
-
...DS_UNAVAILABLE_MESSAGE
|
|
3098
|
-
}
|
|
3099
|
-
},
|
|
3100
|
-
createOnce(context) {
|
|
3101
|
-
const getDS = createLazyLoader(context);
|
|
3102
|
-
const getLazyOptions = createLazyOptions(context, (o) => ({
|
|
3103
|
-
allowlist: new Set(o?.allowlist ?? []),
|
|
3104
|
-
ignorePrefixes: o?.ignorePrefixes ?? []
|
|
3105
|
-
}));
|
|
3106
|
-
function shouldIgnore(className) {
|
|
3107
|
-
const { allowlist, ignorePrefixes } = getLazyOptions();
|
|
3108
|
-
if (allowlist.has(className)) return true;
|
|
3109
|
-
return ignorePrefixes.some((prefix) => className.startsWith(prefix));
|
|
3110
|
-
}
|
|
3111
|
-
function strippedUtility(className) {
|
|
3112
|
-
const { utility, variant } = splitUtilityAndVariant(className);
|
|
3113
|
-
const { bare, position } = splitImportant(utility);
|
|
3114
|
-
return {
|
|
3115
|
-
variant,
|
|
3116
|
-
bare,
|
|
3117
|
-
position
|
|
3118
|
-
};
|
|
3119
|
-
}
|
|
3120
|
-
function check(locations) {
|
|
3121
|
-
if (locations.length === 0) return;
|
|
3122
|
-
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
3123
|
-
if (!ds) return;
|
|
3124
|
-
const { cache } = ds;
|
|
3125
|
-
for (const loc of locations) {
|
|
3126
|
-
const split = splitClassesWithSeparators(loc.value);
|
|
3127
|
-
const classes = split.classes;
|
|
3128
|
-
for (const cls of classes) {
|
|
3129
|
-
if (shouldIgnore(cls)) continue;
|
|
3130
|
-
const validity = cache.classValidity(cls);
|
|
3131
|
-
if (validity === "valid") continue;
|
|
3132
|
-
const { variant, bare, position } = strippedUtility(cls);
|
|
3133
|
-
if (DEPRECATED_MAP[bare]) continue;
|
|
3134
|
-
if (validity === "missing-prefix") {
|
|
3135
|
-
const fixed = `${cache.prefix}:${cls}`;
|
|
3136
|
-
const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? fixed : c));
|
|
3137
|
-
context.report({
|
|
3138
|
-
node: loc.node,
|
|
3139
|
-
messageId: "missingPrefix",
|
|
3140
|
-
data: {
|
|
3141
|
-
className: cls,
|
|
3142
|
-
prefix: cache.prefix,
|
|
3143
|
-
suggestion: fixed
|
|
3144
|
-
},
|
|
3145
|
-
suggest: [{
|
|
3146
|
-
messageId: "suggestReplace",
|
|
3147
|
-
data: {
|
|
3148
|
-
className: cls,
|
|
3149
|
-
replacement: fixed
|
|
3150
|
-
},
|
|
3151
|
-
fix(fixer) {
|
|
3152
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3153
|
-
}
|
|
3154
|
-
}]
|
|
3155
|
-
});
|
|
3156
|
-
continue;
|
|
3157
|
-
}
|
|
3158
|
-
const suggestionBare = findBestSuggestion(bare, cache.validClasses);
|
|
3159
|
-
const suggestion = suggestionBare ? variant + reattachImportant(suggestionBare, position) : null;
|
|
3160
|
-
if (suggestion) {
|
|
3161
|
-
const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? suggestion : c));
|
|
3162
|
-
context.report({
|
|
3163
|
-
node: loc.node,
|
|
3164
|
-
messageId: "unknownWithSuggestion",
|
|
3165
|
-
data: {
|
|
3166
|
-
className: cls,
|
|
3167
|
-
suggestion
|
|
3168
|
-
},
|
|
3169
|
-
suggest: [{
|
|
3170
|
-
messageId: "suggestReplace",
|
|
3171
|
-
data: {
|
|
3172
|
-
className: cls,
|
|
3173
|
-
replacement: suggestion
|
|
3174
|
-
},
|
|
3175
|
-
fix(fixer) {
|
|
3176
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3177
|
-
}
|
|
3178
|
-
}]
|
|
3179
|
-
});
|
|
3180
|
-
} else context.report({
|
|
3181
|
-
node: loc.node,
|
|
3182
|
-
messageId: "unknown",
|
|
3183
|
-
data: { className: cls }
|
|
3184
|
-
});
|
|
3185
|
-
}
|
|
3186
|
-
}
|
|
3187
|
-
}
|
|
3188
|
-
return createExtractorVisitors(context, check);
|
|
3189
|
-
}
|
|
3190
|
-
});
|
|
3191
|
-
//#endregion
|
|
3192
|
-
//#region src/design-system/ds-worker.ts
|
|
3193
|
-
/**
|
|
3194
|
-
* Generic worker-thread service wrapping `__unstable__loadDesignSystem`.
|
|
3195
|
-
*
|
|
3196
|
-
* `sort-service.ts` and `canonicalize-service.ts` used to be near-byte-identical
|
|
3197
|
-
* (~500 LOC) — both spawned a worker that loaded the design system, then served
|
|
3198
|
-
* synchronous requests over a SharedArrayBuffer using `Atomics.wait`. The only
|
|
3199
|
-
* real difference was the body of the request loop: `getClassOrder` vs
|
|
3200
|
-
* `canonicalizeCandidates`. This class encapsulates the entire shared protocol
|
|
3201
|
-
* — SharedArrayBuffer layout, state machine, lifecycle, fail-loud — and each
|
|
3202
|
-
* service shrinks to ~40 LOC of singleton + public signature.
|
|
3203
|
-
*
|
|
3204
|
-
* v1: failures throw `SortServiceError`. The error is sticky for the lifetime
|
|
3205
|
-
* of the process (per cssPath) so subsequent calls don't pay another
|
|
3206
|
-
* init-timeout cost. Callers wrap via `safeGetDS` to surface the failure as a
|
|
3207
|
-
* single `designSystemUnavailable` diagnostic.
|
|
3208
|
-
*/
|
|
3209
|
-
const BUFFER_SIZE = 4 * 1024 * 1024;
|
|
3210
|
-
const HEADER_INTS = 4;
|
|
3211
|
-
const LENGTH_OFFSET = HEADER_INTS * 4;
|
|
3212
|
-
const DATA_OFFSET = 20;
|
|
3213
|
-
const INIT_TIMEOUT = 6e4;
|
|
3214
|
-
const REQUEST_TIMEOUT = 3e4;
|
|
3215
|
-
const MAX_WORKERS = 8;
|
|
3216
|
-
/**
|
|
3217
|
-
* Build the worker script shared by `sort-service` and `canonicalize-service`.
|
|
3218
|
-
* Owns the entire SharedArrayBuffer protocol (offsets derived from the same
|
|
3219
|
-
* constants the host uses, so they can't drift), the design-system load with
|
|
3220
|
-
* error propagation (DS-M4: the real cause is written into the buffer so the
|
|
3221
|
-
* host can surface it, not a generic "failed to load"), the ready signal, and
|
|
3222
|
-
* the request loop. `handlerExpr` is a function expression `(ds, request) =>
|
|
3223
|
-
* result` — the only part that differs between services.
|
|
3224
|
-
*/
|
|
3225
|
-
function makeWorkerScript(handlerExpr, preamble = "") {
|
|
3226
|
-
return `
|
|
3227
|
-
${preamble}
|
|
3228
|
-
const { workerData } = require('worker_threads');
|
|
3229
|
-
async function main() {
|
|
3230
|
-
const { sharedBuffer, cssPath } = workerData;
|
|
3231
|
-
const control = new Int32Array(sharedBuffer, 0, ${HEADER_INTS});
|
|
3232
|
-
const lengthView = new DataView(sharedBuffer, ${LENGTH_OFFSET}, 4);
|
|
3233
|
-
const dataArea = new Uint8Array(sharedBuffer, ${DATA_OFFSET});
|
|
3234
|
-
|
|
3235
|
-
// On load failure, write the real cause into the data region so the host can
|
|
3236
|
-
// report it instead of a bare "failed to load the design system".
|
|
3237
|
-
function signalLoadError(e) {
|
|
3238
|
-
try {
|
|
3239
|
-
const msg = Buffer.from(String((e && e.message) || e || 'unknown design-system load error'), 'utf-8');
|
|
3240
|
-
const n = Math.min(msg.length, dataArea.length);
|
|
3241
|
-
dataArea.set(msg.subarray(0, n), 0);
|
|
3242
|
-
lengthView.setUint32(0, n);
|
|
3243
|
-
} catch {}
|
|
3244
|
-
Atomics.store(control, 2, -1);
|
|
3245
|
-
Atomics.notify(control, 2);
|
|
3246
|
-
}
|
|
3247
|
-
|
|
3248
|
-
let ds;
|
|
3249
|
-
try {
|
|
3250
|
-
const { __unstable__loadDesignSystem } = require(workerData.tailwindNodePath);
|
|
3251
|
-
const { readFileSync } = require('fs');
|
|
3252
|
-
const { dirname } = require('path');
|
|
3253
|
-
const css = readFileSync(cssPath, 'utf-8');
|
|
3254
|
-
ds = await __unstable__loadDesignSystem(css, { base: dirname(cssPath) });
|
|
3255
|
-
} catch (e) {
|
|
3256
|
-
signalLoadError(e);
|
|
3257
|
-
return;
|
|
3258
|
-
}
|
|
3259
|
-
|
|
3260
|
-
Atomics.store(control, 2, 1);
|
|
3261
|
-
Atomics.notify(control, 2);
|
|
3262
|
-
|
|
3263
|
-
const handler = ${handlerExpr};
|
|
3264
|
-
|
|
3265
|
-
while (true) {
|
|
3266
|
-
Atomics.wait(control, 0, 0);
|
|
3267
|
-
const len = lengthView.getUint32(0);
|
|
3268
|
-
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
3269
|
-
Atomics.store(control, 0, 0);
|
|
3270
|
-
|
|
3271
|
-
let response;
|
|
3272
|
-
try {
|
|
3273
|
-
const request = JSON.parse(requestStr);
|
|
3274
|
-
const result = handler(ds, request);
|
|
3275
|
-
response = Buffer.from(JSON.stringify(result), 'utf-8');
|
|
3276
|
-
} catch {
|
|
3277
|
-
response = Buffer.from('null', 'utf-8');
|
|
3278
|
-
}
|
|
3279
|
-
|
|
3280
|
-
// DS-M6: if the response doesn't fit the shared buffer, reply with a
|
|
3281
|
-
// sentinel the host reads as a rejected request, instead of letting
|
|
3282
|
-
// dataArea.set throw and kill the worker (which would hang the caller
|
|
3283
|
-
// until the request timeout and then report a misleading timeout).
|
|
3284
|
-
if (response.length > dataArea.length) {
|
|
3285
|
-
response = Buffer.from('null', 'utf-8');
|
|
3286
|
-
}
|
|
3287
|
-
|
|
3288
|
-
dataArea.set(response, 0);
|
|
3289
|
-
lengthView.setUint32(0, response.length);
|
|
3290
|
-
Atomics.store(control, 1, 1);
|
|
3291
|
-
Atomics.notify(control, 1);
|
|
3292
|
-
}
|
|
3180
|
+
function makeWorkerScript(handlerExpr, preamble = "") {
|
|
3181
|
+
return `
|
|
3182
|
+
${preamble}
|
|
3183
|
+
const { workerData } = require('worker_threads');
|
|
3184
|
+
async function main() {
|
|
3185
|
+
const { sharedBuffer, cssPath } = workerData;
|
|
3186
|
+
const control = new Int32Array(sharedBuffer, 0, ${HEADER_INTS});
|
|
3187
|
+
const lengthView = new DataView(sharedBuffer, ${LENGTH_OFFSET}, 4);
|
|
3188
|
+
const dataArea = new Uint8Array(sharedBuffer, ${DATA_OFFSET});
|
|
3189
|
+
|
|
3190
|
+
// On load failure, write the real cause into the data region so the host can
|
|
3191
|
+
// report it instead of a bare "failed to load the design system".
|
|
3192
|
+
function signalLoadError(e) {
|
|
3193
|
+
try {
|
|
3194
|
+
const msg = Buffer.from(String((e && e.message) || e || 'unknown design-system load error'), 'utf-8');
|
|
3195
|
+
const n = Math.min(msg.length, dataArea.length);
|
|
3196
|
+
dataArea.set(msg.subarray(0, n), 0);
|
|
3197
|
+
lengthView.setUint32(0, n);
|
|
3198
|
+
} catch {}
|
|
3199
|
+
Atomics.store(control, 2, -1);
|
|
3200
|
+
Atomics.notify(control, 2);
|
|
3201
|
+
}
|
|
3202
|
+
|
|
3203
|
+
let ds;
|
|
3204
|
+
try {
|
|
3205
|
+
const { __unstable__loadDesignSystem } = require(workerData.tailwindNodePath);
|
|
3206
|
+
const { readFileSync } = require('fs');
|
|
3207
|
+
const { dirname } = require('path');
|
|
3208
|
+
const css = readFileSync(cssPath, 'utf-8');
|
|
3209
|
+
ds = await __unstable__loadDesignSystem(css, { base: dirname(cssPath) });
|
|
3210
|
+
} catch (e) {
|
|
3211
|
+
signalLoadError(e);
|
|
3212
|
+
return;
|
|
3213
|
+
}
|
|
3214
|
+
|
|
3215
|
+
Atomics.store(control, 2, 1);
|
|
3216
|
+
Atomics.notify(control, 2);
|
|
3217
|
+
|
|
3218
|
+
const handler = ${handlerExpr};
|
|
3219
|
+
|
|
3220
|
+
while (true) {
|
|
3221
|
+
Atomics.wait(control, 0, 0);
|
|
3222
|
+
const len = lengthView.getUint32(0);
|
|
3223
|
+
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
3224
|
+
Atomics.store(control, 0, 0);
|
|
3225
|
+
|
|
3226
|
+
let response;
|
|
3227
|
+
try {
|
|
3228
|
+
const request = JSON.parse(requestStr);
|
|
3229
|
+
const result = handler(ds, request);
|
|
3230
|
+
response = Buffer.from(JSON.stringify(result), 'utf-8');
|
|
3231
|
+
} catch {
|
|
3232
|
+
response = Buffer.from('null', 'utf-8');
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3235
|
+
// DS-M6: if the response doesn't fit the shared buffer, reply with a
|
|
3236
|
+
// sentinel the host reads as a rejected request, instead of letting
|
|
3237
|
+
// dataArea.set throw and kill the worker (which would hang the caller
|
|
3238
|
+
// until the request timeout and then report a misleading timeout).
|
|
3239
|
+
if (response.length > dataArea.length) {
|
|
3240
|
+
response = Buffer.from('null', 'utf-8');
|
|
3241
|
+
}
|
|
3242
|
+
|
|
3243
|
+
dataArea.set(response, 0);
|
|
3244
|
+
lengthView.setUint32(0, response.length);
|
|
3245
|
+
Atomics.store(control, 1, 1);
|
|
3246
|
+
Atomics.notify(control, 1);
|
|
3247
|
+
}
|
|
3293
3248
|
}
|
|
3294
3249
|
main().catch(() => {});
|
|
3295
3250
|
`;
|
|
@@ -3406,102 +3361,358 @@ var DesignSystemWorker = class {
|
|
|
3406
3361
|
this.dropWorker(cssPath);
|
|
3407
3362
|
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker returned null — the request body was rejected or its response did not fit the buffer.`, "This is a bug; please open an issue with the input that triggered it."));
|
|
3408
3363
|
}
|
|
3409
|
-
return parsed;
|
|
3410
|
-
}
|
|
3411
|
-
reset() {
|
|
3412
|
-
for (const state of this.workers.values()) this.terminateWorker(state.worker);
|
|
3413
|
-
this.workers.clear();
|
|
3414
|
-
this.errors.clear();
|
|
3415
|
-
}
|
|
3416
|
-
/** Terminate and forget the worker for a single cssPath (on request failure). */
|
|
3417
|
-
dropWorker(cssPath) {
|
|
3418
|
-
const state = this.workers.get(cssPath);
|
|
3419
|
-
if (state) {
|
|
3420
|
-
this.workers.delete(cssPath);
|
|
3421
|
-
this.terminateWorker(state.worker);
|
|
3364
|
+
return parsed;
|
|
3365
|
+
}
|
|
3366
|
+
reset() {
|
|
3367
|
+
for (const state of this.workers.values()) this.terminateWorker(state.worker);
|
|
3368
|
+
this.workers.clear();
|
|
3369
|
+
this.errors.clear();
|
|
3370
|
+
}
|
|
3371
|
+
/** Terminate and forget the worker for a single cssPath (on request failure). */
|
|
3372
|
+
dropWorker(cssPath) {
|
|
3373
|
+
const state = this.workers.get(cssPath);
|
|
3374
|
+
if (state) {
|
|
3375
|
+
this.workers.delete(cssPath);
|
|
3376
|
+
this.terminateWorker(state.worker);
|
|
3377
|
+
}
|
|
3378
|
+
}
|
|
3379
|
+
terminateWorker(worker) {
|
|
3380
|
+
try {
|
|
3381
|
+
worker.removeAllListeners("error");
|
|
3382
|
+
worker.terminate();
|
|
3383
|
+
} catch {}
|
|
3384
|
+
}
|
|
3385
|
+
};
|
|
3386
|
+
//#endregion
|
|
3387
|
+
//#region src/design-system/declaration-service.ts
|
|
3388
|
+
const declWorker = new DesignSystemWorker({
|
|
3389
|
+
workerScript: makeWorkerScript(`(ds, request) => {
|
|
3390
|
+
const decls = {};
|
|
3391
|
+
const values = {};
|
|
3392
|
+
const invalid = [];
|
|
3393
|
+
// A prefixed design system only resolves the PREFIXED form (\`tw:p-[5px]\`), and
|
|
3394
|
+
// the host asks with prefix-free names — \`extractUtility\` strips the project
|
|
3395
|
+
// prefix along with the variants. Same invariant the precompute follows: apply
|
|
3396
|
+
// the prefix only when talking to the design system, key everything returned
|
|
3397
|
+
// prefix-free. Without this the service resolved NOTHING in a prefixed project,
|
|
3398
|
+
// so every user-written value went silently uncompared.
|
|
3399
|
+
const prefix = (ds.theme && ds.theme.prefix) || '';
|
|
3400
|
+
const pfx = (c) => (prefix && !c.startsWith(prefix + ':')) ? prefix + ':' + c : c;
|
|
3401
|
+
const css = ds.candidatesToCss(request.classes.map(pfx));
|
|
3402
|
+
for (let i = 0; i < request.classes.length; i++) {
|
|
3403
|
+
if (!css[i]) { invalid.push(request.classes[i]); continue; }
|
|
3404
|
+
const cls = request.classes[i];
|
|
3405
|
+
const list = [];
|
|
3406
|
+
// The selector carries the prefix, so the scope classifier needs the
|
|
3407
|
+
// prefixed name even though the result is keyed by the bare one.
|
|
3408
|
+
walkDeclarations(css[i], pfx(cls), (scope, prop, value) => {
|
|
3409
|
+
list.push([scope, prop, value]);
|
|
3410
|
+
if (!(value in values)) {
|
|
3411
|
+
const reads = scanVarReads(value);
|
|
3412
|
+
values[value] = { p: reads[0], f: reads[1], u: isPureVarRead(value) };
|
|
3413
|
+
}
|
|
3414
|
+
});
|
|
3415
|
+
if (list.length > 0) decls[cls] = list;
|
|
3416
|
+
}
|
|
3417
|
+
return { decls, values, invalid };
|
|
3418
|
+
}`, DECL_EXTRACTOR_SOURCE),
|
|
3419
|
+
serviceName: "declarations"
|
|
3420
|
+
});
|
|
3421
|
+
/**
|
|
3422
|
+
* Everything already asked about, per design-system CACHE — not per entry point:
|
|
3423
|
+
* class name → does the design system produce CSS for it. A miss is remembered
|
|
3424
|
+
* too, so a class that compiles to nothing is not re-queried on every AST node it
|
|
3425
|
+
* appears in.
|
|
3426
|
+
*
|
|
3427
|
+
* The lifetime has to match the cache's, because that is where the declarations
|
|
3428
|
+
* are interned. Keyed by path, a rebuilt cache (an mtime bump in a long-lived
|
|
3429
|
+
* editor process, or `resetDesignSystem()`) inherited a fully-populated map and
|
|
3430
|
+
* could never re-learn: the rules went permanently blind to user-written values
|
|
3431
|
+
* with no diagnostic. A WeakMap also means there is nothing to reset.
|
|
3432
|
+
*/
|
|
3433
|
+
const answers = /* @__PURE__ */ new WeakMap();
|
|
3434
|
+
function answersFor(cache) {
|
|
3435
|
+
let map = answers.get(cache);
|
|
3436
|
+
if (!map) {
|
|
3437
|
+
map = /* @__PURE__ */ new Map();
|
|
3438
|
+
answers.set(cache, map);
|
|
3439
|
+
}
|
|
3440
|
+
return map;
|
|
3441
|
+
}
|
|
3442
|
+
/**
|
|
3443
|
+
* Ask the design system about every class there is no answer for yet, intern the
|
|
3444
|
+
* declarations it returns, and record which classes produce no CSS at all.
|
|
3445
|
+
*
|
|
3446
|
+
* **Throws `SortServiceError` when the service is unavailable**, like the sort and
|
|
3447
|
+
* canonicalize services do. It used to swallow the failure and report "no
|
|
3448
|
+
* information", which was defensible when `no-conflicting-classes` was the only
|
|
3449
|
+
* caller — no declarations means no comparison, so the rule just went quiet. It
|
|
3450
|
+
* stopped being defensible once `no-unknown-classes` started asking about
|
|
3451
|
+
* VALIDITY: there, silence sends the rule back to the tolerant heuristic, so a
|
|
3452
|
+
* dead worker quietly reinstates the very false negatives the service exists to
|
|
3453
|
+
* remove, with nothing anywhere to say so.
|
|
3454
|
+
*
|
|
3455
|
+
* The caller decides what to do, and the two postures already exist in the
|
|
3456
|
+
* plugin: a DS-dependent rule surfaces it through `safeGetDS` as
|
|
3457
|
+
* `designSystemUnavailable` (what `enforce-canonical` does with the canonicalize
|
|
3458
|
+
* worker), a DS-OPTIONAL rule catches it and degrades.
|
|
3459
|
+
*/
|
|
3460
|
+
function ask(cssPath, cache, classes) {
|
|
3461
|
+
const known = answersFor(cache);
|
|
3462
|
+
const pending = [...new Set(classes.filter((cls) => !known.has(cls)))];
|
|
3463
|
+
if (pending.length === 0) return;
|
|
3464
|
+
const response = declWorker.callSync(cssPath, { classes: pending });
|
|
3465
|
+
const invalid = new Set(response.invalid);
|
|
3466
|
+
for (const cls of pending) known.set(cls, !invalid.has(cls));
|
|
3467
|
+
for (const [cls, raws] of Object.entries(response.decls)) cache.internDeclarations(cls, raws, response.values);
|
|
3468
|
+
}
|
|
3469
|
+
/**
|
|
3470
|
+
* Resolve and intern declarations for `classes` that the precompute doesn't know.
|
|
3471
|
+
* They land in the cache, so the caller reads them back through
|
|
3472
|
+
* `cache.getCssDeclarations` like any other class.
|
|
3473
|
+
*
|
|
3474
|
+
* Throws `SortServiceError` if the service is unavailable — see `ask`.
|
|
3475
|
+
*/
|
|
3476
|
+
function resolveDeclarationsSync(cssPath, cache, classes) {
|
|
3477
|
+
ask(cssPath, cache, classes);
|
|
3478
|
+
}
|
|
3479
|
+
/**
|
|
3480
|
+
* Which of these classes produce CSS.
|
|
3481
|
+
*
|
|
3482
|
+
* This is what makes validity exact for the classes the precompute cannot
|
|
3483
|
+
* enumerate: `w-45` and `bg-red-5000` are shaped identically, and only Tailwind
|
|
3484
|
+
* knows that it compiles the first and not the second. Throws
|
|
3485
|
+
* `SortServiceError` if the service is unavailable — see `ask`.
|
|
3486
|
+
*/
|
|
3487
|
+
function validateClassesSync(cssPath, cache, classes) {
|
|
3488
|
+
ask(cssPath, cache, classes);
|
|
3489
|
+
return answersFor(cache);
|
|
3490
|
+
}
|
|
3491
|
+
//#endregion
|
|
3492
|
+
//#region src/rules/no-unknown-classes.ts
|
|
3493
|
+
/**
|
|
3494
|
+
* Utility used to probe a variant chain. Any always-valid utility works; `flex`
|
|
3495
|
+
* is the shortest one every Tailwind build has.
|
|
3496
|
+
*/
|
|
3497
|
+
const PROBE_UTILITY = "flex";
|
|
3498
|
+
const noUnknownClasses = defineRule({
|
|
3499
|
+
meta: {
|
|
3500
|
+
type: "problem",
|
|
3501
|
+
docs: { description: "Disallow classes that are not defined in the Tailwind CSS design system" },
|
|
3502
|
+
schema: [{
|
|
3503
|
+
type: "object",
|
|
3504
|
+
properties: {
|
|
3505
|
+
entryPoint: { type: "string" },
|
|
3506
|
+
allowlist: {
|
|
3507
|
+
type: "array",
|
|
3508
|
+
items: { type: "string" }
|
|
3509
|
+
},
|
|
3510
|
+
ignorePrefixes: {
|
|
3511
|
+
type: "array",
|
|
3512
|
+
items: { type: "string" }
|
|
3513
|
+
}
|
|
3514
|
+
},
|
|
3515
|
+
additionalProperties: false
|
|
3516
|
+
}],
|
|
3517
|
+
hasSuggestions: true,
|
|
3518
|
+
defaultOptions: [{
|
|
3519
|
+
allowlist: [],
|
|
3520
|
+
ignorePrefixes: []
|
|
3521
|
+
}],
|
|
3522
|
+
messages: {
|
|
3523
|
+
unknown: "\"{{className}}\" is not a valid Tailwind class.",
|
|
3524
|
+
unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
|
|
3525
|
+
missingPrefix: "\"{{className}}\" needs the \"{{prefix}}:\" prefix to produce CSS. Did you mean \"{{suggestion}}\"?",
|
|
3526
|
+
unknownVariant: "\"{{className}}\" produces no CSS: \"{{variant}}\" is not a variant this design system knows.",
|
|
3527
|
+
unknownVariantWithSuggestion: "\"{{className}}\" produces no CSS: \"{{variant}}\" is not a variant this design system knows. Did you mean \"{{suggestion}}\"?",
|
|
3528
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
3529
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
3530
|
+
}
|
|
3531
|
+
},
|
|
3532
|
+
createOnce(context) {
|
|
3533
|
+
const getDS = createLazyLoader(context);
|
|
3534
|
+
const getLazyOptions = createLazyOptions(context, (o) => ({
|
|
3535
|
+
allowlist: new Set(o?.allowlist ?? []),
|
|
3536
|
+
ignorePrefixes: o?.ignorePrefixes ?? []
|
|
3537
|
+
}));
|
|
3538
|
+
function shouldIgnore(className) {
|
|
3539
|
+
const { allowlist, ignorePrefixes } = getLazyOptions();
|
|
3540
|
+
if (allowlist.has(className)) return true;
|
|
3541
|
+
return ignorePrefixes.some((prefix) => className.startsWith(prefix));
|
|
3542
|
+
}
|
|
3543
|
+
function strippedUtility(className) {
|
|
3544
|
+
const { utility, variant } = splitUtilityAndVariant(className);
|
|
3545
|
+
const { bare, position } = splitImportant(utility);
|
|
3546
|
+
return {
|
|
3547
|
+
variant,
|
|
3548
|
+
bare,
|
|
3549
|
+
position
|
|
3550
|
+
};
|
|
3551
|
+
}
|
|
3552
|
+
/**
|
|
3553
|
+
* Does the design system compile this variant chain?
|
|
3554
|
+
*
|
|
3555
|
+
* `variantOrder` only lists the STATIC variants (71 of them: `hover`, `md`,
|
|
3556
|
+
* `dark`, …). It has no `group`, no `data`, no `@md`, because those are
|
|
3557
|
+
* functional variants whose values are unbounded — so validating a chain
|
|
3558
|
+
* against that list would report `group-hover:` and `data-[x=1]:` as unknown.
|
|
3559
|
+
* Compiling a probe utility under the chain is the only exact answer, and it
|
|
3560
|
+
* costs one design-system call per DISTINCT chain in the project.
|
|
3561
|
+
*/
|
|
3562
|
+
const chainAnswers = /* @__PURE__ */ new Map();
|
|
3563
|
+
function chainProduces(cache, entryPoint, chain) {
|
|
3564
|
+
const key = `${entryPoint}\0${chain}`;
|
|
3565
|
+
const memo = chainAnswers.get(key);
|
|
3566
|
+
if (memo !== void 0) return memo;
|
|
3567
|
+
const probe = `${chain}${PROBE_UTILITY}`;
|
|
3568
|
+
const verdict = validateClassesSync(entryPoint, cache, [probe]).get(probe) ?? null;
|
|
3569
|
+
chainAnswers.set(key, verdict);
|
|
3570
|
+
return verdict;
|
|
3571
|
+
}
|
|
3572
|
+
/** Replace one class in the location and report, with a quick-fix suggestion. */
|
|
3573
|
+
function reportWithSuggestion(loc, classes, cls, replacement, messageId, data, split) {
|
|
3574
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? replacement : c));
|
|
3575
|
+
context.report({
|
|
3576
|
+
node: loc.node,
|
|
3577
|
+
messageId,
|
|
3578
|
+
data,
|
|
3579
|
+
suggest: [{
|
|
3580
|
+
messageId: "suggestReplace",
|
|
3581
|
+
data: {
|
|
3582
|
+
className: cls,
|
|
3583
|
+
replacement
|
|
3584
|
+
},
|
|
3585
|
+
fix(fixer) {
|
|
3586
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3587
|
+
}
|
|
3588
|
+
}]
|
|
3589
|
+
});
|
|
3590
|
+
}
|
|
3591
|
+
function check(locations) {
|
|
3592
|
+
if (locations.length === 0) return;
|
|
3593
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
3594
|
+
if (!ds) return;
|
|
3595
|
+
const { cache, entryPoint } = ds;
|
|
3596
|
+
try {
|
|
3597
|
+
checkLocations(locations, cache, entryPoint);
|
|
3598
|
+
} catch (error) {
|
|
3599
|
+
if (reportFatalDsError(context, error, locations[0].node)) return;
|
|
3600
|
+
throw error;
|
|
3601
|
+
}
|
|
3602
|
+
}
|
|
3603
|
+
function checkLocations(locations, cache, entryPoint) {
|
|
3604
|
+
for (const loc of locations) {
|
|
3605
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3606
|
+
const classes = split.classes;
|
|
3607
|
+
let toVerify = null;
|
|
3608
|
+
for (const cls of classes) {
|
|
3609
|
+
if (shouldIgnore(cls)) continue;
|
|
3610
|
+
const bare = strippedUtility(cls).bare;
|
|
3611
|
+
if (cache.isKnownClass(bare)) continue;
|
|
3612
|
+
if (!toVerify) toVerify = [];
|
|
3613
|
+
toVerify.push(bare);
|
|
3614
|
+
}
|
|
3615
|
+
const verified = toVerify ? validateClassesSync(entryPoint, cache, toVerify) : null;
|
|
3616
|
+
for (const cls of classes) {
|
|
3617
|
+
if (shouldIgnore(cls)) continue;
|
|
3618
|
+
const validity = cache.classValidity(cls);
|
|
3619
|
+
const { variant, bare, position } = strippedUtility(cls);
|
|
3620
|
+
if (validity === "valid" && !variant && cache.isKnownClass(bare)) continue;
|
|
3621
|
+
if (cache.deprecatedReplacement(bare)) continue;
|
|
3622
|
+
if (validity === "missing-prefix") {
|
|
3623
|
+
const fixed = `${cache.prefix}:${cls}`;
|
|
3624
|
+
reportWithSuggestion(loc, classes, cls, fixed, "missingPrefix", {
|
|
3625
|
+
className: cls,
|
|
3626
|
+
prefix: cache.prefix,
|
|
3627
|
+
suggestion: fixed
|
|
3628
|
+
}, split);
|
|
3629
|
+
continue;
|
|
3630
|
+
}
|
|
3631
|
+
const compiles = cache.isKnownClass(bare) ? true : verified?.get(bare);
|
|
3632
|
+
if (validity === "unknown" || compiles === false) {
|
|
3633
|
+
const suggestionBare = findBestSuggestion(bare, cache.validClasses);
|
|
3634
|
+
const suggestion = suggestionBare ? variant + reattachImportant(suggestionBare, position) : null;
|
|
3635
|
+
if (suggestion) reportWithSuggestion(loc, classes, cls, suggestion, "unknownWithSuggestion", {
|
|
3636
|
+
className: cls,
|
|
3637
|
+
suggestion
|
|
3638
|
+
}, split);
|
|
3639
|
+
else context.report({
|
|
3640
|
+
node: loc.node,
|
|
3641
|
+
messageId: "unknown",
|
|
3642
|
+
data: { className: cls }
|
|
3643
|
+
});
|
|
3644
|
+
continue;
|
|
3645
|
+
}
|
|
3646
|
+
if (!variant) continue;
|
|
3647
|
+
if (chainProduces(cache, entryPoint, variant) !== false) continue;
|
|
3648
|
+
const { variant: offending, suggestion, fixedChain } = diagnoseChain(cache, entryPoint, variant);
|
|
3649
|
+
const fixedClass = fixedChain ? fixedChain + reattachImportant(bare, position) : null;
|
|
3650
|
+
if (fixedClass && suggestion) reportWithSuggestion(loc, classes, cls, fixedClass, "unknownVariantWithSuggestion", {
|
|
3651
|
+
className: cls,
|
|
3652
|
+
variant: offending,
|
|
3653
|
+
suggestion
|
|
3654
|
+
}, split);
|
|
3655
|
+
else context.report({
|
|
3656
|
+
node: loc.node,
|
|
3657
|
+
messageId: "unknownVariant",
|
|
3658
|
+
data: {
|
|
3659
|
+
className: cls,
|
|
3660
|
+
variant: offending
|
|
3661
|
+
}
|
|
3662
|
+
});
|
|
3663
|
+
}
|
|
3664
|
+
}
|
|
3665
|
+
}
|
|
3666
|
+
/**
|
|
3667
|
+
* Which segment of an invalid chain is to blame, and what to use instead.
|
|
3668
|
+
*
|
|
3669
|
+
* Every candidate correction is checked against the design system before it
|
|
3670
|
+
* is offered, so a suggestion is never a guess: `group-hoverr` is corrected by
|
|
3671
|
+
* fixing its last dash-segment and confirming that `group-hover:` compiles.
|
|
3672
|
+
* That also means no list of compound roots (`group`, `peer`, `data`, …) has
|
|
3673
|
+
* to be maintained here.
|
|
3674
|
+
*/
|
|
3675
|
+
function diagnoseChain(cache, entryPoint, chain) {
|
|
3676
|
+
const segments = extractVariants(`${chain}${PROBE_UTILITY}`);
|
|
3677
|
+
const names = cache.variantNames();
|
|
3678
|
+
for (const segment of segments) {
|
|
3679
|
+
if (segment.startsWith("[")) continue;
|
|
3680
|
+
if (chainProduces(cache, entryPoint, `${segment}:`) !== false) continue;
|
|
3681
|
+
for (const candidate of correctionsFor(segment, names)) {
|
|
3682
|
+
if (chainProduces(cache, entryPoint, `${candidate}:`) !== true) continue;
|
|
3683
|
+
return {
|
|
3684
|
+
variant: segment,
|
|
3685
|
+
suggestion: candidate,
|
|
3686
|
+
fixedChain: segments.map((s) => s === segment ? candidate : s).join(":") + ":"
|
|
3687
|
+
};
|
|
3688
|
+
}
|
|
3689
|
+
return {
|
|
3690
|
+
variant: segment,
|
|
3691
|
+
suggestion: null,
|
|
3692
|
+
fixedChain: null
|
|
3693
|
+
};
|
|
3694
|
+
}
|
|
3695
|
+
return {
|
|
3696
|
+
variant: chain.slice(0, -1),
|
|
3697
|
+
suggestion: null,
|
|
3698
|
+
fixedChain: null
|
|
3699
|
+
};
|
|
3422
3700
|
}
|
|
3701
|
+
/** Spelling candidates for a variant segment, nearest neighbour first. */
|
|
3702
|
+
function correctionsFor(segment, names) {
|
|
3703
|
+
const out = [];
|
|
3704
|
+
const whole = findBestSuggestion(segment, names);
|
|
3705
|
+
if (whole) out.push(whole);
|
|
3706
|
+
const dash = segment.lastIndexOf("-");
|
|
3707
|
+
if (dash > 0) {
|
|
3708
|
+
const tail = findBestSuggestion(segment.slice(dash + 1), names);
|
|
3709
|
+
if (tail) out.push(`${segment.slice(0, dash)}-${tail}`);
|
|
3710
|
+
}
|
|
3711
|
+
return out;
|
|
3712
|
+
}
|
|
3713
|
+
return createExtractorVisitors(context, check);
|
|
3423
3714
|
}
|
|
3424
|
-
terminateWorker(worker) {
|
|
3425
|
-
try {
|
|
3426
|
-
worker.removeAllListeners("error");
|
|
3427
|
-
worker.terminate();
|
|
3428
|
-
} catch {}
|
|
3429
|
-
}
|
|
3430
|
-
};
|
|
3431
|
-
//#endregion
|
|
3432
|
-
//#region src/design-system/declaration-service.ts
|
|
3433
|
-
const declWorker = new DesignSystemWorker({
|
|
3434
|
-
workerScript: makeWorkerScript(`(ds, request) => {
|
|
3435
|
-
const decls = {};
|
|
3436
|
-
const values = {};
|
|
3437
|
-
// A prefixed design system only resolves the PREFIXED form (\`tw:p-[5px]\`), and
|
|
3438
|
-
// the host asks with prefix-free names — \`extractUtility\` strips the project
|
|
3439
|
-
// prefix along with the variants. Same invariant the precompute follows: apply
|
|
3440
|
-
// the prefix only when talking to the design system, key everything returned
|
|
3441
|
-
// prefix-free. Without this the service resolved NOTHING in a prefixed project,
|
|
3442
|
-
// so every user-written value went silently uncompared.
|
|
3443
|
-
const prefix = (ds.theme && ds.theme.prefix) || '';
|
|
3444
|
-
const pfx = (c) => (prefix && !c.startsWith(prefix + ':')) ? prefix + ':' + c : c;
|
|
3445
|
-
const css = ds.candidatesToCss(request.classes.map(pfx));
|
|
3446
|
-
for (let i = 0; i < request.classes.length; i++) {
|
|
3447
|
-
if (!css[i]) continue;
|
|
3448
|
-
const cls = request.classes[i];
|
|
3449
|
-
const list = [];
|
|
3450
|
-
// The selector carries the prefix, so the scope classifier needs the
|
|
3451
|
-
// prefixed name even though the result is keyed by the bare one.
|
|
3452
|
-
walkDeclarations(css[i], pfx(cls), (scope, prop, value) => {
|
|
3453
|
-
list.push([scope, prop, value]);
|
|
3454
|
-
if (!(value in values)) {
|
|
3455
|
-
const reads = scanVarReads(value);
|
|
3456
|
-
values[value] = { p: reads[0], f: reads[1], u: isPureVarRead(value) };
|
|
3457
|
-
}
|
|
3458
|
-
});
|
|
3459
|
-
if (list.length > 0) decls[cls] = list;
|
|
3460
|
-
}
|
|
3461
|
-
return { decls, values };
|
|
3462
|
-
}`, DECL_EXTRACTOR_SOURCE),
|
|
3463
|
-
serviceName: "declarations"
|
|
3464
3715
|
});
|
|
3465
|
-
/**
|
|
3466
|
-
* Classes already asked about, per design-system CACHE — not per entry point. A
|
|
3467
|
-
* miss is remembered too: a class the design system produces no CSS for must not
|
|
3468
|
-
* be re-queried on every AST node it appears in.
|
|
3469
|
-
*
|
|
3470
|
-
* The lifetime has to match the cache's, because that is where the answers are
|
|
3471
|
-
* interned. Keyed by path, a rebuilt cache (an mtime bump in a long-lived editor
|
|
3472
|
-
* process, or `resetDesignSystem()`) inherited a fully-populated set and could
|
|
3473
|
-
* never re-learn: the rule went permanently blind to user-written values with no
|
|
3474
|
-
* diagnostic. A WeakMap also means there is nothing to reset.
|
|
3475
|
-
*/
|
|
3476
|
-
const queried = /* @__PURE__ */ new WeakMap();
|
|
3477
|
-
function queriedFor(cache) {
|
|
3478
|
-
let set = queried.get(cache);
|
|
3479
|
-
if (!set) {
|
|
3480
|
-
set = /* @__PURE__ */ new Set();
|
|
3481
|
-
queried.set(cache, set);
|
|
3482
|
-
}
|
|
3483
|
-
return set;
|
|
3484
|
-
}
|
|
3485
|
-
/**
|
|
3486
|
-
* Resolve declarations for `classes` that the precompute doesn't know.
|
|
3487
|
-
*
|
|
3488
|
-
* Returns null when the service is unavailable, which the caller treats as "no
|
|
3489
|
-
* information" — the same posture as a class with no declarations, so a broken
|
|
3490
|
-
* worker degrades to today's silence rather than to wrong diagnostics. Real
|
|
3491
|
-
* failures still surface through the sticky error the worker records.
|
|
3492
|
-
*/
|
|
3493
|
-
function resolveDeclarationsSync(cssPath, cache, classes) {
|
|
3494
|
-
const seen = queriedFor(cache);
|
|
3495
|
-
const pending = classes.filter((cls) => !seen.has(cls));
|
|
3496
|
-
if (pending.length === 0) return null;
|
|
3497
|
-
for (const cls of pending) seen.add(cls);
|
|
3498
|
-
try {
|
|
3499
|
-
return declWorker.callSync(cssPath, { classes: pending });
|
|
3500
|
-
} catch (error) {
|
|
3501
|
-
if (error instanceof SortServiceError) return null;
|
|
3502
|
-
throw error;
|
|
3503
|
-
}
|
|
3504
|
-
}
|
|
3505
3716
|
//#endregion
|
|
3506
3717
|
//#region src/rules/no-conflicting-classes/spec.ts
|
|
3507
3718
|
const COMPLEMENTARY_GROUPS = [{
|
|
@@ -3781,15 +3992,6 @@ function shouldSkipPair(a, b, rules = {}) {
|
|
|
3781
3992
|
for (const { a: reA, b: reB } of pairs) if (reA.test(ua) && reB.test(ub) || reA.test(ub) && reB.test(ua)) return true;
|
|
3782
3993
|
return false;
|
|
3783
3994
|
}
|
|
3784
|
-
/**
|
|
3785
|
-
* A class the precompute cannot have enumerated: the value is written by the
|
|
3786
|
-
* user, not by the design system's class list. Asking the design system about
|
|
3787
|
-
* these is what closes the false negatives — `p-4 p-[5px]` used to be silently
|
|
3788
|
-
* accepted while `p-4 p-6` reported.
|
|
3789
|
-
*/
|
|
3790
|
-
function isUserValued(utility) {
|
|
3791
|
-
return utility.includes("[") || utility.includes("(") || utility.includes("/") || /-\d/.test(utility);
|
|
3792
|
-
}
|
|
3793
3995
|
/** Whether the user's `allow` option silences this pair. */
|
|
3794
3996
|
function isAllowed(a, b, allowClasses, allowPairs) {
|
|
3795
3997
|
if (matchesAny(a, allowClasses) || matchesAny(b, allowClasses)) return true;
|
|
@@ -3870,8 +4072,10 @@ const noConflictingClasses = defineRule({
|
|
|
3870
4072
|
if (variantClasses.length < 2) continue;
|
|
3871
4073
|
const unknown = variantClasses.map((cls) => splitImportant(extractUtility(cls)).bare).filter((bare) => cache.getCssDeclarations(bare).length === 0 && isUserValued(bare));
|
|
3872
4074
|
if (unknown.length > 0) {
|
|
3873
|
-
|
|
3874
|
-
|
|
4075
|
+
if (!safeGetDS(() => {
|
|
4076
|
+
resolveDeclarationsSync(ds.entryPoint, cache, unknown);
|
|
4077
|
+
return true;
|
|
4078
|
+
}, context, loc.node)) return;
|
|
3875
4079
|
}
|
|
3876
4080
|
const facts = variantClasses.map((cls) => {
|
|
3877
4081
|
const utility = extractUtility(cls);
|
|
@@ -3946,6 +4150,156 @@ const noConflictingClasses = defineRule({
|
|
|
3946
4150
|
}
|
|
3947
4151
|
});
|
|
3948
4152
|
//#endregion
|
|
4153
|
+
//#region src/utils/report.ts
|
|
4154
|
+
/**
|
|
4155
|
+
* Shared reporter for the "first-as-fix, rest-as-suggestion" pattern.
|
|
4156
|
+
*
|
|
4157
|
+
* Nine rules (enforce-canonical, enforce-logical, enforce-physical,
|
|
4158
|
+
* enforce-consistent-variable-syntax, enforce-consistent-important-position,
|
|
4159
|
+
* enforce-negative-arbitrary-values, no-deprecated-classes,
|
|
4160
|
+
* no-unnecessary-arbitrary-value, prefer-theme-tokens) all emit
|
|
4161
|
+
* replacements via the same shape:
|
|
4162
|
+
*
|
|
4163
|
+
* 1. Collect `{ cls, replacement }` entries from a class string.
|
|
4164
|
+
* 2. Rebuild the full class string with replacements applied.
|
|
4165
|
+
* 3. Emit ONE diagnostic with the autofix attached.
|
|
4166
|
+
* 4. Emit one MORE diagnostic per remaining offender, each carrying
|
|
4167
|
+
* the same autofix as a `suggest`.
|
|
4168
|
+
*
|
|
4169
|
+
* oxlint's fixer is single-shot per AST node, so multiple offenders in the
|
|
4170
|
+
* same class string can only get one mechanical fix; the rest stay as
|
|
4171
|
+
* suggestions the IDE can offer interactively.
|
|
4172
|
+
*/
|
|
4173
|
+
/**
|
|
4174
|
+
* Emit autofix + suggestion diagnostics for a list of class replacements.
|
|
4175
|
+
*
|
|
4176
|
+
* If `offending` is empty, no-op. The fixer's `replaceTextRange` is shared
|
|
4177
|
+
* across all diagnostics — the first one is the primary fix, the rest carry
|
|
4178
|
+
* the same fix as a suggestion (oxlint can only apply one autofix per node).
|
|
4179
|
+
*/
|
|
4180
|
+
function reportClassReplacements(context, loc, split, classes, offending, options) {
|
|
4181
|
+
if (offending.length === 0) return;
|
|
4182
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
4183
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => replacements.get(c) ?? c));
|
|
4184
|
+
const replacementKey = options.replacementKey ?? "replacement";
|
|
4185
|
+
const suggestMessageId = options.suggestMessageId ?? "suggestReplace";
|
|
4186
|
+
const applyFix = (fixer) => fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4187
|
+
const report = context.report;
|
|
4188
|
+
for (let i = 0; i < offending.length; i++) {
|
|
4189
|
+
const { cls, replacement } = offending[i];
|
|
4190
|
+
const primaryData = {
|
|
4191
|
+
className: cls,
|
|
4192
|
+
[replacementKey]: replacement
|
|
4193
|
+
};
|
|
4194
|
+
if (i === 0) report({
|
|
4195
|
+
node: loc.node,
|
|
4196
|
+
messageId: options.messageId,
|
|
4197
|
+
data: primaryData,
|
|
4198
|
+
fix: applyFix
|
|
4199
|
+
});
|
|
4200
|
+
else report({
|
|
4201
|
+
node: loc.node,
|
|
4202
|
+
messageId: options.messageId,
|
|
4203
|
+
data: primaryData,
|
|
4204
|
+
suggest: [{
|
|
4205
|
+
messageId: suggestMessageId,
|
|
4206
|
+
data: {
|
|
4207
|
+
className: cls,
|
|
4208
|
+
replacement
|
|
4209
|
+
},
|
|
4210
|
+
fix: applyFix
|
|
4211
|
+
}]
|
|
4212
|
+
});
|
|
4213
|
+
}
|
|
4214
|
+
}
|
|
4215
|
+
//#endregion
|
|
4216
|
+
//#region src/utils/replacement.ts
|
|
4217
|
+
/**
|
|
4218
|
+
* Returns a predicate that answers "would this replacement produce CSS?".
|
|
4219
|
+
*
|
|
4220
|
+
* `cache.isValid` is the tolerant validity check, so it accepts arbitrary values
|
|
4221
|
+
* (`foo-[5px]`) it cannot verify. That is the right posture here: a rewrite of an
|
|
4222
|
+
* already-invalid class is `no-unknown-classes`' business, and this guard only
|
|
4223
|
+
* has to stop a rewrite from INTRODUCING an invalid class.
|
|
4224
|
+
*/
|
|
4225
|
+
function makeReplacementGuard(cache) {
|
|
4226
|
+
if (!cache) return () => true;
|
|
4227
|
+
return (replacement) => cache.isValid(replacement);
|
|
4228
|
+
}
|
|
4229
|
+
//#endregion
|
|
4230
|
+
//#region src/rules/no-deprecated-classes.ts
|
|
4231
|
+
/**
|
|
4232
|
+
* Fallback rename map, for when no design system is available.
|
|
4233
|
+
*
|
|
4234
|
+
* With an `entryPoint` configured the rule uses the map the precompute derives
|
|
4235
|
+
* from `canonicalizeCandidates`, which is strictly better: it covers the renames
|
|
4236
|
+
* this list misses (`break-words`, `order-none`, the reordered
|
|
4237
|
+
* `bg-left-top`/`object-left-top` position spellings) and it prunes itself when a
|
|
4238
|
+
* Tailwind release stops compiling one of them. This table only has to keep the
|
|
4239
|
+
* unconfigured case working exactly as it did.
|
|
4240
|
+
*/
|
|
4241
|
+
const DEPRECATED_MAP = {
|
|
4242
|
+
"flex-grow": "grow",
|
|
4243
|
+
"flex-grow-0": "grow-0",
|
|
4244
|
+
"flex-shrink": "shrink",
|
|
4245
|
+
"flex-shrink-0": "shrink-0",
|
|
4246
|
+
"overflow-ellipsis": "text-ellipsis",
|
|
4247
|
+
"decoration-slice": "box-decoration-slice",
|
|
4248
|
+
"decoration-clone": "box-decoration-clone",
|
|
4249
|
+
"bg-gradient-to-t": "bg-linear-to-t",
|
|
4250
|
+
"bg-gradient-to-tr": "bg-linear-to-tr",
|
|
4251
|
+
"bg-gradient-to-r": "bg-linear-to-r",
|
|
4252
|
+
"bg-gradient-to-br": "bg-linear-to-br",
|
|
4253
|
+
"bg-gradient-to-b": "bg-linear-to-b",
|
|
4254
|
+
"bg-gradient-to-bl": "bg-linear-to-bl",
|
|
4255
|
+
"bg-gradient-to-l": "bg-linear-to-l",
|
|
4256
|
+
"bg-gradient-to-tl": "bg-linear-to-tl"
|
|
4257
|
+
};
|
|
4258
|
+
const noDeprecatedClasses = defineRule({
|
|
4259
|
+
meta: {
|
|
4260
|
+
type: "problem",
|
|
4261
|
+
docs: { description: "Disallow deprecated Tailwind CSS v4 classes" },
|
|
4262
|
+
fixable: "code",
|
|
4263
|
+
schema: [{
|
|
4264
|
+
type: "object",
|
|
4265
|
+
properties: { entryPoint: { type: "string" } },
|
|
4266
|
+
additionalProperties: false
|
|
4267
|
+
}],
|
|
4268
|
+
hasSuggestions: true,
|
|
4269
|
+
defaultOptions: [{}],
|
|
4270
|
+
messages: {
|
|
4271
|
+
deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead.",
|
|
4272
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
4273
|
+
}
|
|
4274
|
+
},
|
|
4275
|
+
createOnce(context) {
|
|
4276
|
+
const getDS = createLazyLoader(context);
|
|
4277
|
+
function check(locations) {
|
|
4278
|
+
if (locations.length === 0) return;
|
|
4279
|
+
const ds = softGetDS(getDS);
|
|
4280
|
+
const cache = ds ? ds.cache : null;
|
|
4281
|
+
const replacementFor = (bare) => cache?.hasDeprecatedMap ? cache.deprecatedReplacement(bare) : DEPRECATED_MAP[bare] ?? null;
|
|
4282
|
+
const isUsable = makeReplacementGuard(cache);
|
|
4283
|
+
for (const loc of locations) {
|
|
4284
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
4285
|
+
const offending = split.classes.flatMap((cls) => {
|
|
4286
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
4287
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
4288
|
+
const replacement = replacementFor(bareUtility);
|
|
4289
|
+
if (!replacement) return [];
|
|
4290
|
+
const rebuilt = variant + reattachImportant(replacement, position);
|
|
4291
|
+
return isUsable(rebuilt) ? [{
|
|
4292
|
+
cls,
|
|
4293
|
+
replacement: rebuilt
|
|
4294
|
+
}] : [];
|
|
4295
|
+
});
|
|
4296
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "deprecated" });
|
|
4297
|
+
}
|
|
4298
|
+
}
|
|
4299
|
+
return createExtractorVisitors(context, check);
|
|
4300
|
+
}
|
|
4301
|
+
});
|
|
4302
|
+
//#endregion
|
|
3949
4303
|
//#region src/design-system/canonicalize-service.ts
|
|
3950
4304
|
/**
|
|
3951
4305
|
* Persistent canonicalize service backed by `DesignSystemWorker`.
|
|
@@ -4260,10 +4614,17 @@ const enforceCanonical = defineRule({
|
|
|
4260
4614
|
const canonicals = Array.from({ length: classes.length });
|
|
4261
4615
|
const arbitraryIdx = [];
|
|
4262
4616
|
const arbitrary = [];
|
|
4263
|
-
for (let i = 0; i < classes.length; i++)
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4617
|
+
for (let i = 0; i < classes.length; i++) {
|
|
4618
|
+
const { utility } = splitUtilityAndVariant(classes[i]);
|
|
4619
|
+
if (cache.deprecatedReplacement(splitImportant(utility).bare)) {
|
|
4620
|
+
canonicals[i] = classes[i];
|
|
4621
|
+
continue;
|
|
4622
|
+
}
|
|
4623
|
+
if (utilityHasDynamicValue(classes[i])) {
|
|
4624
|
+
arbitraryIdx.push(i);
|
|
4625
|
+
arbitrary.push(classes[i]);
|
|
4626
|
+
} else canonicals[i] = cache.canonicalize(classes[i]);
|
|
4627
|
+
}
|
|
4267
4628
|
if (arbitrary.length > 0) {
|
|
4268
4629
|
const rem = getRem();
|
|
4269
4630
|
const dynamic = safeGetDS(() => canonicalizeClassesSync(entryPoint, arbitrary, rem), context, loc.node);
|
|
@@ -4410,124 +4771,320 @@ const enforceSortOrder = defineRule({
|
|
|
4410
4771
|
}
|
|
4411
4772
|
return createExtractorVisitors(context, check);
|
|
4412
4773
|
}
|
|
4413
|
-
});
|
|
4414
|
-
//#endregion
|
|
4415
|
-
//#region src/rules/enforce-shorthand.ts
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4774
|
+
});
|
|
4775
|
+
//#endregion
|
|
4776
|
+
//#region src/rules/enforce-shorthand.ts
|
|
4777
|
+
/**
|
|
4778
|
+
* The `m`/`p` shape, shared by `margin`, `padding`, `scroll-margin` and
|
|
4779
|
+
* `scroll-padding`: four sides, each axis, and the logical inline pair (`ms`+`me`
|
|
4780
|
+
* IS `margin-inline`, so that one is property-identical rather than merely
|
|
4781
|
+
* equivalent).
|
|
4782
|
+
*/
|
|
4783
|
+
function boxFamilies(p) {
|
|
4784
|
+
return [
|
|
4785
|
+
{
|
|
4786
|
+
parts: [
|
|
4787
|
+
`${p}t`,
|
|
4788
|
+
`${p}r`,
|
|
4789
|
+
`${p}b`,
|
|
4790
|
+
`${p}l`
|
|
4791
|
+
],
|
|
4792
|
+
to: p
|
|
4793
|
+
},
|
|
4794
|
+
{
|
|
4795
|
+
parts: [`${p}t`, `${p}b`],
|
|
4796
|
+
to: `${p}y`
|
|
4797
|
+
},
|
|
4798
|
+
{
|
|
4799
|
+
parts: [`${p}l`, `${p}r`],
|
|
4800
|
+
to: `${p}x`
|
|
4801
|
+
},
|
|
4802
|
+
{
|
|
4803
|
+
parts: [`${p}s`, `${p}e`],
|
|
4804
|
+
to: `${p}x`
|
|
4805
|
+
},
|
|
4806
|
+
{
|
|
4807
|
+
parts: [`${p}x`, `${p}y`],
|
|
4808
|
+
to: p
|
|
4809
|
+
}
|
|
4810
|
+
];
|
|
4811
|
+
}
|
|
4812
|
+
const SHORTHAND_FAMILIES = [
|
|
4813
|
+
...boxFamilies("m"),
|
|
4814
|
+
...boxFamilies("p"),
|
|
4815
|
+
...boxFamilies("scroll-m"),
|
|
4816
|
+
...boxFamilies("scroll-p"),
|
|
4817
|
+
{
|
|
4818
|
+
parts: ["w", "h"],
|
|
4819
|
+
to: "size",
|
|
4820
|
+
perAxisNamespaces: true
|
|
4821
|
+
},
|
|
4822
|
+
{
|
|
4823
|
+
parts: [
|
|
4824
|
+
"rounded-tl",
|
|
4825
|
+
"rounded-tr",
|
|
4826
|
+
"rounded-br",
|
|
4827
|
+
"rounded-bl"
|
|
4828
|
+
],
|
|
4829
|
+
to: "rounded"
|
|
4830
|
+
},
|
|
4831
|
+
{
|
|
4832
|
+
parts: [
|
|
4833
|
+
"rounded-ss",
|
|
4834
|
+
"rounded-se",
|
|
4835
|
+
"rounded-ee",
|
|
4836
|
+
"rounded-es"
|
|
4837
|
+
],
|
|
4838
|
+
to: "rounded"
|
|
4839
|
+
},
|
|
4840
|
+
{
|
|
4841
|
+
parts: ["rounded-tl", "rounded-tr"],
|
|
4842
|
+
to: "rounded-t"
|
|
4843
|
+
},
|
|
4844
|
+
{
|
|
4845
|
+
parts: ["rounded-bl", "rounded-br"],
|
|
4846
|
+
to: "rounded-b"
|
|
4847
|
+
},
|
|
4848
|
+
{
|
|
4849
|
+
parts: ["rounded-tl", "rounded-bl"],
|
|
4850
|
+
to: "rounded-l"
|
|
4851
|
+
},
|
|
4852
|
+
{
|
|
4853
|
+
parts: ["rounded-tr", "rounded-br"],
|
|
4854
|
+
to: "rounded-r"
|
|
4855
|
+
},
|
|
4856
|
+
{
|
|
4857
|
+
parts: ["rounded-ss", "rounded-es"],
|
|
4858
|
+
to: "rounded-s"
|
|
4859
|
+
},
|
|
4860
|
+
{
|
|
4861
|
+
parts: ["rounded-se", "rounded-ee"],
|
|
4862
|
+
to: "rounded-e"
|
|
4863
|
+
},
|
|
4864
|
+
{
|
|
4865
|
+
parts: ["rounded-t", "rounded-b"],
|
|
4866
|
+
to: "rounded"
|
|
4867
|
+
},
|
|
4868
|
+
{
|
|
4869
|
+
parts: ["rounded-l", "rounded-r"],
|
|
4870
|
+
to: "rounded"
|
|
4871
|
+
},
|
|
4872
|
+
{
|
|
4873
|
+
parts: ["rounded-s", "rounded-e"],
|
|
4874
|
+
to: "rounded"
|
|
4875
|
+
},
|
|
4876
|
+
{
|
|
4877
|
+
parts: [
|
|
4878
|
+
"border-t",
|
|
4879
|
+
"border-r",
|
|
4880
|
+
"border-b",
|
|
4881
|
+
"border-l"
|
|
4882
|
+
],
|
|
4883
|
+
to: "border"
|
|
4884
|
+
},
|
|
4885
|
+
{
|
|
4886
|
+
parts: ["border-t", "border-b"],
|
|
4887
|
+
to: "border-y"
|
|
4888
|
+
},
|
|
4889
|
+
{
|
|
4890
|
+
parts: ["border-l", "border-r"],
|
|
4891
|
+
to: "border-x"
|
|
4892
|
+
},
|
|
4893
|
+
{
|
|
4894
|
+
parts: ["border-s", "border-e"],
|
|
4895
|
+
to: "border-x"
|
|
4896
|
+
},
|
|
4897
|
+
{
|
|
4898
|
+
parts: ["border-x", "border-y"],
|
|
4899
|
+
to: "border"
|
|
4900
|
+
},
|
|
4901
|
+
{
|
|
4902
|
+
parts: [
|
|
4903
|
+
"top",
|
|
4904
|
+
"right",
|
|
4905
|
+
"bottom",
|
|
4906
|
+
"left"
|
|
4907
|
+
],
|
|
4908
|
+
to: "inset"
|
|
4909
|
+
},
|
|
4910
|
+
{
|
|
4911
|
+
parts: ["top", "bottom"],
|
|
4912
|
+
to: "inset-y"
|
|
4913
|
+
},
|
|
4914
|
+
{
|
|
4915
|
+
parts: ["left", "right"],
|
|
4916
|
+
to: "inset-x"
|
|
4917
|
+
},
|
|
4918
|
+
{
|
|
4919
|
+
parts: ["start", "end"],
|
|
4920
|
+
to: "inset-x"
|
|
4921
|
+
},
|
|
4922
|
+
{
|
|
4923
|
+
parts: ["inset-x", "inset-y"],
|
|
4924
|
+
to: "inset"
|
|
4925
|
+
},
|
|
4926
|
+
{
|
|
4927
|
+
parts: ["gap-x", "gap-y"],
|
|
4928
|
+
to: "gap"
|
|
4929
|
+
},
|
|
4930
|
+
{
|
|
4931
|
+
parts: ["overflow-x", "overflow-y"],
|
|
4932
|
+
to: "overflow"
|
|
4933
|
+
},
|
|
4934
|
+
{
|
|
4935
|
+
parts: ["overscroll-x", "overscroll-y"],
|
|
4936
|
+
to: "overscroll"
|
|
4937
|
+
},
|
|
4938
|
+
{
|
|
4939
|
+
parts: ["border-spacing-x", "border-spacing-y"],
|
|
4940
|
+
to: "border-spacing"
|
|
4941
|
+
},
|
|
4942
|
+
{
|
|
4943
|
+
parts: ["translate-x", "translate-y"],
|
|
4944
|
+
to: "translate"
|
|
4945
|
+
}
|
|
4946
|
+
];
|
|
4947
|
+
/**
|
|
4948
|
+
* Largest merges first, so four sides collapse to `m-2` instead of also
|
|
4949
|
+
* reporting the `my-2`/`mx-2` halves — three diagnostics for one problem, which
|
|
4950
|
+
* is what this rule used to emit. `sort` is stable, so same-size families keep
|
|
4951
|
+
* their declaration order and the choice stays deterministic.
|
|
4952
|
+
*/
|
|
4953
|
+
const FAMILIES_BY_SIZE = [...SHORTHAND_FAMILIES].sort((a, b) => b.parts.length - a.parts.length);
|
|
4954
|
+
/**
|
|
4955
|
+
* `^<part>-<value>$`, longest prefix first so `rounded-tl-lg` is read as
|
|
4956
|
+
* `rounded-tl` + `lg` and never as `rounded-t` + `l-lg`.
|
|
4957
|
+
*/
|
|
4958
|
+
const VALUE_RE = (() => {
|
|
4959
|
+
const prefixes = [...new Set(SHORTHAND_FAMILIES.flatMap((f) => f.parts))].sort((a, b) => b.length - a.length);
|
|
4960
|
+
return new RegExp(`^(${prefixes.join("|")})-(.+)$`);
|
|
4961
|
+
})();
|
|
4962
|
+
/** A plain number or fraction: always the shared numeric/`--spacing` scale. */
|
|
4963
|
+
const NUMERIC_VALUE_RE = /^\d+(?:\.\d+)?(?:\/\d+(?:\.\d+)?)?$/;
|
|
4964
|
+
/**
|
|
4965
|
+
* Core keywords whose per-axis forms are known to agree, for when no design
|
|
4966
|
+
* system is available to check. Deliberately excludes `screen` (`w-screen` is
|
|
4967
|
+
* `100vw`, `h-screen` is `100vh`) and every named theme token, which is the case
|
|
4968
|
+
* that can only be settled by looking at the emitted CSS.
|
|
4969
|
+
*/
|
|
4970
|
+
const AXIS_SAFE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
4971
|
+
"full",
|
|
4972
|
+
"auto",
|
|
4973
|
+
"min",
|
|
4974
|
+
"max",
|
|
4975
|
+
"fit",
|
|
4976
|
+
"px",
|
|
4419
4977
|
"dvw",
|
|
4420
4978
|
"dvh",
|
|
4421
|
-
"svw",
|
|
4422
|
-
"svh",
|
|
4423
4979
|
"lvw",
|
|
4424
|
-
"lvh"
|
|
4980
|
+
"lvh",
|
|
4981
|
+
"svw",
|
|
4982
|
+
"svh"
|
|
4425
4983
|
]);
|
|
4984
|
+
/** The value is a literal the user wrote, injected verbatim into every part. */
|
|
4985
|
+
function isWrittenValue(value) {
|
|
4986
|
+
return value.startsWith("[") || value.startsWith("(");
|
|
4987
|
+
}
|
|
4426
4988
|
/**
|
|
4427
|
-
*
|
|
4428
|
-
*
|
|
4989
|
+
* Values the class declares on its own box, unconditionally. `null` when the
|
|
4990
|
+
* class produces nothing — which for a named token means it does not exist.
|
|
4429
4991
|
*/
|
|
4430
|
-
function
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
}
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
{
|
|
4467
|
-
parts: [`pl-${value}`, `pr-${value}`],
|
|
4468
|
-
replacement: `px-${value}`
|
|
4469
|
-
},
|
|
4470
|
-
{
|
|
4471
|
-
parts: [`px-${value}`, `py-${value}`],
|
|
4472
|
-
replacement: `p-${value}`
|
|
4473
|
-
},
|
|
4474
|
-
...!INVALID_SIZE_VALUES.has(value) ? [{
|
|
4475
|
-
parts: [`w-${value}`, `h-${value}`],
|
|
4476
|
-
replacement: `size-${value}`
|
|
4477
|
-
}] : [],
|
|
4478
|
-
{
|
|
4479
|
-
parts: [
|
|
4480
|
-
`rounded-tl-${value}`,
|
|
4481
|
-
`rounded-tr-${value}`,
|
|
4482
|
-
`rounded-br-${value}`,
|
|
4483
|
-
`rounded-bl-${value}`
|
|
4484
|
-
],
|
|
4485
|
-
replacement: `rounded-${value}`
|
|
4486
|
-
}
|
|
4487
|
-
];
|
|
4992
|
+
function declaredValues(cache, cls) {
|
|
4993
|
+
const values = /* @__PURE__ */ new Set();
|
|
4994
|
+
for (const decl of cache.getCssDeclarations(cls)) {
|
|
4995
|
+
if (decl.conditional) continue;
|
|
4996
|
+
values.add(decl.value);
|
|
4997
|
+
}
|
|
4998
|
+
return values.size > 0 ? values : null;
|
|
4999
|
+
}
|
|
5000
|
+
function sameValues(a, b) {
|
|
5001
|
+
if (a.size !== b.size) return false;
|
|
5002
|
+
for (const value of a) if (!b.has(value)) return false;
|
|
5003
|
+
return true;
|
|
5004
|
+
}
|
|
5005
|
+
/**
|
|
5006
|
+
* Would collapsing this family at this value keep the CSS identical?
|
|
5007
|
+
*
|
|
5008
|
+
* With a design system this is answered from the emitted declarations. Without
|
|
5009
|
+
* one it falls back to what is safe by construction, which is everything except
|
|
5010
|
+
* a named token on the one family whose parts read different namespaces.
|
|
5011
|
+
*/
|
|
5012
|
+
function mergeIsSafe(family, value, cache) {
|
|
5013
|
+
const structurallySafe = NUMERIC_VALUE_RE.test(value) || isWrittenValue(value);
|
|
5014
|
+
if (!cache) {
|
|
5015
|
+
if (!family.perAxisNamespaces) return true;
|
|
5016
|
+
return structurallySafe || AXIS_SAFE_KEYWORDS.has(value);
|
|
5017
|
+
}
|
|
5018
|
+
if (!cache.isValid(`${family.to}-${value}`)) return false;
|
|
5019
|
+
if (structurallySafe) return true;
|
|
5020
|
+
const expected = declaredValues(cache, `${family.parts[0]}-${value}`);
|
|
5021
|
+
if (!expected) return false;
|
|
5022
|
+
for (let i = 1; i < family.parts.length; i++) {
|
|
5023
|
+
const actual = declaredValues(cache, `${family.parts[i]}-${value}`);
|
|
5024
|
+
if (!actual || !sameValues(expected, actual)) return false;
|
|
5025
|
+
}
|
|
5026
|
+
const replacement = declaredValues(cache, `${family.to}-${value}`);
|
|
5027
|
+
return replacement !== null && sameValues(expected, replacement);
|
|
4488
5028
|
}
|
|
4489
5029
|
const enforceShorthand = defineRule({
|
|
4490
5030
|
meta: {
|
|
4491
5031
|
type: "suggestion",
|
|
4492
5032
|
docs: { description: "Enforce shorthand Tailwind CSS classes when all axes have the same value" },
|
|
4493
5033
|
fixable: "code",
|
|
4494
|
-
schema: [
|
|
5034
|
+
schema: [{
|
|
5035
|
+
type: "object",
|
|
5036
|
+
properties: { entryPoint: { type: "string" } },
|
|
5037
|
+
additionalProperties: false
|
|
5038
|
+
}],
|
|
5039
|
+
defaultOptions: [{}],
|
|
4495
5040
|
messages: { shorthand: "{{parts}} can be simplified to \"{{replacement}}\"." }
|
|
4496
5041
|
},
|
|
4497
5042
|
createOnce(context) {
|
|
5043
|
+
const getDS = createLazyLoader(context);
|
|
4498
5044
|
function check(locations) {
|
|
5045
|
+
const ds = softGetDS(getDS);
|
|
5046
|
+
const cache = ds ? ds.cache : null;
|
|
4499
5047
|
for (const loc of locations) {
|
|
4500
5048
|
const split = splitClassesWithSeparators(loc.value);
|
|
4501
5049
|
const classes = split.classes;
|
|
4502
5050
|
if (classes.length < 2) continue;
|
|
4503
|
-
const
|
|
4504
|
-
const variantValues = /* @__PURE__ */ new Set();
|
|
5051
|
+
const groups = /* @__PURE__ */ new Map();
|
|
4505
5052
|
for (const cls of classes) {
|
|
4506
5053
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
4507
|
-
const
|
|
4508
|
-
|
|
5054
|
+
const { bare, position } = splitImportant(utility);
|
|
5055
|
+
const match = VALUE_RE.exec(bare);
|
|
5056
|
+
if (!match) continue;
|
|
5057
|
+
const [, part, value] = match;
|
|
5058
|
+
const key = `${variant}\0${position ?? ""}\0${value}`;
|
|
5059
|
+
let group = groups.get(key);
|
|
5060
|
+
if (!group) {
|
|
5061
|
+
group = {
|
|
5062
|
+
variant,
|
|
5063
|
+
position,
|
|
5064
|
+
value,
|
|
5065
|
+
byPart: /* @__PURE__ */ new Map()
|
|
5066
|
+
};
|
|
5067
|
+
groups.set(key, group);
|
|
5068
|
+
}
|
|
5069
|
+
group.byPart.set(part, cls);
|
|
4509
5070
|
}
|
|
4510
|
-
for (const
|
|
4511
|
-
|
|
4512
|
-
const
|
|
4513
|
-
const
|
|
4514
|
-
|
|
4515
|
-
|
|
4516
|
-
const
|
|
4517
|
-
const
|
|
4518
|
-
const
|
|
4519
|
-
if (!
|
|
4520
|
-
const
|
|
4521
|
-
const importantEnd = hasImportantSuffix ? "!" : "";
|
|
4522
|
-
const matchParts = rule.parts.map((p) => withVariant(`${importantStart}${p}${importantEnd}`));
|
|
4523
|
-
const remaining = classes.filter((c) => !matchParts.includes(c));
|
|
4524
|
-
remaining.push(withVariant(`${importantStart}${rule.replacement}${importantEnd}`));
|
|
5071
|
+
for (const group of groups.values()) {
|
|
5072
|
+
if (group.byPart.size < 2) continue;
|
|
5073
|
+
const consumed = /* @__PURE__ */ new Set();
|
|
5074
|
+
for (const family of FAMILIES_BY_SIZE) {
|
|
5075
|
+
if (family.parts.some((part) => !group.byPart.has(part) || consumed.has(part))) continue;
|
|
5076
|
+
if (!mergeIsSafe(family, group.value, cache)) continue;
|
|
5077
|
+
const matched = family.parts.map((part) => group.byPart.get(part));
|
|
5078
|
+
const replacement = group.variant + reattachImportant(`${family.to}-${group.value}`, group.position);
|
|
5079
|
+
const remaining = classes.filter((cls) => !matched.includes(cls));
|
|
5080
|
+
if (!remaining.includes(replacement)) remaining.push(replacement);
|
|
5081
|
+
for (const part of family.parts) consumed.add(part);
|
|
4525
5082
|
context.report({
|
|
4526
5083
|
node: loc.node,
|
|
4527
5084
|
messageId: "shorthand",
|
|
4528
5085
|
data: {
|
|
4529
|
-
parts:
|
|
4530
|
-
replacement
|
|
5086
|
+
parts: matched.map((p) => `"${p}"`).join(", "),
|
|
5087
|
+
replacement
|
|
4531
5088
|
},
|
|
4532
5089
|
fix(fixer) {
|
|
4533
5090
|
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
|
|
@@ -4556,7 +5113,8 @@ const LOGICAL_PHYSICAL_SCHEMA = {
|
|
|
4556
5113
|
"block",
|
|
4557
5114
|
"both"
|
|
4558
5115
|
]
|
|
4559
|
-
}
|
|
5116
|
+
},
|
|
5117
|
+
entryPoint: { type: "string" }
|
|
4560
5118
|
},
|
|
4561
5119
|
additionalProperties: false
|
|
4562
5120
|
};
|
|
@@ -4650,17 +5208,73 @@ const PHYSICAL_TO_LOGICAL_MAPPINGS = [
|
|
|
4650
5208
|
from: "scroll-pr",
|
|
4651
5209
|
to: "scroll-pe",
|
|
4652
5210
|
axis: "inline"
|
|
5211
|
+
},
|
|
5212
|
+
{
|
|
5213
|
+
from: "float-left",
|
|
5214
|
+
to: "float-start",
|
|
5215
|
+
axis: "inline",
|
|
5216
|
+
exact: true
|
|
5217
|
+
},
|
|
5218
|
+
{
|
|
5219
|
+
from: "float-right",
|
|
5220
|
+
to: "float-end",
|
|
5221
|
+
axis: "inline",
|
|
5222
|
+
exact: true
|
|
5223
|
+
},
|
|
5224
|
+
{
|
|
5225
|
+
from: "clear-left",
|
|
5226
|
+
to: "clear-start",
|
|
5227
|
+
axis: "inline",
|
|
5228
|
+
exact: true
|
|
5229
|
+
},
|
|
5230
|
+
{
|
|
5231
|
+
from: "clear-right",
|
|
5232
|
+
to: "clear-end",
|
|
5233
|
+
axis: "inline",
|
|
5234
|
+
exact: true
|
|
5235
|
+
},
|
|
5236
|
+
{
|
|
5237
|
+
from: "text-left",
|
|
5238
|
+
to: "text-start",
|
|
5239
|
+
axis: "inline",
|
|
5240
|
+
exact: true
|
|
5241
|
+
},
|
|
5242
|
+
{
|
|
5243
|
+
from: "text-right",
|
|
5244
|
+
to: "text-end",
|
|
5245
|
+
axis: "inline",
|
|
5246
|
+
exact: true
|
|
4653
5247
|
}
|
|
4654
5248
|
];
|
|
5249
|
+
/**
|
|
5250
|
+
* Extra sources for enforce-physical ONLY.
|
|
5251
|
+
*
|
|
5252
|
+
* `enforce-logical` rewrites `left-2` to `start-2`, the spelling Tailwind's own
|
|
5253
|
+
* docs use. `enforce-canonical`, asking the design system, then rewrites that to
|
|
5254
|
+
* `inset-s-2` — same CSS, canonical name. So a codebase that ran both ends up with
|
|
5255
|
+
* `inset-s-*`, and enforce-physical had no way back: its table only knew `start`.
|
|
5256
|
+
*
|
|
5257
|
+
* Kept out of the main table so inverting it doesn't change what enforce-logical
|
|
5258
|
+
* suggests (both spellings map to the same physical class, but only one of them
|
|
5259
|
+
* can be the recommendation).
|
|
5260
|
+
*/
|
|
5261
|
+
const LOGICAL_INSET_ALIASES = [{
|
|
5262
|
+
from: "inset-s",
|
|
5263
|
+
to: "left",
|
|
5264
|
+
axis: "inline"
|
|
5265
|
+
}, {
|
|
5266
|
+
from: "inset-e",
|
|
5267
|
+
to: "right",
|
|
5268
|
+
axis: "inline"
|
|
5269
|
+
}];
|
|
4655
5270
|
/** Invert a directional table. enforce-physical consumes this. */
|
|
4656
5271
|
function invertAxisMappings(mappings) {
|
|
4657
5272
|
return mappings.map((m) => ({
|
|
5273
|
+
...m,
|
|
4658
5274
|
from: m.to,
|
|
4659
|
-
to: m.from
|
|
4660
|
-
axis: m.axis
|
|
5275
|
+
to: m.from
|
|
4661
5276
|
}));
|
|
4662
5277
|
}
|
|
4663
|
-
Object.fromEntries(PHYSICAL_TO_LOGICAL_MAPPINGS.map((m) => [m.from, m.to]));
|
|
4664
5278
|
/**
|
|
4665
5279
|
* Build the `check` callback for a directional-rewrite rule. Both
|
|
4666
5280
|
* enforce-logical and enforce-physical instantiate this with their own
|
|
@@ -4676,6 +5290,7 @@ function createDirectionalMapper(context, opts) {
|
|
|
4676
5290
|
allowlist: compileRegexList(o?.allowlist),
|
|
4677
5291
|
direction: o?.direction ?? "both"
|
|
4678
5292
|
}));
|
|
5293
|
+
const getDS = createLazyLoader(context);
|
|
4679
5294
|
function convertClass(cls) {
|
|
4680
5295
|
const { allowlist, direction } = getState();
|
|
4681
5296
|
if (matchesAny(cls, allowlist)) return null;
|
|
@@ -4683,24 +5298,26 @@ function createDirectionalMapper(context, opts) {
|
|
|
4683
5298
|
const { bare: bareUtility, position } = splitImportant(utility);
|
|
4684
5299
|
const negative = bareUtility.startsWith("-");
|
|
4685
5300
|
const core = negative ? bareUtility.slice(1) : bareUtility;
|
|
4686
|
-
for (const { from, to, axis } of opts.mappings) {
|
|
5301
|
+
for (const { from, to, axis, exact } of opts.mappings) {
|
|
4687
5302
|
if (direction !== "both" && direction !== axis) continue;
|
|
4688
|
-
if (core === from || core.startsWith(`${from}-`))
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
}
|
|
5303
|
+
if (!(core === from || !exact && core.startsWith(`${from}-`))) continue;
|
|
5304
|
+
const suffix = core.slice(from.length);
|
|
5305
|
+
return `${variant}${reattachImportant(`${negative ? "-" : ""}${to}${suffix}`, position)}`;
|
|
4692
5306
|
}
|
|
4693
5307
|
return null;
|
|
4694
5308
|
}
|
|
4695
5309
|
function check(locations) {
|
|
5310
|
+
const ds = softGetDS(getDS);
|
|
5311
|
+
const isUsable = makeReplacementGuard(ds ? ds.cache : null);
|
|
4696
5312
|
for (const loc of locations) {
|
|
4697
5313
|
const split = splitClassesWithSeparators(loc.value);
|
|
4698
5314
|
const offending = split.classes.flatMap((cls) => {
|
|
4699
5315
|
const converted = convertClass(cls);
|
|
4700
|
-
|
|
5316
|
+
if (!converted || !isUsable(converted)) return [];
|
|
5317
|
+
return [{
|
|
4701
5318
|
cls,
|
|
4702
5319
|
replacement: converted
|
|
4703
|
-
}]
|
|
5320
|
+
}];
|
|
4704
5321
|
});
|
|
4705
5322
|
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: opts.messageId });
|
|
4706
5323
|
}
|
|
@@ -4751,7 +5368,7 @@ const enforcePhysical = defineRule({
|
|
|
4751
5368
|
},
|
|
4752
5369
|
createOnce(context) {
|
|
4753
5370
|
const { check } = createDirectionalMapper(context, {
|
|
4754
|
-
mappings: invertAxisMappings(PHYSICAL_TO_LOGICAL_MAPPINGS),
|
|
5371
|
+
mappings: [...invertAxisMappings(PHYSICAL_TO_LOGICAL_MAPPINGS), ...LOGICAL_INSET_ALIASES],
|
|
4755
5372
|
messageId: "usePhysical"
|
|
4756
5373
|
});
|
|
4757
5374
|
return createExtractorVisitors(context, check);
|
|
@@ -4904,7 +5521,7 @@ const noArbitraryValue = defineRule({
|
|
|
4904
5521
|
for (const loc of locations) {
|
|
4905
5522
|
const classes = splitClasses(loc.value);
|
|
4906
5523
|
for (const cls of classes) {
|
|
4907
|
-
if (!
|
|
5524
|
+
if (!utilityHasDynamicValue(cls)) continue;
|
|
4908
5525
|
const utility = splitImportant(extractUtility(cls)).bare;
|
|
4909
5526
|
if (isAllowed(utility)) continue;
|
|
4910
5527
|
context.report({
|
|
@@ -5169,10 +5786,13 @@ const noDarkWithoutLight = defineRule({
|
|
|
5169
5786
|
docs: { description: "Require a base (light) utility when using dark: (or other scheme) variant" },
|
|
5170
5787
|
schema: [{
|
|
5171
5788
|
type: "object",
|
|
5172
|
-
properties: {
|
|
5173
|
-
|
|
5174
|
-
|
|
5175
|
-
|
|
5789
|
+
properties: {
|
|
5790
|
+
variants: {
|
|
5791
|
+
type: "array",
|
|
5792
|
+
items: { type: "string" }
|
|
5793
|
+
},
|
|
5794
|
+
entryPoint: { type: "string" }
|
|
5795
|
+
},
|
|
5176
5796
|
additionalProperties: false
|
|
5177
5797
|
}],
|
|
5178
5798
|
defaultOptions: [{ variants: DEFAULT_VARIANTS }],
|
|
@@ -5180,85 +5800,211 @@ const noDarkWithoutLight = defineRule({
|
|
|
5180
5800
|
},
|
|
5181
5801
|
createOnce(context) {
|
|
5182
5802
|
const getWatchedVariants = createLazyOptions(context, (o) => new Set(o?.variants ?? DEFAULT_VARIANTS));
|
|
5803
|
+
const getDS = createLazyLoader(context);
|
|
5804
|
+
const bareOf = (cls) => splitImportant(extractUtility(cls)).bare;
|
|
5183
5805
|
function check(locations) {
|
|
5184
5806
|
const watchedVariants = getWatchedVariants();
|
|
5807
|
+
const ds = softGetDS(getDS);
|
|
5808
|
+
const cache = ds ? ds.cache : null;
|
|
5185
5809
|
for (const loc of locations) {
|
|
5186
5810
|
const classes = splitClasses(loc.value);
|
|
5811
|
+
if (cache && ds) {
|
|
5812
|
+
let unresolved = null;
|
|
5813
|
+
for (const cls of classes) {
|
|
5814
|
+
const bare = bareOf(cls);
|
|
5815
|
+
if (cache.getCssProperties(bare).length > 0 || !isUserValued(bare)) continue;
|
|
5816
|
+
if (!unresolved) unresolved = [];
|
|
5817
|
+
unresolved.push(bare);
|
|
5818
|
+
}
|
|
5819
|
+
if (unresolved) try {
|
|
5820
|
+
resolveDeclarationsSync(ds.entryPoint, cache, unresolved);
|
|
5821
|
+
} catch (error) {
|
|
5822
|
+
if (!isFatalError(error)) throw error;
|
|
5823
|
+
debugLog(`declaration service unavailable, grouping by prefix only: ${String(error)}`);
|
|
5824
|
+
}
|
|
5825
|
+
}
|
|
5187
5826
|
const basePrefixes = /* @__PURE__ */ new Set();
|
|
5827
|
+
const baseProperties = /* @__PURE__ */ new Set();
|
|
5188
5828
|
const variantClasses = [];
|
|
5189
5829
|
for (const cls of classes) {
|
|
5190
5830
|
const variants = extractVariants(cls);
|
|
5191
5831
|
const prefix = getUtilityPrefix(extractUtility(cls));
|
|
5192
|
-
|
|
5193
|
-
|
|
5194
|
-
|
|
5195
|
-
|
|
5832
|
+
const properties = cache ? cache.getCssProperties(bareOf(cls)) : [];
|
|
5833
|
+
const variant = variants.find((v) => watchedVariants.has(v));
|
|
5834
|
+
if (variant !== void 0) variantClasses.push({
|
|
5835
|
+
cls,
|
|
5836
|
+
variant,
|
|
5837
|
+
prefix,
|
|
5838
|
+
properties
|
|
5839
|
+
});
|
|
5840
|
+
else {
|
|
5841
|
+
basePrefixes.add(prefix);
|
|
5842
|
+
for (const property of properties) baseProperties.add(property);
|
|
5843
|
+
}
|
|
5844
|
+
}
|
|
5845
|
+
for (const { cls, variant, prefix, properties } of variantClasses) {
|
|
5846
|
+
if (basePrefixes.has(prefix)) continue;
|
|
5847
|
+
if (properties.some((property) => baseProperties.has(property))) continue;
|
|
5848
|
+
context.report({
|
|
5849
|
+
node: loc.node,
|
|
5850
|
+
messageId: "missingBase",
|
|
5851
|
+
data: {
|
|
5852
|
+
className: cls,
|
|
5196
5853
|
variant,
|
|
5197
5854
|
prefix
|
|
5198
|
-
}
|
|
5199
|
-
}
|
|
5855
|
+
}
|
|
5856
|
+
});
|
|
5200
5857
|
}
|
|
5201
|
-
for (const { cls, variant, prefix } of variantClasses) if (!basePrefixes.has(prefix)) context.report({
|
|
5202
|
-
node: loc.node,
|
|
5203
|
-
messageId: "missingBase",
|
|
5204
|
-
data: {
|
|
5205
|
-
className: cls,
|
|
5206
|
-
variant,
|
|
5207
|
-
prefix
|
|
5208
|
-
}
|
|
5209
|
-
});
|
|
5210
5858
|
}
|
|
5211
5859
|
}
|
|
5212
5860
|
return createExtractorVisitors(context, check);
|
|
5213
5861
|
}
|
|
5214
5862
|
});
|
|
5215
5863
|
//#endregion
|
|
5864
|
+
//#region src/utils/color-literal.ts
|
|
5865
|
+
/**
|
|
5866
|
+
* Does this CSS value contain a hardcoded color literal, anywhere in it?
|
|
5867
|
+
*
|
|
5868
|
+
* `no-hardcoded-colors` used to answer that with `^`-anchored regexes over the
|
|
5869
|
+
* whole value, which only ever matched a value that IS a color. Every real
|
|
5870
|
+
* shorthand slipped through: `shadow-[0_1px_2px_#000]` carries a hardcoded black
|
|
5871
|
+
* that the rule promised to catch and didn't. Anchoring also forced a companion
|
|
5872
|
+
* list of "color-bearing utility prefixes" to keep the match plausible, and that
|
|
5873
|
+
* list drifted from Tailwind (it has `ring` but not `inset-ring`, `shadow` but not
|
|
5874
|
+
* `inset-shadow`, and no way to spell an arbitrary property like `[color:#f00]`).
|
|
5875
|
+
*
|
|
5876
|
+
* Scanning the value instead of anchoring to it drops both problems: the value
|
|
5877
|
+
* carries the color or it doesn't, whatever utility it is attached to.
|
|
5878
|
+
*
|
|
5879
|
+
* Two things must NOT count, and they are why this is a scanner rather than a
|
|
5880
|
+
* regex:
|
|
5881
|
+
* - quoted strings — `content-['#fff']` is the text `#fff`, not a color;
|
|
5882
|
+
* - `url()` contents — `fill-[url(#gradient)]` references an SVG node by id, the
|
|
5883
|
+
* single most common `#` in a Tailwind class that isn't a color.
|
|
5884
|
+
*
|
|
5885
|
+
* The notation set is exactly the one the rule documents (hex + the CSS color
|
|
5886
|
+
* functions). Named colors (`bg-[red]`) are deliberately absent: they were never
|
|
5887
|
+
* covered, and `currentColor`/`transparent`/`inherit` make that a separate
|
|
5888
|
+
* judgement call rather than a scan.
|
|
5889
|
+
*/
|
|
5890
|
+
/** CSS color functions. `color-mix` is absent on purpose: its arguments are scanned. */
|
|
5891
|
+
const COLOR_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
5892
|
+
"rgb",
|
|
5893
|
+
"rgba",
|
|
5894
|
+
"hsl",
|
|
5895
|
+
"hsla",
|
|
5896
|
+
"hwb",
|
|
5897
|
+
"lab",
|
|
5898
|
+
"lch",
|
|
5899
|
+
"oklab",
|
|
5900
|
+
"oklch",
|
|
5901
|
+
"color"
|
|
5902
|
+
]);
|
|
5903
|
+
/** Hex digit counts CSS accepts: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`. */
|
|
5904
|
+
const HEX_LENGTHS = /* @__PURE__ */ new Set([
|
|
5905
|
+
3,
|
|
5906
|
+
4,
|
|
5907
|
+
6,
|
|
5908
|
+
8
|
|
5909
|
+
]);
|
|
5910
|
+
function isHexDigit(ch) {
|
|
5911
|
+
return ch >= "0" && ch <= "9" || ch >= "a" && ch <= "f" || ch >= "A" && ch <= "F";
|
|
5912
|
+
}
|
|
5913
|
+
/**
|
|
5914
|
+
* `-` belongs to the identifier so `color-mix(` is not read as `color(`. `_` does
|
|
5915
|
+
* NOT: in a Tailwind arbitrary value an underscore stands for a space, so
|
|
5916
|
+
* `0_0_4px_rgb(…)` has to break into words or the function name never surfaces.
|
|
5917
|
+
*/
|
|
5918
|
+
function isIdentChar(ch) {
|
|
5919
|
+
return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch >= "0" && ch <= "9" || ch === "-";
|
|
5920
|
+
}
|
|
5921
|
+
function isIdentStart(ch) {
|
|
5922
|
+
return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z";
|
|
5923
|
+
}
|
|
5924
|
+
/** Index just past the closing quote (or end of input on an unterminated string). */
|
|
5925
|
+
function skipQuoted(value, start) {
|
|
5926
|
+
const quote = value[start];
|
|
5927
|
+
let i = start + 1;
|
|
5928
|
+
while (i < value.length) {
|
|
5929
|
+
if (value[i] === "\\") {
|
|
5930
|
+
i += 2;
|
|
5931
|
+
continue;
|
|
5932
|
+
}
|
|
5933
|
+
if (value[i] === quote) return i + 1;
|
|
5934
|
+
i++;
|
|
5935
|
+
}
|
|
5936
|
+
return value.length;
|
|
5937
|
+
}
|
|
5938
|
+
/** Index just past the matching `)`, given `open` pointing at a `(`. */
|
|
5939
|
+
function skipBalanced(value, open) {
|
|
5940
|
+
let depth = 0;
|
|
5941
|
+
let i = open;
|
|
5942
|
+
while (i < value.length) {
|
|
5943
|
+
const ch = value[i];
|
|
5944
|
+
if (ch === "\"" || ch === "'") {
|
|
5945
|
+
i = skipQuoted(value, i);
|
|
5946
|
+
continue;
|
|
5947
|
+
}
|
|
5948
|
+
if (ch === "(") depth++;
|
|
5949
|
+
else if (ch === ")") {
|
|
5950
|
+
depth--;
|
|
5951
|
+
if (depth === 0) return i + 1;
|
|
5952
|
+
}
|
|
5953
|
+
i++;
|
|
5954
|
+
}
|
|
5955
|
+
return value.length;
|
|
5956
|
+
}
|
|
5957
|
+
function containsColorLiteral(value) {
|
|
5958
|
+
let i = 0;
|
|
5959
|
+
while (i < value.length) {
|
|
5960
|
+
const ch = value[i];
|
|
5961
|
+
if (ch === "\"" || ch === "'") {
|
|
5962
|
+
i = skipQuoted(value, i);
|
|
5963
|
+
continue;
|
|
5964
|
+
}
|
|
5965
|
+
if (ch === "#") {
|
|
5966
|
+
let end = i + 1;
|
|
5967
|
+
while (end < value.length && isHexDigit(value[end])) end++;
|
|
5968
|
+
if (HEX_LENGTHS.has(end - i - 1)) return true;
|
|
5969
|
+
i = end > i + 1 ? end : i + 1;
|
|
5970
|
+
continue;
|
|
5971
|
+
}
|
|
5972
|
+
if (isIdentStart(ch)) {
|
|
5973
|
+
const start = i;
|
|
5974
|
+
while (i < value.length && isIdentChar(value[i])) i++;
|
|
5975
|
+
if (value[i] !== "(") continue;
|
|
5976
|
+
const name = value.slice(start, i).toLowerCase();
|
|
5977
|
+
if (name === "url") {
|
|
5978
|
+
i = skipBalanced(value, i);
|
|
5979
|
+
continue;
|
|
5980
|
+
}
|
|
5981
|
+
if (COLOR_FUNCTIONS.has(name)) return true;
|
|
5982
|
+
continue;
|
|
5983
|
+
}
|
|
5984
|
+
i++;
|
|
5985
|
+
}
|
|
5986
|
+
return false;
|
|
5987
|
+
}
|
|
5988
|
+
//#endregion
|
|
5216
5989
|
//#region src/rules/no-hardcoded-colors.ts
|
|
5217
|
-
const COLOR_UTILITY_PREFIXES = [
|
|
5218
|
-
"bg",
|
|
5219
|
-
"text",
|
|
5220
|
-
"border",
|
|
5221
|
-
"border-t",
|
|
5222
|
-
"border-b",
|
|
5223
|
-
"border-l",
|
|
5224
|
-
"border-r",
|
|
5225
|
-
"border-s",
|
|
5226
|
-
"border-e",
|
|
5227
|
-
"border-x",
|
|
5228
|
-
"border-y",
|
|
5229
|
-
"outline",
|
|
5230
|
-
"ring",
|
|
5231
|
-
"ring-offset",
|
|
5232
|
-
"shadow",
|
|
5233
|
-
"accent",
|
|
5234
|
-
"caret",
|
|
5235
|
-
"fill",
|
|
5236
|
-
"stroke",
|
|
5237
|
-
"decoration",
|
|
5238
|
-
"divide",
|
|
5239
|
-
"placeholder",
|
|
5240
|
-
"from",
|
|
5241
|
-
"via",
|
|
5242
|
-
"to"
|
|
5243
|
-
];
|
|
5244
|
-
const HEX_RE = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
5245
|
-
const RGB_RE = /^rgba?\s*\(/;
|
|
5246
|
-
const HSL_RE = /^hsla?\s*\(/;
|
|
5247
|
-
const OKLCH_RE = /^oklch\s*\(/;
|
|
5248
|
-
const OKLAB_RE = /^oklab\s*\(/;
|
|
5249
|
-
const LCH_RE = /^lch\s*\(/;
|
|
5250
|
-
const LAB_RE = /^lab\s*\(/;
|
|
5251
|
-
const HWB_RE = /^hwb\s*\(/;
|
|
5252
|
-
const COLOR_RE = /^color\s*\(/;
|
|
5253
5990
|
const VAR_REF_RE = /var\(\s*--[\w-]+/i;
|
|
5991
|
+
/**
|
|
5992
|
+
* Does the arbitrary value the user wrote carry a hardcoded colour?
|
|
5993
|
+
*
|
|
5994
|
+
* This used to be a set of `^`-anchored regexes plus a list of "colour-bearing
|
|
5995
|
+
* utility prefixes", and both halves leaked. The anchoring meant only a value
|
|
5996
|
+
* that IS a colour matched, so `shadow-[0_1px_2px_#000]` passed. The prefix list
|
|
5997
|
+
* had `ring` but not `inset-ring`, `shadow` but not `inset-shadow`, and no way to
|
|
5998
|
+
* spell an arbitrary property at all (`[color:#f00]`, `[--brand:#f00]`).
|
|
5999
|
+
*
|
|
6000
|
+
* Neither is needed: the value carries the colour or it doesn't, whatever utility
|
|
6001
|
+
* it is attached to. `containsColorLiteral` skips quoted strings and `url()`
|
|
6002
|
+
* contents, which is what keeps `content-['#fff']` and `fill-[url(#gradient)]` out.
|
|
6003
|
+
*/
|
|
5254
6004
|
function isHardcodedColor(value) {
|
|
5255
6005
|
const trimmed = value.trim();
|
|
5256
6006
|
if (VAR_REF_RE.test(trimmed)) return false;
|
|
5257
|
-
return
|
|
5258
|
-
}
|
|
5259
|
-
function isColorUtility(utility) {
|
|
5260
|
-
for (const prefix of COLOR_UTILITY_PREFIXES) if (utility === prefix || utility.startsWith(`${prefix}-`)) return true;
|
|
5261
|
-
return false;
|
|
6007
|
+
return containsColorLiteral(trimmed);
|
|
5262
6008
|
}
|
|
5263
6009
|
const noHardcodedColors = defineRule({
|
|
5264
6010
|
meta: {
|
|
@@ -5284,8 +6030,6 @@ const noHardcodedColors = defineRule({
|
|
|
5284
6030
|
for (const cls of classes) {
|
|
5285
6031
|
if (allowlist.has(cls)) continue;
|
|
5286
6032
|
if (!hasArbitraryValue(cls)) continue;
|
|
5287
|
-
const utility = splitImportant(extractUtility(cls)).bare;
|
|
5288
|
-
if (!isColorUtility(utility)) continue;
|
|
5289
6033
|
const value = getArbitraryValue(cls);
|
|
5290
6034
|
if (!value) continue;
|
|
5291
6035
|
if (isHardcodedColor(value)) context.report({
|
|
@@ -5316,13 +6060,9 @@ const noContradictingVariants = defineRule({
|
|
|
5316
6060
|
createOnce(context) {
|
|
5317
6061
|
const getDS = createLazyLoader(context);
|
|
5318
6062
|
function variantFactsLookup() {
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
} catch (err) {
|
|
5323
|
-
if (!isFatalError(err)) throw err;
|
|
5324
|
-
return () => void 0;
|
|
5325
|
-
}
|
|
6063
|
+
const ds = softGetDS(getDS);
|
|
6064
|
+
if (!ds) return () => void 0;
|
|
6065
|
+
return (variant) => ds.cache.getVariantFacts(variant);
|
|
5326
6066
|
}
|
|
5327
6067
|
function check(locations) {
|
|
5328
6068
|
const factsFor = variantFactsLookup();
|
|
@@ -5469,15 +6209,9 @@ const consistentVariantOrder = defineRule({
|
|
|
5469
6209
|
const priorityByEntry = /* @__PURE__ */ new Map();
|
|
5470
6210
|
const prefixByEntry = /* @__PURE__ */ new Map();
|
|
5471
6211
|
function resolveForFile() {
|
|
5472
|
-
|
|
5473
|
-
|
|
5474
|
-
|
|
5475
|
-
const ds = getDS();
|
|
5476
|
-
dsCache = ds.cache;
|
|
5477
|
-
entryKey = ds.entryPoint;
|
|
5478
|
-
} catch (err) {
|
|
5479
|
-
if (!isFatalError(err)) throw err;
|
|
5480
|
-
}
|
|
6212
|
+
const ds = softGetDS(getDS);
|
|
6213
|
+
const dsCache = ds ? ds.cache : null;
|
|
6214
|
+
const entryKey = ds ? ds.entryPoint : "";
|
|
5481
6215
|
let priorityOf = priorityByEntry.get(entryKey);
|
|
5482
6216
|
if (!priorityOf) {
|
|
5483
6217
|
priorityOf = buildPriority(getOptions().order, dsCache);
|
|
@@ -5721,6 +6455,106 @@ function detectRawVariable(utility) {
|
|
|
5721
6455
|
};
|
|
5722
6456
|
return null;
|
|
5723
6457
|
}
|
|
6458
|
+
const preferThemeTokens = defineRule({
|
|
6459
|
+
meta: {
|
|
6460
|
+
type: "suggestion",
|
|
6461
|
+
docs: { description: "Prefer named theme-token utilities over raw CSS variable references when a matching utility exists" },
|
|
6462
|
+
fixable: "code",
|
|
6463
|
+
schema: [{
|
|
6464
|
+
type: "object",
|
|
6465
|
+
properties: { entryPoint: { type: "string" } },
|
|
6466
|
+
additionalProperties: false
|
|
6467
|
+
}],
|
|
6468
|
+
hasSuggestions: true,
|
|
6469
|
+
defaultOptions: [{}],
|
|
6470
|
+
messages: {
|
|
6471
|
+
preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
|
|
6472
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
6473
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
6474
|
+
}
|
|
6475
|
+
},
|
|
6476
|
+
createOnce(context) {
|
|
6477
|
+
const getDS = createLazyLoader(context);
|
|
6478
|
+
function check(locations) {
|
|
6479
|
+
if (locations.length === 0) return;
|
|
6480
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
6481
|
+
if (!ds) return;
|
|
6482
|
+
const { cache } = ds;
|
|
6483
|
+
for (const loc of locations) {
|
|
6484
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
6485
|
+
const offending = split.classes.flatMap((cls) => {
|
|
6486
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
6487
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
6488
|
+
const match = detectRawVariable(bareUtility);
|
|
6489
|
+
if (!match) return [];
|
|
6490
|
+
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
6491
|
+
if (!cache.isValid(candidate)) return [];
|
|
6492
|
+
const target = `--${match.varName}`;
|
|
6493
|
+
const baseCandidate = `${match.prefix}-${match.varName}`;
|
|
6494
|
+
if (!cache.getCssDeclarations(baseCandidate).some((decl) => [...decl.readsVars, ...decl.readsFallbackVars].some((read) => cache.themeVarResolvesTo(read, target))) && cache.definesVar(target)) return [];
|
|
6495
|
+
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
6496
|
+
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|
|
6497
|
+
return [{
|
|
6498
|
+
cls,
|
|
6499
|
+
replacement: variant + reattachImportant(candidate, position)
|
|
6500
|
+
}];
|
|
6501
|
+
});
|
|
6502
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "preferNamed" });
|
|
6503
|
+
}
|
|
6504
|
+
}
|
|
6505
|
+
return createExtractorVisitors(context, check);
|
|
6506
|
+
}
|
|
6507
|
+
});
|
|
6508
|
+
//#endregion
|
|
6509
|
+
//#region src/rules/prefer-scale-token.ts
|
|
6510
|
+
/** A length or plain number. Anything else can't be compared arithmetically. */
|
|
6511
|
+
const LENGTH_RE = /^(-?\d*\.?\d+)(rem|px|em)?$/;
|
|
6512
|
+
/**
|
|
6513
|
+
* Parse a CSS length. `em` is treated as `rem`, which is what it is at the root
|
|
6514
|
+
* and the only interpretation available without a layout.
|
|
6515
|
+
*/
|
|
6516
|
+
function measure(value, rootFontSize) {
|
|
6517
|
+
const match = LENGTH_RE.exec(value.trim());
|
|
6518
|
+
if (!match) return null;
|
|
6519
|
+
const n = Number.parseFloat(match[1]);
|
|
6520
|
+
if (!Number.isFinite(n)) return null;
|
|
6521
|
+
switch (match[2]) {
|
|
6522
|
+
case "rem":
|
|
6523
|
+
case "em": return {
|
|
6524
|
+
px: n * rootFontSize,
|
|
6525
|
+
unitless: false
|
|
6526
|
+
};
|
|
6527
|
+
case "px": return {
|
|
6528
|
+
px: n,
|
|
6529
|
+
unitless: false
|
|
6530
|
+
};
|
|
6531
|
+
case void 0: return {
|
|
6532
|
+
px: n,
|
|
6533
|
+
unitless: true
|
|
6534
|
+
};
|
|
6535
|
+
default: return null;
|
|
6536
|
+
}
|
|
6537
|
+
}
|
|
6538
|
+
/**
|
|
6539
|
+
* Same value, allowing for float noise (`0.875rem` × 16 = 13.999999999999998).
|
|
6540
|
+
* A unitless number and a length are never the same value, whatever the digits.
|
|
6541
|
+
*/
|
|
6542
|
+
function sameMeasure(a, b) {
|
|
6543
|
+
return a.unitless === b.unitless && Math.abs(a.px - b.px) < 1e-4;
|
|
6544
|
+
}
|
|
6545
|
+
/**
|
|
6546
|
+
* Is `n` a whole number of `step`s? `10 / 0.5` is exact in binary, `0.3 / 0.1`
|
|
6547
|
+
* is not, so the remainder is compared with a tolerance rather than to zero.
|
|
6548
|
+
*/
|
|
6549
|
+
function isOnStep(n, step) {
|
|
6550
|
+
if (step <= 0) return false;
|
|
6551
|
+
const steps = n / step;
|
|
6552
|
+
return Math.abs(steps - Math.round(steps)) < 1e-4;
|
|
6553
|
+
}
|
|
6554
|
+
/** Trailing zeros make `p-2.50`, which is not what Tailwind calls the class. */
|
|
6555
|
+
function formatStep(n) {
|
|
6556
|
+
return String(Number.parseFloat(n.toFixed(4)));
|
|
6557
|
+
}
|
|
5724
6558
|
//#endregion
|
|
5725
6559
|
//#region src/index.ts
|
|
5726
6560
|
const plugin = definePlugin({
|
|
@@ -5748,26 +6582,77 @@ const plugin = definePlugin({
|
|
|
5748
6582
|
"consistent-variant-order": consistentVariantOrder,
|
|
5749
6583
|
"enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
|
|
5750
6584
|
"no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue,
|
|
5751
|
-
"prefer-theme-tokens":
|
|
6585
|
+
"prefer-theme-tokens": preferThemeTokens,
|
|
6586
|
+
"prefer-scale-token": defineRule({
|
|
5752
6587
|
meta: {
|
|
5753
6588
|
type: "suggestion",
|
|
5754
|
-
docs: { description: "Prefer
|
|
5755
|
-
|
|
6589
|
+
docs: { description: "Prefer the scale step or theme token a hardcoded value is numerically equal to" },
|
|
6590
|
+
hasSuggestions: true,
|
|
5756
6591
|
schema: [{
|
|
5757
6592
|
type: "object",
|
|
5758
|
-
properties: {
|
|
6593
|
+
properties: {
|
|
6594
|
+
entryPoint: { type: "string" },
|
|
6595
|
+
step: { type: "number" },
|
|
6596
|
+
allow: {
|
|
6597
|
+
type: "array",
|
|
6598
|
+
items: { type: "string" }
|
|
6599
|
+
}
|
|
6600
|
+
},
|
|
5759
6601
|
additionalProperties: false
|
|
5760
6602
|
}],
|
|
5761
|
-
hasSuggestions: true,
|
|
5762
6603
|
defaultOptions: [{}],
|
|
5763
6604
|
messages: {
|
|
5764
|
-
|
|
6605
|
+
preferToken: "\"{{className}}\" is the same value as \"{{replacement}}\". Use the token — but check it first: the token resolves through a CSS variable, so the two are equal in this theme, not by construction.",
|
|
5765
6606
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
5766
6607
|
...DS_UNAVAILABLE_MESSAGE
|
|
5767
6608
|
}
|
|
5768
6609
|
},
|
|
5769
6610
|
createOnce(context) {
|
|
5770
6611
|
const getDS = createLazyLoader(context);
|
|
6612
|
+
const getOptions = createLazyOptions(context, (o) => ({
|
|
6613
|
+
step: typeof o?.step === "number" ? o.step : null,
|
|
6614
|
+
allow: o?.allow ?? []
|
|
6615
|
+
}));
|
|
6616
|
+
let _rem = null;
|
|
6617
|
+
function rootFontSize() {
|
|
6618
|
+
if (_rem === null) _rem = rootFontSizeFromSettings(safeSettings(context));
|
|
6619
|
+
return _rem;
|
|
6620
|
+
}
|
|
6621
|
+
/**
|
|
6622
|
+
* The class this literal could have been written as, or null.
|
|
6623
|
+
*
|
|
6624
|
+
* Two families, in order of preference: a named theme token whose value
|
|
6625
|
+
* matches (`rounded-[0.5rem]` → `rounded-lg`), then a step of the spacing
|
|
6626
|
+
* scale (`p-[10px]` → `p-2.5`). Both come from data the precompute derived
|
|
6627
|
+
* from the design system, so neither is a name table.
|
|
6628
|
+
*/
|
|
6629
|
+
function equivalentOf(cache, bare) {
|
|
6630
|
+
const { step: stepOverride, allow } = getOptions();
|
|
6631
|
+
if (allow.some((prefix) => bare.startsWith(prefix))) return null;
|
|
6632
|
+
if (cache.getNamedEquivalent(bare)) return null;
|
|
6633
|
+
const value = getArbitraryValue(bare);
|
|
6634
|
+
if (value === null) return null;
|
|
6635
|
+
const written = measure(value, rootFontSize());
|
|
6636
|
+
if (written === null) return null;
|
|
6637
|
+
const open = bare.indexOf("[");
|
|
6638
|
+
if (open <= 0 || bare[open - 1] !== "-") return null;
|
|
6639
|
+
const prefix = bare.slice(0, open - 1);
|
|
6640
|
+
if (!prefix) return null;
|
|
6641
|
+
for (const [literal, className] of cache.tokenValuesFor(prefix)) {
|
|
6642
|
+
const token = measure(literal, rootFontSize());
|
|
6643
|
+
if (token !== null && sameMeasure(written, token)) return className;
|
|
6644
|
+
}
|
|
6645
|
+
if (!cache.readsScale(prefix)) return null;
|
|
6646
|
+
const scale = cache.scale;
|
|
6647
|
+
if (!scale) return null;
|
|
6648
|
+
const unit = measure(scale.unit, rootFontSize());
|
|
6649
|
+
if (unit === null || unit.px <= 0 || unit.unitless !== written.unitless) return null;
|
|
6650
|
+
const steps = written.px / unit.px;
|
|
6651
|
+
if (steps < 0) return null;
|
|
6652
|
+
if (!isOnStep(steps, stepOverride ?? scale.step)) return null;
|
|
6653
|
+
const candidate = `${prefix}-${formatStep(steps)}`;
|
|
6654
|
+
return cache.isValid(candidate) ? candidate : null;
|
|
6655
|
+
}
|
|
5771
6656
|
function check(locations) {
|
|
5772
6657
|
if (locations.length === 0) return;
|
|
5773
6658
|
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
@@ -5775,24 +6660,33 @@ const plugin = definePlugin({
|
|
|
5775
6660
|
const { cache } = ds;
|
|
5776
6661
|
for (const loc of locations) {
|
|
5777
6662
|
const split = splitClassesWithSeparators(loc.value);
|
|
5778
|
-
const
|
|
6663
|
+
for (const cls of split.classes) {
|
|
6664
|
+
if (!utilityHasDynamicValue(cls)) continue;
|
|
5779
6665
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
5780
|
-
const { bare
|
|
5781
|
-
const
|
|
5782
|
-
if (!
|
|
5783
|
-
const
|
|
5784
|
-
|
|
5785
|
-
|
|
5786
|
-
|
|
5787
|
-
|
|
5788
|
-
|
|
5789
|
-
|
|
5790
|
-
|
|
5791
|
-
|
|
5792
|
-
|
|
5793
|
-
|
|
5794
|
-
|
|
5795
|
-
|
|
6666
|
+
const { bare, position } = splitImportant(utility);
|
|
6667
|
+
const equivalent = equivalentOf(cache, bare);
|
|
6668
|
+
if (!equivalent) continue;
|
|
6669
|
+
const replacement = variant + reattachImportant(equivalent, position);
|
|
6670
|
+
const fixedValue = rebuildClassString(split, split.classes.map((c) => c === cls ? replacement : c));
|
|
6671
|
+
context.report({
|
|
6672
|
+
node: loc.node,
|
|
6673
|
+
messageId: "preferToken",
|
|
6674
|
+
data: {
|
|
6675
|
+
className: cls,
|
|
6676
|
+
replacement
|
|
6677
|
+
},
|
|
6678
|
+
suggest: [{
|
|
6679
|
+
messageId: "suggestReplace",
|
|
6680
|
+
data: {
|
|
6681
|
+
className: cls,
|
|
6682
|
+
replacement
|
|
6683
|
+
},
|
|
6684
|
+
fix(fixer) {
|
|
6685
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
6686
|
+
}
|
|
6687
|
+
}]
|
|
6688
|
+
});
|
|
6689
|
+
}
|
|
5796
6690
|
}
|
|
5797
6691
|
}
|
|
5798
6692
|
return createExtractorVisitors(context, check);
|