oxlint-tailwindcss 0.8.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -33
- package/dist/index.cjs +1384 -1354
- package/dist/index.mjs +1368 -1338
- package/dist/index.mjs.map +1 -1
- package/package.json +20 -19
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
3
|
-
import {
|
|
3
|
+
import { Worker, threadId } from "node:worker_threads";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
|
-
import { existsSync, mkdirSync, readFileSync, statSync,
|
|
5
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, statSync, unlinkSync } from "node:fs";
|
|
6
6
|
import { dirname, join, relative, resolve } from "node:path";
|
|
7
7
|
import { tmpdir } from "node:os";
|
|
8
|
-
import { Worker } from "node:worker_threads";
|
|
9
8
|
//#region \0rolldown/runtime.js
|
|
10
9
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
11
10
|
//#endregion
|
|
@@ -44,8 +43,7 @@ const DEFAULT_EXTRACTOR_CONFIG = {
|
|
|
44
43
|
/^styles?$/
|
|
45
44
|
]
|
|
46
45
|
};
|
|
47
|
-
|
|
48
|
-
let _settingsResolved = false;
|
|
46
|
+
const configCache = /* @__PURE__ */ new WeakMap();
|
|
49
47
|
function mergeUnique(defaults, extras, exclusions) {
|
|
50
48
|
let base = defaults;
|
|
51
49
|
if (exclusions && exclusions.length > 0) {
|
|
@@ -58,34 +56,39 @@ function mergeUnique(defaults, extras, exclusions) {
|
|
|
58
56
|
return [...set];
|
|
59
57
|
}
|
|
60
58
|
/**
|
|
61
|
-
* Returns the extractor config, merging defaults with
|
|
62
|
-
* `settings.tailwindcss`.
|
|
63
|
-
*
|
|
59
|
+
* Returns the extractor config for this rule context, merging defaults with
|
|
60
|
+
* user settings from `settings.tailwindcss`. The result is cached per
|
|
61
|
+
* context (WeakMap-keyed) so settings are read once per rule lifetime.
|
|
62
|
+
*
|
|
63
|
+
* In `createOnce` the settings getter throws — we fall back to the defaults
|
|
64
|
+
* without caching so the next visitor call (where settings ARE available)
|
|
65
|
+
* still resolves the user's overrides.
|
|
64
66
|
*/
|
|
65
67
|
function getExtractorConfig(context) {
|
|
66
|
-
|
|
68
|
+
const cached = configCache.get(context);
|
|
69
|
+
if (cached) return cached;
|
|
67
70
|
let tw;
|
|
68
71
|
try {
|
|
69
72
|
const raw = context.settings?.tailwindcss;
|
|
70
73
|
if (raw && typeof raw === "object") tw = raw;
|
|
71
|
-
_settingsResolved = true;
|
|
72
74
|
} catch {
|
|
73
75
|
return DEFAULT_EXTRACTOR_CONFIG;
|
|
74
76
|
}
|
|
75
77
|
if (!tw) {
|
|
76
|
-
|
|
77
|
-
return
|
|
78
|
+
configCache.set(context, DEFAULT_EXTRACTOR_CONFIG);
|
|
79
|
+
return DEFAULT_EXTRACTOR_CONFIG;
|
|
78
80
|
}
|
|
79
81
|
const exclude = tw.exclude;
|
|
80
82
|
const excludedPatternSources = new Set(exclude?.variablePatterns ?? []);
|
|
81
83
|
const filteredPatterns = excludedPatternSources.size > 0 ? DEFAULT_EXTRACTOR_CONFIG.variablePatterns.filter((re) => !excludedPatternSources.has(re.source)) : DEFAULT_EXTRACTOR_CONFIG.variablePatterns;
|
|
82
|
-
|
|
84
|
+
const resolved = {
|
|
83
85
|
attributes: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.attributes, tw.attributes, exclude?.attributes),
|
|
84
86
|
callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, tw.callees, exclude?.callees),
|
|
85
87
|
tags: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.tags, tw.tags, exclude?.tags),
|
|
86
88
|
variablePatterns: [...filteredPatterns, ...(tw.variablePatterns ?? []).map((p) => new RegExp(p))]
|
|
87
89
|
};
|
|
88
|
-
|
|
90
|
+
configCache.set(context, resolved);
|
|
91
|
+
return resolved;
|
|
89
92
|
}
|
|
90
93
|
/**
|
|
91
94
|
* Creates the 4 standard AST visitor callbacks that all rules use.
|
|
@@ -138,7 +141,7 @@ function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
138
141
|
if (calleeName === "tv") return extractFromTvCall(node);
|
|
139
142
|
if (calleeName === "classed") return extractFromClassedCall(node);
|
|
140
143
|
const results = [];
|
|
141
|
-
for (const arg of node.arguments)
|
|
144
|
+
for (const arg of node.arguments) extractFromExpression(arg, results);
|
|
142
145
|
return results;
|
|
143
146
|
}
|
|
144
147
|
/**
|
|
@@ -156,7 +159,7 @@ function extractFromTaggedTemplate(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
156
159
|
*/
|
|
157
160
|
function extractObjectValues(node) {
|
|
158
161
|
const results = [];
|
|
159
|
-
for (const prop of node.properties) if (prop.type === "Property")
|
|
162
|
+
for (const prop of node.properties) if (prop.type === "Property") extractFromExpression(prop.value, results);
|
|
160
163
|
return results;
|
|
161
164
|
}
|
|
162
165
|
/**
|
|
@@ -183,7 +186,7 @@ function extractFromClassedCall(node) {
|
|
|
183
186
|
}
|
|
184
187
|
skippedFirst = true;
|
|
185
188
|
if (arg.type === "ObjectExpression") results.push(...extractFromCvaConfig(arg));
|
|
186
|
-
else
|
|
189
|
+
else extractFromExpression(arg, results);
|
|
187
190
|
}
|
|
188
191
|
return results;
|
|
189
192
|
}
|
|
@@ -194,7 +197,7 @@ function extractFromClassedCall(node) {
|
|
|
194
197
|
function extractFromCvaCall(node) {
|
|
195
198
|
const results = [];
|
|
196
199
|
for (const arg of node.arguments) if (arg.type === "ObjectExpression") results.push(...extractFromCvaConfig(arg));
|
|
197
|
-
else
|
|
200
|
+
else extractFromExpression(arg, results);
|
|
198
201
|
return results;
|
|
199
202
|
}
|
|
200
203
|
/**
|
|
@@ -209,10 +212,10 @@ function extractFromCvaConfig(node) {
|
|
|
209
212
|
const name = getPropertyName(prop.key);
|
|
210
213
|
if (name === "variants" && prop.value.type === "ObjectExpression") for (const category of prop.value.properties) {
|
|
211
214
|
if (category.type !== "Property" || category.value.type !== "ObjectExpression") continue;
|
|
212
|
-
for (const variant of category.value.properties) if (variant.type === "Property")
|
|
215
|
+
for (const variant of category.value.properties) if (variant.type === "Property") extractFromExpression(variant.value, results);
|
|
213
216
|
}
|
|
214
217
|
else if (name === "compoundVariants" && prop.value.type === "ArrayExpression") results.push(...extractClassFromCompoundEntries(prop.value));
|
|
215
|
-
else if (name === "defaultVariants") {} else
|
|
218
|
+
else if (name === "defaultVariants") {} else extractFromExpression(prop.value, results);
|
|
216
219
|
}
|
|
217
220
|
return results;
|
|
218
221
|
}
|
|
@@ -223,7 +226,7 @@ function extractFromCvaConfig(node) {
|
|
|
223
226
|
function extractFromTvCall(node) {
|
|
224
227
|
const results = [];
|
|
225
228
|
for (const arg of node.arguments) if (arg.type === "ObjectExpression") results.push(...extractFromTvConfig(arg));
|
|
226
|
-
else
|
|
229
|
+
else extractFromExpression(arg, results);
|
|
227
230
|
return results;
|
|
228
231
|
}
|
|
229
232
|
/**
|
|
@@ -236,20 +239,20 @@ function extractFromTvConfig(node) {
|
|
|
236
239
|
for (const prop of node.properties) {
|
|
237
240
|
if (prop.type !== "Property") continue;
|
|
238
241
|
const name = getPropertyName(prop.key);
|
|
239
|
-
if (name === "base")
|
|
242
|
+
if (name === "base") extractFromExpression(prop.value, results);
|
|
240
243
|
else if (name === "slots" && prop.value.type === "ObjectExpression") {
|
|
241
|
-
for (const slot of prop.value.properties) if (slot.type === "Property")
|
|
244
|
+
for (const slot of prop.value.properties) if (slot.type === "Property") extractFromExpression(slot.value, results);
|
|
242
245
|
} else if (name === "variants" && prop.value.type === "ObjectExpression") for (const category of prop.value.properties) {
|
|
243
246
|
if (category.type !== "Property" || category.value.type !== "ObjectExpression") continue;
|
|
244
247
|
for (const variant of category.value.properties) {
|
|
245
248
|
if (variant.type !== "Property") continue;
|
|
246
249
|
if (variant.value.type === "ObjectExpression") {
|
|
247
|
-
for (const slotProp of variant.value.properties) if (slotProp.type === "Property")
|
|
248
|
-
} else
|
|
250
|
+
for (const slotProp of variant.value.properties) if (slotProp.type === "Property") extractFromExpression(slotProp.value, results);
|
|
251
|
+
} else extractFromExpression(variant.value, results);
|
|
249
252
|
}
|
|
250
253
|
}
|
|
251
254
|
else if ((name === "compoundVariants" || name === "compoundSlots") && prop.value.type === "ArrayExpression") results.push(...extractClassFromCompoundEntries(prop.value));
|
|
252
|
-
else if (name === "defaultVariants") {} else
|
|
255
|
+
else if (name === "defaultVariants") {} else extractFromExpression(prop.value, results);
|
|
253
256
|
}
|
|
254
257
|
return results;
|
|
255
258
|
}
|
|
@@ -264,7 +267,7 @@ function extractClassFromCompoundEntries(node) {
|
|
|
264
267
|
for (const prop of element.properties) {
|
|
265
268
|
if (prop.type !== "Property") continue;
|
|
266
269
|
const name = getPropertyName(prop.key);
|
|
267
|
-
if (name === "class" || name === "className")
|
|
270
|
+
if (name === "class" || name === "className") extractFromExpression(prop.value, results);
|
|
268
271
|
}
|
|
269
272
|
}
|
|
270
273
|
return results;
|
|
@@ -286,34 +289,51 @@ function extractFromVariableDeclarator(node, config = DEFAULT_EXTRACTOR_CONFIG)
|
|
|
286
289
|
if (!node.init) return [];
|
|
287
290
|
return extractFromExpression(node.init);
|
|
288
291
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
292
|
+
/**
|
|
293
|
+
* Walk an expression and append every class-bearing location to `out`.
|
|
294
|
+
*
|
|
295
|
+
* Internal helpers pass their accumulator down to avoid the
|
|
296
|
+
* `extractFromExpression(x, results)` pattern, which allocates an
|
|
297
|
+
* intermediate array per recursive call. Public callers may omit `out` to
|
|
298
|
+
* receive a fresh array.
|
|
299
|
+
*/
|
|
300
|
+
function extractFromExpression(node, out = []) {
|
|
301
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
302
|
+
out.push({
|
|
303
|
+
value: node.value,
|
|
304
|
+
node,
|
|
305
|
+
range: [node.range[0] + 1, node.range[1] - 1]
|
|
306
|
+
});
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
309
|
+
if (node.type === "TemplateLiteral") return appendFromTemplateLiteral(node, out);
|
|
310
|
+
if (node.type === "ConditionalExpression") {
|
|
311
|
+
extractFromExpression(node.consequent, out);
|
|
312
|
+
extractFromExpression(node.alternate, out);
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
if (node.type === "LogicalExpression") return extractFromExpression(node.right, out);
|
|
316
|
+
if (node.type === "ArrayExpression") {
|
|
317
|
+
for (const element of node.elements) if (element && element.type !== "SpreadElement") extractFromExpression(element, out);
|
|
318
|
+
return out;
|
|
319
|
+
}
|
|
298
320
|
if (node.type === "ObjectExpression") {
|
|
299
|
-
const
|
|
300
|
-
for (const prop of node.properties) if (prop.type === "Property" && prop.key.type === "Literal" && typeof prop.key.value === "string") results.push({
|
|
321
|
+
for (const prop of node.properties) if (prop.type === "Property" && prop.key.type === "Literal" && typeof prop.key.value === "string") out.push({
|
|
301
322
|
value: prop.key.value,
|
|
302
323
|
node: prop.key,
|
|
303
324
|
range: [prop.key.range[0] + 1, prop.key.range[1] - 1]
|
|
304
325
|
});
|
|
305
|
-
return
|
|
326
|
+
return out;
|
|
306
327
|
}
|
|
307
|
-
return
|
|
328
|
+
return out;
|
|
308
329
|
}
|
|
309
|
-
function
|
|
310
|
-
const results = [];
|
|
330
|
+
function appendFromTemplateLiteral(node, out) {
|
|
311
331
|
for (let i = 0; i < node.quasis.length; i++) {
|
|
312
332
|
const quasi = node.quasis[i];
|
|
313
333
|
const value = quasi.value.raw;
|
|
314
334
|
if (value.trim().length > 0) {
|
|
315
335
|
const start = quasi.range[0] + 1;
|
|
316
|
-
|
|
336
|
+
out.push({
|
|
317
337
|
value,
|
|
318
338
|
node: quasi,
|
|
319
339
|
range: [start, start + value.length],
|
|
@@ -322,7 +342,10 @@ function extractFromTemplateLiteral(node) {
|
|
|
322
342
|
});
|
|
323
343
|
}
|
|
324
344
|
}
|
|
325
|
-
return
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
function extractFromTemplateLiteral(node) {
|
|
348
|
+
return appendFromTemplateLiteral(node, []);
|
|
326
349
|
}
|
|
327
350
|
function getCalleeName(node) {
|
|
328
351
|
if (node.type === "Identifier") return node.name;
|
|
@@ -536,21 +559,6 @@ function findBestSuggestion(input, candidates, maxDistance = 3) {
|
|
|
536
559
|
return best;
|
|
537
560
|
}
|
|
538
561
|
//#endregion
|
|
539
|
-
//#region src/utils/floating-point.ts
|
|
540
|
-
/**
|
|
541
|
-
* Rounds rem/em/px values in class strings to avoid
|
|
542
|
-
* JavaScript floating point issues.
|
|
543
|
-
*
|
|
544
|
-
* Example: "text-lg/[2.4000000000000004rem]" -> "text-lg/[2.4rem]"
|
|
545
|
-
*
|
|
546
|
-
* Regression test for: better-tailwindcss#320
|
|
547
|
-
*/
|
|
548
|
-
function roundRemValue(className) {
|
|
549
|
-
return className.replace(/\[(-?\d+\.\d{7,})(rem|em|px|%)\]/g, (_, num, unit) => {
|
|
550
|
-
return `[${Number.parseFloat(Number.parseFloat(num).toFixed(6))}${unit}]`;
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
//#endregion
|
|
554
562
|
//#region src/utils/class-parser.ts
|
|
555
563
|
/**
|
|
556
564
|
* Extracts variants from a class string, respecting bracket depth.
|
|
@@ -657,23 +665,53 @@ function getArbitraryValue(cls) {
|
|
|
657
665
|
}
|
|
658
666
|
return null;
|
|
659
667
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
668
|
+
/**
|
|
669
|
+
* Strip the `!` important modifier from a utility's bare form (no variant
|
|
670
|
+
* prefix). Returns the bare string and the original position. Operate on
|
|
671
|
+
* the utility part — call `splitUtilityAndVariant` first if you have the
|
|
672
|
+
* full class. `parseClass` does both in one pass; use that when you need
|
|
673
|
+
* the full ParsedClass shape.
|
|
674
|
+
*/
|
|
675
|
+
function splitImportant(utility) {
|
|
676
|
+
if (utility.startsWith("!")) return {
|
|
677
|
+
bare: utility.slice(1),
|
|
678
|
+
position: "prefix"
|
|
667
679
|
};
|
|
668
|
-
if (
|
|
669
|
-
bare:
|
|
670
|
-
|
|
680
|
+
if (utility.endsWith("!")) return {
|
|
681
|
+
bare: utility.slice(0, -1),
|
|
682
|
+
position: "suffix"
|
|
671
683
|
};
|
|
672
684
|
return {
|
|
673
|
-
bare:
|
|
674
|
-
|
|
685
|
+
bare: utility,
|
|
686
|
+
position: null
|
|
675
687
|
};
|
|
676
688
|
}
|
|
689
|
+
/**
|
|
690
|
+
* Reattach `!` at the position recorded by `splitImportant`.
|
|
691
|
+
* The inverse of `splitImportant`: `reattachImportant(splitImportant(x).bare, splitImportant(x).position) === x`.
|
|
692
|
+
*/
|
|
693
|
+
function reattachImportant(bare, position) {
|
|
694
|
+
if (position === "prefix") return `!${bare}`;
|
|
695
|
+
if (position === "suffix") return `${bare}!`;
|
|
696
|
+
return bare;
|
|
697
|
+
}
|
|
698
|
+
//#endregion
|
|
699
|
+
//#region src/utils/floating-point.ts
|
|
700
|
+
/**
|
|
701
|
+
* Rounds rem/em/px values in class strings to avoid
|
|
702
|
+
* JavaScript floating point issues.
|
|
703
|
+
*
|
|
704
|
+
* Example: "text-lg/[2.4000000000000004rem]" -> "text-lg/[2.4rem]"
|
|
705
|
+
*
|
|
706
|
+
* Regression test for: better-tailwindcss#320
|
|
707
|
+
*/
|
|
708
|
+
function roundRemValue(className) {
|
|
709
|
+
return className.replace(/\[(-?\d+\.\d{7,})(rem|em|px|%)\]/g, (_, num, unit) => {
|
|
710
|
+
return `[${Number.parseFloat(Number.parseFloat(num).toFixed(6))}${unit}]`;
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
//#endregion
|
|
714
|
+
//#region src/design-system/cache.ts
|
|
677
715
|
var DesignSystemCache = class DesignSystemCache {
|
|
678
716
|
canonicalMap = /* @__PURE__ */ new Map();
|
|
679
717
|
validitySet = /* @__PURE__ */ new Set();
|
|
@@ -714,12 +752,12 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
714
752
|
const canonicalUtility = this.canonicalMap.get(utility);
|
|
715
753
|
if (canonicalUtility !== void 0) return className.slice(0, className.length - utility.length) + canonicalUtility;
|
|
716
754
|
}
|
|
717
|
-
const { bare,
|
|
718
|
-
if (
|
|
755
|
+
const { bare, position } = splitImportant(utility);
|
|
756
|
+
if (position) {
|
|
719
757
|
const canonicalBare = this.canonicalMap.get(bare);
|
|
720
758
|
if (canonicalBare !== void 0) {
|
|
721
759
|
const variantPrefix = className.slice(0, className.length - utility.length);
|
|
722
|
-
return
|
|
760
|
+
return position === "prefix" ? variantPrefix + "!" + canonicalBare : variantPrefix + canonicalBare + "!";
|
|
723
761
|
}
|
|
724
762
|
}
|
|
725
763
|
return className;
|
|
@@ -749,6 +787,20 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
749
787
|
if (variant.startsWith("aria-")) return (this.variantOrderMap.get("inert") ?? 48) + 2;
|
|
750
788
|
return 0;
|
|
751
789
|
}
|
|
790
|
+
_prefixOrderMap = null;
|
|
791
|
+
buildPrefixOrderMap() {
|
|
792
|
+
const index = /* @__PURE__ */ new Map();
|
|
793
|
+
for (const [cls, order] of this.orderMap) {
|
|
794
|
+
if (order === null) continue;
|
|
795
|
+
for (let i = cls.indexOf("-"); i >= 0; i = cls.indexOf("-", i + 1)) {
|
|
796
|
+
const p = cls.slice(0, i);
|
|
797
|
+
if (!index.has(p)) index.set(p, order);
|
|
798
|
+
const pd = p + "-";
|
|
799
|
+
if (!index.has(pd)) index.set(pd, order);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return index;
|
|
803
|
+
}
|
|
752
804
|
/** Find the order of the first class matching a prefix (e.g. "max-w-" → order of "max-w-0") */
|
|
753
805
|
findOrderByPrefix(prefix) {
|
|
754
806
|
const withoutDash = prefix.endsWith("-") ? prefix.slice(0, -1) : null;
|
|
@@ -756,7 +808,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
756
808
|
const order = this.orderMap.get(withoutDash);
|
|
757
809
|
if (order != null) return order;
|
|
758
810
|
}
|
|
759
|
-
|
|
811
|
+
if (!this._prefixOrderMap) this._prefixOrderMap = this.buildPrefixOrderMap();
|
|
812
|
+
return this._prefixOrderMap.get(prefix);
|
|
760
813
|
}
|
|
761
814
|
getKnownPrefixes() {
|
|
762
815
|
if (!this._knownPrefixes) {
|
|
@@ -772,7 +825,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
772
825
|
if (this.validitySet.has(className)) return true;
|
|
773
826
|
const utility = extractUtility(className);
|
|
774
827
|
if (utility !== className && this.validitySet.has(utility)) return true;
|
|
775
|
-
const { bare } =
|
|
828
|
+
const { bare } = splitImportant(utility);
|
|
776
829
|
if (bare !== utility && this.validitySet.has(bare)) return true;
|
|
777
830
|
const slashIdx = bare.lastIndexOf("/");
|
|
778
831
|
if (slashIdx > 0) {
|
|
@@ -796,8 +849,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
796
849
|
const hasVariant = utility !== className;
|
|
797
850
|
let baseOrder = this.orderMap.get(utility) ?? void 0;
|
|
798
851
|
if (baseOrder === void 0) {
|
|
799
|
-
const { bare,
|
|
800
|
-
if (
|
|
852
|
+
const { bare, position } = splitImportant(utility);
|
|
853
|
+
if (position) baseOrder = this.orderMap.get(bare) ?? void 0;
|
|
801
854
|
}
|
|
802
855
|
if (baseOrder === void 0) {
|
|
803
856
|
const slashIdx = utility.lastIndexOf("/");
|
|
@@ -805,13 +858,13 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
805
858
|
const base = utility.slice(0, slashIdx);
|
|
806
859
|
baseOrder = this.orderMap.get(base) ?? void 0;
|
|
807
860
|
if (baseOrder === void 0) {
|
|
808
|
-
const { bare,
|
|
809
|
-
if (
|
|
861
|
+
const { bare, position } = splitImportant(base);
|
|
862
|
+
if (position) baseOrder = this.orderMap.get(bare) ?? void 0;
|
|
810
863
|
}
|
|
811
864
|
}
|
|
812
865
|
}
|
|
813
866
|
if (baseOrder === void 0) {
|
|
814
|
-
const stripped =
|
|
867
|
+
const stripped = splitImportant(utility).bare;
|
|
815
868
|
const bracketIdx = stripped.indexOf("[");
|
|
816
869
|
if (bracketIdx > 0) {
|
|
817
870
|
const prefix = stripped.slice(0, bracketIdx);
|
|
@@ -819,7 +872,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
819
872
|
}
|
|
820
873
|
}
|
|
821
874
|
if (baseOrder === void 0) {
|
|
822
|
-
const stripped =
|
|
875
|
+
const stripped = splitImportant(utility).bare;
|
|
823
876
|
const parenIdx = stripped.indexOf("(");
|
|
824
877
|
if (parenIdx > 0) {
|
|
825
878
|
const prefix = stripped.slice(0, parenIdx);
|
|
@@ -827,7 +880,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
827
880
|
}
|
|
828
881
|
}
|
|
829
882
|
if (baseOrder === void 0) {
|
|
830
|
-
const stripped =
|
|
883
|
+
const stripped = splitImportant(utility).bare;
|
|
831
884
|
const numericMatch = /^(.+)-(\d+\.?\d*)$/.exec(stripped);
|
|
832
885
|
if (numericMatch && this.getKnownPrefixes().has(numericMatch[1])) baseOrder = this.findOrderByPrefix(numericMatch[1] + "-");
|
|
833
886
|
}
|
|
@@ -846,8 +899,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
846
899
|
getCssProperties(className) {
|
|
847
900
|
const result = this.cssPropsMap.get(className);
|
|
848
901
|
if (result) return result;
|
|
849
|
-
const { bare,
|
|
850
|
-
if (
|
|
902
|
+
const { bare, position } = splitImportant(className);
|
|
903
|
+
if (position) return this.cssPropsMap.get(bare) ?? [];
|
|
851
904
|
return [];
|
|
852
905
|
}
|
|
853
906
|
getVariantPriority(variant) {
|
|
@@ -859,29 +912,186 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
859
912
|
getNamedEquivalent(className) {
|
|
860
913
|
const result = this.arbitraryEquivMap.get(className);
|
|
861
914
|
if (result) return result;
|
|
862
|
-
const { bare,
|
|
863
|
-
if (
|
|
915
|
+
const { bare, position } = splitImportant(className);
|
|
916
|
+
if (position) return this.arbitraryEquivMap.get(bare) ?? null;
|
|
864
917
|
return null;
|
|
865
918
|
}
|
|
866
919
|
};
|
|
867
920
|
//#endregion
|
|
921
|
+
//#region src/utils/fatal.ts
|
|
922
|
+
/**
|
|
923
|
+
* Fatal error types and reporter for the deterministic v1 design.
|
|
924
|
+
*
|
|
925
|
+
* The plugin used to silently skip when the design system could not be
|
|
926
|
+
* loaded or the worker thread misbehaved. v1 fails loud: every DS-dependent
|
|
927
|
+
* rule catches these errors and surfaces them as a single fatal lint
|
|
928
|
+
* diagnostic so the user sees exactly what to fix.
|
|
929
|
+
*/
|
|
930
|
+
/**
|
|
931
|
+
* The single `messageId` every DS-dependent rule uses to surface a fatal
|
|
932
|
+
* design-system load failure. Exported as a constant so the rule's
|
|
933
|
+
* `meta.messages` and `context.report()` calls in `reportFatalDsError`
|
|
934
|
+
* cannot drift apart silently.
|
|
935
|
+
*/
|
|
936
|
+
const DS_UNAVAILABLE_MESSAGE_ID = "designSystemUnavailable";
|
|
937
|
+
/** Pre-shaped entry for `meta.messages`. Spread it: `messages: { ..., ...DS_UNAVAILABLE_MESSAGE }`. */
|
|
938
|
+
const DS_UNAVAILABLE_MESSAGE = { [DS_UNAVAILABLE_MESSAGE_ID]: "{{message}}" };
|
|
939
|
+
var OxlintTailwindError = class extends Error {
|
|
940
|
+
/** A short, actionable hint shown alongside the error message. */
|
|
941
|
+
hint;
|
|
942
|
+
constructor(message, hint, options) {
|
|
943
|
+
super(message, options);
|
|
944
|
+
this.name = this.constructor.name;
|
|
945
|
+
this.hint = hint;
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
/** No `settings.tailwindcss.entryPoint` was provided and a DS-dependent rule needs one. */
|
|
949
|
+
var MissingEntryPointError = class extends OxlintTailwindError {};
|
|
950
|
+
/** `settings.tailwindcss.entryPoint` is an array of strings — removed in v1. */
|
|
951
|
+
var DeprecatedEntryPointShapeError = class extends OxlintTailwindError {};
|
|
952
|
+
/** `__unstable__loadDesignSystem` failed (bad CSS path, malformed CSS, install issue). */
|
|
953
|
+
var DesignSystemLoadError = class extends OxlintTailwindError {};
|
|
954
|
+
/** The sort or canonicalize worker thread failed to initialize or timed out. */
|
|
955
|
+
var SortServiceError = class extends OxlintTailwindError {};
|
|
956
|
+
/** Type-guard: did this error originate from the plugin's fail-loud infrastructure? */
|
|
957
|
+
function isFatalError(err) {
|
|
958
|
+
return err instanceof OxlintTailwindError;
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Format a fatal error into the diagnostic message a rule should report.
|
|
962
|
+
*
|
|
963
|
+
* Pulls in the optional `hint` (a one-line "here's how to fix it" follow-up)
|
|
964
|
+
* so the message is self-contained even when the user is reading it in an
|
|
965
|
+
* editor without the docs site open.
|
|
966
|
+
*/
|
|
967
|
+
function formatFatalError(err) {
|
|
968
|
+
return err.hint ? `${err.message}\n\nHint: ${err.hint}` : err.message;
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Helper for DS-dependent rules to convert a caught fatal error into a
|
|
972
|
+
* single diagnostic. Returns `true` if the error was reported (and the
|
|
973
|
+
* visitor should bail out), `false` otherwise — the rule layer rethrows
|
|
974
|
+
* non-fatal errors to surface real bugs.
|
|
975
|
+
*/
|
|
976
|
+
function reportFatalDsError(context, err, node) {
|
|
977
|
+
if (!isFatalError(err)) return false;
|
|
978
|
+
context.report({
|
|
979
|
+
node,
|
|
980
|
+
messageId: DS_UNAVAILABLE_MESSAGE_ID,
|
|
981
|
+
data: { message: formatFatalError(err) }
|
|
982
|
+
});
|
|
983
|
+
return true;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Call `getDS()` and route fatal errors through `reportFatalDsError`.
|
|
987
|
+
*
|
|
988
|
+
* Returns the load result on success, or `null` after a fatal error has been
|
|
989
|
+
* reported (rules should `if (!ds) return` and exit the visitor). Non-fatal
|
|
990
|
+
* errors are re-thrown so genuine bugs aren't hidden.
|
|
991
|
+
*/
|
|
992
|
+
function safeGetDS(getDS, context, node) {
|
|
993
|
+
try {
|
|
994
|
+
return getDS();
|
|
995
|
+
} catch (err) {
|
|
996
|
+
if (reportFatalDsError(context, err, node)) return null;
|
|
997
|
+
throw err;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
//#endregion
|
|
1001
|
+
//#region src/design-system/tailwind-node.ts
|
|
1002
|
+
/**
|
|
1003
|
+
* Single source of truth for the resolved `@tailwindcss/node` entry path
|
|
1004
|
+
* and its installed version. Three call sites (sync-loader, sort-service,
|
|
1005
|
+
* canonicalize-service) used to each call `require.resolve` independently
|
|
1006
|
+
* — this module resolves once at load and exposes the results.
|
|
1007
|
+
*
|
|
1008
|
+
* The version is read from `dist/../package.json` (the published layout)
|
|
1009
|
+
* with a walk-up fallback for unconventional installs.
|
|
1010
|
+
*/
|
|
1011
|
+
function resolveTailwindNodePath() {
|
|
1012
|
+
try {
|
|
1013
|
+
return __require.resolve("@tailwindcss/node");
|
|
1014
|
+
} catch {
|
|
1015
|
+
return null;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
function readVersionFromEntry(entryPath) {
|
|
1019
|
+
let dir = dirname(entryPath);
|
|
1020
|
+
for (let i = 0; i < 5; i++) {
|
|
1021
|
+
const pkgJson = join(dir, "package.json");
|
|
1022
|
+
if (existsSync(pkgJson)) try {
|
|
1023
|
+
const pkg = JSON.parse(readFileSync(pkgJson, "utf-8"));
|
|
1024
|
+
if (pkg.name === "@tailwindcss/node" && pkg.version) return pkg.version;
|
|
1025
|
+
} catch {}
|
|
1026
|
+
const parent = dirname(dir);
|
|
1027
|
+
if (parent === dir) break;
|
|
1028
|
+
dir = parent;
|
|
1029
|
+
}
|
|
1030
|
+
return "unknown";
|
|
1031
|
+
}
|
|
1032
|
+
const entryPath = resolveTailwindNodePath();
|
|
1033
|
+
/**
|
|
1034
|
+
* Absolute path to `@tailwindcss/node`'s main entry, or `null` if not
|
|
1035
|
+
* installed. Workers receive this path via workerData; the parent does
|
|
1036
|
+
* the resolution because module resolution from a child process's `eval`
|
|
1037
|
+
* context (with cwd inside a tmp dir or under pnpm strict workspaces) can
|
|
1038
|
+
* fail to find the bare specifier.
|
|
1039
|
+
*/
|
|
1040
|
+
const TAILWIND_NODE_PATH = entryPath;
|
|
1041
|
+
/**
|
|
1042
|
+
* Installed `@tailwindcss/node` version, or `'unknown'`. Used as part of
|
|
1043
|
+
* the disk-cache key in sync-loader so cached precompute output is
|
|
1044
|
+
* invalidated when the consumer upgrades.
|
|
1045
|
+
*/
|
|
1046
|
+
const TAILWIND_NODE_VERSION = entryPath !== null ? readVersionFromEntry(entryPath) : "unknown";
|
|
1047
|
+
//#endregion
|
|
868
1048
|
//#region src/design-system/sync-loader.ts
|
|
869
1049
|
/**
|
|
870
|
-
* Synchronous design system loader using
|
|
1050
|
+
* Synchronous design system loader using a worker_thread.
|
|
871
1051
|
*
|
|
872
1052
|
* The problem: __unstable__loadDesignSystem is async, but oxlint's createOnce is sync.
|
|
873
|
-
* The solution:
|
|
874
|
-
*
|
|
1053
|
+
* The solution: run the design-system load + precompute in a worker_thread that writes
|
|
1054
|
+
* the result JSON straight to the disk cache and signals completion over a
|
|
1055
|
+
* SharedArrayBuffer, while the main thread blocks on `Atomics.wait`. This runs ONCE per
|
|
1056
|
+
* unique CSS entry point at plugin init time.
|
|
1057
|
+
*
|
|
1058
|
+
* Why a worker_thread and not a forked child process (the pre-1.1 design): `execFileSync`
|
|
1059
|
+
* does `fork()` of the oxlint host (Rust + embedded Node). Under Linux overcommit
|
|
1060
|
+
* accounting on memory-constrained CI runners, forking a large-RSS process is rejected
|
|
1061
|
+
* with `spawnSync … ENOMEM` even though the child immediately `exec`s (#24). A
|
|
1062
|
+
* worker_thread creates a thread in-process — no address-space duplication — so it is
|
|
1063
|
+
* immune. This mirrors what `sort-service.ts` / `canonicalize-service.ts` already do.
|
|
875
1064
|
*
|
|
876
1065
|
* For arbitrary values (bg-[#123]) that aren't in the class list, we use heuristics.
|
|
877
1066
|
*/
|
|
878
1067
|
const PRECOMPUTE_SCRIPT = `
|
|
879
|
-
//
|
|
880
|
-
//
|
|
881
|
-
//
|
|
882
|
-
//
|
|
883
|
-
const {
|
|
884
|
-
const {
|
|
1068
|
+
// Runs as a worker_thread (NOT a forked child process). Inputs arrive via
|
|
1069
|
+
// workerData; the result is written straight to the disk cache and completion
|
|
1070
|
+
// is signaled over the SharedArrayBuffer's control[2] (0=loading, 1=done,
|
|
1071
|
+
// -1=error), mirroring sort-service.ts / canonicalize-service.ts.
|
|
1072
|
+
const { workerData } = require('worker_threads');
|
|
1073
|
+
const { tailwindNodePath, cssPath: WD_CSS_PATH, cachePath: WD_CACHE_PATH, tmpPath: WD_TMP_PATH, sharedBuffer } = workerData;
|
|
1074
|
+
const control = new Int32Array(sharedBuffer, 0, 4);
|
|
1075
|
+
// On failure, write the error message into the data region so the main thread
|
|
1076
|
+
// can surface the real cause instead of a bare "undefined".
|
|
1077
|
+
const errLengthView = new DataView(sharedBuffer, 16, 4);
|
|
1078
|
+
const errDataArea = new Uint8Array(sharedBuffer, 20);
|
|
1079
|
+
function signalError(e) {
|
|
1080
|
+
try {
|
|
1081
|
+
const msg = Buffer.from(String((e && e.message) || e || 'unknown precompute error'), 'utf-8');
|
|
1082
|
+
const n = Math.min(msg.length, errDataArea.length);
|
|
1083
|
+
errDataArea.set(msg.subarray(0, n), 0);
|
|
1084
|
+
errLengthView.setUint32(0, n);
|
|
1085
|
+
} catch {}
|
|
1086
|
+
Atomics.store(control, 2, -1);
|
|
1087
|
+
Atomics.notify(control, 2);
|
|
1088
|
+
}
|
|
1089
|
+
// Resolve @tailwindcss/node via an absolute path passed in by the parent —
|
|
1090
|
+
// bare-specifier lookup from the worker's cwd fails under pnpm strict
|
|
1091
|
+
// workspaces (the module lives under node_modules/.pnpm/... which is not on the
|
|
1092
|
+
// resolution path from the consumer's project root).
|
|
1093
|
+
const { __unstable__loadDesignSystem } = require(tailwindNodePath);
|
|
1094
|
+
const { readFileSync, writeFileSync, renameSync } = require('fs');
|
|
885
1095
|
const { dirname, resolve } = require('path');
|
|
886
1096
|
|
|
887
1097
|
function resolveImport(specifier, baseDir) {
|
|
@@ -958,7 +1168,7 @@ function extractComponentClasses(cssPath, baseDir) {
|
|
|
958
1168
|
}
|
|
959
1169
|
|
|
960
1170
|
async function main() {
|
|
961
|
-
const cssPath =
|
|
1171
|
+
const cssPath = WD_CSS_PATH;
|
|
962
1172
|
const css = readFileSync(cssPath, 'utf-8');
|
|
963
1173
|
const base = dirname(cssPath);
|
|
964
1174
|
const ds = await __unstable__loadDesignSystem(css, { base });
|
|
@@ -1236,9 +1446,15 @@ async function main() {
|
|
|
1236
1446
|
}
|
|
1237
1447
|
}
|
|
1238
1448
|
|
|
1239
|
-
|
|
1449
|
+
const json = JSON.stringify({ validClasses, canonical, order, cssProps, variantOrder, componentClasses, arbitraryEquivalents });
|
|
1450
|
+
// Atomic write: write to a unique temp path then rename, so a peer isolate
|
|
1451
|
+
// busy-waiting on the cache file never observes a half-written JSON.
|
|
1452
|
+
writeFileSync(WD_TMP_PATH, json);
|
|
1453
|
+
renameSync(WD_TMP_PATH, WD_CACHE_PATH);
|
|
1240
1454
|
}
|
|
1241
|
-
main()
|
|
1455
|
+
main()
|
|
1456
|
+
.then(() => { Atomics.store(control, 2, 1); Atomics.notify(control, 2); })
|
|
1457
|
+
.catch((e) => signalError(e));
|
|
1242
1458
|
`;
|
|
1243
1459
|
const CACHE_DIR = join(tmpdir(), "oxlint-tailwindcss");
|
|
1244
1460
|
/**
|
|
@@ -1255,35 +1471,7 @@ const CACHE_DIR = join(tmpdir(), "oxlint-tailwindcss");
|
|
|
1255
1471
|
function computeCacheKey(scriptContent, tailwindVersion) {
|
|
1256
1472
|
return `${createHash("md5").update(scriptContent).digest("hex").slice(0, 8)}:${tailwindVersion}`;
|
|
1257
1473
|
}
|
|
1258
|
-
|
|
1259
|
-
try {
|
|
1260
|
-
let dir = dirname(__require.resolve("@tailwindcss/node"));
|
|
1261
|
-
while (true) {
|
|
1262
|
-
const candidate = join(dir, "package.json");
|
|
1263
|
-
if (existsSync(candidate)) {
|
|
1264
|
-
const pkg = JSON.parse(readFileSync(candidate, "utf-8"));
|
|
1265
|
-
if (pkg.name === "@tailwindcss/node" && pkg.version) return pkg.version;
|
|
1266
|
-
}
|
|
1267
|
-
const parent = dirname(dir);
|
|
1268
|
-
if (parent === dir) break;
|
|
1269
|
-
dir = parent;
|
|
1270
|
-
}
|
|
1271
|
-
} catch {}
|
|
1272
|
-
return "unknown";
|
|
1273
|
-
}
|
|
1274
|
-
const CACHE_KEY = computeCacheKey(PRECOMPUTE_SCRIPT, readTailwindVersion());
|
|
1275
|
-
/**
|
|
1276
|
-
* Two-level disk cache for monorepo deduplication:
|
|
1277
|
-
*
|
|
1278
|
-
* Level 1 — mtime index (.idx): maps path+mtime → content hash (fast-path, avoids reading CSS)
|
|
1279
|
-
* Level 2 — content cache (.json): maps content hash → precomputed data (shared across packages)
|
|
1280
|
-
*
|
|
1281
|
-
* In monorepos, multiple packages with identical CSS (e.g. `@import 'tailwindcss'`) at different
|
|
1282
|
-
* paths share a single content cache entry, avoiding redundant child process spawns.
|
|
1283
|
-
*/
|
|
1284
|
-
function getMtimeIndexPath(cssPath, mtime) {
|
|
1285
|
-
return join(CACHE_DIR, `${createHash("md5").update(`${CACHE_KEY}:${cssPath}:${mtime}`).digest("hex")}.idx`);
|
|
1286
|
-
}
|
|
1474
|
+
const CACHE_KEY = computeCacheKey(PRECOMPUTE_SCRIPT, TAILWIND_NODE_VERSION);
|
|
1287
1475
|
function computeContentHash(content) {
|
|
1288
1476
|
return createHash("md5").update(`${CACHE_KEY}:${content}`).digest("hex");
|
|
1289
1477
|
}
|
|
@@ -1291,152 +1479,178 @@ function getContentCachePath(contentHash) {
|
|
|
1291
1479
|
return join(CACHE_DIR, `${contentHash}.json`);
|
|
1292
1480
|
}
|
|
1293
1481
|
function tryReadCache(cachePath) {
|
|
1482
|
+
const raw = tryReadRawCache(cachePath);
|
|
1483
|
+
if (raw === null) return null;
|
|
1294
1484
|
try {
|
|
1295
|
-
return JSON.parse(
|
|
1485
|
+
return JSON.parse(raw);
|
|
1296
1486
|
} catch {
|
|
1297
1487
|
return null;
|
|
1298
1488
|
}
|
|
1299
1489
|
}
|
|
1300
|
-
function
|
|
1490
|
+
function tryReadRawCache(cachePath) {
|
|
1301
1491
|
try {
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
}
|
|
1492
|
+
return readFileSync(cachePath, "utf-8");
|
|
1493
|
+
} catch {
|
|
1494
|
+
return null;
|
|
1495
|
+
}
|
|
1306
1496
|
}
|
|
1307
|
-
|
|
1308
|
-
|
|
1497
|
+
const LOCK_POLL_MS = 50;
|
|
1498
|
+
const SLEEP_BUFFER = new Int32Array(new SharedArrayBuffer(4));
|
|
1499
|
+
function syncSleep(ms) {
|
|
1500
|
+
Atomics.wait(SLEEP_BUFFER, 0, 0, ms);
|
|
1501
|
+
}
|
|
1502
|
+
/** Age of the lock file in ms, or Infinity if it has already vanished. */
|
|
1503
|
+
function lockAgeMs(lockPath) {
|
|
1309
1504
|
try {
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
const cached = tryReadCache(getContentCachePath(readFileSync(mtimeIndexPath, "utf-8").trim()));
|
|
1314
|
-
if (cached) return cached;
|
|
1315
|
-
} catch {}
|
|
1316
|
-
const contentHash = computeContentHash(readFileSync(resolvedPath, "utf-8"));
|
|
1317
|
-
const contentCachePath = getContentCachePath(contentHash);
|
|
1318
|
-
const cached = tryReadCache(contentCachePath);
|
|
1319
|
-
if (cached) {
|
|
1320
|
-
try {
|
|
1321
|
-
mkdirSync(CACHE_DIR, { recursive: true });
|
|
1322
|
-
writeFileSync(mtimeIndexPath, contentHash);
|
|
1323
|
-
} catch {}
|
|
1324
|
-
return cached;
|
|
1325
|
-
}
|
|
1326
|
-
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
1327
|
-
const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
1328
|
-
encoding: "utf-8",
|
|
1329
|
-
timeout: timeout ?? 3e4,
|
|
1330
|
-
maxBuffer: 50 * 1024 * 1024,
|
|
1331
|
-
env: {
|
|
1332
|
-
...process.env,
|
|
1333
|
-
TAILWIND_CSS_PATH: resolvedPath,
|
|
1334
|
-
TAILWIND_NODE_PATH: tailwindNodePath
|
|
1335
|
-
},
|
|
1336
|
-
cwd: dirname(resolvedPath)
|
|
1337
|
-
});
|
|
1338
|
-
writeCacheFiles(contentCachePath, mtimeIndexPath, contentHash, stdout);
|
|
1339
|
-
return JSON.parse(stdout);
|
|
1340
|
-
} catch (error) {
|
|
1341
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
1342
|
-
if (msg.includes("Cannot find module '@tailwindcss/node'")) console.error(`[oxlint-tailwindcss] Could not resolve '@tailwindcss/node' for "${resolvedPath}". If you are using pnpm with strict hoisting, add '@tailwindcss/node' as a direct devDependency, or upgrade oxlint-tailwindcss to >= 0.7.0 which resolves it from the plugin's own install location. DS-dependent rules will be skipped for this file.`);
|
|
1343
|
-
else console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, msg);
|
|
1344
|
-
return null;
|
|
1505
|
+
return Date.now() - statSync(lockPath).mtimeMs;
|
|
1506
|
+
} catch {
|
|
1507
|
+
return Number.POSITIVE_INFINITY;
|
|
1345
1508
|
}
|
|
1346
1509
|
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1510
|
+
function tryUnlink(path) {
|
|
1511
|
+
try {
|
|
1512
|
+
unlinkSync(path);
|
|
1513
|
+
} catch {}
|
|
1514
|
+
}
|
|
1515
|
+
/** control[2] — the worker's ready/done signal (0=loading, 1=done, -1=error). */
|
|
1516
|
+
const PRECOMPUTE_SIGNAL_INDEX = 2;
|
|
1517
|
+
const ERR_BUFFER_SIZE = 64 * 1024;
|
|
1518
|
+
let precomputeSeq = 0;
|
|
1350
1519
|
/**
|
|
1351
|
-
*
|
|
1352
|
-
*
|
|
1520
|
+
* Choose the diagnostic hint based on the underlying error code. ENOMEM/EAGAIN
|
|
1521
|
+
* is memory pressure, not a CSS or timeout problem — the legacy hint pointed
|
|
1522
|
+
* users at the wrong levers (issue #24 / birdman CI report). Exported for tests.
|
|
1353
1523
|
*/
|
|
1354
|
-
function
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
}
|
|
1364
|
-
return null;
|
|
1524
|
+
function precomputeHint(cause) {
|
|
1525
|
+
const code = cause?.code;
|
|
1526
|
+
const message = cause instanceof Error ? cause.message : "";
|
|
1527
|
+
if (code === "ENOMEM" || code === "EAGAIN" || message.includes("ENOMEM")) return "The host ran out of memory spawning the precompute worker. This is rare now that precompute runs in a worker thread; if it persists, lower oxlint concurrency (e.g. `--threads`) or give the machine/CI runner more memory.";
|
|
1528
|
+
return "Check the CSS file (and its imports) for syntax errors. If this looks like a timeout, raise `settings.tailwindcss.timeout`.";
|
|
1529
|
+
}
|
|
1530
|
+
function precomputeLoadError(resolvedPath, cause) {
|
|
1531
|
+
const causeError = cause instanceof Error ? cause : void 0;
|
|
1532
|
+
return new DesignSystemLoadError(`Failed to precompute design system from "${resolvedPath}": ${causeError?.message ?? String(cause)}`, precomputeHint(cause), { cause: causeError });
|
|
1365
1533
|
}
|
|
1366
1534
|
/**
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1535
|
+
* Run the precompute in a worker_thread (not a forked child). The worker loads
|
|
1536
|
+
* the design system in-process, writes the JSON straight to `cachePath`, and
|
|
1537
|
+
* signals completion over a SharedArrayBuffer; the main thread blocks on
|
|
1538
|
+
* `Atomics.wait`. Because worker_threads don't fork the host's address space,
|
|
1539
|
+
* this avoids the `spawnSync … ENOMEM` the fork-based path hit on
|
|
1540
|
+
* memory-constrained CI runners (#24).
|
|
1541
|
+
*
|
|
1542
|
+
* Throws `DesignSystemLoadError` on spawn failure, worker error, or timeout.
|
|
1543
|
+
* The worker writes the result itself, so this returns nothing — callers read
|
|
1544
|
+
* it back from `cachePath`.
|
|
1369
1545
|
*/
|
|
1370
|
-
function
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1546
|
+
function runPrecomputeViaWorker(resolvedPath, tailwindNodePath, cachePath, timeout) {
|
|
1547
|
+
const sharedBuffer = new SharedArrayBuffer(ERR_BUFFER_SIZE);
|
|
1548
|
+
const control = new Int32Array(sharedBuffer, 0, 4);
|
|
1549
|
+
const errLengthView = new DataView(sharedBuffer, 16, 4);
|
|
1550
|
+
const errDataArea = new Uint8Array(sharedBuffer, 20);
|
|
1551
|
+
const tmpPath = `${cachePath}.tmp.${process.pid}.${threadId}.${precomputeSeq++}`;
|
|
1552
|
+
let worker;
|
|
1553
|
+
try {
|
|
1554
|
+
worker = new Worker(PRECOMPUTE_SCRIPT, {
|
|
1555
|
+
eval: true,
|
|
1556
|
+
workerData: {
|
|
1557
|
+
sharedBuffer,
|
|
1558
|
+
cssPath: resolvedPath,
|
|
1559
|
+
cachePath,
|
|
1560
|
+
tmpPath,
|
|
1561
|
+
tailwindNodePath
|
|
1562
|
+
}
|
|
1563
|
+
});
|
|
1564
|
+
} catch (cause) {
|
|
1565
|
+
throw precomputeLoadError(resolvedPath, cause);
|
|
1566
|
+
}
|
|
1567
|
+
let workerError;
|
|
1568
|
+
worker.on("error", (err) => {
|
|
1569
|
+
workerError = err;
|
|
1570
|
+
});
|
|
1571
|
+
try {
|
|
1572
|
+
if (Atomics.wait(control, PRECOMPUTE_SIGNAL_INDEX, 0, timeout) === "timed-out") throw new DesignSystemLoadError(`Timed out precomputing design system from "${resolvedPath}" after ${timeout}ms.`, "Raise `settings.tailwindcss.timeout` if your machine or CI runner is slow, or verify the CSS imports resolve.");
|
|
1573
|
+
if (control[PRECOMPUTE_SIGNAL_INDEX] === -1) {
|
|
1574
|
+
const len = errLengthView.getUint32(0);
|
|
1575
|
+
throw precomputeLoadError(resolvedPath, len > 0 ? new Error(Buffer.from(errDataArea.slice(0, len)).toString("utf-8")) : workerError);
|
|
1383
1576
|
}
|
|
1577
|
+
} finally {
|
|
1578
|
+
tryUnlink(tmpPath);
|
|
1579
|
+
worker.terminate();
|
|
1384
1580
|
}
|
|
1385
|
-
return false;
|
|
1386
1581
|
}
|
|
1387
|
-
const CANDIDATE_DIRS = [
|
|
1388
|
-
"src",
|
|
1389
|
-
".",
|
|
1390
|
-
"app",
|
|
1391
|
-
"styles",
|
|
1392
|
-
"style",
|
|
1393
|
-
"css",
|
|
1394
|
-
"assets",
|
|
1395
|
-
"assets/css",
|
|
1396
|
-
"resources/css"
|
|
1397
|
-
];
|
|
1398
|
-
const CANDIDATE_NAMES = [
|
|
1399
|
-
"app",
|
|
1400
|
-
"globals",
|
|
1401
|
-
"global",
|
|
1402
|
-
"style",
|
|
1403
|
-
"styles",
|
|
1404
|
-
"index",
|
|
1405
|
-
"main",
|
|
1406
|
-
"tailwind",
|
|
1407
|
-
"tailwindcss"
|
|
1408
|
-
];
|
|
1409
|
-
const CANDIDATE_PATHS = CANDIDATE_DIRS.flatMap((dir) => CANDIDATE_NAMES.map((name) => dir === "." ? `${name}.css` : `${dir}/${name}.css`));
|
|
1410
|
-
const TAILWIND_SIGNALS = [
|
|
1411
|
-
"@import \"tailwindcss\"",
|
|
1412
|
-
"@import 'tailwindcss'",
|
|
1413
|
-
"@tailwind base",
|
|
1414
|
-
"@import tailwindcss"
|
|
1415
|
-
];
|
|
1416
1582
|
/**
|
|
1417
|
-
*
|
|
1418
|
-
*
|
|
1583
|
+
* Run the precompute worker, then read back the JSON it wrote to `cachePath`.
|
|
1584
|
+
* Used by both the lock-winner path and the uncoordinated degrade path.
|
|
1419
1585
|
*/
|
|
1420
|
-
function
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1586
|
+
function precomputeAndRead(resolvedPath, tailwindNodePath, cachePath, timeout) {
|
|
1587
|
+
runPrecomputeViaWorker(resolvedPath, tailwindNodePath, cachePath, timeout);
|
|
1588
|
+
const raw = tryReadRawCache(cachePath);
|
|
1589
|
+
if (raw === null) throw new DesignSystemLoadError(`Precompute worker finished but wrote no cache file for "${resolvedPath}".`, "This usually means the system temp directory is not writable; check its permissions.");
|
|
1590
|
+
return raw;
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Return the raw precompute JSON for `contentHash`, spawning at most one
|
|
1594
|
+
* precompute worker across all isolates competing for the same hash. Whoever
|
|
1595
|
+
* wins the lock computes and writes the cache; the rest wait for that file.
|
|
1596
|
+
*/
|
|
1597
|
+
function computeWithLock(resolvedPath, tailwindNodePath, contentHash, cachePath, timeout) {
|
|
1598
|
+
const lockPath = join(CACHE_DIR, `${contentHash}.lock`);
|
|
1599
|
+
const staleMs = timeout + 3e4;
|
|
1600
|
+
try {
|
|
1601
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
1602
|
+
} catch {
|
|
1603
|
+
return precomputeAndRead(resolvedPath, tailwindNodePath, cachePath, timeout);
|
|
1604
|
+
}
|
|
1605
|
+
const start = Date.now();
|
|
1606
|
+
for (;;) {
|
|
1607
|
+
const raw = tryReadRawCache(cachePath);
|
|
1608
|
+
if (raw !== null) return raw;
|
|
1609
|
+
let lockFd;
|
|
1610
|
+
try {
|
|
1611
|
+
lockFd = openSync(lockPath, "wx");
|
|
1612
|
+
} catch (cause) {
|
|
1613
|
+
if (cause.code === "EEXIST") {
|
|
1614
|
+
if (lockAgeMs(lockPath) > staleMs || Date.now() - start > staleMs * 2) tryUnlink(lockPath);
|
|
1615
|
+
else syncSleep(LOCK_POLL_MS);
|
|
1431
1616
|
continue;
|
|
1432
1617
|
}
|
|
1618
|
+
return precomputeAndRead(resolvedPath, tailwindNodePath, cachePath, timeout);
|
|
1619
|
+
}
|
|
1620
|
+
try {
|
|
1621
|
+
return precomputeAndRead(resolvedPath, tailwindNodePath, cachePath, timeout);
|
|
1622
|
+
} finally {
|
|
1623
|
+
closeSync(lockFd);
|
|
1624
|
+
tryUnlink(lockPath);
|
|
1433
1625
|
}
|
|
1434
|
-
if (existsSync(join(currentDir, "package.json"))) break;
|
|
1435
|
-
const parentDir = dirname(currentDir);
|
|
1436
|
-
if (parentDir === currentDir) break;
|
|
1437
|
-
currentDir = parentDir;
|
|
1438
1626
|
}
|
|
1439
|
-
|
|
1627
|
+
}
|
|
1628
|
+
/**
|
|
1629
|
+
* Synchronously load and precompute the design system for a CSS file.
|
|
1630
|
+
*
|
|
1631
|
+
* Throws `DesignSystemLoadError` on any failure (missing file, worker
|
|
1632
|
+
* error, missing `@tailwindcss/node`, etc.). Callers catch via
|
|
1633
|
+
* `reportFatalDsError` to surface a single Program-level diagnostic.
|
|
1634
|
+
*/
|
|
1635
|
+
function loadDesignSystemSync(cssPath, timeout) {
|
|
1636
|
+
const resolvedPath = resolve(cssPath);
|
|
1637
|
+
let content;
|
|
1638
|
+
try {
|
|
1639
|
+
content = readFileSync(resolvedPath, "utf-8");
|
|
1640
|
+
} catch (cause) {
|
|
1641
|
+
throw new DesignSystemLoadError(`Could not read CSS entry point: ${resolvedPath}`, "Verify the path resolves from the oxlint working directory and the file is readable.", { cause: cause instanceof Error ? cause : void 0 });
|
|
1642
|
+
}
|
|
1643
|
+
const contentHash = computeContentHash(content);
|
|
1644
|
+
const contentCachePath = getContentCachePath(contentHash);
|
|
1645
|
+
const cached = tryReadCache(contentCachePath);
|
|
1646
|
+
if (cached) return cached;
|
|
1647
|
+
if (TAILWIND_NODE_PATH === null) throw new DesignSystemLoadError(`Could not resolve '@tailwindcss/node' while loading "${resolvedPath}".`, "Install '@tailwindcss/node' (or upgrade oxlint-tailwindcss) and re-run.");
|
|
1648
|
+
const raw = computeWithLock(resolvedPath, TAILWIND_NODE_PATH, contentHash, contentCachePath, timeout ?? 6e4);
|
|
1649
|
+
try {
|
|
1650
|
+
return JSON.parse(raw);
|
|
1651
|
+
} catch (cause) {
|
|
1652
|
+
throw new DesignSystemLoadError(`Precompute output for "${resolvedPath}" was not valid JSON.`, "This is likely a bug in oxlint-tailwindcss. Please open an issue with the CSS that triggered it.", { cause: cause instanceof Error ? cause : void 0 });
|
|
1653
|
+
}
|
|
1440
1654
|
}
|
|
1441
1655
|
//#endregion
|
|
1442
1656
|
//#region src/design-system/debug.ts
|
|
@@ -1467,48 +1681,154 @@ function debugLog(message) {
|
|
|
1467
1681
|
if (_enabled || process.env.DEBUG === "oxlint-tailwindcss") console.error(`${PREFIX} ${message}`);
|
|
1468
1682
|
}
|
|
1469
1683
|
//#endregion
|
|
1684
|
+
//#region src/utils/context.ts
|
|
1685
|
+
/**
|
|
1686
|
+
* Safely read `context.options[0]`. Returns `undefined` if the getter throws
|
|
1687
|
+
* (oxlint < 1.31.0) or if options aren't populated yet.
|
|
1688
|
+
*/
|
|
1689
|
+
function safeOptions(context) {
|
|
1690
|
+
try {
|
|
1691
|
+
return context.options?.[0] ?? void 0;
|
|
1692
|
+
} catch {
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Safely read `context.settings`. Same try/catch pattern as `safeOptions`
|
|
1698
|
+
* for the createOnce-throws case.
|
|
1699
|
+
*/
|
|
1700
|
+
function safeSettings(context) {
|
|
1701
|
+
try {
|
|
1702
|
+
return context.settings ?? void 0;
|
|
1703
|
+
} catch {
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
/** Safely read `context.filename`. Same try/catch pattern. */
|
|
1708
|
+
function safeFilename(context) {
|
|
1709
|
+
try {
|
|
1710
|
+
return context.filename ?? void 0;
|
|
1711
|
+
} catch {
|
|
1712
|
+
return;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* Lazily memoize a compiled options object across visitor calls.
|
|
1717
|
+
*
|
|
1718
|
+
* Every rule with options reinvents:
|
|
1719
|
+
*
|
|
1720
|
+
* let _x: CompiledOptions | null = null
|
|
1721
|
+
* function getX() {
|
|
1722
|
+
* if (_x === null) _x = compile(safeOptions<RawOptions>(context))
|
|
1723
|
+
* return _x
|
|
1724
|
+
* }
|
|
1725
|
+
*
|
|
1726
|
+
* This wraps the pattern: pass the context and a `compile` function (which
|
|
1727
|
+
* receives the raw options object — possibly undefined) and get a memoized
|
|
1728
|
+
* thunk back. Inside `createOnce`, options aren't available yet, so the first
|
|
1729
|
+
* visitor call triggers compilation; every subsequent call is O(1).
|
|
1730
|
+
*/
|
|
1731
|
+
function createLazyOptions(context, compile) {
|
|
1732
|
+
let cached = null;
|
|
1733
|
+
return () => {
|
|
1734
|
+
if (cached) return cached.value;
|
|
1735
|
+
cached = { value: compile(safeOptions(context)) };
|
|
1736
|
+
return cached.value;
|
|
1737
|
+
};
|
|
1738
|
+
}
|
|
1739
|
+
//#endregion
|
|
1470
1740
|
//#region src/design-system/loader.ts
|
|
1471
1741
|
const dsCache = /* @__PURE__ */ new Map();
|
|
1472
|
-
const autoDetectCache = /* @__PURE__ */ new Map();
|
|
1473
|
-
let lastLoadedPath = null;
|
|
1474
1742
|
/**
|
|
1475
|
-
*
|
|
1476
|
-
*
|
|
1743
|
+
* Memoizes DS-load FAILURES per entry point, keyed by `(resolvedPath, mtime)`.
|
|
1744
|
+
*
|
|
1745
|
+
* Without this, a single environmental failure (e.g. `spawnSync … ENOMEM` on a
|
|
1746
|
+
* constrained CI runner) was re-attempted on every AST node × every rule ×
|
|
1747
|
+
* every file — the storm that turned one failure into ~18k re-spawns and 21k
|
|
1748
|
+
* per-class errors (issue #24 / birdman report). Caching the failure collapses
|
|
1749
|
+
* it to one attempt per entry point per process. Keyed by mtime so fixing the
|
|
1750
|
+
* CSS (mtime changes) invalidates the entry and a fresh load is attempted.
|
|
1751
|
+
*/
|
|
1752
|
+
const dsFailureCache = /* @__PURE__ */ new Map();
|
|
1753
|
+
function isEntryPointMapping(v) {
|
|
1754
|
+
if (typeof v !== "object" || v === null) return false;
|
|
1755
|
+
const m = v;
|
|
1756
|
+
return (typeof m.files === "string" || Array.isArray(m.files) && m.files.every((s) => typeof s === "string")) && typeof m.use === "string";
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Read `settings.tailwindcss.entryPoint` and normalize it to the v1 shape.
|
|
1760
|
+
*
|
|
1761
|
+
* Throws `DeprecatedEntryPointShapeError` if the legacy `string[]` shape is
|
|
1762
|
+
* still in use (removed in v1; the user must migrate to `{ files, use }[]`).
|
|
1477
1763
|
*/
|
|
1478
1764
|
function entryPointFromSettings(settings) {
|
|
1479
1765
|
const tw = settings?.tailwindcss;
|
|
1480
|
-
if (tw
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1766
|
+
if (!tw || typeof tw !== "object" || !("entryPoint" in tw)) return void 0;
|
|
1767
|
+
const ep = tw.entryPoint;
|
|
1768
|
+
if (typeof ep === "string") return ep;
|
|
1769
|
+
if (Array.isArray(ep)) {
|
|
1770
|
+
if (ep.length === 0) return void 0;
|
|
1771
|
+
if (ep.every((e) => typeof e === "string")) throw new DeprecatedEntryPointShapeError("`settings.tailwindcss.entryPoint: string[]` was removed in v1.0.0.", "Convert each entry into an object with explicit globs:\n entryPoint: [\n { files: \"packages/app/**\", use: \"packages/app/src/styles.css\" },\n ...\n ]\nSee https://oxlint-tailwindcss.pages.dev/migration/v0-to-v1");
|
|
1772
|
+
if (ep.every(isEntryPointMapping)) return ep;
|
|
1773
|
+
throw new MissingEntryPointError("`settings.tailwindcss.entryPoint` must be either a string or an array of `{ files, use }` objects.", "Each item needs `files` (glob or array of globs) and `use` (path to a CSS file).");
|
|
1484
1774
|
}
|
|
1485
1775
|
}
|
|
1486
1776
|
/**
|
|
1487
|
-
*
|
|
1488
|
-
*
|
|
1777
|
+
* Compile a minimal glob pattern to a regular expression.
|
|
1778
|
+
*
|
|
1779
|
+
* Supported syntax: `**` (any depth), `*` (any chars except `/`), `?` (one
|
|
1780
|
+
* char except `/`), plus literal segments. Anchored on both ends.
|
|
1781
|
+
*
|
|
1782
|
+
* Memoized across calls because `resolveByGlobMapping` runs per lint visit;
|
|
1783
|
+
* recompiling the same pattern per AST node would be wasted work.
|
|
1489
1784
|
*/
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
const
|
|
1493
|
-
|
|
1494
|
-
let
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1785
|
+
const globRegexCache = /* @__PURE__ */ new Map();
|
|
1786
|
+
function globToRegExp(glob) {
|
|
1787
|
+
const cached = globRegexCache.get(glob);
|
|
1788
|
+
if (cached) return cached;
|
|
1789
|
+
let re = "";
|
|
1790
|
+
let i = 0;
|
|
1791
|
+
while (i < glob.length) {
|
|
1792
|
+
const c = glob[i];
|
|
1793
|
+
if (c === "*" && glob[i + 1] === "*") {
|
|
1794
|
+
re += ".*";
|
|
1795
|
+
i += 2;
|
|
1796
|
+
if (glob[i] === "/") i++;
|
|
1797
|
+
} else if (c === "*") {
|
|
1798
|
+
re += "[^/]*";
|
|
1799
|
+
i++;
|
|
1800
|
+
} else if (c === "?") {
|
|
1801
|
+
re += "[^/]";
|
|
1802
|
+
i++;
|
|
1803
|
+
} else if ("+()|.\\$^{}[]".includes(c)) {
|
|
1804
|
+
re += "\\" + c;
|
|
1805
|
+
i++;
|
|
1806
|
+
} else {
|
|
1807
|
+
re += c;
|
|
1808
|
+
i++;
|
|
1500
1809
|
}
|
|
1501
1810
|
}
|
|
1502
|
-
|
|
1811
|
+
const compiled = new RegExp("^" + re + "$");
|
|
1812
|
+
globRegexCache.set(glob, compiled);
|
|
1813
|
+
return compiled;
|
|
1503
1814
|
}
|
|
1504
|
-
function
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1815
|
+
function matchesAnyGlob(filePath, globs) {
|
|
1816
|
+
return (typeof globs === "string" ? [globs] : globs).some((g) => globToRegExp(g).test(filePath));
|
|
1817
|
+
}
|
|
1818
|
+
/**
|
|
1819
|
+
* Resolve a file path to its entry-point CSS via a mapping array.
|
|
1820
|
+
* First-match-wins. Paths are matched relative to `baseDir` (typically the
|
|
1821
|
+
* process CWD where oxlint runs), with forward-slash normalization for
|
|
1822
|
+
* cross-platform consistency. Returns the absolute CSS path, or `null` if
|
|
1823
|
+
* no mapping matches.
|
|
1824
|
+
*/
|
|
1825
|
+
function resolveByGlobMapping(mappings, filePath, baseDir) {
|
|
1826
|
+
const relPath = relative(baseDir, resolve(filePath)).split("\\").join("/");
|
|
1827
|
+
for (const m of mappings) if (matchesAnyGlob(relPath, m.files)) return resolve(baseDir, m.use);
|
|
1828
|
+
return null;
|
|
1509
1829
|
}
|
|
1510
1830
|
/**
|
|
1511
|
-
*
|
|
1831
|
+
* Extract `rootFontSize` from `settings.tailwindcss` (default: 16).
|
|
1512
1832
|
*/
|
|
1513
1833
|
function rootFontSizeFromSettings(settings) {
|
|
1514
1834
|
const tw = settings?.tailwindcss;
|
|
@@ -1518,9 +1838,6 @@ function rootFontSizeFromSettings(settings) {
|
|
|
1518
1838
|
}
|
|
1519
1839
|
return 16;
|
|
1520
1840
|
}
|
|
1521
|
-
/**
|
|
1522
|
-
* Extracts `timeout` from `context.settings.tailwindcss`.
|
|
1523
|
-
*/
|
|
1524
1841
|
function timeoutFromSettings(settings) {
|
|
1525
1842
|
const tw = settings?.tailwindcss;
|
|
1526
1843
|
if (tw && typeof tw === "object" && "timeout" in tw) {
|
|
@@ -1529,183 +1846,178 @@ function timeoutFromSettings(settings) {
|
|
|
1529
1846
|
}
|
|
1530
1847
|
}
|
|
1531
1848
|
/**
|
|
1532
|
-
*
|
|
1533
|
-
* resolve to the same entry point. Avoids repeated filesystem walks.
|
|
1534
|
-
*/
|
|
1535
|
-
function cachedAutoDetect(filePath) {
|
|
1536
|
-
if (!filePath) return autoDetectEntryPoint(filePath);
|
|
1537
|
-
const dir = dirname(resolve(filePath));
|
|
1538
|
-
const cached = autoDetectCache.get(dir);
|
|
1539
|
-
if (cached !== void 0) return cached;
|
|
1540
|
-
const result = autoDetectEntryPoint(filePath);
|
|
1541
|
-
autoDetectCache.set(dir, result);
|
|
1542
|
-
return result;
|
|
1543
|
-
}
|
|
1544
|
-
/**
|
|
1545
|
-
* Pure resolution of which CSS entry point to use for a given context.
|
|
1546
|
-
*
|
|
1547
|
-
* Resolution order: rule option `entryPoint` > `settingsEntry` (string or
|
|
1548
|
-
* closest-of-array) > `autoDetect(filePath)` > `lastPath` fallback.
|
|
1849
|
+
* Load the design system for a specific CSS entry point.
|
|
1549
1850
|
*
|
|
1550
|
-
*
|
|
1551
|
-
*
|
|
1552
|
-
*
|
|
1553
|
-
* results never update the fallback (prevents cross-package contamination
|
|
1554
|
-
* in monorepos).
|
|
1851
|
+
* Throws `DesignSystemLoadError` if the load fails for any reason. Callers
|
|
1852
|
+
* (rules via `createLazyLoader`) catch via `reportFatalDsError` and surface
|
|
1853
|
+
* the failure as a single Program-level diagnostic.
|
|
1555
1854
|
*
|
|
1556
|
-
*
|
|
1557
|
-
*
|
|
1855
|
+
* Cached per absolute entry-point path, keyed by mtime. In monorepos each
|
|
1856
|
+
* unique CSS gets its own cache entry.
|
|
1558
1857
|
*/
|
|
1559
|
-
function
|
|
1560
|
-
const
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1858
|
+
function getLoadedDesignSystem(cssPath, settings) {
|
|
1859
|
+
const resolvedPath = resolve(cssPath);
|
|
1860
|
+
let mtime;
|
|
1861
|
+
try {
|
|
1862
|
+
mtime = statSync(resolvedPath).mtimeMs;
|
|
1863
|
+
} catch (cause) {
|
|
1864
|
+
throw new DesignSystemLoadError(`Could not stat CSS entry point: ${resolvedPath}`, "Check that the file exists and is readable. The path is resolved relative to the oxlint working directory.", { cause: cause instanceof Error ? cause : void 0 });
|
|
1865
|
+
}
|
|
1866
|
+
const cached = dsCache.get(resolvedPath);
|
|
1867
|
+
if (cached && cached.mtime === mtime) return {
|
|
1868
|
+
cache: cached.cache,
|
|
1869
|
+
entryPoint: resolvedPath
|
|
1570
1870
|
};
|
|
1871
|
+
const failed = dsFailureCache.get(resolvedPath);
|
|
1872
|
+
if (failed && failed.mtime === mtime) throw failed.error;
|
|
1873
|
+
let data;
|
|
1874
|
+
try {
|
|
1875
|
+
data = loadDesignSystemSync(resolvedPath, timeoutFromSettings(settings));
|
|
1876
|
+
} catch (err) {
|
|
1877
|
+
if (isFatalError(err)) dsFailureCache.set(resolvedPath, {
|
|
1878
|
+
error: err,
|
|
1879
|
+
mtime
|
|
1880
|
+
});
|
|
1881
|
+
throw err;
|
|
1882
|
+
}
|
|
1883
|
+
const cache = DesignSystemCache.fromPrecomputed(data);
|
|
1884
|
+
dsCache.set(resolvedPath, {
|
|
1885
|
+
cache,
|
|
1886
|
+
mtime
|
|
1887
|
+
});
|
|
1888
|
+
debugLog(`Loaded design system from "${resolvedPath}"`);
|
|
1571
1889
|
return {
|
|
1572
|
-
|
|
1573
|
-
|
|
1890
|
+
cache,
|
|
1891
|
+
entryPoint: resolvedPath
|
|
1574
1892
|
};
|
|
1575
1893
|
}
|
|
1576
1894
|
/**
|
|
1577
|
-
*
|
|
1578
|
-
*
|
|
1895
|
+
* Lazy DS loader for a rule. Resolves the correct design system for each
|
|
1896
|
+
* file being linted, using (in priority order):
|
|
1897
|
+
*
|
|
1898
|
+
* 1. Rule option `entryPoint` (a string path).
|
|
1899
|
+
* 2. `settings.tailwindcss.entryPoint` as a string.
|
|
1900
|
+
* 3. `settings.tailwindcss.entryPoint` as an `EntryPointMapping[]` —
|
|
1901
|
+
* first matching glob (relative to CWD) wins.
|
|
1579
1902
|
*
|
|
1580
|
-
*
|
|
1581
|
-
*
|
|
1582
|
-
*
|
|
1903
|
+
* If none of those produce an entry point for the current file, throws
|
|
1904
|
+
* `MissingEntryPointError`. If the resolved CSS fails to load, throws
|
|
1905
|
+
* `DesignSystemLoadError`. Both extend `OxlintTailwindError` and are
|
|
1906
|
+
* meant to be caught by `reportFatalDsError` in the rule's `check()`.
|
|
1583
1907
|
*
|
|
1584
|
-
*
|
|
1908
|
+
* `context.settings` and `context.filename` are unavailable in `createOnce`,
|
|
1909
|
+
* so the actual work is deferred until the returned thunk is invoked from
|
|
1910
|
+
* inside a visitor.
|
|
1585
1911
|
*/
|
|
1586
|
-
function
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
try {
|
|
1597
|
-
const mtime = statSync(resolvedPath).mtimeMs;
|
|
1598
|
-
const cached = dsCache.get(resolvedPath);
|
|
1599
|
-
if (cached && cached.mtime === mtime) {
|
|
1600
|
-
if (isExplicit) lastLoadedPath = resolvedPath;
|
|
1601
|
-
return {
|
|
1602
|
-
cache: cached.cache,
|
|
1603
|
-
entryPoint: resolvedPath
|
|
1604
|
-
};
|
|
1912
|
+
function createLazyLoader(context) {
|
|
1913
|
+
let debugInitialized = false;
|
|
1914
|
+
let lastFilePath;
|
|
1915
|
+
let lastResult;
|
|
1916
|
+
let lastError;
|
|
1917
|
+
return () => {
|
|
1918
|
+
const filePath = safeFilename(context);
|
|
1919
|
+
if (filePath !== void 0 && filePath === lastFilePath) {
|
|
1920
|
+
if (lastError) throw lastError;
|
|
1921
|
+
if (lastResult) return lastResult;
|
|
1605
1922
|
}
|
|
1606
|
-
const
|
|
1607
|
-
|
|
1608
|
-
const cache = DesignSystemCache.fromPrecomputed(data);
|
|
1609
|
-
dsCache.set(resolvedPath, {
|
|
1610
|
-
cache,
|
|
1611
|
-
mtime
|
|
1612
|
-
});
|
|
1613
|
-
debugLog(`Loaded design system from "${resolvedPath}"`);
|
|
1614
|
-
if (isExplicit) lastLoadedPath = resolvedPath;
|
|
1615
|
-
return {
|
|
1616
|
-
cache,
|
|
1617
|
-
entryPoint: resolvedPath
|
|
1618
|
-
};
|
|
1619
|
-
} catch {
|
|
1620
|
-
return null;
|
|
1621
|
-
}
|
|
1622
|
-
}
|
|
1623
|
-
/**
|
|
1624
|
-
* Creates a lazy DS loader that resolves the correct design system per file.
|
|
1625
|
-
*
|
|
1626
|
-
* In `createOnce`, `context.settings` and `context.filename` throw. When visitors
|
|
1627
|
-
* run, they become available. The loader re-resolves when the filename changes,
|
|
1628
|
-
* supporting monorepos where different files need different design systems.
|
|
1629
|
-
*
|
|
1630
|
-
* When a fixed entryPoint is provided (rule option or settings), it's used for all
|
|
1631
|
-
* files and cached after first resolution.
|
|
1632
|
-
*/
|
|
1633
|
-
function createLazyLoader(context) {
|
|
1634
|
-
let lastFilePath;
|
|
1635
|
-
let lastResult = null;
|
|
1636
|
-
let fixedEntryResolved = false;
|
|
1637
|
-
let fixedResult = null;
|
|
1638
|
-
let debugInitialized = false;
|
|
1639
|
-
return () => {
|
|
1640
|
-
let entryPoint;
|
|
1641
|
-
try {
|
|
1642
|
-
entryPoint = (context.options?.[0])?.entryPoint;
|
|
1643
|
-
} catch {}
|
|
1644
|
-
let settings;
|
|
1645
|
-
try {
|
|
1646
|
-
settings = context.settings;
|
|
1647
|
-
} catch {}
|
|
1648
|
-
let filePath;
|
|
1649
|
-
try {
|
|
1650
|
-
filePath = context.filename;
|
|
1651
|
-
} catch {}
|
|
1923
|
+
const ruleOptionEntry = safeOptions(context)?.entryPoint;
|
|
1924
|
+
const settings = safeSettings(context);
|
|
1652
1925
|
if (!debugInitialized && settings) {
|
|
1653
1926
|
debugInitialized = true;
|
|
1654
1927
|
setDebugEnabled(isDebugEnabled(settings));
|
|
1655
1928
|
}
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
if (
|
|
1663
|
-
|
|
1664
|
-
}
|
|
1665
|
-
if (Array.isArray(settingsEntry)) {
|
|
1666
|
-
const closest = resolveClosestEntryPoint(settingsEntry, filePath);
|
|
1667
|
-
if (filePath && filePath === lastFilePath) return lastResult;
|
|
1668
|
-
if (filePath) lastFilePath = filePath;
|
|
1669
|
-
lastResult = getLoadedDesignSystem(closest, settings, filePath);
|
|
1670
|
-
if (lastResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
|
|
1671
|
-
return lastResult;
|
|
1929
|
+
lastFilePath = filePath;
|
|
1930
|
+
lastResult = void 0;
|
|
1931
|
+
lastError = void 0;
|
|
1932
|
+
try {
|
|
1933
|
+
lastResult = getLoadedDesignSystem(resolveEntryPointForFile(ruleOptionEntry, entryPointFromSettings(settings), filePath), settings);
|
|
1934
|
+
} catch (err) {
|
|
1935
|
+
if (isFatalError(err) && err instanceof Error) lastError = err;
|
|
1936
|
+
throw err;
|
|
1672
1937
|
}
|
|
1673
|
-
if (filePath
|
|
1674
|
-
if (filePath) lastFilePath = filePath;
|
|
1675
|
-
lastResult = getLoadedDesignSystem(void 0, settings, filePath);
|
|
1676
|
-
if (lastResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
|
|
1938
|
+
if (filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
|
|
1677
1939
|
return lastResult;
|
|
1678
1940
|
};
|
|
1679
1941
|
}
|
|
1680
|
-
//#endregion
|
|
1681
|
-
//#region src/types.ts
|
|
1682
1942
|
/**
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1685
|
-
* In oxlint, `context.options` is `null` inside `createOnce()` — options are
|
|
1686
|
-
* only populated when visitors run. Call this lazily (inside `check()` or a
|
|
1687
|
-
* visitor) to get the user's configured options.
|
|
1943
|
+
* Pure resolution from (rule option, settings entry, file path) to a CSS
|
|
1944
|
+
* entry-point path. Exported for testability.
|
|
1688
1945
|
*
|
|
1689
|
-
*
|
|
1946
|
+
* Throws `MissingEntryPointError` if nothing resolves. The error message
|
|
1947
|
+
* names the file so the user knows which one tripped the configuration.
|
|
1690
1948
|
*/
|
|
1691
|
-
function
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1949
|
+
function resolveEntryPointForFile(ruleOptionEntry, settingsEntry, filePath) {
|
|
1950
|
+
if (ruleOptionEntry) return ruleOptionEntry;
|
|
1951
|
+
if (typeof settingsEntry === "string") return settingsEntry;
|
|
1952
|
+
if (Array.isArray(settingsEntry)) {
|
|
1953
|
+
if (!filePath) throw new MissingEntryPointError("`settings.tailwindcss.entryPoint` is a mapping array but no file path is available to match against.", "This usually means the rule ran outside the linter context. Open an issue if you see this in a real lint run.");
|
|
1954
|
+
const matched = resolveByGlobMapping(settingsEntry, filePath, process.cwd());
|
|
1955
|
+
if (matched) return matched;
|
|
1956
|
+
throw new MissingEntryPointError(`No \`entryPoint\` mapping matched \`${filePath}\`.`, "Add a mapping covering this file to `settings.tailwindcss.entryPoint`, or use a `\"**\"` fallback entry.");
|
|
1696
1957
|
}
|
|
1958
|
+
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).");
|
|
1697
1959
|
}
|
|
1960
|
+
//#endregion
|
|
1961
|
+
//#region src/utils/report.ts
|
|
1698
1962
|
/**
|
|
1699
|
-
*
|
|
1963
|
+
* Shared reporter for the "first-as-fix, rest-as-suggestion" pattern.
|
|
1964
|
+
*
|
|
1965
|
+
* Nine rules (enforce-canonical, enforce-logical, enforce-physical,
|
|
1966
|
+
* enforce-consistent-variable-syntax, enforce-consistent-important-position,
|
|
1967
|
+
* enforce-negative-arbitrary-values, no-deprecated-classes,
|
|
1968
|
+
* no-unnecessary-arbitrary-value, prefer-theme-tokens) all emit
|
|
1969
|
+
* replacements via the same shape:
|
|
1700
1970
|
*
|
|
1701
|
-
*
|
|
1702
|
-
*
|
|
1971
|
+
* 1. Collect `{ cls, replacement }` entries from a class string.
|
|
1972
|
+
* 2. Rebuild the full class string with replacements applied.
|
|
1973
|
+
* 3. Emit ONE diagnostic with the autofix attached.
|
|
1974
|
+
* 4. Emit one MORE diagnostic per remaining offender, each carrying
|
|
1975
|
+
* the same autofix as a `suggest`.
|
|
1976
|
+
*
|
|
1977
|
+
* oxlint's fixer is single-shot per AST node, so multiple offenders in the
|
|
1978
|
+
* same class string can only get one mechanical fix; the rest stay as
|
|
1979
|
+
* suggestions the IDE can offer interactively.
|
|
1703
1980
|
*/
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1981
|
+
/**
|
|
1982
|
+
* Emit autofix + suggestion diagnostics for a list of class replacements.
|
|
1983
|
+
*
|
|
1984
|
+
* If `offending` is empty, no-op. The fixer's `replaceTextRange` is shared
|
|
1985
|
+
* across all diagnostics — the first one is the primary fix, the rest carry
|
|
1986
|
+
* the same fix as a suggestion (oxlint can only apply one autofix per node).
|
|
1987
|
+
*/
|
|
1988
|
+
function reportClassReplacements(context, loc, split, classes, offending, options) {
|
|
1989
|
+
if (offending.length === 0) return;
|
|
1990
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1991
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => replacements.get(c) ?? c));
|
|
1992
|
+
const replacementKey = options.replacementKey ?? "replacement";
|
|
1993
|
+
const suggestMessageId = options.suggestMessageId ?? "suggestReplace";
|
|
1994
|
+
const applyFix = (fixer) => fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1995
|
+
const report = context.report;
|
|
1996
|
+
for (let i = 0; i < offending.length; i++) {
|
|
1997
|
+
const { cls, replacement } = offending[i];
|
|
1998
|
+
const primaryData = {
|
|
1999
|
+
className: cls,
|
|
2000
|
+
[replacementKey]: replacement
|
|
2001
|
+
};
|
|
2002
|
+
if (i === 0) report({
|
|
2003
|
+
node: loc.node,
|
|
2004
|
+
messageId: options.messageId,
|
|
2005
|
+
data: primaryData,
|
|
2006
|
+
fix: applyFix
|
|
2007
|
+
});
|
|
2008
|
+
else report({
|
|
2009
|
+
node: loc.node,
|
|
2010
|
+
messageId: options.messageId,
|
|
2011
|
+
data: primaryData,
|
|
2012
|
+
suggest: [{
|
|
2013
|
+
messageId: suggestMessageId,
|
|
2014
|
+
data: {
|
|
2015
|
+
className: cls,
|
|
2016
|
+
replacement
|
|
2017
|
+
},
|
|
2018
|
+
fix: applyFix
|
|
2019
|
+
}]
|
|
2020
|
+
});
|
|
1709
2021
|
}
|
|
1710
2022
|
}
|
|
1711
2023
|
//#endregion
|
|
@@ -1738,71 +2050,30 @@ const noDeprecatedClasses = defineRule({
|
|
|
1738
2050
|
additionalProperties: false
|
|
1739
2051
|
}],
|
|
1740
2052
|
hasSuggestions: true,
|
|
2053
|
+
defaultOptions: [{}],
|
|
1741
2054
|
messages: {
|
|
1742
2055
|
deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead.",
|
|
1743
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2056
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
2057
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
1744
2058
|
}
|
|
1745
2059
|
},
|
|
1746
2060
|
createOnce(context) {
|
|
1747
2061
|
const getDS = createLazyLoader(context);
|
|
1748
2062
|
function check(locations) {
|
|
1749
|
-
|
|
2063
|
+
if (locations.length === 0) return;
|
|
2064
|
+
if (!safeGetDS(getDS, context, locations[0].node)) return;
|
|
1750
2065
|
for (const loc of locations) {
|
|
1751
2066
|
const split = splitClassesWithSeparators(loc.value);
|
|
1752
|
-
const
|
|
1753
|
-
const offending = [];
|
|
1754
|
-
for (const cls of classes) {
|
|
2067
|
+
const offending = split.classes.flatMap((cls) => {
|
|
1755
2068
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1756
|
-
const
|
|
1757
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
1758
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
2069
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
1759
2070
|
const replacement = DEPRECATED_MAP[bareUtility];
|
|
1760
|
-
|
|
1761
|
-
if (dsResult) {
|
|
1762
|
-
const canonical = dsResult.cache.canonicalize(bareUtility);
|
|
1763
|
-
if (canonical !== bareUtility && canonical === replacement) {}
|
|
1764
|
-
}
|
|
1765
|
-
const fullReplacement = variant + (hasImportantPrefix ? "!" : "") + replacement + (hasImportantSuffix ? "!" : "");
|
|
1766
|
-
offending.push({
|
|
2071
|
+
return replacement ? [{
|
|
1767
2072
|
cls,
|
|
1768
|
-
replacement:
|
|
1769
|
-
}
|
|
1770
|
-
}
|
|
1771
|
-
|
|
1772
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1773
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
1774
|
-
for (let i = 0; i < offending.length; i++) {
|
|
1775
|
-
const { cls, replacement } = offending[i];
|
|
1776
|
-
if (i === 0) context.report({
|
|
1777
|
-
node: loc.node,
|
|
1778
|
-
messageId: "deprecated",
|
|
1779
|
-
data: {
|
|
1780
|
-
className: cls,
|
|
1781
|
-
replacement
|
|
1782
|
-
},
|
|
1783
|
-
fix(fixer) {
|
|
1784
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1785
|
-
}
|
|
1786
|
-
});
|
|
1787
|
-
else context.report({
|
|
1788
|
-
node: loc.node,
|
|
1789
|
-
messageId: "deprecated",
|
|
1790
|
-
data: {
|
|
1791
|
-
className: cls,
|
|
1792
|
-
replacement
|
|
1793
|
-
},
|
|
1794
|
-
suggest: [{
|
|
1795
|
-
messageId: "suggestReplace",
|
|
1796
|
-
data: {
|
|
1797
|
-
className: cls,
|
|
1798
|
-
replacement
|
|
1799
|
-
},
|
|
1800
|
-
fix(fixer) {
|
|
1801
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1802
|
-
}
|
|
1803
|
-
}]
|
|
1804
|
-
});
|
|
1805
|
-
}
|
|
2073
|
+
replacement: variant + reattachImportant(replacement, position)
|
|
2074
|
+
}] : [];
|
|
2075
|
+
});
|
|
2076
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "deprecated" });
|
|
1806
2077
|
}
|
|
1807
2078
|
}
|
|
1808
2079
|
return createExtractorVisitors(context, check);
|
|
@@ -1837,37 +2108,27 @@ const noUnknownClasses = defineRule({
|
|
|
1837
2108
|
messages: {
|
|
1838
2109
|
unknown: "\"{{className}}\" is not a valid Tailwind class.",
|
|
1839
2110
|
unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
|
|
1840
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2111
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
2112
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
1841
2113
|
}
|
|
1842
2114
|
},
|
|
1843
2115
|
createOnce(context) {
|
|
1844
2116
|
const getDS = createLazyLoader(context);
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
const opts = safeOptions(context);
|
|
1850
|
-
_allowlist = new Set(opts?.allowlist ?? []);
|
|
1851
|
-
_ignorePrefixes = opts?.ignorePrefixes ?? [];
|
|
1852
|
-
}
|
|
1853
|
-
return {
|
|
1854
|
-
allowlist: _allowlist,
|
|
1855
|
-
ignorePrefixes: _ignorePrefixes
|
|
1856
|
-
};
|
|
1857
|
-
}
|
|
2117
|
+
const getLazyOptions = createLazyOptions(context, (o) => ({
|
|
2118
|
+
allowlist: new Set(o?.allowlist ?? []),
|
|
2119
|
+
ignorePrefixes: o?.ignorePrefixes ?? []
|
|
2120
|
+
}));
|
|
1858
2121
|
function shouldIgnore(className) {
|
|
1859
2122
|
const { allowlist, ignorePrefixes } = getLazyOptions();
|
|
1860
2123
|
if (allowlist.has(className)) return true;
|
|
1861
2124
|
return ignorePrefixes.some((prefix) => className.startsWith(prefix));
|
|
1862
2125
|
}
|
|
1863
2126
|
function stripModifiers(className) {
|
|
1864
|
-
|
|
1865
|
-
if (stripped.startsWith("!")) stripped = stripped.slice(1);
|
|
1866
|
-
if (stripped.endsWith("!")) stripped = stripped.slice(0, -1);
|
|
1867
|
-
return stripped;
|
|
2127
|
+
return splitImportant(className).bare;
|
|
1868
2128
|
}
|
|
1869
2129
|
function check(locations) {
|
|
1870
|
-
|
|
2130
|
+
if (locations.length === 0) return;
|
|
2131
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
1871
2132
|
if (!ds) return;
|
|
1872
2133
|
const { cache } = ds;
|
|
1873
2134
|
for (const loc of locations) {
|
|
@@ -1911,29 +2172,73 @@ const noUnknownClasses = defineRule({
|
|
|
1911
2172
|
}
|
|
1912
2173
|
});
|
|
1913
2174
|
//#endregion
|
|
1914
|
-
//#region src/rules/no-conflicting-classes.ts
|
|
2175
|
+
//#region src/rules/no-conflicting-classes/spec.ts
|
|
1915
2176
|
const COMPLEMENTARY_GROUPS = [
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
2177
|
+
{
|
|
2178
|
+
pattern: /^(from|via|to)-/,
|
|
2179
|
+
reason: "Gradient stops (`from-*`, `via-*`, `to-*`) share `--tw-gradient-stops`; different prefixes compose, same prefix conflicts."
|
|
2180
|
+
},
|
|
2181
|
+
{
|
|
2182
|
+
pattern: /^(transition|duration|ease|delay)(?:-|$)/,
|
|
2183
|
+
reason: "`transition-*` composes with `duration-*`, `ease-*`, `delay-*` — `transition-all` itself has no `--tw-*` vars so the heuristic misses it."
|
|
2184
|
+
},
|
|
2185
|
+
{
|
|
2186
|
+
pattern: /^-?(translate|scale|rotate|skew)-/,
|
|
2187
|
+
reason: "Transform axes (translate / scale / rotate / skew) compose into a single `transform`; the overlap is not captured in `cssProps`."
|
|
2188
|
+
},
|
|
2189
|
+
{
|
|
2190
|
+
pattern: /^-?mask-((?:linear|radial|conic|[trblxy])(?:-(?:from|via|to|at))?)(?:-|$)/,
|
|
2191
|
+
reason: "Mask gradients: different families (e.g. `mask-linear-*` vs `mask-radial-*`) or roles (`from`/`via`/`to`/`at`) compose; same family+role conflicts."
|
|
2192
|
+
},
|
|
2193
|
+
{
|
|
2194
|
+
pattern: /^prose(?:-|$)/,
|
|
2195
|
+
reason: "`prose` + `prose-sm`/`prose-lg`/`prose-xl` modify the same scope and are designed to compose."
|
|
2196
|
+
}
|
|
1921
2197
|
];
|
|
1922
2198
|
const COMPOSITION_PAIRS = [
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
2199
|
+
{
|
|
2200
|
+
a: /^text-/,
|
|
2201
|
+
b: /^leading-/,
|
|
2202
|
+
reason: "`text-*` sets line-height via its size token; `leading-*` overrides it."
|
|
2203
|
+
},
|
|
2204
|
+
{
|
|
2205
|
+
a: /^text-/,
|
|
2206
|
+
b: /^tracking-/,
|
|
2207
|
+
reason: "`text-*` may set letter-spacing via its size token; `tracking-*` overrides it."
|
|
2208
|
+
},
|
|
2209
|
+
{
|
|
2210
|
+
a: /^border(?:-[0-9]|$)/,
|
|
2211
|
+
b: /^border-(?:solid|dashed|dotted|double|hidden|none)$/,
|
|
2212
|
+
reason: "`border-*` (width) composes with `border-{solid,dashed,…}` (style)."
|
|
2213
|
+
},
|
|
2214
|
+
{
|
|
2215
|
+
a: /^divide-/,
|
|
2216
|
+
b: /^border(?:-[trblxyse])?-/,
|
|
2217
|
+
reason: "`divide-*` styles direct children; `border-*` styles the element itself."
|
|
2218
|
+
},
|
|
2219
|
+
{
|
|
2220
|
+
a: /^prose(?:-|$)/,
|
|
2221
|
+
b: /^max-w-/,
|
|
2222
|
+
reason: "`prose` sets a default max-width; `max-w-*` overrides it."
|
|
2223
|
+
},
|
|
2224
|
+
{
|
|
2225
|
+
a: /^animate-in$/,
|
|
2226
|
+
b: /^(?:fade|spin|zoom|blur)-in(?:-|$)|^slide-in-from-/,
|
|
2227
|
+
reason: "`animate-in` initializes all `--tw-enter-*` vars; `*-in` modifiers each override one. (`tailwindcss-animate` / `tw-animate-css`.)"
|
|
2228
|
+
},
|
|
2229
|
+
{
|
|
2230
|
+
a: /^animate-out$/,
|
|
2231
|
+
b: /^(?:fade|spin|zoom|blur)-out(?:-|$)|^slide-out-to-/,
|
|
2232
|
+
reason: "`animate-out` initializes all `--tw-exit-*` vars; `*-out` modifiers each override one."
|
|
2233
|
+
},
|
|
2234
|
+
{
|
|
2235
|
+
a: /^mask-(?:add|subtract|intersect|exclude)$/,
|
|
2236
|
+
b: /^-?mask-(?:linear|radial|conic|[trblxy])-/,
|
|
2237
|
+
reason: "`mask-{add,subtract,intersect,exclude}` are mask-composite modes that compose with mask-gradient utilities."
|
|
2238
|
+
}
|
|
1931
2239
|
];
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
if (utility.endsWith("!")) return utility.slice(0, -1);
|
|
1935
|
-
return utility;
|
|
1936
|
-
}
|
|
2240
|
+
//#endregion
|
|
2241
|
+
//#region src/rules/no-conflicting-classes.ts
|
|
1937
2242
|
/**
|
|
1938
2243
|
* Two utilities compose via CSS custom properties if both define their own
|
|
1939
2244
|
* --tw-* properties that don't overlap — they each contribute to a shared
|
|
@@ -1966,18 +2271,21 @@ function isNarrowingOverride(propsEarlier, propsLater) {
|
|
|
1966
2271
|
function shouldSkipPair(a, b, propsA, propsB, rules = {}) {
|
|
1967
2272
|
if (isCompositionViaCssVars(propsA, propsB)) return true;
|
|
1968
2273
|
if (isNarrowingOverride(propsA, propsB)) return true;
|
|
1969
|
-
const ua =
|
|
1970
|
-
const ub =
|
|
1971
|
-
const groups = rules.complementaryGroups
|
|
1972
|
-
for (const re of groups) {
|
|
2274
|
+
const ua = splitImportant(extractUtility(a)).bare;
|
|
2275
|
+
const ub = splitImportant(extractUtility(b)).bare;
|
|
2276
|
+
const groups = rules.complementaryGroups !== void 0 ? rules.complementaryGroups.map((pattern) => ({ pattern })) : COMPLEMENTARY_GROUPS;
|
|
2277
|
+
for (const { pattern: re } of groups) {
|
|
1973
2278
|
const ma = ua.match(re);
|
|
1974
2279
|
const mb = ub.match(re);
|
|
1975
2280
|
if (!ma || !mb) continue;
|
|
1976
2281
|
if (ma[1] === void 0) return true;
|
|
1977
2282
|
if (ma[1] !== mb[1]) return true;
|
|
1978
2283
|
}
|
|
1979
|
-
const pairs = rules.compositionPairs
|
|
1980
|
-
|
|
2284
|
+
const pairs = rules.compositionPairs !== void 0 ? rules.compositionPairs.map(([a, b]) => ({
|
|
2285
|
+
a,
|
|
2286
|
+
b
|
|
2287
|
+
})) : COMPOSITION_PAIRS;
|
|
2288
|
+
for (const { a: reA, b: reB } of pairs) if (reA.test(ua) && reB.test(ub) || reA.test(ub) && reB.test(ua)) return true;
|
|
1981
2289
|
return false;
|
|
1982
2290
|
}
|
|
1983
2291
|
const noConflictingClasses = defineRule({
|
|
@@ -1989,12 +2297,17 @@ const noConflictingClasses = defineRule({
|
|
|
1989
2297
|
properties: { entryPoint: { type: "string" } },
|
|
1990
2298
|
additionalProperties: false
|
|
1991
2299
|
}],
|
|
1992
|
-
|
|
2300
|
+
defaultOptions: [{}],
|
|
2301
|
+
messages: {
|
|
2302
|
+
conflict: "\"{{classA}}\" and \"{{classB}}\" affect {{properties}}. \"{{winner}}\" takes precedence (appears later).",
|
|
2303
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
2304
|
+
}
|
|
1993
2305
|
},
|
|
1994
2306
|
createOnce(context) {
|
|
1995
2307
|
const getDS = createLazyLoader(context);
|
|
1996
2308
|
function check(locations) {
|
|
1997
|
-
|
|
2309
|
+
if (locations.length === 0) return;
|
|
2310
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
1998
2311
|
if (!ds) return;
|
|
1999
2312
|
const { cache } = ds;
|
|
2000
2313
|
for (const loc of locations) {
|
|
@@ -2011,7 +2324,7 @@ const noConflictingClasses = defineRule({
|
|
|
2011
2324
|
if (variantClasses.length < 2) continue;
|
|
2012
2325
|
const propsMap = /* @__PURE__ */ new Map();
|
|
2013
2326
|
for (const cls of variantClasses) {
|
|
2014
|
-
const props = cache.getCssProperties(
|
|
2327
|
+
const props = cache.getCssProperties(splitImportant(extractUtility(cls)).bare);
|
|
2015
2328
|
propsMap.set(cls, props);
|
|
2016
2329
|
}
|
|
2017
2330
|
for (let i = 0; i < variantClasses.length; i++) {
|
|
@@ -2044,42 +2357,168 @@ const noConflictingClasses = defineRule({
|
|
|
2044
2357
|
}
|
|
2045
2358
|
});
|
|
2046
2359
|
//#endregion
|
|
2047
|
-
//#region src/design-system/
|
|
2360
|
+
//#region src/design-system/ds-worker.ts
|
|
2048
2361
|
/**
|
|
2049
|
-
*
|
|
2362
|
+
* Generic worker-thread service wrapping `__unstable__loadDesignSystem`.
|
|
2050
2363
|
*
|
|
2051
|
-
*
|
|
2052
|
-
*
|
|
2053
|
-
* requests
|
|
2364
|
+
* `sort-service.ts` and `canonicalize-service.ts` used to be near-byte-identical
|
|
2365
|
+
* (~500 LOC) — both spawned a worker that loaded the design system, then served
|
|
2366
|
+
* synchronous requests over a SharedArrayBuffer using `Atomics.wait`. The only
|
|
2367
|
+
* real difference was the body of the request loop: `getClassOrder` vs
|
|
2368
|
+
* `canonicalizeCandidates`. This class encapsulates the entire shared protocol
|
|
2369
|
+
* — SharedArrayBuffer layout, state machine, lifecycle, fail-loud — and each
|
|
2370
|
+
* service shrinks to ~40 LOC of singleton + public signature.
|
|
2054
2371
|
*
|
|
2055
|
-
*
|
|
2056
|
-
*
|
|
2057
|
-
*
|
|
2058
|
-
*
|
|
2372
|
+
* v1: failures throw `SortServiceError`. The error is sticky for the lifetime
|
|
2373
|
+
* of the process (per cssPath) so subsequent calls don't pay another
|
|
2374
|
+
* init-timeout cost. Callers wrap via `safeGetDS` to surface the failure as a
|
|
2375
|
+
* single `designSystemUnavailable` diagnostic.
|
|
2376
|
+
*/
|
|
2377
|
+
const BUFFER_SIZE = 4 * 1024 * 1024;
|
|
2378
|
+
const HEADER_INTS = 4;
|
|
2379
|
+
const DATA_OFFSET = 20;
|
|
2380
|
+
const INIT_TIMEOUT = 6e4;
|
|
2381
|
+
const REQUEST_TIMEOUT = 3e4;
|
|
2382
|
+
var DesignSystemWorker = class {
|
|
2383
|
+
opts;
|
|
2384
|
+
ready = null;
|
|
2385
|
+
lastError = null;
|
|
2386
|
+
constructor(opts) {
|
|
2387
|
+
this.opts = opts;
|
|
2388
|
+
}
|
|
2389
|
+
/**
|
|
2390
|
+
* Ensure the worker is running and pointed at `cssPath`. Returns the
|
|
2391
|
+
* ready state. Throws `SortServiceError` on any failure (init timeout,
|
|
2392
|
+
* spawn error, DS-load error). Sticky: subsequent calls for the same
|
|
2393
|
+
* cssPath rethrow without retrying.
|
|
2394
|
+
*/
|
|
2395
|
+
ensure(cssPath) {
|
|
2396
|
+
if (this.lastError && this.ready?.cssPath === cssPath) throw this.lastError;
|
|
2397
|
+
if (this.ready && this.ready.cssPath === cssPath) return this.ready;
|
|
2398
|
+
if (this.ready) this.cleanup();
|
|
2399
|
+
this.lastError = null;
|
|
2400
|
+
if (TAILWIND_NODE_PATH === null) {
|
|
2401
|
+
this.lastError = new SortServiceError(`Could not resolve '@tailwindcss/node' for the ${this.opts.serviceName} worker.`, "Install '@tailwindcss/node' (or upgrade oxlint-tailwindcss) and re-run.");
|
|
2402
|
+
throw this.lastError;
|
|
2403
|
+
}
|
|
2404
|
+
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);
|
|
2405
|
+
const controlArray = new Int32Array(sharedBuffer, 0, HEADER_INTS);
|
|
2406
|
+
const lengthView = new DataView(sharedBuffer, HEADER_INTS * 4, 4);
|
|
2407
|
+
const dataArea = new Uint8Array(sharedBuffer, DATA_OFFSET);
|
|
2408
|
+
let worker;
|
|
2409
|
+
try {
|
|
2410
|
+
worker = new Worker(this.opts.workerScript, {
|
|
2411
|
+
eval: true,
|
|
2412
|
+
workerData: {
|
|
2413
|
+
sharedBuffer,
|
|
2414
|
+
cssPath,
|
|
2415
|
+
tailwindNodePath: TAILWIND_NODE_PATH
|
|
2416
|
+
}
|
|
2417
|
+
});
|
|
2418
|
+
} catch (cause) {
|
|
2419
|
+
this.lastError = new SortServiceError(`Failed to spawn ${this.opts.serviceName} worker for "${cssPath}".`, "This is unexpected; please open an issue with the error details.", { cause: cause instanceof Error ? cause : void 0 });
|
|
2420
|
+
throw this.lastError;
|
|
2421
|
+
}
|
|
2422
|
+
worker.unref();
|
|
2423
|
+
worker.on("error", (err) => {
|
|
2424
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker died: ${err.message}`, "The worker will not be restarted in this process. Restart the lint session.", { cause: err });
|
|
2425
|
+
});
|
|
2426
|
+
if (Atomics.wait(controlArray, 2, 0, INIT_TIMEOUT) === "timed-out") {
|
|
2427
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker timed out loading the design system from "${cssPath}" after ${INIT_TIMEOUT}ms.`, "Raise settings.tailwindcss.timeout if your machine is slow, or verify the CSS imports resolve.");
|
|
2428
|
+
this.terminateWorker(worker);
|
|
2429
|
+
throw this.lastError;
|
|
2430
|
+
}
|
|
2431
|
+
if (controlArray[2] === -1) {
|
|
2432
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker failed to load the design system from "${cssPath}".`, "Verify @tailwindcss/node is installed and the CSS file (and its imports) is valid.");
|
|
2433
|
+
this.terminateWorker(worker);
|
|
2434
|
+
throw this.lastError;
|
|
2435
|
+
}
|
|
2436
|
+
this.ready = {
|
|
2437
|
+
worker,
|
|
2438
|
+
controlArray,
|
|
2439
|
+
lengthView,
|
|
2440
|
+
dataArea,
|
|
2441
|
+
cssPath
|
|
2442
|
+
};
|
|
2443
|
+
return this.ready;
|
|
2444
|
+
}
|
|
2445
|
+
/**
|
|
2446
|
+
* Send a request to the worker and synchronously wait for the response.
|
|
2447
|
+
* Throws `SortServiceError` on any failure.
|
|
2448
|
+
*/
|
|
2449
|
+
callSync(cssPath, req) {
|
|
2450
|
+
const state = this.ensure(cssPath);
|
|
2451
|
+
const requestBytes = Buffer.from(JSON.stringify(req), "utf-8");
|
|
2452
|
+
if (requestBytes.length > BUFFER_SIZE - DATA_OFFSET) throw new SortServiceError(`${this.opts.serviceName} request payload is too large (${requestBytes.length} > ${BUFFER_SIZE - DATA_OFFSET} bytes).`, "This is unexpected with typical class lists; please open an issue.");
|
|
2453
|
+
state.dataArea.set(requestBytes, 0);
|
|
2454
|
+
state.lengthView.setUint32(0, requestBytes.length);
|
|
2455
|
+
Atomics.store(state.controlArray, 0, 1);
|
|
2456
|
+
Atomics.notify(state.controlArray, 0);
|
|
2457
|
+
if (Atomics.wait(state.controlArray, 1, 0, REQUEST_TIMEOUT) === "timed-out") {
|
|
2458
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker request timed out after ${REQUEST_TIMEOUT}ms.`, "Raise settings.tailwindcss.timeout if your machine is slow.");
|
|
2459
|
+
this.cleanup();
|
|
2460
|
+
throw this.lastError;
|
|
2461
|
+
}
|
|
2462
|
+
const responseLen = state.lengthView.getUint32(0);
|
|
2463
|
+
const responseStr = Buffer.from(state.dataArea.slice(0, responseLen)).toString("utf-8");
|
|
2464
|
+
Atomics.store(state.controlArray, 1, 0);
|
|
2465
|
+
let parsed;
|
|
2466
|
+
try {
|
|
2467
|
+
parsed = JSON.parse(responseStr);
|
|
2468
|
+
} catch (cause) {
|
|
2469
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker returned non-JSON response.`, "This is a bug; please open an issue.", { cause: cause instanceof Error ? cause : void 0 });
|
|
2470
|
+
this.cleanup();
|
|
2471
|
+
throw this.lastError;
|
|
2472
|
+
}
|
|
2473
|
+
if (parsed === null) {
|
|
2474
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker returned null — request body was rejected.`, "This is a bug; please open an issue with the input that triggered it.");
|
|
2475
|
+
this.cleanup();
|
|
2476
|
+
throw this.lastError;
|
|
2477
|
+
}
|
|
2478
|
+
return parsed;
|
|
2479
|
+
}
|
|
2480
|
+
reset() {
|
|
2481
|
+
this.cleanup();
|
|
2482
|
+
this.lastError = null;
|
|
2483
|
+
}
|
|
2484
|
+
cleanup() {
|
|
2485
|
+
if (this.ready) {
|
|
2486
|
+
this.terminateWorker(this.ready.worker);
|
|
2487
|
+
this.ready = null;
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
terminateWorker(worker) {
|
|
2491
|
+
try {
|
|
2492
|
+
worker.terminate();
|
|
2493
|
+
} catch {}
|
|
2494
|
+
}
|
|
2495
|
+
};
|
|
2496
|
+
//#endregion
|
|
2497
|
+
//#region src/design-system/canonicalize-service.ts
|
|
2498
|
+
/**
|
|
2499
|
+
* Persistent canonicalize service backed by `DesignSystemWorker`.
|
|
2059
2500
|
*
|
|
2060
|
-
*
|
|
2061
|
-
*
|
|
2062
|
-
*
|
|
2501
|
+
* Calls `ds.canonicalizeCandidates([cls], { rem })` one class at a time
|
|
2502
|
+
* (the design system deduplicates its input, so batching changes the
|
|
2503
|
+
* result shape — see sync-loader.ts). The shared protocol lives in
|
|
2504
|
+
* `./ds-worker.ts`.
|
|
2063
2505
|
*
|
|
2064
|
-
* ## Per-class
|
|
2506
|
+
* ## Per-class cache (process-wide)
|
|
2065
2507
|
*
|
|
2066
|
-
*
|
|
2067
|
-
*
|
|
2068
|
-
*
|
|
2508
|
+
* Results are cached by `${cssPath}\0${rem}\0${class}`. The worker is
|
|
2509
|
+
* invoked only for cache misses. In practice the cache converges quickly —
|
|
2510
|
+
* after the first few files any given class has been seen, and every
|
|
2511
|
+
* subsequent lookup is O(1).
|
|
2069
2512
|
*/
|
|
2070
|
-
const
|
|
2071
|
-
|
|
2072
|
-
const DATA_OFFSET$1 = HEADER_INTS$1 * 4 + 4;
|
|
2073
|
-
const INIT_TIMEOUT$1 = 3e4;
|
|
2074
|
-
const REQUEST_TIMEOUT$1 = 1e4;
|
|
2075
|
-
const WORKER_SCRIPT$1 = `
|
|
2513
|
+
const canonWorker = new DesignSystemWorker({
|
|
2514
|
+
workerScript: `
|
|
2076
2515
|
const { workerData } = require('worker_threads');
|
|
2077
2516
|
|
|
2078
2517
|
async function main() {
|
|
2079
2518
|
const { sharedBuffer, cssPath } = workerData;
|
|
2080
|
-
const control = new Int32Array(sharedBuffer, 0,
|
|
2081
|
-
const lengthView = new DataView(sharedBuffer,
|
|
2082
|
-
const dataArea = new Uint8Array(sharedBuffer,
|
|
2519
|
+
const control = new Int32Array(sharedBuffer, 0, 4);
|
|
2520
|
+
const lengthView = new DataView(sharedBuffer, 4 * 4, 4);
|
|
2521
|
+
const dataArea = new Uint8Array(sharedBuffer, 4 * 4 + 4);
|
|
2083
2522
|
|
|
2084
2523
|
let ds;
|
|
2085
2524
|
try {
|
|
@@ -2094,14 +2533,11 @@ async function main() {
|
|
|
2094
2533
|
return;
|
|
2095
2534
|
}
|
|
2096
2535
|
|
|
2097
|
-
// Signal ready
|
|
2098
2536
|
Atomics.store(control, 2, 1);
|
|
2099
2537
|
Atomics.notify(control, 2);
|
|
2100
2538
|
|
|
2101
|
-
// Request loop
|
|
2102
2539
|
while (true) {
|
|
2103
2540
|
Atomics.wait(control, 0, 0);
|
|
2104
|
-
|
|
2105
2541
|
const len = lengthView.getUint32(0);
|
|
2106
2542
|
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
2107
2543
|
Atomics.store(control, 0, 0);
|
|
@@ -2123,117 +2559,32 @@ async function main() {
|
|
|
2123
2559
|
|
|
2124
2560
|
dataArea.set(response, 0);
|
|
2125
2561
|
lengthView.setUint32(0, response.length);
|
|
2126
|
-
|
|
2127
2562
|
Atomics.store(control, 1, 1);
|
|
2128
2563
|
Atomics.notify(control, 1);
|
|
2129
2564
|
}
|
|
2130
2565
|
}
|
|
2131
2566
|
main();
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
let lengthView$1 = null;
|
|
2136
|
-
let dataArea$1 = null;
|
|
2137
|
-
let initialized$1 = false;
|
|
2138
|
-
let available$1 = true;
|
|
2139
|
-
let currentCssPath$1 = null;
|
|
2140
|
-
const canonCache = /* @__PURE__ */ new Map();
|
|
2141
|
-
function ensureService$1(cssPath) {
|
|
2142
|
-
if (initialized$1 && currentCssPath$1 === cssPath) return available$1;
|
|
2143
|
-
if (initialized$1) {
|
|
2144
|
-
cleanup$1();
|
|
2145
|
-
initialized$1 = false;
|
|
2146
|
-
available$1 = true;
|
|
2147
|
-
controlArray$1 = null;
|
|
2148
|
-
lengthView$1 = null;
|
|
2149
|
-
dataArea$1 = null;
|
|
2150
|
-
}
|
|
2151
|
-
initialized$1 = true;
|
|
2152
|
-
currentCssPath$1 = cssPath;
|
|
2153
|
-
try {
|
|
2154
|
-
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
2155
|
-
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE$1);
|
|
2156
|
-
controlArray$1 = new Int32Array(sharedBuffer, 0, HEADER_INTS$1);
|
|
2157
|
-
lengthView$1 = new DataView(sharedBuffer, HEADER_INTS$1 * 4, 4);
|
|
2158
|
-
dataArea$1 = new Uint8Array(sharedBuffer, DATA_OFFSET$1);
|
|
2159
|
-
worker$1 = new Worker(WORKER_SCRIPT$1, {
|
|
2160
|
-
eval: true,
|
|
2161
|
-
workerData: {
|
|
2162
|
-
sharedBuffer,
|
|
2163
|
-
cssPath,
|
|
2164
|
-
tailwindNodePath
|
|
2165
|
-
}
|
|
2166
|
-
});
|
|
2167
|
-
worker$1.unref();
|
|
2168
|
-
worker$1.on("error", () => {
|
|
2169
|
-
available$1 = false;
|
|
2170
|
-
worker$1 = null;
|
|
2171
|
-
});
|
|
2172
|
-
worker$1.on("exit", () => {
|
|
2173
|
-
worker$1 = null;
|
|
2174
|
-
});
|
|
2175
|
-
if (Atomics.wait(controlArray$1, 2, 0, INIT_TIMEOUT$1) === "timed-out" || controlArray$1[2] === -1) {
|
|
2176
|
-
available$1 = false;
|
|
2177
|
-
cleanup$1();
|
|
2178
|
-
return false;
|
|
2179
|
-
}
|
|
2180
|
-
return true;
|
|
2181
|
-
} catch {
|
|
2182
|
-
available$1 = false;
|
|
2183
|
-
cleanup$1();
|
|
2184
|
-
return false;
|
|
2185
|
-
}
|
|
2186
|
-
}
|
|
2187
|
-
function cleanup$1() {
|
|
2188
|
-
if (worker$1) {
|
|
2189
|
-
try {
|
|
2190
|
-
worker$1.terminate();
|
|
2191
|
-
} catch {}
|
|
2192
|
-
worker$1 = null;
|
|
2193
|
-
}
|
|
2194
|
-
}
|
|
2567
|
+
`,
|
|
2568
|
+
serviceName: "canonicalize"
|
|
2569
|
+
});
|
|
2195
2570
|
/**
|
|
2196
|
-
*
|
|
2197
|
-
*
|
|
2198
|
-
*
|
|
2571
|
+
* Process-wide cache of canonicalized classes.
|
|
2572
|
+
* Key: `${cssPath}\0${rem}\0${className}` — isolates monorepos (multiple
|
|
2573
|
+
* DSs) and different rem settings. Value: canonicalized class string.
|
|
2199
2574
|
*/
|
|
2200
|
-
|
|
2201
|
-
if (!controlArray$1 || !dataArea$1 || !lengthView$1) return null;
|
|
2202
|
-
try {
|
|
2203
|
-
const request = Buffer.from(JSON.stringify({
|
|
2204
|
-
classes,
|
|
2205
|
-
rem
|
|
2206
|
-
}), "utf-8");
|
|
2207
|
-
if (request.length > BUFFER_SIZE$1 - DATA_OFFSET$1) return null;
|
|
2208
|
-
dataArea$1.set(request, 0);
|
|
2209
|
-
lengthView$1.setUint32(0, request.length);
|
|
2210
|
-
Atomics.store(controlArray$1, 0, 1);
|
|
2211
|
-
Atomics.notify(controlArray$1, 0);
|
|
2212
|
-
if (Atomics.wait(controlArray$1, 1, 0, REQUEST_TIMEOUT$1) === "timed-out") {
|
|
2213
|
-
available$1 = false;
|
|
2214
|
-
cleanup$1();
|
|
2215
|
-
return null;
|
|
2216
|
-
}
|
|
2217
|
-
const responseLen = lengthView$1.getUint32(0);
|
|
2218
|
-
const responseStr = Buffer.from(dataArea$1.slice(0, responseLen)).toString("utf-8");
|
|
2219
|
-
Atomics.store(controlArray$1, 1, 0);
|
|
2220
|
-
return JSON.parse(responseStr);
|
|
2221
|
-
} catch {
|
|
2222
|
-
available$1 = false;
|
|
2223
|
-
cleanup$1();
|
|
2224
|
-
return null;
|
|
2225
|
-
}
|
|
2226
|
-
}
|
|
2575
|
+
const canonCache = /* @__PURE__ */ new Map();
|
|
2227
2576
|
/**
|
|
2228
|
-
* Canonicalize classes
|
|
2229
|
-
*
|
|
2230
|
-
*
|
|
2577
|
+
* Canonicalize classes via the worker thread.
|
|
2578
|
+
*
|
|
2579
|
+
* Throws `SortServiceError` on any worker failure (init timeout, request
|
|
2580
|
+
* timeout, worker crash). Callers should wrap via `safeGetDS` so the
|
|
2581
|
+
* failure surfaces as a single `designSystemUnavailable` diagnostic.
|
|
2231
2582
|
*
|
|
2232
|
-
*
|
|
2583
|
+
* Returns an array the same length and order as `classes`. Uses a
|
|
2584
|
+
* process-wide per-class cache: the worker is invoked only for classes
|
|
2233
2585
|
* not already seen with this (cssPath, rem) combination.
|
|
2234
2586
|
*/
|
|
2235
2587
|
function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
2236
|
-
if (!ensureService$1(cssPath)) return null;
|
|
2237
2588
|
const out = Array.from({ length: classes.length });
|
|
2238
2589
|
const missingIdx = [];
|
|
2239
2590
|
const missing = [];
|
|
@@ -2249,8 +2600,11 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
|
2249
2600
|
}
|
|
2250
2601
|
if (missing.length === 0) return out;
|
|
2251
2602
|
const uniqueMissing = [...new Set(missing)];
|
|
2252
|
-
const fresh =
|
|
2253
|
-
|
|
2603
|
+
const fresh = canonWorker.callSync(cssPath, {
|
|
2604
|
+
classes: uniqueMissing,
|
|
2605
|
+
rem
|
|
2606
|
+
});
|
|
2607
|
+
if (fresh.length !== uniqueMissing.length) throw new SortServiceError(`Canonicalize worker returned ${fresh.length} results for ${uniqueMissing.length} inputs.`, "This is a bug; please open an issue.");
|
|
2254
2608
|
const freshByClass = /* @__PURE__ */ new Map();
|
|
2255
2609
|
for (let k = 0; k < uniqueMissing.length; k++) freshByClass.set(uniqueMissing[k], fresh[k]);
|
|
2256
2610
|
for (let j = 0; j < missing.length; j++) {
|
|
@@ -2264,20 +2618,20 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
|
2264
2618
|
//#endregion
|
|
2265
2619
|
//#region src/rules/enforce-canonical.ts
|
|
2266
2620
|
/**
|
|
2267
|
-
* Preserve the user's
|
|
2268
|
-
*
|
|
2269
|
-
*
|
|
2621
|
+
* Preserve the user's `!` position after canonicalization.
|
|
2622
|
+
*
|
|
2623
|
+
* `canonicalizeCandidates` normalizes `!` to suffix; this restores the
|
|
2624
|
+
* original prefix-or-suffix-or-none position so `enforce-consistent-important-position`
|
|
2625
|
+
* remains the single source of truth for ! placement policy.
|
|
2626
|
+
*
|
|
2627
|
+
* Bracket-aware via `splitUtilityAndVariant` — arbitrary variants like
|
|
2628
|
+
* `[&>svg]:!w-4` round-trip correctly.
|
|
2270
2629
|
*/
|
|
2271
2630
|
function preserveImportantPosition(original, canonicalized) {
|
|
2272
|
-
const
|
|
2273
|
-
const
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
if (origHasPrefix) {
|
|
2277
|
-
const variantPrefix = bare.slice(0, bare.length - bare.replace(/^[a-z0-9[\]*@-]*:/g, "").length);
|
|
2278
|
-
return variantPrefix + "!" + bare.slice(variantPrefix.length);
|
|
2279
|
-
}
|
|
2280
|
-
return bare + "!";
|
|
2631
|
+
const { position } = splitImportant(splitUtilityAndVariant(original).utility);
|
|
2632
|
+
const { utility, variant } = splitUtilityAndVariant(canonicalized);
|
|
2633
|
+
const bare = splitImportant(utility).bare;
|
|
2634
|
+
return variant + reattachImportant(bare, position);
|
|
2281
2635
|
}
|
|
2282
2636
|
const enforceCanonical = defineRule({
|
|
2283
2637
|
meta: {
|
|
@@ -2290,9 +2644,11 @@ const enforceCanonical = defineRule({
|
|
|
2290
2644
|
additionalProperties: false
|
|
2291
2645
|
}],
|
|
2292
2646
|
hasSuggestions: true,
|
|
2647
|
+
defaultOptions: [{}],
|
|
2293
2648
|
messages: {
|
|
2294
2649
|
nonCanonical: "\"{{className}}\" can be written as \"{{canonical}}\". Use the canonical form.",
|
|
2295
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2650
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
2651
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
2296
2652
|
}
|
|
2297
2653
|
},
|
|
2298
2654
|
createOnce(context) {
|
|
@@ -2303,7 +2659,8 @@ const enforceCanonical = defineRule({
|
|
|
2303
2659
|
return _rem;
|
|
2304
2660
|
}
|
|
2305
2661
|
function check(locations) {
|
|
2306
|
-
|
|
2662
|
+
if (locations.length === 0) return;
|
|
2663
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
2307
2664
|
if (!ds) return;
|
|
2308
2665
|
const { cache, entryPoint } = ds;
|
|
2309
2666
|
for (const loc of locations) {
|
|
@@ -2317,51 +2674,18 @@ const enforceCanonical = defineRule({
|
|
|
2317
2674
|
arbitrary.push(classes[i]);
|
|
2318
2675
|
} else canonicals[i] = cache.canonicalize(classes[i]);
|
|
2319
2676
|
if (arbitrary.length > 0) {
|
|
2320
|
-
const
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
let firstNonCanonical = true;
|
|
2325
|
-
for (let i = 0; i < classes.length; i++) {
|
|
2326
|
-
const cls = classes[i];
|
|
2327
|
-
const canonical = canonicals[i];
|
|
2328
|
-
if (canonical === cls) continue;
|
|
2329
|
-
if (firstNonCanonical) {
|
|
2330
|
-
firstNonCanonical = false;
|
|
2331
|
-
const fixedValue = rebuildClassString(split, canonicals);
|
|
2332
|
-
context.report({
|
|
2333
|
-
node: loc.node,
|
|
2334
|
-
messageId: "nonCanonical",
|
|
2335
|
-
data: {
|
|
2336
|
-
className: cls,
|
|
2337
|
-
canonical
|
|
2338
|
-
},
|
|
2339
|
-
fix(fixer) {
|
|
2340
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2341
|
-
}
|
|
2342
|
-
});
|
|
2343
|
-
} else {
|
|
2344
|
-
const fixedValue = rebuildClassString(split, canonicals);
|
|
2345
|
-
context.report({
|
|
2346
|
-
node: loc.node,
|
|
2347
|
-
messageId: "nonCanonical",
|
|
2348
|
-
data: {
|
|
2349
|
-
className: cls,
|
|
2350
|
-
canonical
|
|
2351
|
-
},
|
|
2352
|
-
suggest: [{
|
|
2353
|
-
messageId: "suggestReplace",
|
|
2354
|
-
data: {
|
|
2355
|
-
className: cls,
|
|
2356
|
-
replacement: canonical
|
|
2357
|
-
},
|
|
2358
|
-
fix(fixer) {
|
|
2359
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2360
|
-
}
|
|
2361
|
-
}]
|
|
2362
|
-
});
|
|
2363
|
-
}
|
|
2677
|
+
const rem = getRem();
|
|
2678
|
+
const dynamic = safeGetDS(() => canonicalizeClassesSync(entryPoint, arbitrary, rem), context, loc.node);
|
|
2679
|
+
if (!dynamic) return;
|
|
2680
|
+
for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = preserveImportantPosition(arbitrary[k], dynamic[k]);
|
|
2364
2681
|
}
|
|
2682
|
+
reportClassReplacements(context, loc, split, classes, classes.flatMap((cls, i) => canonicals[i] === cls ? [] : [{
|
|
2683
|
+
cls,
|
|
2684
|
+
replacement: canonicals[i]
|
|
2685
|
+
}]), {
|
|
2686
|
+
messageId: "nonCanonical",
|
|
2687
|
+
replacementKey: "canonical"
|
|
2688
|
+
});
|
|
2365
2689
|
}
|
|
2366
2690
|
}
|
|
2367
2691
|
return createExtractorVisitors(context, check);
|
|
@@ -2370,31 +2694,21 @@ const enforceCanonical = defineRule({
|
|
|
2370
2694
|
//#endregion
|
|
2371
2695
|
//#region src/design-system/sort-service.ts
|
|
2372
2696
|
/**
|
|
2373
|
-
* Persistent sort service
|
|
2374
|
-
*
|
|
2375
|
-
* The problem: `ds.getClassOrder()` is the only way to get the exact official
|
|
2376
|
-
* Tailwind CSS sort order (matching oxfmt/prettier-plugin-tailwindcss), but the
|
|
2377
|
-
* design system is async to load and the oxlint plugin API is sync.
|
|
2378
|
-
*
|
|
2379
|
-
* The solution: spawn a Worker thread that loads the DS once, then accepts sort
|
|
2380
|
-
* requests via SharedArrayBuffer + Atomics. The parent uses `Atomics.wait()`
|
|
2381
|
-
* (with timeout) for synchronous blocking without risk of infinite hangs.
|
|
2697
|
+
* Persistent sort service backed by `DesignSystemWorker`.
|
|
2382
2698
|
*
|
|
2383
|
-
*
|
|
2699
|
+
* Calls `ds.getClassOrder(classes)` and returns the class names in the
|
|
2700
|
+
* Tailwind-canonical order. The shared protocol (SharedArrayBuffer,
|
|
2701
|
+
* Atomics.wait, fail-loud, sticky lastError) lives in `./ds-worker.ts`.
|
|
2384
2702
|
*/
|
|
2385
|
-
const
|
|
2386
|
-
|
|
2387
|
-
const DATA_OFFSET = HEADER_INTS * 4 + 4;
|
|
2388
|
-
const INIT_TIMEOUT = 3e4;
|
|
2389
|
-
const REQUEST_TIMEOUT = 1e4;
|
|
2390
|
-
const WORKER_SCRIPT = `
|
|
2703
|
+
const sortWorker = new DesignSystemWorker({
|
|
2704
|
+
workerScript: `
|
|
2391
2705
|
const { workerData } = require('worker_threads');
|
|
2392
2706
|
|
|
2393
2707
|
async function main() {
|
|
2394
2708
|
const { sharedBuffer, cssPath } = workerData;
|
|
2395
|
-
const control = new Int32Array(sharedBuffer, 0,
|
|
2396
|
-
const lengthView = new DataView(sharedBuffer,
|
|
2397
|
-
const dataArea = new Uint8Array(sharedBuffer,
|
|
2709
|
+
const control = new Int32Array(sharedBuffer, 0, 4);
|
|
2710
|
+
const lengthView = new DataView(sharedBuffer, 4 * 4, 4);
|
|
2711
|
+
const dataArea = new Uint8Array(sharedBuffer, 4 * 4 + 4);
|
|
2398
2712
|
|
|
2399
2713
|
let ds;
|
|
2400
2714
|
try {
|
|
@@ -2409,17 +2723,14 @@ async function main() {
|
|
|
2409
2723
|
return;
|
|
2410
2724
|
}
|
|
2411
2725
|
|
|
2412
|
-
// Signal ready
|
|
2413
2726
|
Atomics.store(control, 2, 1);
|
|
2414
2727
|
Atomics.notify(control, 2);
|
|
2415
2728
|
|
|
2416
|
-
// Request loop
|
|
2417
2729
|
while (true) {
|
|
2418
|
-
Atomics.wait(control, 0, 0);
|
|
2419
|
-
|
|
2730
|
+
Atomics.wait(control, 0, 0);
|
|
2420
2731
|
const len = lengthView.getUint32(0);
|
|
2421
2732
|
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
2422
|
-
Atomics.store(control, 0, 0);
|
|
2733
|
+
Atomics.store(control, 0, 0);
|
|
2423
2734
|
|
|
2424
2735
|
let response;
|
|
2425
2736
|
try {
|
|
@@ -2440,102 +2751,24 @@ async function main() {
|
|
|
2440
2751
|
|
|
2441
2752
|
dataArea.set(response, 0);
|
|
2442
2753
|
lengthView.setUint32(0, response.length);
|
|
2443
|
-
|
|
2444
|
-
Atomics.store(control, 1, 1); // signal response
|
|
2754
|
+
Atomics.store(control, 1, 1);
|
|
2445
2755
|
Atomics.notify(control, 1);
|
|
2446
2756
|
}
|
|
2447
2757
|
}
|
|
2448
2758
|
main();
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
let lengthView = null;
|
|
2453
|
-
let dataArea = null;
|
|
2454
|
-
let initialized = false;
|
|
2455
|
-
let available = true;
|
|
2456
|
-
let currentCssPath = null;
|
|
2457
|
-
function ensureService(cssPath) {
|
|
2458
|
-
if (initialized && currentCssPath === cssPath) return available;
|
|
2459
|
-
if (initialized) {
|
|
2460
|
-
cleanup();
|
|
2461
|
-
initialized = false;
|
|
2462
|
-
available = true;
|
|
2463
|
-
controlArray = null;
|
|
2464
|
-
lengthView = null;
|
|
2465
|
-
dataArea = null;
|
|
2466
|
-
}
|
|
2467
|
-
initialized = true;
|
|
2468
|
-
currentCssPath = cssPath;
|
|
2469
|
-
try {
|
|
2470
|
-
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
2471
|
-
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);
|
|
2472
|
-
controlArray = new Int32Array(sharedBuffer, 0, HEADER_INTS);
|
|
2473
|
-
lengthView = new DataView(sharedBuffer, HEADER_INTS * 4, 4);
|
|
2474
|
-
dataArea = new Uint8Array(sharedBuffer, DATA_OFFSET);
|
|
2475
|
-
worker = new Worker(WORKER_SCRIPT, {
|
|
2476
|
-
eval: true,
|
|
2477
|
-
workerData: {
|
|
2478
|
-
sharedBuffer,
|
|
2479
|
-
cssPath,
|
|
2480
|
-
tailwindNodePath
|
|
2481
|
-
}
|
|
2482
|
-
});
|
|
2483
|
-
worker.unref();
|
|
2484
|
-
worker.on("error", () => {
|
|
2485
|
-
available = false;
|
|
2486
|
-
worker = null;
|
|
2487
|
-
});
|
|
2488
|
-
worker.on("exit", () => {
|
|
2489
|
-
worker = null;
|
|
2490
|
-
});
|
|
2491
|
-
if (Atomics.wait(controlArray, 2, 0, INIT_TIMEOUT) === "timed-out" || controlArray[2] === -1) {
|
|
2492
|
-
available = false;
|
|
2493
|
-
cleanup();
|
|
2494
|
-
return false;
|
|
2495
|
-
}
|
|
2496
|
-
return true;
|
|
2497
|
-
} catch {
|
|
2498
|
-
available = false;
|
|
2499
|
-
cleanup();
|
|
2500
|
-
return false;
|
|
2501
|
-
}
|
|
2502
|
-
}
|
|
2503
|
-
function cleanup() {
|
|
2504
|
-
if (worker) {
|
|
2505
|
-
try {
|
|
2506
|
-
worker.terminate();
|
|
2507
|
-
} catch {}
|
|
2508
|
-
worker = null;
|
|
2509
|
-
}
|
|
2510
|
-
}
|
|
2759
|
+
`,
|
|
2760
|
+
serviceName: "sort"
|
|
2761
|
+
});
|
|
2511
2762
|
/**
|
|
2512
|
-
* Sort classes using the official Tailwind
|
|
2513
|
-
*
|
|
2763
|
+
* Sort classes via worker thread using the official Tailwind class order.
|
|
2764
|
+
*
|
|
2765
|
+
* Throws `SortServiceError` on any failure (worker init timeout, request
|
|
2766
|
+
* timeout, worker crash, payload too large). Callers should wrap via
|
|
2767
|
+
* `safeGetDS` so the failure surfaces as a single `designSystemUnavailable`
|
|
2768
|
+
* diagnostic instead of crashing the lint.
|
|
2514
2769
|
*/
|
|
2515
2770
|
function sortClassesSync(cssPath, classes) {
|
|
2516
|
-
|
|
2517
|
-
if (!controlArray || !dataArea || !lengthView) return null;
|
|
2518
|
-
try {
|
|
2519
|
-
const request = Buffer.from(JSON.stringify(classes), "utf-8");
|
|
2520
|
-
if (request.length > BUFFER_SIZE - DATA_OFFSET) return null;
|
|
2521
|
-
dataArea.set(request, 0);
|
|
2522
|
-
lengthView.setUint32(0, request.length);
|
|
2523
|
-
Atomics.store(controlArray, 0, 1);
|
|
2524
|
-
Atomics.notify(controlArray, 0);
|
|
2525
|
-
if (Atomics.wait(controlArray, 1, 0, REQUEST_TIMEOUT) === "timed-out") {
|
|
2526
|
-
available = false;
|
|
2527
|
-
cleanup();
|
|
2528
|
-
return null;
|
|
2529
|
-
}
|
|
2530
|
-
const responseLen = lengthView.getUint32(0);
|
|
2531
|
-
const responseStr = Buffer.from(dataArea.slice(0, responseLen)).toString("utf-8");
|
|
2532
|
-
Atomics.store(controlArray, 1, 0);
|
|
2533
|
-
return JSON.parse(responseStr);
|
|
2534
|
-
} catch {
|
|
2535
|
-
available = false;
|
|
2536
|
-
cleanup();
|
|
2537
|
-
return null;
|
|
2538
|
-
}
|
|
2771
|
+
return sortWorker.callSync(cssPath, classes);
|
|
2539
2772
|
}
|
|
2540
2773
|
//#endregion
|
|
2541
2774
|
//#region src/rules/enforce-sort-order.ts
|
|
@@ -2555,31 +2788,23 @@ const enforceSortOrder = defineRule({
|
|
|
2555
2788
|
},
|
|
2556
2789
|
additionalProperties: false
|
|
2557
2790
|
}],
|
|
2558
|
-
|
|
2791
|
+
defaultOptions: [{ mode: "default" }],
|
|
2792
|
+
messages: {
|
|
2793
|
+
unsorted: "Tailwind classes are not in the recommended order.",
|
|
2794
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
2795
|
+
}
|
|
2559
2796
|
},
|
|
2560
2797
|
createOnce(context) {
|
|
2561
2798
|
const getDS = createLazyLoader(context);
|
|
2562
|
-
|
|
2563
|
-
function getMode() {
|
|
2564
|
-
if (_mode === null) _mode = safeOptions(context)?.mode ?? "default";
|
|
2565
|
-
return _mode;
|
|
2566
|
-
}
|
|
2799
|
+
const getMode = createLazyOptions(context, (o) => o?.mode ?? "default");
|
|
2567
2800
|
function check(locations) {
|
|
2568
|
-
|
|
2801
|
+
if (locations.length === 0) return;
|
|
2802
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
2569
2803
|
if (!ds) return;
|
|
2570
2804
|
const { cache, entryPoint } = ds;
|
|
2571
2805
|
const mode = getMode();
|
|
2572
2806
|
function sortDefault(classes) {
|
|
2573
|
-
|
|
2574
|
-
if (dynamic) return dynamic;
|
|
2575
|
-
return [...cache.getClassOrder(classes)].sort((a, b) => {
|
|
2576
|
-
if (a[1] === null && b[1] === null) return 0;
|
|
2577
|
-
if (a[1] === null) return -1;
|
|
2578
|
-
if (b[1] === null) return 1;
|
|
2579
|
-
if (a[1] < b[1]) return -1;
|
|
2580
|
-
if (a[1] > b[1]) return 1;
|
|
2581
|
-
return 0;
|
|
2582
|
-
}).map(([name]) => name);
|
|
2807
|
+
return sortClassesSync(entryPoint, classes);
|
|
2583
2808
|
}
|
|
2584
2809
|
function sortStrict(classes) {
|
|
2585
2810
|
const groups = /* @__PURE__ */ new Map();
|
|
@@ -2623,7 +2848,8 @@ const enforceSortOrder = defineRule({
|
|
|
2623
2848
|
const split = splitClassesWithSeparators(loc.value);
|
|
2624
2849
|
const classes = split.classes;
|
|
2625
2850
|
if (classes.length < 2) continue;
|
|
2626
|
-
const sortedNames = mode === "strict" ? sortStrict(classes) : sortDefault(classes);
|
|
2851
|
+
const sortedNames = mode === "strict" ? sortStrict(classes) : safeGetDS(() => sortDefault(classes), context, loc.node);
|
|
2852
|
+
if (!sortedNames) return;
|
|
2627
2853
|
if (classes.every((name, i) => name === sortedNames[i])) continue;
|
|
2628
2854
|
context.report({
|
|
2629
2855
|
node: loc.node,
|
|
@@ -2721,8 +2947,7 @@ const enforceShorthand = defineRule({
|
|
|
2721
2947
|
const classSet = new Set(classes);
|
|
2722
2948
|
const values = /* @__PURE__ */ new Set();
|
|
2723
2949
|
for (const cls of classes) {
|
|
2724
|
-
const
|
|
2725
|
-
const match = VALUE_RE.exec(bare);
|
|
2950
|
+
const match = VALUE_RE.exec(splitImportant(cls).bare);
|
|
2726
2951
|
if (match) values.add(match[1]);
|
|
2727
2952
|
}
|
|
2728
2953
|
for (const value of values) {
|
|
@@ -2756,180 +2981,234 @@ const enforceShorthand = defineRule({
|
|
|
2756
2981
|
}
|
|
2757
2982
|
});
|
|
2758
2983
|
//#endregion
|
|
2984
|
+
//#region src/utils/allowlist.ts
|
|
2985
|
+
/**
|
|
2986
|
+
* Shared regex-allowlist helpers used by rules that accept an `allowlist`
|
|
2987
|
+
* option of regex source strings (currently enforce-logical / enforce-physical).
|
|
2988
|
+
*
|
|
2989
|
+
* Invalid regex sources are skipped rather than thrown so a typo in one
|
|
2990
|
+
* pattern doesn't blow up the entire lint run.
|
|
2991
|
+
*/
|
|
2992
|
+
function compileRegexList(patterns) {
|
|
2993
|
+
if (!patterns || patterns.length === 0) return [];
|
|
2994
|
+
const compiled = [];
|
|
2995
|
+
for (const p of patterns) try {
|
|
2996
|
+
compiled.push(new RegExp(p));
|
|
2997
|
+
} catch {}
|
|
2998
|
+
return compiled;
|
|
2999
|
+
}
|
|
3000
|
+
function matchesAny(value, list) {
|
|
3001
|
+
return list.some((re) => re.test(value));
|
|
3002
|
+
}
|
|
3003
|
+
//#endregion
|
|
2759
3004
|
//#region src/rules/enforce-logical.ts
|
|
2760
|
-
const
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
"scroll-pl": "scroll-ps",
|
|
2778
|
-
"scroll-pr": "scroll-pe"
|
|
3005
|
+
const LOGICAL_PHYSICAL_SCHEMA = {
|
|
3006
|
+
type: "object",
|
|
3007
|
+
properties: {
|
|
3008
|
+
allowlist: {
|
|
3009
|
+
type: "array",
|
|
3010
|
+
items: { type: "string" }
|
|
3011
|
+
},
|
|
3012
|
+
direction: {
|
|
3013
|
+
type: "string",
|
|
3014
|
+
enum: [
|
|
3015
|
+
"inline",
|
|
3016
|
+
"block",
|
|
3017
|
+
"both"
|
|
3018
|
+
]
|
|
3019
|
+
}
|
|
3020
|
+
},
|
|
3021
|
+
additionalProperties: false
|
|
2779
3022
|
};
|
|
3023
|
+
const PHYSICAL_TO_LOGICAL_MAPPINGS = [
|
|
3024
|
+
{
|
|
3025
|
+
from: "ml",
|
|
3026
|
+
to: "ms",
|
|
3027
|
+
axis: "inline"
|
|
3028
|
+
},
|
|
3029
|
+
{
|
|
3030
|
+
from: "mr",
|
|
3031
|
+
to: "me",
|
|
3032
|
+
axis: "inline"
|
|
3033
|
+
},
|
|
3034
|
+
{
|
|
3035
|
+
from: "pl",
|
|
3036
|
+
to: "ps",
|
|
3037
|
+
axis: "inline"
|
|
3038
|
+
},
|
|
3039
|
+
{
|
|
3040
|
+
from: "pr",
|
|
3041
|
+
to: "pe",
|
|
3042
|
+
axis: "inline"
|
|
3043
|
+
},
|
|
3044
|
+
{
|
|
3045
|
+
from: "left",
|
|
3046
|
+
to: "start",
|
|
3047
|
+
axis: "inline"
|
|
3048
|
+
},
|
|
3049
|
+
{
|
|
3050
|
+
from: "right",
|
|
3051
|
+
to: "end",
|
|
3052
|
+
axis: "inline"
|
|
3053
|
+
},
|
|
3054
|
+
{
|
|
3055
|
+
from: "border-l",
|
|
3056
|
+
to: "border-s",
|
|
3057
|
+
axis: "inline"
|
|
3058
|
+
},
|
|
3059
|
+
{
|
|
3060
|
+
from: "border-r",
|
|
3061
|
+
to: "border-e",
|
|
3062
|
+
axis: "inline"
|
|
3063
|
+
},
|
|
3064
|
+
{
|
|
3065
|
+
from: "rounded-l",
|
|
3066
|
+
to: "rounded-s",
|
|
3067
|
+
axis: "inline"
|
|
3068
|
+
},
|
|
3069
|
+
{
|
|
3070
|
+
from: "rounded-r",
|
|
3071
|
+
to: "rounded-e",
|
|
3072
|
+
axis: "inline"
|
|
3073
|
+
},
|
|
3074
|
+
{
|
|
3075
|
+
from: "rounded-tl",
|
|
3076
|
+
to: "rounded-ss",
|
|
3077
|
+
axis: "inline"
|
|
3078
|
+
},
|
|
3079
|
+
{
|
|
3080
|
+
from: "rounded-tr",
|
|
3081
|
+
to: "rounded-se",
|
|
3082
|
+
axis: "inline"
|
|
3083
|
+
},
|
|
3084
|
+
{
|
|
3085
|
+
from: "rounded-bl",
|
|
3086
|
+
to: "rounded-es",
|
|
3087
|
+
axis: "inline"
|
|
3088
|
+
},
|
|
3089
|
+
{
|
|
3090
|
+
from: "rounded-br",
|
|
3091
|
+
to: "rounded-ee",
|
|
3092
|
+
axis: "inline"
|
|
3093
|
+
},
|
|
3094
|
+
{
|
|
3095
|
+
from: "scroll-ml",
|
|
3096
|
+
to: "scroll-ms",
|
|
3097
|
+
axis: "inline"
|
|
3098
|
+
},
|
|
3099
|
+
{
|
|
3100
|
+
from: "scroll-mr",
|
|
3101
|
+
to: "scroll-me",
|
|
3102
|
+
axis: "inline"
|
|
3103
|
+
},
|
|
3104
|
+
{
|
|
3105
|
+
from: "scroll-pl",
|
|
3106
|
+
to: "scroll-ps",
|
|
3107
|
+
axis: "inline"
|
|
3108
|
+
},
|
|
3109
|
+
{
|
|
3110
|
+
from: "scroll-pr",
|
|
3111
|
+
to: "scroll-pe",
|
|
3112
|
+
axis: "inline"
|
|
3113
|
+
}
|
|
3114
|
+
];
|
|
3115
|
+
/** Invert a directional table. enforce-physical consumes this. */
|
|
3116
|
+
function invertAxisMappings(mappings) {
|
|
3117
|
+
return mappings.map((m) => ({
|
|
3118
|
+
from: m.to,
|
|
3119
|
+
to: m.from,
|
|
3120
|
+
axis: m.axis
|
|
3121
|
+
}));
|
|
3122
|
+
}
|
|
3123
|
+
Object.fromEntries(PHYSICAL_TO_LOGICAL_MAPPINGS.map((m) => [m.from, m.to]));
|
|
3124
|
+
/**
|
|
3125
|
+
* Build the `check` callback for a directional-rewrite rule. Both
|
|
3126
|
+
* enforce-logical and enforce-physical instantiate this with their own
|
|
3127
|
+
* mapping table and messageId; the rule's `createOnce` wires it through
|
|
3128
|
+
* `createExtractorVisitors`.
|
|
3129
|
+
*
|
|
3130
|
+
* Owns: option compilation (allowlist + direction), per-class conversion
|
|
3131
|
+
* (linear scan of the mapping table respecting `!`-prefix/suffix), and
|
|
3132
|
+
* the `reportClassReplacements` dispatch.
|
|
3133
|
+
*/
|
|
3134
|
+
function createDirectionalMapper(context, opts) {
|
|
3135
|
+
const getState = createLazyOptions(context, (o) => ({
|
|
3136
|
+
allowlist: compileRegexList(o?.allowlist),
|
|
3137
|
+
direction: o?.direction ?? "both"
|
|
3138
|
+
}));
|
|
3139
|
+
function convertClass(cls) {
|
|
3140
|
+
const { allowlist, direction } = getState();
|
|
3141
|
+
if (matchesAny(cls, allowlist)) return null;
|
|
3142
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3143
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3144
|
+
for (const { from, to, axis } of opts.mappings) {
|
|
3145
|
+
if (direction !== "both" && direction !== axis) continue;
|
|
3146
|
+
if (bareUtility === from || bareUtility.startsWith(`${from}-`)) return `${variant}${reattachImportant(to + bareUtility.slice(from.length), position)}`;
|
|
3147
|
+
}
|
|
3148
|
+
return null;
|
|
3149
|
+
}
|
|
3150
|
+
function check(locations) {
|
|
3151
|
+
for (const loc of locations) {
|
|
3152
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3153
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3154
|
+
const converted = convertClass(cls);
|
|
3155
|
+
return converted ? [{
|
|
3156
|
+
cls,
|
|
3157
|
+
replacement: converted
|
|
3158
|
+
}] : [];
|
|
3159
|
+
});
|
|
3160
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: opts.messageId });
|
|
3161
|
+
}
|
|
3162
|
+
}
|
|
3163
|
+
return { check };
|
|
3164
|
+
}
|
|
2780
3165
|
const enforceLogical = defineRule({
|
|
2781
3166
|
meta: {
|
|
2782
3167
|
type: "suggestion",
|
|
2783
3168
|
docs: { description: "Enforce logical (RTL-friendly) Tailwind CSS properties instead of physical ones" },
|
|
2784
3169
|
fixable: "code",
|
|
2785
|
-
schema: [],
|
|
3170
|
+
schema: [LOGICAL_PHYSICAL_SCHEMA],
|
|
2786
3171
|
hasSuggestions: true,
|
|
3172
|
+
defaultOptions: [{
|
|
3173
|
+
allowlist: [],
|
|
3174
|
+
direction: "both"
|
|
3175
|
+
}],
|
|
2787
3176
|
messages: {
|
|
2788
3177
|
useLogical: "\"{{className}}\" uses a physical property. Use \"{{replacement}}\" for LTR/RTL support.",
|
|
2789
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2790
|
-
}
|
|
2791
|
-
},
|
|
2792
|
-
createOnce(context) {
|
|
2793
|
-
function convertClass(cls) {
|
|
2794
|
-
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
2795
|
-
const hasImportantPrefix = utility.startsWith("!");
|
|
2796
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
2797
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
2798
|
-
for (const [physical, logical] of Object.entries(PHYSICAL_TO_LOGICAL)) if (bareUtility === physical || bareUtility.startsWith(`${physical}-`)) {
|
|
2799
|
-
const suffix = bareUtility.slice(physical.length);
|
|
2800
|
-
return `${variant}${hasImportantPrefix ? "!" : ""}${logical}${suffix}${hasImportantSuffix ? "!" : ""}`;
|
|
2801
|
-
}
|
|
2802
|
-
return null;
|
|
2803
|
-
}
|
|
2804
|
-
function check(locations) {
|
|
2805
|
-
for (const loc of locations) {
|
|
2806
|
-
const split = splitClassesWithSeparators(loc.value);
|
|
2807
|
-
const classes = split.classes;
|
|
2808
|
-
const offending = [];
|
|
2809
|
-
for (const cls of classes) {
|
|
2810
|
-
const converted = convertClass(cls);
|
|
2811
|
-
if (converted) offending.push({
|
|
2812
|
-
cls,
|
|
2813
|
-
replacement: converted
|
|
2814
|
-
});
|
|
2815
|
-
}
|
|
2816
|
-
if (offending.length === 0) continue;
|
|
2817
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2818
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2819
|
-
for (let i = 0; i < offending.length; i++) {
|
|
2820
|
-
const { cls, replacement } = offending[i];
|
|
2821
|
-
if (i === 0) context.report({
|
|
2822
|
-
node: loc.node,
|
|
2823
|
-
messageId: "useLogical",
|
|
2824
|
-
data: {
|
|
2825
|
-
className: cls,
|
|
2826
|
-
replacement
|
|
2827
|
-
},
|
|
2828
|
-
fix(fixer) {
|
|
2829
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2830
|
-
}
|
|
2831
|
-
});
|
|
2832
|
-
else context.report({
|
|
2833
|
-
node: loc.node,
|
|
2834
|
-
messageId: "useLogical",
|
|
2835
|
-
data: {
|
|
2836
|
-
className: cls,
|
|
2837
|
-
replacement
|
|
2838
|
-
},
|
|
2839
|
-
suggest: [{
|
|
2840
|
-
messageId: "suggestReplace",
|
|
2841
|
-
data: {
|
|
2842
|
-
className: cls,
|
|
2843
|
-
replacement
|
|
2844
|
-
},
|
|
2845
|
-
fix(fixer) {
|
|
2846
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2847
|
-
}
|
|
2848
|
-
}]
|
|
2849
|
-
});
|
|
2850
|
-
}
|
|
2851
|
-
}
|
|
3178
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2852
3179
|
}
|
|
3180
|
+
},
|
|
3181
|
+
createOnce(context) {
|
|
3182
|
+
const { check } = createDirectionalMapper(context, {
|
|
3183
|
+
mappings: PHYSICAL_TO_LOGICAL_MAPPINGS,
|
|
3184
|
+
messageId: "useLogical"
|
|
3185
|
+
});
|
|
2853
3186
|
return createExtractorVisitors(context, check);
|
|
2854
3187
|
}
|
|
2855
3188
|
});
|
|
2856
3189
|
//#endregion
|
|
2857
3190
|
//#region src/rules/enforce-physical.ts
|
|
2858
|
-
const LOGICAL_TO_PHYSICAL = {};
|
|
2859
|
-
for (const [physical, logical] of Object.entries(PHYSICAL_TO_LOGICAL)) LOGICAL_TO_PHYSICAL[logical] = physical;
|
|
2860
3191
|
const enforcePhysical = defineRule({
|
|
2861
3192
|
meta: {
|
|
2862
3193
|
type: "suggestion",
|
|
2863
3194
|
docs: { description: "Enforce physical Tailwind CSS properties instead of logical ones for consistency in LTR-only projects" },
|
|
2864
3195
|
fixable: "code",
|
|
2865
|
-
schema: [],
|
|
3196
|
+
schema: [LOGICAL_PHYSICAL_SCHEMA],
|
|
2866
3197
|
hasSuggestions: true,
|
|
3198
|
+
defaultOptions: [{
|
|
3199
|
+
allowlist: [],
|
|
3200
|
+
direction: "both"
|
|
3201
|
+
}],
|
|
2867
3202
|
messages: {
|
|
2868
3203
|
usePhysical: "\"{{className}}\" uses a logical property. Use \"{{replacement}}\" for consistency.",
|
|
2869
3204
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2870
3205
|
}
|
|
2871
3206
|
},
|
|
2872
3207
|
createOnce(context) {
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
2878
|
-
for (const [logical, physical] of Object.entries(LOGICAL_TO_PHYSICAL)) if (bareUtility === logical || bareUtility.startsWith(`${logical}-`)) {
|
|
2879
|
-
const suffix = bareUtility.slice(logical.length);
|
|
2880
|
-
return `${variant}${hasImportantPrefix ? "!" : ""}${physical}${suffix}${hasImportantSuffix ? "!" : ""}`;
|
|
2881
|
-
}
|
|
2882
|
-
return null;
|
|
2883
|
-
}
|
|
2884
|
-
function check(locations) {
|
|
2885
|
-
for (const loc of locations) {
|
|
2886
|
-
const split = splitClassesWithSeparators(loc.value);
|
|
2887
|
-
const classes = split.classes;
|
|
2888
|
-
const offending = [];
|
|
2889
|
-
for (const cls of classes) {
|
|
2890
|
-
const converted = convertClass(cls);
|
|
2891
|
-
if (converted) offending.push({
|
|
2892
|
-
cls,
|
|
2893
|
-
replacement: converted
|
|
2894
|
-
});
|
|
2895
|
-
}
|
|
2896
|
-
if (offending.length === 0) continue;
|
|
2897
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2898
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2899
|
-
for (let i = 0; i < offending.length; i++) {
|
|
2900
|
-
const { cls, replacement } = offending[i];
|
|
2901
|
-
if (i === 0) context.report({
|
|
2902
|
-
node: loc.node,
|
|
2903
|
-
messageId: "usePhysical",
|
|
2904
|
-
data: {
|
|
2905
|
-
className: cls,
|
|
2906
|
-
replacement
|
|
2907
|
-
},
|
|
2908
|
-
fix(fixer) {
|
|
2909
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2910
|
-
}
|
|
2911
|
-
});
|
|
2912
|
-
else context.report({
|
|
2913
|
-
node: loc.node,
|
|
2914
|
-
messageId: "usePhysical",
|
|
2915
|
-
data: {
|
|
2916
|
-
className: cls,
|
|
2917
|
-
replacement
|
|
2918
|
-
},
|
|
2919
|
-
suggest: [{
|
|
2920
|
-
messageId: "suggestReplace",
|
|
2921
|
-
data: {
|
|
2922
|
-
className: cls,
|
|
2923
|
-
replacement
|
|
2924
|
-
},
|
|
2925
|
-
fix(fixer) {
|
|
2926
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2927
|
-
}
|
|
2928
|
-
}]
|
|
2929
|
-
});
|
|
2930
|
-
}
|
|
2931
|
-
}
|
|
2932
|
-
}
|
|
3208
|
+
const { check } = createDirectionalMapper(context, {
|
|
3209
|
+
mappings: invertAxisMappings(PHYSICAL_TO_LOGICAL_MAPPINGS),
|
|
3210
|
+
messageId: "usePhysical"
|
|
3211
|
+
});
|
|
2933
3212
|
return createExtractorVisitors(context, check);
|
|
2934
3213
|
}
|
|
2935
3214
|
});
|
|
@@ -2949,11 +3228,7 @@ const maxClassCount = defineRule({
|
|
|
2949
3228
|
messages: { tooMany: "Too many Tailwind classes ({{count}}). Maximum allowed is {{max}}. Consider extracting into a component or utility." }
|
|
2950
3229
|
},
|
|
2951
3230
|
createOnce(context) {
|
|
2952
|
-
|
|
2953
|
-
function getMax() {
|
|
2954
|
-
if (_max === null) _max = safeOptions(context)?.max ?? DEFAULT_MAX;
|
|
2955
|
-
return _max;
|
|
2956
|
-
}
|
|
3231
|
+
const getMax = createLazyOptions(context, (o) => o?.max ?? DEFAULT_MAX);
|
|
2957
3232
|
function check(locations) {
|
|
2958
3233
|
const max = getMax();
|
|
2959
3234
|
for (const loc of locations) {
|
|
@@ -2999,26 +3274,23 @@ const noRestrictedClasses = defineRule({
|
|
|
2999
3274
|
},
|
|
3000
3275
|
additionalProperties: false
|
|
3001
3276
|
}],
|
|
3277
|
+
defaultOptions: [{
|
|
3278
|
+
classes: [],
|
|
3279
|
+
patterns: []
|
|
3280
|
+
}],
|
|
3002
3281
|
messages: {
|
|
3003
3282
|
restricted: "\"{{className}}\" is restricted.",
|
|
3004
3283
|
restrictedWithMessage: "\"{{className}}\" is restricted: {{message}}"
|
|
3005
3284
|
}
|
|
3006
3285
|
},
|
|
3007
3286
|
createOnce(context) {
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
regex: new RegExp(p.pattern),
|
|
3016
|
-
message: p.message
|
|
3017
|
-
}))
|
|
3018
|
-
};
|
|
3019
|
-
}
|
|
3020
|
-
return _config;
|
|
3021
|
-
}
|
|
3287
|
+
const getConfig = createLazyOptions(context, (o) => ({
|
|
3288
|
+
restrictedClasses: new Set(o?.classes ?? []),
|
|
3289
|
+
patterns: (o?.patterns ?? []).map((p) => ({
|
|
3290
|
+
regex: new RegExp(p.pattern),
|
|
3291
|
+
message: p.message
|
|
3292
|
+
}))
|
|
3293
|
+
}));
|
|
3022
3294
|
function check(locations) {
|
|
3023
3295
|
const { restrictedClasses, patterns } = getConfig();
|
|
3024
3296
|
if (restrictedClasses.size === 0 && patterns.length === 0) return;
|
|
@@ -3069,14 +3341,11 @@ const noArbitraryValue = defineRule({
|
|
|
3069
3341
|
} },
|
|
3070
3342
|
additionalProperties: false
|
|
3071
3343
|
}],
|
|
3344
|
+
defaultOptions: [{ allow: [] }],
|
|
3072
3345
|
messages: { noArbitrary: "\"{{className}}\" uses an arbitrary value. Use a design token or extend your theme instead." }
|
|
3073
3346
|
},
|
|
3074
3347
|
createOnce(context) {
|
|
3075
|
-
|
|
3076
|
-
function getAllowPrefixes() {
|
|
3077
|
-
if (_allowPrefixes === null) _allowPrefixes = safeOptions(context)?.allow ?? [];
|
|
3078
|
-
return _allowPrefixes;
|
|
3079
|
-
}
|
|
3348
|
+
const getAllowPrefixes = createLazyOptions(context, (o) => o?.allow ?? []);
|
|
3080
3349
|
function isAllowed(utility) {
|
|
3081
3350
|
return getAllowPrefixes().some((prefix) => utility.startsWith(prefix));
|
|
3082
3351
|
}
|
|
@@ -3085,9 +3354,7 @@ const noArbitraryValue = defineRule({
|
|
|
3085
3354
|
const classes = splitClasses(loc.value);
|
|
3086
3355
|
for (const cls of classes) {
|
|
3087
3356
|
if (!hasArbitraryValue(cls)) continue;
|
|
3088
|
-
|
|
3089
|
-
if (utility.startsWith("!")) utility = utility.slice(1);
|
|
3090
|
-
else if (utility.endsWith("!")) utility = utility.slice(0, -1);
|
|
3357
|
+
const utility = splitImportant(extractUtility(cls)).bare;
|
|
3091
3358
|
if (isAllowed(utility)) continue;
|
|
3092
3359
|
context.report({
|
|
3093
3360
|
node: loc.node,
|
|
@@ -3124,11 +3391,7 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
3124
3391
|
}
|
|
3125
3392
|
},
|
|
3126
3393
|
createOnce(context) {
|
|
3127
|
-
|
|
3128
|
-
function getPosition() {
|
|
3129
|
-
if (_position === null) _position = safeOptions(context)?.position ?? "suffix";
|
|
3130
|
-
return _position;
|
|
3131
|
-
}
|
|
3394
|
+
const getPosition = createLazyOptions(context, (o) => o?.position ?? "suffix");
|
|
3132
3395
|
function fixUtility(utility, variantPfx) {
|
|
3133
3396
|
const position = getPosition();
|
|
3134
3397
|
if (position === "prefix" && utility.endsWith("!") && !utility.startsWith("!")) return `${variantPfx}!${utility.slice(0, -1)}`;
|
|
@@ -3136,54 +3399,18 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
3136
3399
|
return null;
|
|
3137
3400
|
}
|
|
3138
3401
|
function check(locations) {
|
|
3139
|
-
const
|
|
3402
|
+
const messageId = getPosition() === "prefix" ? "usePrefix" : "useSuffix";
|
|
3140
3403
|
for (const loc of locations) {
|
|
3141
3404
|
const split = splitClassesWithSeparators(loc.value);
|
|
3142
|
-
const
|
|
3143
|
-
const offending = [];
|
|
3144
|
-
for (const cls of classes) {
|
|
3405
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3145
3406
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3146
3407
|
const fixed = fixUtility(utility, variant);
|
|
3147
|
-
|
|
3408
|
+
return fixed ? [{
|
|
3148
3409
|
cls,
|
|
3149
3410
|
replacement: fixed
|
|
3150
|
-
}
|
|
3151
|
-
}
|
|
3152
|
-
|
|
3153
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3154
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3155
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3156
|
-
const { cls, replacement } = offending[i];
|
|
3157
|
-
if (i === 0) context.report({
|
|
3158
|
-
node: loc.node,
|
|
3159
|
-
messageId: position === "prefix" ? "usePrefix" : "useSuffix",
|
|
3160
|
-
data: {
|
|
3161
|
-
className: cls,
|
|
3162
|
-
replacement
|
|
3163
|
-
},
|
|
3164
|
-
fix(fixer) {
|
|
3165
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3166
|
-
}
|
|
3167
|
-
});
|
|
3168
|
-
else context.report({
|
|
3169
|
-
node: loc.node,
|
|
3170
|
-
messageId: position === "prefix" ? "usePrefix" : "useSuffix",
|
|
3171
|
-
data: {
|
|
3172
|
-
className: cls,
|
|
3173
|
-
replacement
|
|
3174
|
-
},
|
|
3175
|
-
suggest: [{
|
|
3176
|
-
messageId: "suggestReplace",
|
|
3177
|
-
data: {
|
|
3178
|
-
className: cls,
|
|
3179
|
-
replacement
|
|
3180
|
-
},
|
|
3181
|
-
fix(fixer) {
|
|
3182
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3183
|
-
}
|
|
3184
|
-
}]
|
|
3185
|
-
});
|
|
3186
|
-
}
|
|
3411
|
+
}] : [];
|
|
3412
|
+
});
|
|
3413
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId });
|
|
3187
3414
|
}
|
|
3188
3415
|
}
|
|
3189
3416
|
return createExtractorVisitors(context, check);
|
|
@@ -3226,50 +3453,14 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
3226
3453
|
function check(locations) {
|
|
3227
3454
|
for (const loc of locations) {
|
|
3228
3455
|
const split = splitClassesWithSeparators(loc.value);
|
|
3229
|
-
const
|
|
3230
|
-
const offending = [];
|
|
3231
|
-
for (const cls of classes) {
|
|
3456
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3232
3457
|
const fixed = fixClass(cls);
|
|
3233
|
-
|
|
3458
|
+
return fixed ? [{
|
|
3234
3459
|
cls,
|
|
3235
3460
|
replacement: fixed
|
|
3236
|
-
}
|
|
3237
|
-
}
|
|
3238
|
-
|
|
3239
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3240
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3241
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3242
|
-
const { cls, replacement } = offending[i];
|
|
3243
|
-
if (i === 0) context.report({
|
|
3244
|
-
node: loc.node,
|
|
3245
|
-
messageId: "moveNegative",
|
|
3246
|
-
data: {
|
|
3247
|
-
className: cls,
|
|
3248
|
-
replacement
|
|
3249
|
-
},
|
|
3250
|
-
fix(fixer) {
|
|
3251
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3252
|
-
}
|
|
3253
|
-
});
|
|
3254
|
-
else context.report({
|
|
3255
|
-
node: loc.node,
|
|
3256
|
-
messageId: "moveNegative",
|
|
3257
|
-
data: {
|
|
3258
|
-
className: cls,
|
|
3259
|
-
replacement
|
|
3260
|
-
},
|
|
3261
|
-
suggest: [{
|
|
3262
|
-
messageId: "suggestReplace",
|
|
3263
|
-
data: {
|
|
3264
|
-
className: cls,
|
|
3265
|
-
replacement
|
|
3266
|
-
},
|
|
3267
|
-
fix(fixer) {
|
|
3268
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3269
|
-
}
|
|
3270
|
-
}]
|
|
3271
|
-
});
|
|
3272
|
-
}
|
|
3461
|
+
}] : [];
|
|
3462
|
+
});
|
|
3463
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "moveNegative" });
|
|
3273
3464
|
}
|
|
3274
3465
|
}
|
|
3275
3466
|
return createExtractorVisitors(context, check);
|
|
@@ -3281,22 +3472,18 @@ const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-
|
|
|
3281
3472
|
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
3282
3473
|
function convertClass(cls, syntax) {
|
|
3283
3474
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3284
|
-
const
|
|
3285
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3286
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3287
|
-
const importantStart = hasImportantPrefix ? "!" : "";
|
|
3288
|
-
const importantEnd = hasImportantSuffix ? "!" : "";
|
|
3475
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3289
3476
|
if (syntax === "shorthand") {
|
|
3290
3477
|
const match = EXPLICIT_VAR_RE.exec(bareUtility);
|
|
3291
3478
|
if (match) {
|
|
3292
3479
|
const [, prefix, varName] = match;
|
|
3293
|
-
return `${variant}${
|
|
3480
|
+
return `${variant}${reattachImportant(`${prefix}-(${varName})`, position)}`;
|
|
3294
3481
|
}
|
|
3295
3482
|
} else {
|
|
3296
3483
|
const match = SHORTHAND_VAR_RE.exec(bareUtility);
|
|
3297
3484
|
if (match) {
|
|
3298
3485
|
const [, prefix, varName] = match;
|
|
3299
|
-
return `${variant}${
|
|
3486
|
+
return `${variant}${reattachImportant(`${prefix}-[var(${varName})]`, position)}`;
|
|
3300
3487
|
}
|
|
3301
3488
|
}
|
|
3302
3489
|
return null;
|
|
@@ -3323,60 +3510,20 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
3323
3510
|
}
|
|
3324
3511
|
},
|
|
3325
3512
|
createOnce(context) {
|
|
3326
|
-
|
|
3327
|
-
function getSyntax() {
|
|
3328
|
-
if (_syntax === null) _syntax = safeOptions(context)?.syntax ?? "shorthand";
|
|
3329
|
-
return _syntax;
|
|
3330
|
-
}
|
|
3513
|
+
const getSyntax = createLazyOptions(context, (o) => o?.syntax ?? "shorthand");
|
|
3331
3514
|
function check(locations) {
|
|
3332
3515
|
const syntax = getSyntax();
|
|
3516
|
+
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
3333
3517
|
for (const loc of locations) {
|
|
3334
3518
|
const split = splitClassesWithSeparators(loc.value);
|
|
3335
|
-
const
|
|
3336
|
-
const offending = [];
|
|
3337
|
-
for (const cls of classes) {
|
|
3519
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3338
3520
|
const converted = convertClass(cls, syntax);
|
|
3339
|
-
|
|
3521
|
+
return converted ? [{
|
|
3340
3522
|
cls,
|
|
3341
3523
|
replacement: converted
|
|
3342
|
-
}
|
|
3343
|
-
}
|
|
3344
|
-
|
|
3345
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3346
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3347
|
-
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
3348
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3349
|
-
const { cls, replacement } = offending[i];
|
|
3350
|
-
if (i === 0) context.report({
|
|
3351
|
-
node: loc.node,
|
|
3352
|
-
messageId,
|
|
3353
|
-
data: {
|
|
3354
|
-
className: cls,
|
|
3355
|
-
replacement
|
|
3356
|
-
},
|
|
3357
|
-
fix(fixer) {
|
|
3358
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3359
|
-
}
|
|
3360
|
-
});
|
|
3361
|
-
else context.report({
|
|
3362
|
-
node: loc.node,
|
|
3363
|
-
messageId,
|
|
3364
|
-
data: {
|
|
3365
|
-
className: cls,
|
|
3366
|
-
replacement
|
|
3367
|
-
},
|
|
3368
|
-
suggest: [{
|
|
3369
|
-
messageId: "suggestReplace",
|
|
3370
|
-
data: {
|
|
3371
|
-
className: cls,
|
|
3372
|
-
replacement
|
|
3373
|
-
},
|
|
3374
|
-
fix(fixer) {
|
|
3375
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3376
|
-
}
|
|
3377
|
-
}]
|
|
3378
|
-
});
|
|
3379
|
-
}
|
|
3524
|
+
}] : [];
|
|
3525
|
+
});
|
|
3526
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId });
|
|
3380
3527
|
}
|
|
3381
3528
|
}
|
|
3382
3529
|
return createExtractorVisitors(context, check);
|
|
@@ -3422,9 +3569,7 @@ const KNOWN_PREFIXES = [
|
|
|
3422
3569
|
* Used to group utilities by their property type.
|
|
3423
3570
|
*/
|
|
3424
3571
|
function getUtilityPrefix(utility) {
|
|
3425
|
-
let u = utility;
|
|
3426
|
-
if (u.startsWith("!")) u = u.slice(1);
|
|
3427
|
-
else if (u.endsWith("!")) u = u.slice(0, -1);
|
|
3572
|
+
let u = splitImportant(utility).bare;
|
|
3428
3573
|
if (u.startsWith("-")) u = u.slice(1);
|
|
3429
3574
|
for (const prefix of KNOWN_PREFIXES) if (u === prefix || u.startsWith(`${prefix}-`)) return prefix;
|
|
3430
3575
|
const dashIdx = u.indexOf("-");
|
|
@@ -3446,14 +3591,7 @@ const noDarkWithoutLight = defineRule({
|
|
|
3446
3591
|
messages: { missingBase: "\"{{className}}\" uses the {{variant}} variant, but there is no base \"{{prefix}}-*\" class on this element." }
|
|
3447
3592
|
},
|
|
3448
3593
|
createOnce(context) {
|
|
3449
|
-
|
|
3450
|
-
function getWatchedVariants() {
|
|
3451
|
-
if (_watchedVariants === null) {
|
|
3452
|
-
const options = safeOptions(context);
|
|
3453
|
-
_watchedVariants = new Set(options?.variants ?? DEFAULT_VARIANTS);
|
|
3454
|
-
}
|
|
3455
|
-
return _watchedVariants;
|
|
3456
|
-
}
|
|
3594
|
+
const getWatchedVariants = createLazyOptions(context, (o) => new Set(o?.variants ?? DEFAULT_VARIANTS));
|
|
3457
3595
|
function check(locations) {
|
|
3458
3596
|
const watchedVariants = getWatchedVariants();
|
|
3459
3597
|
for (const loc of locations) {
|
|
@@ -3549,17 +3687,11 @@ const noHardcodedColors = defineRule({
|
|
|
3549
3687
|
},
|
|
3550
3688
|
additionalProperties: false
|
|
3551
3689
|
}],
|
|
3690
|
+
defaultOptions: [{ allow: [] }],
|
|
3552
3691
|
messages: { noHardcoded: "\"{{className}}\" uses a hardcoded color value. Use a design token from your theme instead." }
|
|
3553
3692
|
},
|
|
3554
3693
|
createOnce(context) {
|
|
3555
|
-
|
|
3556
|
-
function getAllowlist() {
|
|
3557
|
-
if (_allowlist === null) {
|
|
3558
|
-
const options = safeOptions(context);
|
|
3559
|
-
_allowlist = new Set(options?.allow ?? []);
|
|
3560
|
-
}
|
|
3561
|
-
return _allowlist;
|
|
3562
|
-
}
|
|
3694
|
+
const getAllowlist = createLazyOptions(context, (o) => new Set(o?.allow ?? []));
|
|
3563
3695
|
function check(locations) {
|
|
3564
3696
|
const allowlist = getAllowlist();
|
|
3565
3697
|
for (const loc of locations) {
|
|
@@ -3567,9 +3699,7 @@ const noHardcodedColors = defineRule({
|
|
|
3567
3699
|
for (const cls of classes) {
|
|
3568
3700
|
if (allowlist.has(cls)) continue;
|
|
3569
3701
|
if (!hasArbitraryValue(cls)) continue;
|
|
3570
|
-
|
|
3571
|
-
if (utility.startsWith("!")) utility = utility.slice(1);
|
|
3572
|
-
else if (utility.endsWith("!")) utility = utility.slice(0, -1);
|
|
3702
|
+
const utility = splitImportant(extractUtility(cls)).bare;
|
|
3573
3703
|
if (!isColorUtility(utility)) continue;
|
|
3574
3704
|
const value = getArbitraryValue(cls);
|
|
3575
3705
|
if (!value) continue;
|
|
@@ -3740,6 +3870,7 @@ const consistentVariantOrder = defineRule({
|
|
|
3740
3870
|
additionalProperties: false
|
|
3741
3871
|
}],
|
|
3742
3872
|
hasSuggestions: true,
|
|
3873
|
+
defaultOptions: [{}],
|
|
3743
3874
|
messages: {
|
|
3744
3875
|
wrongOrder: "\"{{className}}\" has variants in wrong order. Use \"{{replacement}}\" instead.",
|
|
3745
3876
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
@@ -3747,54 +3878,33 @@ const consistentVariantOrder = defineRule({
|
|
|
3747
3878
|
},
|
|
3748
3879
|
createOnce(context) {
|
|
3749
3880
|
const getDS = createLazyLoader(context);
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
};
|
|
3763
|
-
} else if (dsCache && dsCache.hasVariantOrder()) {
|
|
3764
|
-
for (const variant of DEFAULT_VARIANT_ORDER) {
|
|
3765
|
-
const p = dsCache.getVariantPriority(variant);
|
|
3766
|
-
if (p !== null) priorityMap.set(variant, p);
|
|
3767
|
-
}
|
|
3768
|
-
_config = {
|
|
3769
|
-
priorityMap,
|
|
3770
|
-
fallbackPriority: Number.MAX_SAFE_INTEGER,
|
|
3771
|
-
dsCache
|
|
3772
|
-
};
|
|
3773
|
-
} else {
|
|
3774
|
-
for (let i = 0; i < DEFAULT_VARIANT_ORDER.length; i++) priorityMap.set(DEFAULT_VARIANT_ORDER[i], i);
|
|
3775
|
-
_config = {
|
|
3776
|
-
priorityMap,
|
|
3777
|
-
fallbackPriority: DEFAULT_VARIANT_ORDER.length,
|
|
3778
|
-
dsCache
|
|
3779
|
-
};
|
|
3780
|
-
}
|
|
3881
|
+
const getPriority = createLazyOptions(context, (o) => {
|
|
3882
|
+
let dsCache = null;
|
|
3883
|
+
try {
|
|
3884
|
+
dsCache = getDS().cache;
|
|
3885
|
+
} catch (err) {
|
|
3886
|
+
if (!isFatalError(err)) throw err;
|
|
3887
|
+
}
|
|
3888
|
+
if (o?.order) {
|
|
3889
|
+
const map = /* @__PURE__ */ new Map();
|
|
3890
|
+
for (let i = 0; i < o.order.length; i++) map.set(o.order[i], i);
|
|
3891
|
+
const fallback = o.order.length;
|
|
3892
|
+
return (v) => map.get(v) ?? fallback;
|
|
3781
3893
|
}
|
|
3782
|
-
return _config;
|
|
3783
|
-
}
|
|
3784
|
-
function getVariantPriority(variant) {
|
|
3785
|
-
const { priorityMap, fallbackPriority, dsCache } = getConfig();
|
|
3786
|
-
const priority = priorityMap.get(variant);
|
|
3787
|
-
if (priority !== void 0) return priority;
|
|
3788
3894
|
if (dsCache && dsCache.hasVariantOrder()) {
|
|
3789
|
-
const
|
|
3790
|
-
|
|
3895
|
+
const ds = dsCache;
|
|
3896
|
+
return (v) => ds.getVariantPriority(v) ?? Number.MAX_SAFE_INTEGER;
|
|
3791
3897
|
}
|
|
3792
|
-
|
|
3793
|
-
|
|
3898
|
+
const map = /* @__PURE__ */ new Map();
|
|
3899
|
+
for (let i = 0; i < DEFAULT_VARIANT_ORDER.length; i++) map.set(DEFAULT_VARIANT_ORDER[i], i);
|
|
3900
|
+
const fallback = DEFAULT_VARIANT_ORDER.length;
|
|
3901
|
+
return (v) => map.get(v) ?? fallback;
|
|
3902
|
+
});
|
|
3794
3903
|
function reorderClass(cls) {
|
|
3795
3904
|
const variants = extractVariants(cls);
|
|
3796
3905
|
if (variants.length < 2) return null;
|
|
3797
|
-
const
|
|
3906
|
+
const priorityOf = getPriority();
|
|
3907
|
+
const sorted = [...variants].sort((a, b) => priorityOf(a) - priorityOf(b));
|
|
3798
3908
|
const nonPseudo = [];
|
|
3799
3909
|
const pseudo = [];
|
|
3800
3910
|
for (const v of sorted) if (PSEUDO_ELEMENTS.has(v)) pseudo.push(v);
|
|
@@ -3879,16 +3989,8 @@ const enforceConsistentLineWrapping = defineRule({
|
|
|
3879
3989
|
}
|
|
3880
3990
|
},
|
|
3881
3991
|
createOnce(context) {
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
|
|
3885
|
-
return _printWidth;
|
|
3886
|
-
}
|
|
3887
|
-
let _classesPerLine = null;
|
|
3888
|
-
function getClassesPerLine() {
|
|
3889
|
-
if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
|
|
3890
|
-
return _classesPerLine;
|
|
3891
|
-
}
|
|
3992
|
+
const getPrintWidth = createLazyOptions(context, (o) => o?.printWidth ?? DEFAULT_PRINT_WIDTH);
|
|
3993
|
+
const getClassesPerLine = createLazyOptions(context, (o) => o?.classesPerLine);
|
|
3892
3994
|
function check(locations) {
|
|
3893
3995
|
const printWidth = getPrintWidth();
|
|
3894
3996
|
const classesPerLine = getClassesPerLine();
|
|
@@ -3947,69 +4049,33 @@ const noUnnecessaryArbitraryValue = defineRule({
|
|
|
3947
4049
|
additionalProperties: false
|
|
3948
4050
|
}],
|
|
3949
4051
|
hasSuggestions: true,
|
|
4052
|
+
defaultOptions: [{}],
|
|
3950
4053
|
messages: {
|
|
3951
4054
|
unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead.",
|
|
3952
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
4055
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
4056
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
3953
4057
|
}
|
|
3954
4058
|
},
|
|
3955
4059
|
createOnce(context) {
|
|
3956
4060
|
const getDS = createLazyLoader(context);
|
|
3957
4061
|
function check(locations) {
|
|
3958
|
-
|
|
4062
|
+
if (locations.length === 0) return;
|
|
4063
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
3959
4064
|
if (!ds) return;
|
|
3960
4065
|
const { cache } = ds;
|
|
3961
4066
|
for (const loc of locations) {
|
|
3962
4067
|
const split = splitClassesWithSeparators(loc.value);
|
|
3963
|
-
const
|
|
3964
|
-
|
|
3965
|
-
for (const cls of classes) {
|
|
3966
|
-
if (!hasArbitraryValue(cls)) continue;
|
|
4068
|
+
const offending = split.classes.flatMap((cls) => {
|
|
4069
|
+
if (!hasArbitraryValue(cls)) return [];
|
|
3967
4070
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3968
|
-
const
|
|
3969
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3970
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
4071
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3971
4072
|
const named = cache.getNamedEquivalent(bareUtility);
|
|
3972
|
-
|
|
3973
|
-
offending.push({
|
|
4073
|
+
return named ? [{
|
|
3974
4074
|
cls,
|
|
3975
|
-
replacement: variant + (
|
|
3976
|
-
}
|
|
3977
|
-
}
|
|
3978
|
-
|
|
3979
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3980
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3981
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3982
|
-
const { cls, replacement } = offending[i];
|
|
3983
|
-
if (i === 0) context.report({
|
|
3984
|
-
node: loc.node,
|
|
3985
|
-
messageId: "unnecessaryArbitrary",
|
|
3986
|
-
data: {
|
|
3987
|
-
className: cls,
|
|
3988
|
-
replacement
|
|
3989
|
-
},
|
|
3990
|
-
fix(fixer) {
|
|
3991
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3992
|
-
}
|
|
3993
|
-
});
|
|
3994
|
-
else context.report({
|
|
3995
|
-
node: loc.node,
|
|
3996
|
-
messageId: "unnecessaryArbitrary",
|
|
3997
|
-
data: {
|
|
3998
|
-
className: cls,
|
|
3999
|
-
replacement
|
|
4000
|
-
},
|
|
4001
|
-
suggest: [{
|
|
4002
|
-
messageId: "suggestReplace",
|
|
4003
|
-
data: {
|
|
4004
|
-
className: cls,
|
|
4005
|
-
replacement
|
|
4006
|
-
},
|
|
4007
|
-
fix(fixer) {
|
|
4008
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4009
|
-
}
|
|
4010
|
-
}]
|
|
4011
|
-
});
|
|
4012
|
-
}
|
|
4075
|
+
replacement: variant + reattachImportant(named, position)
|
|
4076
|
+
}] : [];
|
|
4077
|
+
});
|
|
4078
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "unnecessaryArbitrary" });
|
|
4013
4079
|
}
|
|
4014
4080
|
}
|
|
4015
4081
|
return createExtractorVisitors(context, check);
|
|
@@ -4080,73 +4146,37 @@ const plugin = definePlugin({
|
|
|
4080
4146
|
additionalProperties: false
|
|
4081
4147
|
}],
|
|
4082
4148
|
hasSuggestions: true,
|
|
4149
|
+
defaultOptions: [{}],
|
|
4083
4150
|
messages: {
|
|
4084
4151
|
preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
|
|
4085
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
4152
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
4153
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
4086
4154
|
}
|
|
4087
4155
|
},
|
|
4088
4156
|
createOnce(context) {
|
|
4089
4157
|
const getDS = createLazyLoader(context);
|
|
4090
4158
|
function check(locations) {
|
|
4091
|
-
|
|
4159
|
+
if (locations.length === 0) return;
|
|
4160
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
4092
4161
|
if (!ds) return;
|
|
4093
4162
|
const { cache } = ds;
|
|
4094
4163
|
for (const loc of locations) {
|
|
4095
4164
|
const split = splitClassesWithSeparators(loc.value);
|
|
4096
|
-
const
|
|
4097
|
-
const offending = [];
|
|
4098
|
-
for (const cls of classes) {
|
|
4165
|
+
const offending = split.classes.flatMap((cls) => {
|
|
4099
4166
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
4100
|
-
const
|
|
4101
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
4102
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
4167
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
4103
4168
|
const match = detectRawVariable(bareUtility);
|
|
4104
|
-
if (!match)
|
|
4169
|
+
if (!match) return [];
|
|
4105
4170
|
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
4106
|
-
if (!cache.isValid(candidate))
|
|
4171
|
+
if (!cache.isValid(candidate)) return [];
|
|
4107
4172
|
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
4108
|
-
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`)
|
|
4109
|
-
|
|
4110
|
-
offending.push({
|
|
4173
|
+
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|
|
4174
|
+
return [{
|
|
4111
4175
|
cls,
|
|
4112
|
-
replacement
|
|
4113
|
-
}
|
|
4114
|
-
}
|
|
4115
|
-
|
|
4116
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
4117
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
4118
|
-
for (let i = 0; i < offending.length; i++) {
|
|
4119
|
-
const { cls, replacement } = offending[i];
|
|
4120
|
-
if (i === 0) context.report({
|
|
4121
|
-
node: loc.node,
|
|
4122
|
-
messageId: "preferNamed",
|
|
4123
|
-
data: {
|
|
4124
|
-
className: cls,
|
|
4125
|
-
replacement
|
|
4126
|
-
},
|
|
4127
|
-
fix(fixer) {
|
|
4128
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4129
|
-
}
|
|
4130
|
-
});
|
|
4131
|
-
else context.report({
|
|
4132
|
-
node: loc.node,
|
|
4133
|
-
messageId: "preferNamed",
|
|
4134
|
-
data: {
|
|
4135
|
-
className: cls,
|
|
4136
|
-
replacement
|
|
4137
|
-
},
|
|
4138
|
-
suggest: [{
|
|
4139
|
-
messageId: "suggestReplace",
|
|
4140
|
-
data: {
|
|
4141
|
-
className: cls,
|
|
4142
|
-
replacement
|
|
4143
|
-
},
|
|
4144
|
-
fix(fixer) {
|
|
4145
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4146
|
-
}
|
|
4147
|
-
}]
|
|
4148
|
-
});
|
|
4149
|
-
}
|
|
4176
|
+
replacement: variant + reattachImportant(candidate, position)
|
|
4177
|
+
}];
|
|
4178
|
+
});
|
|
4179
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "preferNamed" });
|
|
4150
4180
|
}
|
|
4151
4181
|
}
|
|
4152
4182
|
return createExtractorVisitors(context, check);
|