oxlint-tailwindcss 1.5.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +448 -79
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +448 -79
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3,7 +3,7 @@ let node_crypto = require("node:crypto");
|
|
|
3
3
|
let node_fs = require("node:fs");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
let node_os = require("node:os");
|
|
6
|
-
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.
|
|
6
|
+
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.76.0/node_modules/@oxlint/plugins/index.js
|
|
7
7
|
/**
|
|
8
8
|
* Define a plugin.
|
|
9
9
|
*
|
|
@@ -959,6 +959,7 @@ function roundRemValue(className) {
|
|
|
959
959
|
//#endregion
|
|
960
960
|
//#region src/design-system/cache.ts
|
|
961
961
|
const EMPTY_DECLARATIONS = [];
|
|
962
|
+
const EMPTY_TOKEN_VALUES = [];
|
|
962
963
|
var DesignSystemCache = class DesignSystemCache {
|
|
963
964
|
canonicalMap = /* @__PURE__ */ new Map();
|
|
964
965
|
deprecatedMap = /* @__PURE__ */ new Map();
|
|
@@ -970,6 +971,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
970
971
|
propsMemo = /* @__PURE__ */ new Map();
|
|
971
972
|
partialSet = /* @__PURE__ */ new Set();
|
|
972
973
|
valueIdByText = null;
|
|
974
|
+
nextValueId = 0;
|
|
973
975
|
variantOrderMap = /* @__PURE__ */ new Map();
|
|
974
976
|
variantFactsMap = /* @__PURE__ */ new Map();
|
|
975
977
|
arbitraryEquivMap = /* @__PURE__ */ new Map();
|
|
@@ -980,6 +982,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
980
982
|
componentSet = /* @__PURE__ */ new Set();
|
|
981
983
|
themeRefs = /* @__PURE__ */ new Map();
|
|
982
984
|
definedVarSet = /* @__PURE__ */ new Set();
|
|
985
|
+
tokenValuesMap = /* @__PURE__ */ new Map();
|
|
986
|
+
scaleFacts = null;
|
|
983
987
|
static fromPrecomputed(data) {
|
|
984
988
|
const cache = new DesignSystemCache();
|
|
985
989
|
cache._validClasses = data.validClasses;
|
|
@@ -1007,6 +1011,12 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1007
1011
|
}
|
|
1008
1012
|
for (const name of data.definedVars ?? []) cache.definedVarSet.add(name);
|
|
1009
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
|
+
};
|
|
1010
1020
|
cache._prefix = data.prefix ?? "";
|
|
1011
1021
|
if (data.arbitraryEquivalents) for (const [arb, named] of Object.entries(data.arbitraryEquivalents)) cache.arbitraryEquivMap.set(arb, named);
|
|
1012
1022
|
return cache;
|
|
@@ -1129,10 +1139,63 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1129
1139
|
isKnownClass(className) {
|
|
1130
1140
|
return this.validitySet.has(this.stripProjectPrefix(className));
|
|
1131
1141
|
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Is this a NAMED group/peer marker — `group/menu-item`, `peer/menu-button`?
|
|
1144
|
+
*
|
|
1145
|
+
* A marker binds a variant to one specific ancestor or sibling, and the
|
|
1146
|
+
* consumer's compiled selector hard-codes it as a class selector:
|
|
1147
|
+
* `group-hover/menu-item:underline` emits
|
|
1148
|
+
* `:is(:where(.group\/menu-item):hover *)`. A class selector matches whole
|
|
1149
|
+
* tokens, so bare `group` does not satisfy it — the named marker is required
|
|
1150
|
+
* markup. Tailwind emits no CSS for the marker itself, which is why
|
|
1151
|
+
* `no-unknown-classes` cannot ask "does it compile?" about one.
|
|
1152
|
+
*
|
|
1153
|
+
* Derived, not listed: the base must be a precomputed class that emits ZERO
|
|
1154
|
+
* declarations, which across every fixture resolves to exactly `group` and
|
|
1155
|
+
* `peer` (2 of ~23.6k). It self-prunes if Tailwind drops the variants,
|
|
1156
|
+
* self-extends if a new CSS-less marker appears, and excludes `@container/main`
|
|
1157
|
+
* for free — that one HAS declarations. The component set is subtracted
|
|
1158
|
+
* because a class referenced only through `[class~="…"]` (`not-prose`) also has
|
|
1159
|
+
* no declarations of its own, and `not-prose/x` is not Tailwind syntax.
|
|
1160
|
+
*
|
|
1161
|
+
* The name is user-chosen and Tailwind never checks that it exists, so any
|
|
1162
|
+
* NON-EMPTY name is accepted — including the shapes only an arbitrary modifier
|
|
1163
|
+
* can reach (`group/*`, `group/a/b`, `peer//x`), which is why this splits at
|
|
1164
|
+
* the FIRST slash rather than the last one `isValid` uses. The empty name is
|
|
1165
|
+
* the one genuinely dead spelling: `group-hover/` compiles to nothing.
|
|
1166
|
+
*/
|
|
1167
|
+
isMarkerClass(className) {
|
|
1168
|
+
const bare = this.stripProjectPrefix(className);
|
|
1169
|
+
const slash = bare.indexOf("/");
|
|
1170
|
+
if (slash <= 0 || slash === bare.length - 1) return false;
|
|
1171
|
+
const base = bare.slice(0, slash);
|
|
1172
|
+
return this.validitySet.has(base) && !this.componentSet.has(base) && this.getCssDeclarations(base).length === 0;
|
|
1173
|
+
}
|
|
1132
1174
|
/** Variant names the design system reports, for suggesting a typo's neighbour. */
|
|
1133
1175
|
variantNames() {
|
|
1134
1176
|
return [...this.variantOrderMap.keys()];
|
|
1135
1177
|
}
|
|
1178
|
+
/**
|
|
1179
|
+
* The numeric theme tokens this utility prefix can be written with, as
|
|
1180
|
+
* `[literal value, class]`. Empty when the prefix has none.
|
|
1181
|
+
*/
|
|
1182
|
+
tokenValuesFor(prefix) {
|
|
1183
|
+
return this.tokenValuesMap.get(this.stripProjectPrefix(prefix)) ?? EMPTY_TOKEN_VALUES;
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* The spacing scale as the design system describes it: the resolved value of
|
|
1187
|
+
* `--spacing`, the granularity Tailwind's own enumerated steps use, and whether
|
|
1188
|
+
* a given prefix reads it. `null` when the theme has no `--spacing`.
|
|
1189
|
+
*/
|
|
1190
|
+
get scale() {
|
|
1191
|
+
return this.scaleFacts ? {
|
|
1192
|
+
unit: this.scaleFacts.unit,
|
|
1193
|
+
step: this.scaleFacts.step
|
|
1194
|
+
} : null;
|
|
1195
|
+
}
|
|
1196
|
+
readsScale(prefix) {
|
|
1197
|
+
return this.scaleFacts?.prefixes.has(this.stripProjectPrefix(prefix)) ?? false;
|
|
1198
|
+
}
|
|
1136
1199
|
isValid(className) {
|
|
1137
1200
|
if (this.validitySet.has(className)) return true;
|
|
1138
1201
|
const utility = extractUtility(className);
|
|
@@ -1277,7 +1340,10 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1277
1340
|
internDeclarations(className, raws, valueFacts) {
|
|
1278
1341
|
const index = this.declIndex;
|
|
1279
1342
|
if (!index) return EMPTY_DECLARATIONS;
|
|
1280
|
-
if (!this.valueIdByText)
|
|
1343
|
+
if (!this.valueIdByText) {
|
|
1344
|
+
this.valueIdByText = new Map(index.values.map((value, id) => [value, id]));
|
|
1345
|
+
this.nextValueId = index.values.length;
|
|
1346
|
+
}
|
|
1281
1347
|
const decls = [];
|
|
1282
1348
|
let hasElement = false;
|
|
1283
1349
|
let hasDescendant = false;
|
|
@@ -1285,8 +1351,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1285
1351
|
for (const [scopeToken, prop, value] of raws) {
|
|
1286
1352
|
let valueId = this.valueIdByText.get(value);
|
|
1287
1353
|
if (valueId === void 0) {
|
|
1288
|
-
valueId =
|
|
1289
|
-
index.values.push(value);
|
|
1354
|
+
valueId = this.nextValueId++;
|
|
1290
1355
|
this.valueIdByText.set(value, valueId);
|
|
1291
1356
|
}
|
|
1292
1357
|
const facts = valueFacts[value];
|
|
@@ -1807,7 +1872,12 @@ function resolveImport(specifier, baseDir) {
|
|
|
1807
1872
|
return null;
|
|
1808
1873
|
}
|
|
1809
1874
|
|
|
1810
|
-
|
|
1875
|
+
// The entry CSS plus every \`@import\` that resolves, one level deep. Shared by
|
|
1876
|
+
// the component-class scan and the \`definedVars\` scan: a custom property declared
|
|
1877
|
+
// in an imported file is as real to the browser as one in the entry, and treating
|
|
1878
|
+
// it as undefined makes prefer-theme-tokens propose a token that can change the
|
|
1879
|
+
// design (the #78 hazard, in miniature).
|
|
1880
|
+
function collectCssSources(cssPath, baseDir) {
|
|
1811
1881
|
let css;
|
|
1812
1882
|
try { css = readFileSync(cssPath, 'utf-8'); } catch { return []; }
|
|
1813
1883
|
const files = [css];
|
|
@@ -1819,29 +1889,71 @@ function extractComponentClasses(cssPath, baseDir) {
|
|
|
1819
1889
|
try { files.push(readFileSync(resolved, 'utf-8')); } catch {}
|
|
1820
1890
|
}
|
|
1821
1891
|
}
|
|
1822
|
-
|
|
1892
|
+
return files;
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
// Class names the project writes in its own CSS, read from the SELECTORS only.
|
|
1896
|
+
//
|
|
1897
|
+
// This used to scan whole files with two regexes, which harvested identifiers
|
|
1898
|
+
// from places that are not selectors at all: the URLs in Tailwind's own preflight
|
|
1899
|
+
// comments (\`developer.mozilla.org\` → \`mozilla\`, \`org\`; \`bugs.chromium.org\`;
|
|
1900
|
+
// \`-webkit-\`) and the decimals in declaration values (\`padding: 0.5rem\` →
|
|
1901
|
+
// \`5rem\`). Everything here reaches \`validitySet\`, so every project accepted
|
|
1902
|
+
// \`com\`, \`org\`, \`css\` and \`5rem\` as valid Tailwind classes.
|
|
1903
|
+
//
|
|
1904
|
+
// A selector is the text between the last \`{\`, \`}\` or \`;\` and the next \`{\`, so
|
|
1905
|
+
// scanning only what precedes a \`{\` skips declaration bodies by construction —
|
|
1906
|
+
// a declaration ends in \`;\` or \`}\` and never reaches one. Comments and strings
|
|
1907
|
+
// are stepped over. Requiring a CSS identifier start (never a digit) is the
|
|
1908
|
+
// second line of defence, for a decimal inside an at-rule prelude
|
|
1909
|
+
// (\`@media (min-width: 40.5rem)\`).
|
|
1910
|
+
function extractComponentClasses(cssPath, baseDir) {
|
|
1911
|
+
const files = collectCssSources(cssPath, baseDir);
|
|
1912
|
+
const result = new Set();
|
|
1913
|
+
const classRe = /\\.(-?[a-zA-Z_][\\w-]*)/g;
|
|
1823
1914
|
for (const content of files) {
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1915
|
+
let prelude = '';
|
|
1916
|
+
for (let i = 0; i < content.length; i++) {
|
|
1917
|
+
const c = content[i];
|
|
1918
|
+
if (c === '/' && content[i + 1] === '*') {
|
|
1919
|
+
const end = content.indexOf('*/', i + 2);
|
|
1920
|
+
i = end === -1 ? content.length : end + 1;
|
|
1921
|
+
continue;
|
|
1922
|
+
}
|
|
1923
|
+
if (c === '"' || c === "'") {
|
|
1924
|
+
const quote = c;
|
|
1832
1925
|
i++;
|
|
1926
|
+
while (i < content.length && content[i] !== quote) {
|
|
1927
|
+
if (content[i] === '\\\\') i++;
|
|
1928
|
+
i++;
|
|
1929
|
+
}
|
|
1930
|
+
continue;
|
|
1931
|
+
}
|
|
1932
|
+
if (c === '{') {
|
|
1933
|
+
classRe.lastIndex = 0;
|
|
1934
|
+
let m;
|
|
1935
|
+
while ((m = classRe.exec(prelude)) !== null) result.add(m[1]);
|
|
1936
|
+
prelude = '';
|
|
1937
|
+
continue;
|
|
1833
1938
|
}
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1939
|
+
if (c === '}' || c === ';') {
|
|
1940
|
+
// A statement at-rule carries a selector even though it has no block:
|
|
1941
|
+
// \`@custom-variant sidebar-open (&:where(.sidebar-open *));\` makes that
|
|
1942
|
+
// class load-bearing exactly the way \`group\`/\`peer\` are, so it has to
|
|
1943
|
+
// stay valid. Plain declarations never start with \`@\`, so this readmits
|
|
1944
|
+
// the selector without readmitting \`padding: 0.5rem\`.
|
|
1945
|
+
if (c === ';' && prelude.trimStart().charCodeAt(0) === 64) {
|
|
1946
|
+
classRe.lastIndex = 0;
|
|
1947
|
+
let m;
|
|
1948
|
+
while ((m = classRe.exec(prelude)) !== null) result.add(m[1]);
|
|
1949
|
+
}
|
|
1950
|
+
prelude = '';
|
|
1951
|
+
continue;
|
|
1952
|
+
}
|
|
1953
|
+
prelude += c;
|
|
1838
1954
|
}
|
|
1839
|
-
// Scan all class selectors anywhere in the file (.class-name)
|
|
1840
|
-
const classSelRe = /\\.([a-zA-Z_][\\w-]*)/g;
|
|
1841
|
-
let cs;
|
|
1842
|
-
while ((cs = classSelRe.exec(content)) !== null) result.push(cs[1]);
|
|
1843
1955
|
}
|
|
1844
|
-
return [...
|
|
1956
|
+
return [...result];
|
|
1845
1957
|
}
|
|
1846
1958
|
|
|
1847
1959
|
${DECL_EXTRACTOR_SOURCE}
|
|
@@ -2246,11 +2358,13 @@ async function main() {
|
|
|
2246
2358
|
}
|
|
2247
2359
|
|
|
2248
2360
|
// Component classes from @layer components
|
|
2249
|
-
const
|
|
2361
|
+
const componentSet = new Set(extractComponentClasses(cssPath, base));
|
|
2250
2362
|
|
|
2251
2363
|
// Extract class names from attribute selectors [class~="..."] in CSS output.
|
|
2252
2364
|
// Plugins like @tailwindcss/typography use these for modifier classes (e.g. "not-prose")
|
|
2253
2365
|
// that don't generate their own CSS but are referenced in other classes' selectors.
|
|
2366
|
+
// Interned in a Set: typography references \`not-prose\` from ~230 selectors, and
|
|
2367
|
+
// pushing one entry per occurrence only inflated the cache artifact.
|
|
2254
2368
|
const attrClassRe = /\\[class~="([^"]+)"\\]/g;
|
|
2255
2369
|
for (let i = 0; i < cssResults.length; i++) {
|
|
2256
2370
|
if (cssResults[i]) {
|
|
@@ -2258,10 +2372,11 @@ async function main() {
|
|
|
2258
2372
|
attrClassRe.lastIndex = 0;
|
|
2259
2373
|
while ((acm = attrClassRe.exec(cssResults[i])) !== null) {
|
|
2260
2374
|
// Typography-style plugins emit \`[class~="tw:not-prose"]\` under a prefix.
|
|
2261
|
-
|
|
2375
|
+
componentSet.add(unpfx(acm[1]));
|
|
2262
2376
|
}
|
|
2263
2377
|
}
|
|
2264
2378
|
}
|
|
2379
|
+
const componentClasses = [...componentSet];
|
|
2265
2380
|
|
|
2266
2381
|
// Arbitrary equivalents: map arbitrary forms to named equivalents.
|
|
2267
2382
|
// Enumerate every dash split point so multi-segment utilities (e.g.
|
|
@@ -2305,20 +2420,21 @@ async function main() {
|
|
|
2305
2420
|
}
|
|
2306
2421
|
}
|
|
2307
2422
|
|
|
2308
|
-
//
|
|
2309
|
-
//
|
|
2310
|
-
//
|
|
2311
|
-
//
|
|
2312
|
-
//
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
const definedVars = [...new Set(css.match(/--[\\w-]+(?=\\s*:)/g) || [])];
|
|
2423
|
+
// Custom properties the project defines, across the entry AND its resolved
|
|
2424
|
+
// @imports — splitting the theme across files is the normal shadcn/ui layout,
|
|
2425
|
+
// and a definition we cannot see reads as "undefined", which is what makes
|
|
2426
|
+
// prefer-theme-tokens propose a token that means something else. One level
|
|
2427
|
+
// deep, same as the component-class scan it shares \`collectCssSources\` with.
|
|
2428
|
+
const definedVars = [...new Set(
|
|
2429
|
+
collectCssSources(cssPath, base).flatMap(src => src.match(/--[\\w-]+(?=\\s*:)/g) || [])
|
|
2430
|
+
)];
|
|
2317
2431
|
|
|
2318
2432
|
const themeRefs = {};
|
|
2433
|
+
const themeValues = new Map();
|
|
2319
2434
|
if (ds.theme && typeof ds.theme.entries === 'function') {
|
|
2320
2435
|
for (const [name, entry] of ds.theme.entries()) {
|
|
2321
2436
|
const value = entry && typeof entry.value === 'string' ? entry.value : '';
|
|
2437
|
+
themeValues.set(name, value);
|
|
2322
2438
|
if (!value.includes('var(')) continue;
|
|
2323
2439
|
const reads = scanVarReads(value);
|
|
2324
2440
|
const all = [...new Set([...reads[0], ...reads[1]])];
|
|
@@ -2326,7 +2442,77 @@ async function main() {
|
|
|
2326
2442
|
}
|
|
2327
2443
|
}
|
|
2328
2444
|
|
|
2329
|
-
|
|
2445
|
+
// ── What a literal value could have been written as (for prefer-scale-token) ──
|
|
2446
|
+
//
|
|
2447
|
+
// Two families, both derived. Neither is a name table: the emitted CSS says
|
|
2448
|
+
// which variable a class reads and the theme says what that variable is worth.
|
|
2449
|
+
|
|
2450
|
+
// A single declaration whose whole value is one \`var(--x)\` that the theme
|
|
2451
|
+
// resolves to a NUMBER. Numeric because that is the only thing the rule can
|
|
2452
|
+
// compare — a colour token could never match a literal the user typed — and
|
|
2453
|
+
// SINGLE because a class that declares more than the original is not an
|
|
2454
|
+
// equivalent: \`text-sm\` sets \`line-height\` too, so \`text-[14px]\` is not it.
|
|
2455
|
+
const NUMERIC_LITERAL = /^-?\\d*\\.?\\d+(rem|px|em|%|s|ms|deg)?$/;
|
|
2456
|
+
const tokenValues = {};
|
|
2457
|
+
for (const cls of validClasses) {
|
|
2458
|
+
if (cls.includes('[') || cls.includes('/')) continue;
|
|
2459
|
+
const css = declCss[cls];
|
|
2460
|
+
if (!css) continue;
|
|
2461
|
+
const open = css.indexOf('{');
|
|
2462
|
+
const close = css.indexOf('}');
|
|
2463
|
+
if (open < 0 || close < 0) continue;
|
|
2464
|
+
const body = css.slice(open + 1, close).replace(/\\s+/g, ' ').replace(/;\\s*$/, '').trim();
|
|
2465
|
+
const m = /^([a-z-]+):\\s*var\\((--[\\w-]+)\\)$/.exec(body);
|
|
2466
|
+
if (!m) continue;
|
|
2467
|
+
const value = themeValues.get(m[2]);
|
|
2468
|
+
if (!value || !NUMERIC_LITERAL.test(value)) continue;
|
|
2469
|
+
const dash = cls.lastIndexOf('-');
|
|
2470
|
+
if (dash <= 0) continue;
|
|
2471
|
+
const prefix = unpfx(cls).slice(0, unpfx(cls).lastIndexOf('-'));
|
|
2472
|
+
if (!prefix) continue;
|
|
2473
|
+
if (!tokenValues[prefix]) tokenValues[prefix] = [];
|
|
2474
|
+
tokenValues[prefix].push([value, unpfx(cls)]);
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
// The spacing scale: its unit, which prefixes read it, and the granularity
|
|
2478
|
+
// Tailwind's own enumerated steps use. Every step getClassList() lists is a
|
|
2479
|
+
// multiple of that granularity, so deriving it is how the rule stays inside
|
|
2480
|
+
// what Tailwind does instead of inventing a threshold.
|
|
2481
|
+
const scaleUnit = themeValues.get('--spacing') || '';
|
|
2482
|
+
const scalePrefixes = [];
|
|
2483
|
+
if (scaleUnit) {
|
|
2484
|
+
const probes = [...knownPrefixes];
|
|
2485
|
+
const probeResults = ds.candidatesToCss(probes.map(p => pfx(p + '-1')));
|
|
2486
|
+
for (let i = 0; i < probes.length; i++) {
|
|
2487
|
+
const css = probeResults[i];
|
|
2488
|
+
if (!css) continue;
|
|
2489
|
+
// \`size-1\` emits width AND height, both reading the unit — one match is enough.
|
|
2490
|
+
if (css.includes('var(--spacing)')) scalePrefixes.push(probes[i]);
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
let scaleStep = 0;
|
|
2494
|
+
if (scalePrefixes.length > 0) {
|
|
2495
|
+
const steps = new Set();
|
|
2496
|
+
const scaleSet = new Set(scalePrefixes);
|
|
2497
|
+
for (const cls of validClasses) {
|
|
2498
|
+
const dash = cls.lastIndexOf('-');
|
|
2499
|
+
if (dash <= 0) continue;
|
|
2500
|
+
if (!scaleSet.has(cls.slice(0, dash))) continue;
|
|
2501
|
+
const suffix = cls.slice(dash + 1);
|
|
2502
|
+
if (!/^\\d*\\.?\\d+$/.test(suffix)) continue;
|
|
2503
|
+
steps.add(parseFloat(suffix));
|
|
2504
|
+
}
|
|
2505
|
+
const sorted = [...steps].sort((a, b) => a - b);
|
|
2506
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
2507
|
+
const gap = Math.round((sorted[i] - sorted[i - 1]) * 1000) / 1000;
|
|
2508
|
+
if (gap > 0 && (scaleStep === 0 || gap < scaleStep)) scaleStep = gap;
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
const scale = scaleUnit && scalePrefixes.length > 0
|
|
2512
|
+
? { unit: scaleUnit, step: scaleStep || 1, prefixes: scalePrefixes.sort() }
|
|
2513
|
+
: undefined;
|
|
2514
|
+
|
|
2515
|
+
const json = JSON.stringify({ validClasses, canonical, deprecated, order, cssDeclarations, variantOrder, variantFacts, componentClasses, arbitraryEquivalents, themeRefs, definedVars, tokenValues, scale, prefix });
|
|
2330
2516
|
// Atomic write: write to a unique temp path then rename, so a peer isolate
|
|
2331
2517
|
// busy-waiting on the cache file never observes a half-written JSON.
|
|
2332
2518
|
writeFileSync(WD_TMP_PATH, json);
|
|
@@ -3329,45 +3515,49 @@ function answersFor(cache) {
|
|
|
3329
3515
|
* Ask the design system about every class there is no answer for yet, intern the
|
|
3330
3516
|
* declarations it returns, and record which classes produce no CSS at all.
|
|
3331
3517
|
*
|
|
3332
|
-
*
|
|
3333
|
-
*
|
|
3334
|
-
*
|
|
3335
|
-
*
|
|
3518
|
+
* **Throws `SortServiceError` when the service is unavailable**, like the sort and
|
|
3519
|
+
* canonicalize services do. It used to swallow the failure and report "no
|
|
3520
|
+
* information", which was defensible when `no-conflicting-classes` was the only
|
|
3521
|
+
* caller — no declarations means no comparison, so the rule just went quiet. It
|
|
3522
|
+
* stopped being defensible once `no-unknown-classes` started asking about
|
|
3523
|
+
* VALIDITY: there, silence sends the rule back to the tolerant heuristic, so a
|
|
3524
|
+
* dead worker quietly reinstates the very false negatives the service exists to
|
|
3525
|
+
* remove, with nothing anywhere to say so.
|
|
3526
|
+
*
|
|
3527
|
+
* The caller decides what to do, and the two postures already exist in the
|
|
3528
|
+
* plugin: a DS-dependent rule surfaces it through `safeGetDS` as
|
|
3529
|
+
* `designSystemUnavailable` (what `enforce-canonical` does with the canonicalize
|
|
3530
|
+
* worker), a DS-OPTIONAL rule catches it and degrades.
|
|
3336
3531
|
*/
|
|
3337
3532
|
function ask(cssPath, cache, classes) {
|
|
3338
3533
|
const known = answersFor(cache);
|
|
3339
3534
|
const pending = [...new Set(classes.filter((cls) => !known.has(cls)))];
|
|
3340
|
-
if (pending.length === 0) return
|
|
3341
|
-
|
|
3342
|
-
try {
|
|
3343
|
-
response = declWorker.callSync(cssPath, { classes: pending });
|
|
3344
|
-
} catch (error) {
|
|
3345
|
-
if (error instanceof SortServiceError) return false;
|
|
3346
|
-
throw error;
|
|
3347
|
-
}
|
|
3535
|
+
if (pending.length === 0) return;
|
|
3536
|
+
const response = declWorker.callSync(cssPath, { classes: pending });
|
|
3348
3537
|
const invalid = new Set(response.invalid);
|
|
3349
3538
|
for (const cls of pending) known.set(cls, !invalid.has(cls));
|
|
3350
3539
|
for (const [cls, raws] of Object.entries(response.decls)) cache.internDeclarations(cls, raws, response.values);
|
|
3351
|
-
return true;
|
|
3352
3540
|
}
|
|
3353
3541
|
/**
|
|
3354
3542
|
* Resolve and intern declarations for `classes` that the precompute doesn't know.
|
|
3355
3543
|
* They land in the cache, so the caller reads them back through
|
|
3356
3544
|
* `cache.getCssDeclarations` like any other class.
|
|
3545
|
+
*
|
|
3546
|
+
* Throws `SortServiceError` if the service is unavailable — see `ask`.
|
|
3357
3547
|
*/
|
|
3358
3548
|
function resolveDeclarationsSync(cssPath, cache, classes) {
|
|
3359
3549
|
ask(cssPath, cache, classes);
|
|
3360
3550
|
}
|
|
3361
3551
|
/**
|
|
3362
|
-
* Which of these classes produce CSS.
|
|
3363
|
-
* the caller can fall back instead of reading silence as an answer.
|
|
3552
|
+
* Which of these classes produce CSS.
|
|
3364
3553
|
*
|
|
3365
3554
|
* This is what makes validity exact for the classes the precompute cannot
|
|
3366
3555
|
* enumerate: `w-45` and `bg-red-5000` are shaped identically, and only Tailwind
|
|
3367
|
-
* knows that it compiles the first and not the second.
|
|
3556
|
+
* knows that it compiles the first and not the second. Throws
|
|
3557
|
+
* `SortServiceError` if the service is unavailable — see `ask`.
|
|
3368
3558
|
*/
|
|
3369
3559
|
function validateClassesSync(cssPath, cache, classes) {
|
|
3370
|
-
|
|
3560
|
+
ask(cssPath, cache, classes);
|
|
3371
3561
|
return answersFor(cache);
|
|
3372
3562
|
}
|
|
3373
3563
|
//#endregion
|
|
@@ -3447,8 +3637,7 @@ const noUnknownClasses = defineRule({
|
|
|
3447
3637
|
const memo = chainAnswers.get(key);
|
|
3448
3638
|
if (memo !== void 0) return memo;
|
|
3449
3639
|
const probe = `${chain}${PROBE_UTILITY}`;
|
|
3450
|
-
const
|
|
3451
|
-
const verdict = answers ? answers.get(probe) ?? null : null;
|
|
3640
|
+
const verdict = validateClassesSync(entryPoint, cache, [probe]).get(probe) ?? null;
|
|
3452
3641
|
chainAnswers.set(key, verdict);
|
|
3453
3642
|
return verdict;
|
|
3454
3643
|
}
|
|
@@ -3476,6 +3665,14 @@ const noUnknownClasses = defineRule({
|
|
|
3476
3665
|
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
3477
3666
|
if (!ds) return;
|
|
3478
3667
|
const { cache, entryPoint } = ds;
|
|
3668
|
+
try {
|
|
3669
|
+
checkLocations(locations, cache, entryPoint);
|
|
3670
|
+
} catch (error) {
|
|
3671
|
+
if (reportFatalDsError(context, error, locations[0].node)) return;
|
|
3672
|
+
throw error;
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
function checkLocations(locations, cache, entryPoint) {
|
|
3479
3676
|
for (const loc of locations) {
|
|
3480
3677
|
const split = splitClassesWithSeparators(loc.value);
|
|
3481
3678
|
const classes = split.classes;
|
|
@@ -3484,6 +3681,7 @@ const noUnknownClasses = defineRule({
|
|
|
3484
3681
|
if (shouldIgnore(cls)) continue;
|
|
3485
3682
|
const bare = strippedUtility(cls).bare;
|
|
3486
3683
|
if (cache.isKnownClass(bare)) continue;
|
|
3684
|
+
if (cache.isMarkerClass(bare)) continue;
|
|
3487
3685
|
if (!toVerify) toVerify = [];
|
|
3488
3686
|
toVerify.push(bare);
|
|
3489
3687
|
}
|
|
@@ -3503,6 +3701,7 @@ const noUnknownClasses = defineRule({
|
|
|
3503
3701
|
}, split);
|
|
3504
3702
|
continue;
|
|
3505
3703
|
}
|
|
3704
|
+
if (cache.isMarkerClass(bare) && (!cache.prefix || variant.startsWith(`${cache.prefix}:`))) continue;
|
|
3506
3705
|
const compiles = cache.isKnownClass(bare) ? true : verified?.get(bare);
|
|
3507
3706
|
if (validity === "unknown" || compiles === false) {
|
|
3508
3707
|
const suggestionBare = findBestSuggestion(bare, cache.validClasses);
|
|
@@ -3946,7 +4145,12 @@ const noConflictingClasses = defineRule({
|
|
|
3946
4145
|
for (const [, variantClasses] of byVariant) {
|
|
3947
4146
|
if (variantClasses.length < 2) continue;
|
|
3948
4147
|
const unknown = variantClasses.map((cls) => splitImportant(extractUtility(cls)).bare).filter((bare) => cache.getCssDeclarations(bare).length === 0 && isUserValued(bare));
|
|
3949
|
-
if (unknown.length > 0)
|
|
4148
|
+
if (unknown.length > 0) {
|
|
4149
|
+
if (!safeGetDS(() => {
|
|
4150
|
+
resolveDeclarationsSync(ds.entryPoint, cache, unknown);
|
|
4151
|
+
return true;
|
|
4152
|
+
}, context, loc.node)) return;
|
|
4153
|
+
}
|
|
3950
4154
|
const facts = variantClasses.map((cls) => {
|
|
3951
4155
|
const utility = extractUtility(cls);
|
|
3952
4156
|
const bare = splitImportant(utility).bare;
|
|
@@ -5686,7 +5890,12 @@ const noDarkWithoutLight = defineRule({
|
|
|
5686
5890
|
if (!unresolved) unresolved = [];
|
|
5687
5891
|
unresolved.push(bare);
|
|
5688
5892
|
}
|
|
5689
|
-
if (unresolved)
|
|
5893
|
+
if (unresolved) try {
|
|
5894
|
+
resolveDeclarationsSync(ds.entryPoint, cache, unresolved);
|
|
5895
|
+
} catch (error) {
|
|
5896
|
+
if (!isFatalError(error)) throw error;
|
|
5897
|
+
debugLog(`declaration service unavailable, grouping by prefix only: ${String(error)}`);
|
|
5898
|
+
}
|
|
5690
5899
|
}
|
|
5691
5900
|
const basePrefixes = /* @__PURE__ */ new Set();
|
|
5692
5901
|
const baseProperties = /* @__PURE__ */ new Set();
|
|
@@ -6320,6 +6529,106 @@ function detectRawVariable(utility) {
|
|
|
6320
6529
|
};
|
|
6321
6530
|
return null;
|
|
6322
6531
|
}
|
|
6532
|
+
const preferThemeTokens = defineRule({
|
|
6533
|
+
meta: {
|
|
6534
|
+
type: "suggestion",
|
|
6535
|
+
docs: { description: "Prefer named theme-token utilities over raw CSS variable references when a matching utility exists" },
|
|
6536
|
+
fixable: "code",
|
|
6537
|
+
schema: [{
|
|
6538
|
+
type: "object",
|
|
6539
|
+
properties: { entryPoint: { type: "string" } },
|
|
6540
|
+
additionalProperties: false
|
|
6541
|
+
}],
|
|
6542
|
+
hasSuggestions: true,
|
|
6543
|
+
defaultOptions: [{}],
|
|
6544
|
+
messages: {
|
|
6545
|
+
preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
|
|
6546
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
6547
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
6548
|
+
}
|
|
6549
|
+
},
|
|
6550
|
+
createOnce(context) {
|
|
6551
|
+
const getDS = createLazyLoader(context);
|
|
6552
|
+
function check(locations) {
|
|
6553
|
+
if (locations.length === 0) return;
|
|
6554
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
6555
|
+
if (!ds) return;
|
|
6556
|
+
const { cache } = ds;
|
|
6557
|
+
for (const loc of locations) {
|
|
6558
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
6559
|
+
const offending = split.classes.flatMap((cls) => {
|
|
6560
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
6561
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
6562
|
+
const match = detectRawVariable(bareUtility);
|
|
6563
|
+
if (!match) return [];
|
|
6564
|
+
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
6565
|
+
if (!cache.isValid(candidate)) return [];
|
|
6566
|
+
const target = `--${match.varName}`;
|
|
6567
|
+
const baseCandidate = `${match.prefix}-${match.varName}`;
|
|
6568
|
+
if (!cache.getCssDeclarations(baseCandidate).some((decl) => [...decl.readsVars, ...decl.readsFallbackVars].some((read) => cache.themeVarResolvesTo(read, target))) && cache.definesVar(target)) return [];
|
|
6569
|
+
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
6570
|
+
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|
|
6571
|
+
return [{
|
|
6572
|
+
cls,
|
|
6573
|
+
replacement: variant + reattachImportant(candidate, position)
|
|
6574
|
+
}];
|
|
6575
|
+
});
|
|
6576
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "preferNamed" });
|
|
6577
|
+
}
|
|
6578
|
+
}
|
|
6579
|
+
return createExtractorVisitors(context, check);
|
|
6580
|
+
}
|
|
6581
|
+
});
|
|
6582
|
+
//#endregion
|
|
6583
|
+
//#region src/rules/prefer-scale-token.ts
|
|
6584
|
+
/** A length or plain number. Anything else can't be compared arithmetically. */
|
|
6585
|
+
const LENGTH_RE = /^(-?\d*\.?\d+)(rem|px|em)?$/;
|
|
6586
|
+
/**
|
|
6587
|
+
* Parse a CSS length. `em` is treated as `rem`, which is what it is at the root
|
|
6588
|
+
* and the only interpretation available without a layout.
|
|
6589
|
+
*/
|
|
6590
|
+
function measure(value, rootFontSize) {
|
|
6591
|
+
const match = LENGTH_RE.exec(value.trim());
|
|
6592
|
+
if (!match) return null;
|
|
6593
|
+
const n = Number.parseFloat(match[1]);
|
|
6594
|
+
if (!Number.isFinite(n)) return null;
|
|
6595
|
+
switch (match[2]) {
|
|
6596
|
+
case "rem":
|
|
6597
|
+
case "em": return {
|
|
6598
|
+
px: n * rootFontSize,
|
|
6599
|
+
unitless: false
|
|
6600
|
+
};
|
|
6601
|
+
case "px": return {
|
|
6602
|
+
px: n,
|
|
6603
|
+
unitless: false
|
|
6604
|
+
};
|
|
6605
|
+
case void 0: return {
|
|
6606
|
+
px: n,
|
|
6607
|
+
unitless: true
|
|
6608
|
+
};
|
|
6609
|
+
default: return null;
|
|
6610
|
+
}
|
|
6611
|
+
}
|
|
6612
|
+
/**
|
|
6613
|
+
* Same value, allowing for float noise (`0.875rem` × 16 = 13.999999999999998).
|
|
6614
|
+
* A unitless number and a length are never the same value, whatever the digits.
|
|
6615
|
+
*/
|
|
6616
|
+
function sameMeasure(a, b) {
|
|
6617
|
+
return a.unitless === b.unitless && Math.abs(a.px - b.px) < 1e-4;
|
|
6618
|
+
}
|
|
6619
|
+
/**
|
|
6620
|
+
* Is `n` a whole number of `step`s? `10 / 0.5` is exact in binary, `0.3 / 0.1`
|
|
6621
|
+
* is not, so the remainder is compared with a tolerance rather than to zero.
|
|
6622
|
+
*/
|
|
6623
|
+
function isOnStep(n, step) {
|
|
6624
|
+
if (step <= 0) return false;
|
|
6625
|
+
const steps = n / step;
|
|
6626
|
+
return Math.abs(steps - Math.round(steps)) < 1e-4;
|
|
6627
|
+
}
|
|
6628
|
+
/** Trailing zeros make `p-2.50`, which is not what Tailwind calls the class. */
|
|
6629
|
+
function formatStep(n) {
|
|
6630
|
+
return String(Number.parseFloat(n.toFixed(4)));
|
|
6631
|
+
}
|
|
6323
6632
|
//#endregion
|
|
6324
6633
|
//#region src/index.ts
|
|
6325
6634
|
const plugin = definePlugin({
|
|
@@ -6347,26 +6656,77 @@ const plugin = definePlugin({
|
|
|
6347
6656
|
"consistent-variant-order": consistentVariantOrder,
|
|
6348
6657
|
"enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
|
|
6349
6658
|
"no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue,
|
|
6350
|
-
"prefer-theme-tokens":
|
|
6659
|
+
"prefer-theme-tokens": preferThemeTokens,
|
|
6660
|
+
"prefer-scale-token": defineRule({
|
|
6351
6661
|
meta: {
|
|
6352
6662
|
type: "suggestion",
|
|
6353
|
-
docs: { description: "Prefer
|
|
6354
|
-
|
|
6663
|
+
docs: { description: "Prefer the scale step or theme token a hardcoded value is numerically equal to" },
|
|
6664
|
+
hasSuggestions: true,
|
|
6355
6665
|
schema: [{
|
|
6356
6666
|
type: "object",
|
|
6357
|
-
properties: {
|
|
6667
|
+
properties: {
|
|
6668
|
+
entryPoint: { type: "string" },
|
|
6669
|
+
step: { type: "number" },
|
|
6670
|
+
allow: {
|
|
6671
|
+
type: "array",
|
|
6672
|
+
items: { type: "string" }
|
|
6673
|
+
}
|
|
6674
|
+
},
|
|
6358
6675
|
additionalProperties: false
|
|
6359
6676
|
}],
|
|
6360
|
-
hasSuggestions: true,
|
|
6361
6677
|
defaultOptions: [{}],
|
|
6362
6678
|
messages: {
|
|
6363
|
-
|
|
6679
|
+
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.",
|
|
6364
6680
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
6365
6681
|
...DS_UNAVAILABLE_MESSAGE
|
|
6366
6682
|
}
|
|
6367
6683
|
},
|
|
6368
6684
|
createOnce(context) {
|
|
6369
6685
|
const getDS = createLazyLoader(context);
|
|
6686
|
+
const getOptions = createLazyOptions(context, (o) => ({
|
|
6687
|
+
step: typeof o?.step === "number" ? o.step : null,
|
|
6688
|
+
allow: o?.allow ?? []
|
|
6689
|
+
}));
|
|
6690
|
+
let _rem = null;
|
|
6691
|
+
function rootFontSize() {
|
|
6692
|
+
if (_rem === null) _rem = rootFontSizeFromSettings(safeSettings(context));
|
|
6693
|
+
return _rem;
|
|
6694
|
+
}
|
|
6695
|
+
/**
|
|
6696
|
+
* The class this literal could have been written as, or null.
|
|
6697
|
+
*
|
|
6698
|
+
* Two families, in order of preference: a named theme token whose value
|
|
6699
|
+
* matches (`rounded-[0.5rem]` → `rounded-lg`), then a step of the spacing
|
|
6700
|
+
* scale (`p-[10px]` → `p-2.5`). Both come from data the precompute derived
|
|
6701
|
+
* from the design system, so neither is a name table.
|
|
6702
|
+
*/
|
|
6703
|
+
function equivalentOf(cache, bare) {
|
|
6704
|
+
const { step: stepOverride, allow } = getOptions();
|
|
6705
|
+
if (allow.some((prefix) => bare.startsWith(prefix))) return null;
|
|
6706
|
+
if (cache.getNamedEquivalent(bare)) return null;
|
|
6707
|
+
const value = getArbitraryValue(bare);
|
|
6708
|
+
if (value === null) return null;
|
|
6709
|
+
const written = measure(value, rootFontSize());
|
|
6710
|
+
if (written === null) return null;
|
|
6711
|
+
const open = bare.indexOf("[");
|
|
6712
|
+
if (open <= 0 || bare[open - 1] !== "-") return null;
|
|
6713
|
+
const prefix = bare.slice(0, open - 1);
|
|
6714
|
+
if (!prefix) return null;
|
|
6715
|
+
for (const [literal, className] of cache.tokenValuesFor(prefix)) {
|
|
6716
|
+
const token = measure(literal, rootFontSize());
|
|
6717
|
+
if (token !== null && sameMeasure(written, token)) return className;
|
|
6718
|
+
}
|
|
6719
|
+
if (!cache.readsScale(prefix)) return null;
|
|
6720
|
+
const scale = cache.scale;
|
|
6721
|
+
if (!scale) return null;
|
|
6722
|
+
const unit = measure(scale.unit, rootFontSize());
|
|
6723
|
+
if (unit === null || unit.px <= 0 || unit.unitless !== written.unitless) return null;
|
|
6724
|
+
const steps = written.px / unit.px;
|
|
6725
|
+
if (steps < 0) return null;
|
|
6726
|
+
if (!isOnStep(steps, stepOverride ?? scale.step)) return null;
|
|
6727
|
+
const candidate = `${prefix}-${formatStep(steps)}`;
|
|
6728
|
+
return cache.isValid(candidate) ? candidate : null;
|
|
6729
|
+
}
|
|
6370
6730
|
function check(locations) {
|
|
6371
6731
|
if (locations.length === 0) return;
|
|
6372
6732
|
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
@@ -6374,24 +6734,33 @@ const plugin = definePlugin({
|
|
|
6374
6734
|
const { cache } = ds;
|
|
6375
6735
|
for (const loc of locations) {
|
|
6376
6736
|
const split = splitClassesWithSeparators(loc.value);
|
|
6377
|
-
const
|
|
6737
|
+
for (const cls of split.classes) {
|
|
6738
|
+
if (!utilityHasDynamicValue(cls)) continue;
|
|
6378
6739
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
6379
|
-
const { bare
|
|
6380
|
-
const
|
|
6381
|
-
if (!
|
|
6382
|
-
const
|
|
6383
|
-
|
|
6384
|
-
|
|
6385
|
-
|
|
6386
|
-
|
|
6387
|
-
|
|
6388
|
-
|
|
6389
|
-
|
|
6390
|
-
|
|
6391
|
-
|
|
6392
|
-
|
|
6393
|
-
|
|
6394
|
-
|
|
6740
|
+
const { bare, position } = splitImportant(utility);
|
|
6741
|
+
const equivalent = equivalentOf(cache, bare);
|
|
6742
|
+
if (!equivalent) continue;
|
|
6743
|
+
const replacement = variant + reattachImportant(equivalent, position);
|
|
6744
|
+
const fixedValue = rebuildClassString(split, split.classes.map((c) => c === cls ? replacement : c));
|
|
6745
|
+
context.report({
|
|
6746
|
+
node: loc.node,
|
|
6747
|
+
messageId: "preferToken",
|
|
6748
|
+
data: {
|
|
6749
|
+
className: cls,
|
|
6750
|
+
replacement
|
|
6751
|
+
},
|
|
6752
|
+
suggest: [{
|
|
6753
|
+
messageId: "suggestReplace",
|
|
6754
|
+
data: {
|
|
6755
|
+
className: cls,
|
|
6756
|
+
replacement
|
|
6757
|
+
},
|
|
6758
|
+
fix(fixer) {
|
|
6759
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
6760
|
+
}
|
|
6761
|
+
}]
|
|
6762
|
+
});
|
|
6763
|
+
}
|
|
6395
6764
|
}
|
|
6396
6765
|
}
|
|
6397
6766
|
return createExtractorVisitors(context, check);
|