oxlint-tailwindcss 0.7.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -35
- package/dist/index.cjs +1317 -1297
- package/dist/index.d.cts +1 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1299 -1279
- package/dist/index.mjs.map +1 -1
- package/package.json +26 -23
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
3
3
|
import { execFileSync } from "node:child_process";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
|
-
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { dirname, join, relative, resolve } from "node:path";
|
|
7
7
|
import { tmpdir } from "node:os";
|
|
8
8
|
import { Worker } from "node:worker_threads";
|
|
@@ -44,8 +44,7 @@ const DEFAULT_EXTRACTOR_CONFIG = {
|
|
|
44
44
|
/^styles?$/
|
|
45
45
|
]
|
|
46
46
|
};
|
|
47
|
-
|
|
48
|
-
let _settingsResolved = false;
|
|
47
|
+
const configCache = /* @__PURE__ */ new WeakMap();
|
|
49
48
|
function mergeUnique(defaults, extras, exclusions) {
|
|
50
49
|
let base = defaults;
|
|
51
50
|
if (exclusions && exclusions.length > 0) {
|
|
@@ -58,34 +57,39 @@ function mergeUnique(defaults, extras, exclusions) {
|
|
|
58
57
|
return [...set];
|
|
59
58
|
}
|
|
60
59
|
/**
|
|
61
|
-
* Returns the extractor config, merging defaults with
|
|
62
|
-
* `settings.tailwindcss`.
|
|
63
|
-
*
|
|
60
|
+
* Returns the extractor config for this rule context, merging defaults with
|
|
61
|
+
* user settings from `settings.tailwindcss`. The result is cached per
|
|
62
|
+
* context (WeakMap-keyed) so settings are read once per rule lifetime.
|
|
63
|
+
*
|
|
64
|
+
* In `createOnce` the settings getter throws — we fall back to the defaults
|
|
65
|
+
* without caching so the next visitor call (where settings ARE available)
|
|
66
|
+
* still resolves the user's overrides.
|
|
64
67
|
*/
|
|
65
68
|
function getExtractorConfig(context) {
|
|
66
|
-
|
|
69
|
+
const cached = configCache.get(context);
|
|
70
|
+
if (cached) return cached;
|
|
67
71
|
let tw;
|
|
68
72
|
try {
|
|
69
73
|
const raw = context.settings?.tailwindcss;
|
|
70
74
|
if (raw && typeof raw === "object") tw = raw;
|
|
71
|
-
_settingsResolved = true;
|
|
72
75
|
} catch {
|
|
73
76
|
return DEFAULT_EXTRACTOR_CONFIG;
|
|
74
77
|
}
|
|
75
78
|
if (!tw) {
|
|
76
|
-
|
|
77
|
-
return
|
|
79
|
+
configCache.set(context, DEFAULT_EXTRACTOR_CONFIG);
|
|
80
|
+
return DEFAULT_EXTRACTOR_CONFIG;
|
|
78
81
|
}
|
|
79
82
|
const exclude = tw.exclude;
|
|
80
83
|
const excludedPatternSources = new Set(exclude?.variablePatterns ?? []);
|
|
81
84
|
const filteredPatterns = excludedPatternSources.size > 0 ? DEFAULT_EXTRACTOR_CONFIG.variablePatterns.filter((re) => !excludedPatternSources.has(re.source)) : DEFAULT_EXTRACTOR_CONFIG.variablePatterns;
|
|
82
|
-
|
|
85
|
+
const resolved = {
|
|
83
86
|
attributes: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.attributes, tw.attributes, exclude?.attributes),
|
|
84
87
|
callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, tw.callees, exclude?.callees),
|
|
85
88
|
tags: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.tags, tw.tags, exclude?.tags),
|
|
86
89
|
variablePatterns: [...filteredPatterns, ...(tw.variablePatterns ?? []).map((p) => new RegExp(p))]
|
|
87
90
|
};
|
|
88
|
-
|
|
91
|
+
configCache.set(context, resolved);
|
|
92
|
+
return resolved;
|
|
89
93
|
}
|
|
90
94
|
/**
|
|
91
95
|
* Creates the 4 standard AST visitor callbacks that all rules use.
|
|
@@ -138,7 +142,7 @@ function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
138
142
|
if (calleeName === "tv") return extractFromTvCall(node);
|
|
139
143
|
if (calleeName === "classed") return extractFromClassedCall(node);
|
|
140
144
|
const results = [];
|
|
141
|
-
for (const arg of node.arguments)
|
|
145
|
+
for (const arg of node.arguments) extractFromExpression(arg, results);
|
|
142
146
|
return results;
|
|
143
147
|
}
|
|
144
148
|
/**
|
|
@@ -156,7 +160,7 @@ function extractFromTaggedTemplate(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
|
156
160
|
*/
|
|
157
161
|
function extractObjectValues(node) {
|
|
158
162
|
const results = [];
|
|
159
|
-
for (const prop of node.properties) if (prop.type === "Property")
|
|
163
|
+
for (const prop of node.properties) if (prop.type === "Property") extractFromExpression(prop.value, results);
|
|
160
164
|
return results;
|
|
161
165
|
}
|
|
162
166
|
/**
|
|
@@ -183,7 +187,7 @@ function extractFromClassedCall(node) {
|
|
|
183
187
|
}
|
|
184
188
|
skippedFirst = true;
|
|
185
189
|
if (arg.type === "ObjectExpression") results.push(...extractFromCvaConfig(arg));
|
|
186
|
-
else
|
|
190
|
+
else extractFromExpression(arg, results);
|
|
187
191
|
}
|
|
188
192
|
return results;
|
|
189
193
|
}
|
|
@@ -194,7 +198,7 @@ function extractFromClassedCall(node) {
|
|
|
194
198
|
function extractFromCvaCall(node) {
|
|
195
199
|
const results = [];
|
|
196
200
|
for (const arg of node.arguments) if (arg.type === "ObjectExpression") results.push(...extractFromCvaConfig(arg));
|
|
197
|
-
else
|
|
201
|
+
else extractFromExpression(arg, results);
|
|
198
202
|
return results;
|
|
199
203
|
}
|
|
200
204
|
/**
|
|
@@ -209,10 +213,10 @@ function extractFromCvaConfig(node) {
|
|
|
209
213
|
const name = getPropertyName(prop.key);
|
|
210
214
|
if (name === "variants" && prop.value.type === "ObjectExpression") for (const category of prop.value.properties) {
|
|
211
215
|
if (category.type !== "Property" || category.value.type !== "ObjectExpression") continue;
|
|
212
|
-
for (const variant of category.value.properties) if (variant.type === "Property")
|
|
216
|
+
for (const variant of category.value.properties) if (variant.type === "Property") extractFromExpression(variant.value, results);
|
|
213
217
|
}
|
|
214
218
|
else if (name === "compoundVariants" && prop.value.type === "ArrayExpression") results.push(...extractClassFromCompoundEntries(prop.value));
|
|
215
|
-
else if (name === "defaultVariants") {} else
|
|
219
|
+
else if (name === "defaultVariants") {} else extractFromExpression(prop.value, results);
|
|
216
220
|
}
|
|
217
221
|
return results;
|
|
218
222
|
}
|
|
@@ -223,7 +227,7 @@ function extractFromCvaConfig(node) {
|
|
|
223
227
|
function extractFromTvCall(node) {
|
|
224
228
|
const results = [];
|
|
225
229
|
for (const arg of node.arguments) if (arg.type === "ObjectExpression") results.push(...extractFromTvConfig(arg));
|
|
226
|
-
else
|
|
230
|
+
else extractFromExpression(arg, results);
|
|
227
231
|
return results;
|
|
228
232
|
}
|
|
229
233
|
/**
|
|
@@ -236,20 +240,20 @@ function extractFromTvConfig(node) {
|
|
|
236
240
|
for (const prop of node.properties) {
|
|
237
241
|
if (prop.type !== "Property") continue;
|
|
238
242
|
const name = getPropertyName(prop.key);
|
|
239
|
-
if (name === "base")
|
|
243
|
+
if (name === "base") extractFromExpression(prop.value, results);
|
|
240
244
|
else if (name === "slots" && prop.value.type === "ObjectExpression") {
|
|
241
|
-
for (const slot of prop.value.properties) if (slot.type === "Property")
|
|
245
|
+
for (const slot of prop.value.properties) if (slot.type === "Property") extractFromExpression(slot.value, results);
|
|
242
246
|
} else if (name === "variants" && prop.value.type === "ObjectExpression") for (const category of prop.value.properties) {
|
|
243
247
|
if (category.type !== "Property" || category.value.type !== "ObjectExpression") continue;
|
|
244
248
|
for (const variant of category.value.properties) {
|
|
245
249
|
if (variant.type !== "Property") continue;
|
|
246
250
|
if (variant.value.type === "ObjectExpression") {
|
|
247
|
-
for (const slotProp of variant.value.properties) if (slotProp.type === "Property")
|
|
248
|
-
} else
|
|
251
|
+
for (const slotProp of variant.value.properties) if (slotProp.type === "Property") extractFromExpression(slotProp.value, results);
|
|
252
|
+
} else extractFromExpression(variant.value, results);
|
|
249
253
|
}
|
|
250
254
|
}
|
|
251
255
|
else if ((name === "compoundVariants" || name === "compoundSlots") && prop.value.type === "ArrayExpression") results.push(...extractClassFromCompoundEntries(prop.value));
|
|
252
|
-
else if (name === "defaultVariants") {} else
|
|
256
|
+
else if (name === "defaultVariants") {} else extractFromExpression(prop.value, results);
|
|
253
257
|
}
|
|
254
258
|
return results;
|
|
255
259
|
}
|
|
@@ -264,7 +268,7 @@ function extractClassFromCompoundEntries(node) {
|
|
|
264
268
|
for (const prop of element.properties) {
|
|
265
269
|
if (prop.type !== "Property") continue;
|
|
266
270
|
const name = getPropertyName(prop.key);
|
|
267
|
-
if (name === "class" || name === "className")
|
|
271
|
+
if (name === "class" || name === "className") extractFromExpression(prop.value, results);
|
|
268
272
|
}
|
|
269
273
|
}
|
|
270
274
|
return results;
|
|
@@ -286,34 +290,51 @@ function extractFromVariableDeclarator(node, config = DEFAULT_EXTRACTOR_CONFIG)
|
|
|
286
290
|
if (!node.init) return [];
|
|
287
291
|
return extractFromExpression(node.init);
|
|
288
292
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
293
|
+
/**
|
|
294
|
+
* Walk an expression and append every class-bearing location to `out`.
|
|
295
|
+
*
|
|
296
|
+
* Internal helpers pass their accumulator down to avoid the
|
|
297
|
+
* `extractFromExpression(x, results)` pattern, which allocates an
|
|
298
|
+
* intermediate array per recursive call. Public callers may omit `out` to
|
|
299
|
+
* receive a fresh array.
|
|
300
|
+
*/
|
|
301
|
+
function extractFromExpression(node, out = []) {
|
|
302
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
303
|
+
out.push({
|
|
304
|
+
value: node.value,
|
|
305
|
+
node,
|
|
306
|
+
range: [node.range[0] + 1, node.range[1] - 1]
|
|
307
|
+
});
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
if (node.type === "TemplateLiteral") return appendFromTemplateLiteral(node, out);
|
|
311
|
+
if (node.type === "ConditionalExpression") {
|
|
312
|
+
extractFromExpression(node.consequent, out);
|
|
313
|
+
extractFromExpression(node.alternate, out);
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
if (node.type === "LogicalExpression") return extractFromExpression(node.right, out);
|
|
317
|
+
if (node.type === "ArrayExpression") {
|
|
318
|
+
for (const element of node.elements) if (element && element.type !== "SpreadElement") extractFromExpression(element, out);
|
|
319
|
+
return out;
|
|
320
|
+
}
|
|
298
321
|
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({
|
|
322
|
+
for (const prop of node.properties) if (prop.type === "Property" && prop.key.type === "Literal" && typeof prop.key.value === "string") out.push({
|
|
301
323
|
value: prop.key.value,
|
|
302
324
|
node: prop.key,
|
|
303
325
|
range: [prop.key.range[0] + 1, prop.key.range[1] - 1]
|
|
304
326
|
});
|
|
305
|
-
return
|
|
327
|
+
return out;
|
|
306
328
|
}
|
|
307
|
-
return
|
|
329
|
+
return out;
|
|
308
330
|
}
|
|
309
|
-
function
|
|
310
|
-
const results = [];
|
|
331
|
+
function appendFromTemplateLiteral(node, out) {
|
|
311
332
|
for (let i = 0; i < node.quasis.length; i++) {
|
|
312
333
|
const quasi = node.quasis[i];
|
|
313
334
|
const value = quasi.value.raw;
|
|
314
335
|
if (value.trim().length > 0) {
|
|
315
336
|
const start = quasi.range[0] + 1;
|
|
316
|
-
|
|
337
|
+
out.push({
|
|
317
338
|
value,
|
|
318
339
|
node: quasi,
|
|
319
340
|
range: [start, start + value.length],
|
|
@@ -322,7 +343,10 @@ function extractFromTemplateLiteral(node) {
|
|
|
322
343
|
});
|
|
323
344
|
}
|
|
324
345
|
}
|
|
325
|
-
return
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
function extractFromTemplateLiteral(node) {
|
|
349
|
+
return appendFromTemplateLiteral(node, []);
|
|
326
350
|
}
|
|
327
351
|
function getCalleeName(node) {
|
|
328
352
|
if (node.type === "Identifier") return node.name;
|
|
@@ -536,21 +560,6 @@ function findBestSuggestion(input, candidates, maxDistance = 3) {
|
|
|
536
560
|
return best;
|
|
537
561
|
}
|
|
538
562
|
//#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
563
|
//#region src/utils/class-parser.ts
|
|
555
564
|
/**
|
|
556
565
|
* Extracts variants from a class string, respecting bracket depth.
|
|
@@ -657,23 +666,53 @@ function getArbitraryValue(cls) {
|
|
|
657
666
|
}
|
|
658
667
|
return null;
|
|
659
668
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
669
|
+
/**
|
|
670
|
+
* Strip the `!` important modifier from a utility's bare form (no variant
|
|
671
|
+
* prefix). Returns the bare string and the original position. Operate on
|
|
672
|
+
* the utility part — call `splitUtilityAndVariant` first if you have the
|
|
673
|
+
* full class. `parseClass` does both in one pass; use that when you need
|
|
674
|
+
* the full ParsedClass shape.
|
|
675
|
+
*/
|
|
676
|
+
function splitImportant(utility) {
|
|
677
|
+
if (utility.startsWith("!")) return {
|
|
678
|
+
bare: utility.slice(1),
|
|
679
|
+
position: "prefix"
|
|
667
680
|
};
|
|
668
|
-
if (
|
|
669
|
-
bare:
|
|
670
|
-
|
|
681
|
+
if (utility.endsWith("!")) return {
|
|
682
|
+
bare: utility.slice(0, -1),
|
|
683
|
+
position: "suffix"
|
|
671
684
|
};
|
|
672
685
|
return {
|
|
673
|
-
bare:
|
|
674
|
-
|
|
686
|
+
bare: utility,
|
|
687
|
+
position: null
|
|
675
688
|
};
|
|
676
689
|
}
|
|
690
|
+
/**
|
|
691
|
+
* Reattach `!` at the position recorded by `splitImportant`.
|
|
692
|
+
* The inverse of `splitImportant`: `reattachImportant(splitImportant(x).bare, splitImportant(x).position) === x`.
|
|
693
|
+
*/
|
|
694
|
+
function reattachImportant(bare, position) {
|
|
695
|
+
if (position === "prefix") return `!${bare}`;
|
|
696
|
+
if (position === "suffix") return `${bare}!`;
|
|
697
|
+
return bare;
|
|
698
|
+
}
|
|
699
|
+
//#endregion
|
|
700
|
+
//#region src/utils/floating-point.ts
|
|
701
|
+
/**
|
|
702
|
+
* Rounds rem/em/px values in class strings to avoid
|
|
703
|
+
* JavaScript floating point issues.
|
|
704
|
+
*
|
|
705
|
+
* Example: "text-lg/[2.4000000000000004rem]" -> "text-lg/[2.4rem]"
|
|
706
|
+
*
|
|
707
|
+
* Regression test for: better-tailwindcss#320
|
|
708
|
+
*/
|
|
709
|
+
function roundRemValue(className) {
|
|
710
|
+
return className.replace(/\[(-?\d+\.\d{7,})(rem|em|px|%)\]/g, (_, num, unit) => {
|
|
711
|
+
return `[${Number.parseFloat(Number.parseFloat(num).toFixed(6))}${unit}]`;
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
//#endregion
|
|
715
|
+
//#region src/design-system/cache.ts
|
|
677
716
|
var DesignSystemCache = class DesignSystemCache {
|
|
678
717
|
canonicalMap = /* @__PURE__ */ new Map();
|
|
679
718
|
validitySet = /* @__PURE__ */ new Set();
|
|
@@ -714,12 +753,12 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
714
753
|
const canonicalUtility = this.canonicalMap.get(utility);
|
|
715
754
|
if (canonicalUtility !== void 0) return className.slice(0, className.length - utility.length) + canonicalUtility;
|
|
716
755
|
}
|
|
717
|
-
const { bare,
|
|
718
|
-
if (
|
|
756
|
+
const { bare, position } = splitImportant(utility);
|
|
757
|
+
if (position) {
|
|
719
758
|
const canonicalBare = this.canonicalMap.get(bare);
|
|
720
759
|
if (canonicalBare !== void 0) {
|
|
721
760
|
const variantPrefix = className.slice(0, className.length - utility.length);
|
|
722
|
-
return
|
|
761
|
+
return position === "prefix" ? variantPrefix + "!" + canonicalBare : variantPrefix + canonicalBare + "!";
|
|
723
762
|
}
|
|
724
763
|
}
|
|
725
764
|
return className;
|
|
@@ -749,6 +788,20 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
749
788
|
if (variant.startsWith("aria-")) return (this.variantOrderMap.get("inert") ?? 48) + 2;
|
|
750
789
|
return 0;
|
|
751
790
|
}
|
|
791
|
+
_prefixOrderMap = null;
|
|
792
|
+
buildPrefixOrderMap() {
|
|
793
|
+
const index = /* @__PURE__ */ new Map();
|
|
794
|
+
for (const [cls, order] of this.orderMap) {
|
|
795
|
+
if (order === null) continue;
|
|
796
|
+
for (let i = cls.indexOf("-"); i >= 0; i = cls.indexOf("-", i + 1)) {
|
|
797
|
+
const p = cls.slice(0, i);
|
|
798
|
+
if (!index.has(p)) index.set(p, order);
|
|
799
|
+
const pd = p + "-";
|
|
800
|
+
if (!index.has(pd)) index.set(pd, order);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
return index;
|
|
804
|
+
}
|
|
752
805
|
/** Find the order of the first class matching a prefix (e.g. "max-w-" → order of "max-w-0") */
|
|
753
806
|
findOrderByPrefix(prefix) {
|
|
754
807
|
const withoutDash = prefix.endsWith("-") ? prefix.slice(0, -1) : null;
|
|
@@ -756,7 +809,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
756
809
|
const order = this.orderMap.get(withoutDash);
|
|
757
810
|
if (order != null) return order;
|
|
758
811
|
}
|
|
759
|
-
|
|
812
|
+
if (!this._prefixOrderMap) this._prefixOrderMap = this.buildPrefixOrderMap();
|
|
813
|
+
return this._prefixOrderMap.get(prefix);
|
|
760
814
|
}
|
|
761
815
|
getKnownPrefixes() {
|
|
762
816
|
if (!this._knownPrefixes) {
|
|
@@ -772,7 +826,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
772
826
|
if (this.validitySet.has(className)) return true;
|
|
773
827
|
const utility = extractUtility(className);
|
|
774
828
|
if (utility !== className && this.validitySet.has(utility)) return true;
|
|
775
|
-
const { bare } =
|
|
829
|
+
const { bare } = splitImportant(utility);
|
|
776
830
|
if (bare !== utility && this.validitySet.has(bare)) return true;
|
|
777
831
|
const slashIdx = bare.lastIndexOf("/");
|
|
778
832
|
if (slashIdx > 0) {
|
|
@@ -796,8 +850,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
796
850
|
const hasVariant = utility !== className;
|
|
797
851
|
let baseOrder = this.orderMap.get(utility) ?? void 0;
|
|
798
852
|
if (baseOrder === void 0) {
|
|
799
|
-
const { bare,
|
|
800
|
-
if (
|
|
853
|
+
const { bare, position } = splitImportant(utility);
|
|
854
|
+
if (position) baseOrder = this.orderMap.get(bare) ?? void 0;
|
|
801
855
|
}
|
|
802
856
|
if (baseOrder === void 0) {
|
|
803
857
|
const slashIdx = utility.lastIndexOf("/");
|
|
@@ -805,13 +859,13 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
805
859
|
const base = utility.slice(0, slashIdx);
|
|
806
860
|
baseOrder = this.orderMap.get(base) ?? void 0;
|
|
807
861
|
if (baseOrder === void 0) {
|
|
808
|
-
const { bare,
|
|
809
|
-
if (
|
|
862
|
+
const { bare, position } = splitImportant(base);
|
|
863
|
+
if (position) baseOrder = this.orderMap.get(bare) ?? void 0;
|
|
810
864
|
}
|
|
811
865
|
}
|
|
812
866
|
}
|
|
813
867
|
if (baseOrder === void 0) {
|
|
814
|
-
const stripped =
|
|
868
|
+
const stripped = splitImportant(utility).bare;
|
|
815
869
|
const bracketIdx = stripped.indexOf("[");
|
|
816
870
|
if (bracketIdx > 0) {
|
|
817
871
|
const prefix = stripped.slice(0, bracketIdx);
|
|
@@ -819,7 +873,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
819
873
|
}
|
|
820
874
|
}
|
|
821
875
|
if (baseOrder === void 0) {
|
|
822
|
-
const stripped =
|
|
876
|
+
const stripped = splitImportant(utility).bare;
|
|
823
877
|
const parenIdx = stripped.indexOf("(");
|
|
824
878
|
if (parenIdx > 0) {
|
|
825
879
|
const prefix = stripped.slice(0, parenIdx);
|
|
@@ -827,7 +881,7 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
827
881
|
}
|
|
828
882
|
}
|
|
829
883
|
if (baseOrder === void 0) {
|
|
830
|
-
const stripped =
|
|
884
|
+
const stripped = splitImportant(utility).bare;
|
|
831
885
|
const numericMatch = /^(.+)-(\d+\.?\d*)$/.exec(stripped);
|
|
832
886
|
if (numericMatch && this.getKnownPrefixes().has(numericMatch[1])) baseOrder = this.findOrderByPrefix(numericMatch[1] + "-");
|
|
833
887
|
}
|
|
@@ -846,8 +900,8 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
846
900
|
getCssProperties(className) {
|
|
847
901
|
const result = this.cssPropsMap.get(className);
|
|
848
902
|
if (result) return result;
|
|
849
|
-
const { bare,
|
|
850
|
-
if (
|
|
903
|
+
const { bare, position } = splitImportant(className);
|
|
904
|
+
if (position) return this.cssPropsMap.get(bare) ?? [];
|
|
851
905
|
return [];
|
|
852
906
|
}
|
|
853
907
|
getVariantPriority(variant) {
|
|
@@ -859,12 +913,139 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
859
913
|
getNamedEquivalent(className) {
|
|
860
914
|
const result = this.arbitraryEquivMap.get(className);
|
|
861
915
|
if (result) return result;
|
|
862
|
-
const { bare,
|
|
863
|
-
if (
|
|
916
|
+
const { bare, position } = splitImportant(className);
|
|
917
|
+
if (position) return this.arbitraryEquivMap.get(bare) ?? null;
|
|
864
918
|
return null;
|
|
865
919
|
}
|
|
866
920
|
};
|
|
867
921
|
//#endregion
|
|
922
|
+
//#region src/utils/fatal.ts
|
|
923
|
+
/**
|
|
924
|
+
* Fatal error types and reporter for the deterministic v1 design.
|
|
925
|
+
*
|
|
926
|
+
* The plugin used to silently skip when the design system could not be
|
|
927
|
+
* loaded or the worker thread misbehaved. v1 fails loud: every DS-dependent
|
|
928
|
+
* rule catches these errors and surfaces them as a single fatal lint
|
|
929
|
+
* diagnostic so the user sees exactly what to fix.
|
|
930
|
+
*/
|
|
931
|
+
/**
|
|
932
|
+
* The single `messageId` every DS-dependent rule uses to surface a fatal
|
|
933
|
+
* design-system load failure. Exported as a constant so the rule's
|
|
934
|
+
* `meta.messages` and `context.report()` calls in `reportFatalDsError`
|
|
935
|
+
* cannot drift apart silently.
|
|
936
|
+
*/
|
|
937
|
+
const DS_UNAVAILABLE_MESSAGE_ID = "designSystemUnavailable";
|
|
938
|
+
/** Pre-shaped entry for `meta.messages`. Spread it: `messages: { ..., ...DS_UNAVAILABLE_MESSAGE }`. */
|
|
939
|
+
const DS_UNAVAILABLE_MESSAGE = { [DS_UNAVAILABLE_MESSAGE_ID]: "{{message}}" };
|
|
940
|
+
var OxlintTailwindError = class extends Error {
|
|
941
|
+
/** A short, actionable hint shown alongside the error message. */
|
|
942
|
+
hint;
|
|
943
|
+
constructor(message, hint, options) {
|
|
944
|
+
super(message, options);
|
|
945
|
+
this.name = this.constructor.name;
|
|
946
|
+
this.hint = hint;
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
/** No `settings.tailwindcss.entryPoint` was provided and a DS-dependent rule needs one. */
|
|
950
|
+
var MissingEntryPointError = class extends OxlintTailwindError {};
|
|
951
|
+
/** `settings.tailwindcss.entryPoint` is an array of strings — removed in v1. */
|
|
952
|
+
var DeprecatedEntryPointShapeError = class extends OxlintTailwindError {};
|
|
953
|
+
/** `__unstable__loadDesignSystem` failed (bad CSS path, malformed CSS, install issue). */
|
|
954
|
+
var DesignSystemLoadError = class extends OxlintTailwindError {};
|
|
955
|
+
/** The sort or canonicalize worker thread failed to initialize or timed out. */
|
|
956
|
+
var SortServiceError = class extends OxlintTailwindError {};
|
|
957
|
+
/** Type-guard: did this error originate from the plugin's fail-loud infrastructure? */
|
|
958
|
+
function isFatalError(err) {
|
|
959
|
+
return err instanceof OxlintTailwindError;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Format a fatal error into the diagnostic message a rule should report.
|
|
963
|
+
*
|
|
964
|
+
* Pulls in the optional `hint` (a one-line "here's how to fix it" follow-up)
|
|
965
|
+
* so the message is self-contained even when the user is reading it in an
|
|
966
|
+
* editor without the docs site open.
|
|
967
|
+
*/
|
|
968
|
+
function formatFatalError(err) {
|
|
969
|
+
return err.hint ? `${err.message}\n\nHint: ${err.hint}` : err.message;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Helper for DS-dependent rules to convert a caught fatal error into a
|
|
973
|
+
* single diagnostic. Returns `true` if the error was reported (and the
|
|
974
|
+
* visitor should bail out), `false` otherwise — the rule layer rethrows
|
|
975
|
+
* non-fatal errors to surface real bugs.
|
|
976
|
+
*/
|
|
977
|
+
function reportFatalDsError(context, err, node) {
|
|
978
|
+
if (!isFatalError(err)) return false;
|
|
979
|
+
context.report({
|
|
980
|
+
node,
|
|
981
|
+
messageId: DS_UNAVAILABLE_MESSAGE_ID,
|
|
982
|
+
data: { message: formatFatalError(err) }
|
|
983
|
+
});
|
|
984
|
+
return true;
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* Call `getDS()` and route fatal errors through `reportFatalDsError`.
|
|
988
|
+
*
|
|
989
|
+
* Returns the load result on success, or `null` after a fatal error has been
|
|
990
|
+
* reported (rules should `if (!ds) return` and exit the visitor). Non-fatal
|
|
991
|
+
* errors are re-thrown so genuine bugs aren't hidden.
|
|
992
|
+
*/
|
|
993
|
+
function safeGetDS(getDS, context, node) {
|
|
994
|
+
try {
|
|
995
|
+
return getDS();
|
|
996
|
+
} catch (err) {
|
|
997
|
+
if (reportFatalDsError(context, err, node)) return null;
|
|
998
|
+
throw err;
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
//#endregion
|
|
1002
|
+
//#region src/design-system/tailwind-node.ts
|
|
1003
|
+
/**
|
|
1004
|
+
* Single source of truth for the resolved `@tailwindcss/node` entry path
|
|
1005
|
+
* and its installed version. Three call sites (sync-loader, sort-service,
|
|
1006
|
+
* canonicalize-service) used to each call `require.resolve` independently
|
|
1007
|
+
* — this module resolves once at load and exposes the results.
|
|
1008
|
+
*
|
|
1009
|
+
* The version is read from `dist/../package.json` (the published layout)
|
|
1010
|
+
* with a walk-up fallback for unconventional installs.
|
|
1011
|
+
*/
|
|
1012
|
+
function resolveTailwindNodePath() {
|
|
1013
|
+
try {
|
|
1014
|
+
return __require.resolve("@tailwindcss/node");
|
|
1015
|
+
} catch {
|
|
1016
|
+
return null;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
function readVersionFromEntry(entryPath) {
|
|
1020
|
+
let dir = dirname(entryPath);
|
|
1021
|
+
for (let i = 0; i < 5; i++) {
|
|
1022
|
+
const pkgJson = join(dir, "package.json");
|
|
1023
|
+
if (existsSync(pkgJson)) try {
|
|
1024
|
+
const pkg = JSON.parse(readFileSync(pkgJson, "utf-8"));
|
|
1025
|
+
if (pkg.name === "@tailwindcss/node" && pkg.version) return pkg.version;
|
|
1026
|
+
} catch {}
|
|
1027
|
+
const parent = dirname(dir);
|
|
1028
|
+
if (parent === dir) break;
|
|
1029
|
+
dir = parent;
|
|
1030
|
+
}
|
|
1031
|
+
return "unknown";
|
|
1032
|
+
}
|
|
1033
|
+
const entryPath = resolveTailwindNodePath();
|
|
1034
|
+
/**
|
|
1035
|
+
* Absolute path to `@tailwindcss/node`'s main entry, or `null` if not
|
|
1036
|
+
* installed. Workers receive this path via workerData; the parent does
|
|
1037
|
+
* the resolution because module resolution from a child process's `eval`
|
|
1038
|
+
* context (with cwd inside a tmp dir or under pnpm strict workspaces) can
|
|
1039
|
+
* fail to find the bare specifier.
|
|
1040
|
+
*/
|
|
1041
|
+
const TAILWIND_NODE_PATH = entryPath;
|
|
1042
|
+
/**
|
|
1043
|
+
* Installed `@tailwindcss/node` version, or `'unknown'`. Used as part of
|
|
1044
|
+
* the disk-cache key in sync-loader so cached precompute output is
|
|
1045
|
+
* invalidated when the consumer upgrades.
|
|
1046
|
+
*/
|
|
1047
|
+
const TAILWIND_NODE_VERSION = entryPath !== null ? readVersionFromEntry(entryPath) : "unknown";
|
|
1048
|
+
//#endregion
|
|
868
1049
|
//#region src/design-system/sync-loader.ts
|
|
869
1050
|
/**
|
|
870
1051
|
* Synchronous design system loader using execFileSync.
|
|
@@ -1197,7 +1378,12 @@ async function main() {
|
|
|
1197
1378
|
}
|
|
1198
1379
|
}
|
|
1199
1380
|
|
|
1200
|
-
// Arbitrary equivalents: map arbitrary forms to named equivalents
|
|
1381
|
+
// Arbitrary equivalents: map arbitrary forms to named equivalents.
|
|
1382
|
+
// Enumerate every dash split point so multi-segment utilities (e.g.
|
|
1383
|
+
// bg-card-foreground) emit candidates for every prefix; lastIndexOf
|
|
1384
|
+
// alone drops the shorter prefix and misses multi-segment mappings.
|
|
1385
|
+
// Start at indexOf('-', 1) so negative utilities (e.g. -translate-x-1)
|
|
1386
|
+
// keep their leading '-' in every prefix instead of producing '' + '-[…]'.
|
|
1201
1387
|
const arbitraryEquivalents = {};
|
|
1202
1388
|
const candidates = [];
|
|
1203
1389
|
for (const cls of validClasses) {
|
|
@@ -1209,10 +1395,10 @@ async function main() {
|
|
|
1209
1395
|
const pvMatch = cssText.match(/^\\s+([\\w-]+)\\s*:\\s*(.+?)\\s*;?\\s*$/m);
|
|
1210
1396
|
if (!pvMatch) continue;
|
|
1211
1397
|
const value = pvMatch[2].trim().replace(/;$/, '');
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1398
|
+
for (let dashPos = cls.indexOf('-', 1); dashPos > 0; dashPos = cls.indexOf('-', dashPos + 1)) {
|
|
1399
|
+
const prefix = cls.slice(0, dashPos);
|
|
1400
|
+
candidates.push({ arbitraryForm: prefix + '-[' + value + ']', namedCls: cls, namedCss: cssText });
|
|
1401
|
+
}
|
|
1216
1402
|
}
|
|
1217
1403
|
function extractDeclarations(css) {
|
|
1218
1404
|
const openBrace = css.indexOf('{');
|
|
@@ -1236,62 +1422,73 @@ async function main() {
|
|
|
1236
1422
|
main().catch(e => { process.stderr.write(e.message); process.exit(1); });
|
|
1237
1423
|
`;
|
|
1238
1424
|
const CACHE_DIR = join(tmpdir(), "oxlint-tailwindcss");
|
|
1239
|
-
const CACHE_VERSION = 13;
|
|
1240
1425
|
/**
|
|
1241
|
-
*
|
|
1242
|
-
*
|
|
1243
|
-
*
|
|
1244
|
-
*
|
|
1245
|
-
*
|
|
1246
|
-
*
|
|
1247
|
-
*
|
|
1426
|
+
* Cache key derived from:
|
|
1427
|
+
* - md5(PRECOMPUTE_SCRIPT): auto-invalidates when our precompute logic changes,
|
|
1428
|
+
* since the shape and content of the cached JSON is fully determined by what
|
|
1429
|
+
* this script prints to stdout.
|
|
1430
|
+
* - @tailwindcss/node version: auto-invalidates when the consumer upgrades
|
|
1431
|
+
* tailwindcss (e.g. 4.2 → 4.3 adding `zoom-*`, `tab-*`, `scrollbar-*` etc.),
|
|
1432
|
+
* so `validClasses`, `cssProps`, `canonical`, and `arbitraryEquivalents`
|
|
1433
|
+
* reflect the installed version. @tailwindcss/node and tailwindcss are
|
|
1434
|
+
* published together, so their versions match.
|
|
1248
1435
|
*/
|
|
1249
|
-
function
|
|
1250
|
-
return
|
|
1436
|
+
function computeCacheKey(scriptContent, tailwindVersion) {
|
|
1437
|
+
return `${createHash("md5").update(scriptContent).digest("hex").slice(0, 8)}:${tailwindVersion}`;
|
|
1251
1438
|
}
|
|
1439
|
+
const CACHE_KEY = computeCacheKey(PRECOMPUTE_SCRIPT, TAILWIND_NODE_VERSION);
|
|
1252
1440
|
function computeContentHash(content) {
|
|
1253
|
-
return createHash("md5").update(
|
|
1441
|
+
return createHash("md5").update(`${CACHE_KEY}:${content}`).digest("hex");
|
|
1254
1442
|
}
|
|
1255
1443
|
function getContentCachePath(contentHash) {
|
|
1256
1444
|
return join(CACHE_DIR, `${contentHash}.json`);
|
|
1257
1445
|
}
|
|
1258
1446
|
function tryReadCache(cachePath) {
|
|
1447
|
+
const raw = tryReadRawCache(cachePath);
|
|
1448
|
+
if (raw === null) return null;
|
|
1449
|
+
try {
|
|
1450
|
+
return JSON.parse(raw);
|
|
1451
|
+
} catch {
|
|
1452
|
+
return null;
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
function tryReadRawCache(cachePath) {
|
|
1259
1456
|
try {
|
|
1260
|
-
return
|
|
1457
|
+
return readFileSync(cachePath, "utf-8");
|
|
1261
1458
|
} catch {
|
|
1262
1459
|
return null;
|
|
1263
1460
|
}
|
|
1264
1461
|
}
|
|
1265
|
-
function
|
|
1462
|
+
function writeCacheFile(contentCachePath, data) {
|
|
1266
1463
|
try {
|
|
1267
1464
|
mkdirSync(CACHE_DIR, { recursive: true });
|
|
1268
1465
|
writeFileSync(contentCachePath, data);
|
|
1269
|
-
writeFileSync(mtimeIndexPath, contentHash);
|
|
1270
1466
|
} catch {}
|
|
1271
1467
|
}
|
|
1272
|
-
|
|
1273
|
-
|
|
1468
|
+
const LOCK_POLL_MS = 50;
|
|
1469
|
+
const SLEEP_BUFFER = new Int32Array(new SharedArrayBuffer(4));
|
|
1470
|
+
function syncSleep(ms) {
|
|
1471
|
+
Atomics.wait(SLEEP_BUFFER, 0, 0, ms);
|
|
1472
|
+
}
|
|
1473
|
+
/** Age of the lock file in ms, or Infinity if it has already vanished. */
|
|
1474
|
+
function lockAgeMs(lockPath) {
|
|
1274
1475
|
try {
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
return cached;
|
|
1290
|
-
}
|
|
1291
|
-
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
1292
|
-
const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
1476
|
+
return Date.now() - statSync(lockPath).mtimeMs;
|
|
1477
|
+
} catch {
|
|
1478
|
+
return Number.POSITIVE_INFINITY;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
function tryUnlink(path) {
|
|
1482
|
+
try {
|
|
1483
|
+
unlinkSync(path);
|
|
1484
|
+
} catch {}
|
|
1485
|
+
}
|
|
1486
|
+
/** Fork the precompute child once. Throws `DesignSystemLoadError` on failure. */
|
|
1487
|
+
function runPrecompute(resolvedPath, tailwindNodePath, timeout) {
|
|
1488
|
+
try {
|
|
1489
|
+
return execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
1293
1490
|
encoding: "utf-8",
|
|
1294
|
-
timeout
|
|
1491
|
+
timeout,
|
|
1295
1492
|
maxBuffer: 50 * 1024 * 1024,
|
|
1296
1493
|
env: {
|
|
1297
1494
|
...process.env,
|
|
@@ -1300,108 +1497,75 @@ function loadDesignSystemSync(cssPath, timeout) {
|
|
|
1300
1497
|
},
|
|
1301
1498
|
cwd: dirname(resolvedPath)
|
|
1302
1499
|
});
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
1307
|
-
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.`);
|
|
1308
|
-
else console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, msg);
|
|
1309
|
-
return null;
|
|
1500
|
+
} catch (cause) {
|
|
1501
|
+
const causeError = cause instanceof Error ? cause : void 0;
|
|
1502
|
+
throw new DesignSystemLoadError(`Failed to precompute design system from "${resolvedPath}": ${causeError?.message ?? String(cause)}`, "Check the CSS file for syntax errors. If this looks like a timeout, raise `settings.tailwindcss.timeout`.", { cause: causeError });
|
|
1310
1503
|
}
|
|
1311
1504
|
}
|
|
1312
|
-
//#endregion
|
|
1313
|
-
//#region src/design-system/auto-detect.ts
|
|
1314
|
-
const IMPORT_RE = /@import\s+['"]([^'"]+)['"]/g;
|
|
1315
1505
|
/**
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
1506
|
+
* Return the raw precompute JSON for `contentHash`, forking at most one child
|
|
1507
|
+
* across all isolates competing for the same hash. Whoever wins the lock
|
|
1508
|
+
* computes and writes the cache; the rest wait for that file.
|
|
1318
1509
|
*/
|
|
1319
|
-
function
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
if (parent === dir) break;
|
|
1327
|
-
dir = parent;
|
|
1510
|
+
function computeWithLock(resolvedPath, tailwindNodePath, contentHash, cachePath, timeout) {
|
|
1511
|
+
const lockPath = join(CACHE_DIR, `${contentHash}.lock`);
|
|
1512
|
+
const staleMs = timeout + 3e4;
|
|
1513
|
+
try {
|
|
1514
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
1515
|
+
} catch {
|
|
1516
|
+
return runPrecompute(resolvedPath, tailwindNodePath, timeout);
|
|
1328
1517
|
}
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
*/
|
|
1335
|
-
function hasTailwindSignalInImports(content, baseDir, signals) {
|
|
1336
|
-
IMPORT_RE.lastIndex = 0;
|
|
1337
|
-
let match;
|
|
1338
|
-
while ((match = IMPORT_RE.exec(content)) !== null) {
|
|
1339
|
-
const specifier = match[1];
|
|
1340
|
-
if (specifier === "tailwindcss") continue;
|
|
1341
|
-
const resolved = resolveImport(specifier, baseDir);
|
|
1342
|
-
if (!resolved) continue;
|
|
1518
|
+
const start = Date.now();
|
|
1519
|
+
for (;;) {
|
|
1520
|
+
const raw = tryReadRawCache(cachePath);
|
|
1521
|
+
if (raw !== null) return raw;
|
|
1522
|
+
let lockFd;
|
|
1343
1523
|
try {
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1524
|
+
lockFd = openSync(lockPath, "wx");
|
|
1525
|
+
} catch (cause) {
|
|
1526
|
+
if (cause.code === "EEXIST") {
|
|
1527
|
+
if (lockAgeMs(lockPath) > staleMs || Date.now() - start > staleMs * 2) tryUnlink(lockPath);
|
|
1528
|
+
else syncSleep(LOCK_POLL_MS);
|
|
1529
|
+
continue;
|
|
1530
|
+
}
|
|
1531
|
+
return runPrecompute(resolvedPath, tailwindNodePath, timeout);
|
|
1532
|
+
}
|
|
1533
|
+
try {
|
|
1534
|
+
const stdout = runPrecompute(resolvedPath, tailwindNodePath, timeout);
|
|
1535
|
+
writeCacheFile(cachePath, stdout);
|
|
1536
|
+
return stdout;
|
|
1537
|
+
} finally {
|
|
1538
|
+
closeSync(lockFd);
|
|
1539
|
+
tryUnlink(lockPath);
|
|
1348
1540
|
}
|
|
1349
1541
|
}
|
|
1350
|
-
return false;
|
|
1351
1542
|
}
|
|
1352
|
-
const CANDIDATE_DIRS = [
|
|
1353
|
-
"src",
|
|
1354
|
-
".",
|
|
1355
|
-
"app",
|
|
1356
|
-
"styles",
|
|
1357
|
-
"style",
|
|
1358
|
-
"css",
|
|
1359
|
-
"assets",
|
|
1360
|
-
"assets/css",
|
|
1361
|
-
"resources/css"
|
|
1362
|
-
];
|
|
1363
|
-
const CANDIDATE_NAMES = [
|
|
1364
|
-
"app",
|
|
1365
|
-
"globals",
|
|
1366
|
-
"global",
|
|
1367
|
-
"style",
|
|
1368
|
-
"styles",
|
|
1369
|
-
"index",
|
|
1370
|
-
"main",
|
|
1371
|
-
"tailwind",
|
|
1372
|
-
"tailwindcss"
|
|
1373
|
-
];
|
|
1374
|
-
const CANDIDATE_PATHS = CANDIDATE_DIRS.flatMap((dir) => CANDIDATE_NAMES.map((name) => dir === "." ? `${name}.css` : `${dir}/${name}.css`));
|
|
1375
|
-
const TAILWIND_SIGNALS = [
|
|
1376
|
-
"@import \"tailwindcss\"",
|
|
1377
|
-
"@import 'tailwindcss'",
|
|
1378
|
-
"@tailwind base",
|
|
1379
|
-
"@import tailwindcss"
|
|
1380
|
-
];
|
|
1381
1543
|
/**
|
|
1382
|
-
*
|
|
1383
|
-
*
|
|
1544
|
+
* Synchronously load and precompute the design system for a CSS file.
|
|
1545
|
+
*
|
|
1546
|
+
* Throws `DesignSystemLoadError` on any failure (missing file, child
|
|
1547
|
+
* process error, missing `@tailwindcss/node`, etc.). Callers catch via
|
|
1548
|
+
* `reportFatalDsError` to surface a single Program-level diagnostic.
|
|
1384
1549
|
*/
|
|
1385
|
-
function
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1550
|
+
function loadDesignSystemSync(cssPath, timeout) {
|
|
1551
|
+
const resolvedPath = resolve(cssPath);
|
|
1552
|
+
let content;
|
|
1553
|
+
try {
|
|
1554
|
+
content = readFileSync(resolvedPath, "utf-8");
|
|
1555
|
+
} catch (cause) {
|
|
1556
|
+
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 });
|
|
1557
|
+
}
|
|
1558
|
+
const contentHash = computeContentHash(content);
|
|
1559
|
+
const contentCachePath = getContentCachePath(contentHash);
|
|
1560
|
+
const cached = tryReadCache(contentCachePath);
|
|
1561
|
+
if (cached) return cached;
|
|
1562
|
+
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.");
|
|
1563
|
+
const stdout = computeWithLock(resolvedPath, TAILWIND_NODE_PATH, contentHash, contentCachePath, timeout ?? 6e4);
|
|
1564
|
+
try {
|
|
1565
|
+
return JSON.parse(stdout);
|
|
1566
|
+
} catch (cause) {
|
|
1567
|
+
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 });
|
|
1403
1568
|
}
|
|
1404
|
-
return null;
|
|
1405
1569
|
}
|
|
1406
1570
|
//#endregion
|
|
1407
1571
|
//#region src/design-system/debug.ts
|
|
@@ -1432,48 +1596,143 @@ function debugLog(message) {
|
|
|
1432
1596
|
if (_enabled || process.env.DEBUG === "oxlint-tailwindcss") console.error(`${PREFIX} ${message}`);
|
|
1433
1597
|
}
|
|
1434
1598
|
//#endregion
|
|
1599
|
+
//#region src/utils/context.ts
|
|
1600
|
+
/**
|
|
1601
|
+
* Safely read `context.options[0]`. Returns `undefined` if the getter throws
|
|
1602
|
+
* (oxlint < 1.31.0) or if options aren't populated yet.
|
|
1603
|
+
*/
|
|
1604
|
+
function safeOptions(context) {
|
|
1605
|
+
try {
|
|
1606
|
+
return context.options?.[0] ?? void 0;
|
|
1607
|
+
} catch {
|
|
1608
|
+
return;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
/**
|
|
1612
|
+
* Safely read `context.settings`. Same try/catch pattern as `safeOptions`
|
|
1613
|
+
* for the createOnce-throws case.
|
|
1614
|
+
*/
|
|
1615
|
+
function safeSettings(context) {
|
|
1616
|
+
try {
|
|
1617
|
+
return context.settings ?? void 0;
|
|
1618
|
+
} catch {
|
|
1619
|
+
return;
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
/** Safely read `context.filename`. Same try/catch pattern. */
|
|
1623
|
+
function safeFilename(context) {
|
|
1624
|
+
try {
|
|
1625
|
+
return context.filename ?? void 0;
|
|
1626
|
+
} catch {
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Lazily memoize a compiled options object across visitor calls.
|
|
1632
|
+
*
|
|
1633
|
+
* Every rule with options reinvents:
|
|
1634
|
+
*
|
|
1635
|
+
* let _x: CompiledOptions | null = null
|
|
1636
|
+
* function getX() {
|
|
1637
|
+
* if (_x === null) _x = compile(safeOptions<RawOptions>(context))
|
|
1638
|
+
* return _x
|
|
1639
|
+
* }
|
|
1640
|
+
*
|
|
1641
|
+
* This wraps the pattern: pass the context and a `compile` function (which
|
|
1642
|
+
* receives the raw options object — possibly undefined) and get a memoized
|
|
1643
|
+
* thunk back. Inside `createOnce`, options aren't available yet, so the first
|
|
1644
|
+
* visitor call triggers compilation; every subsequent call is O(1).
|
|
1645
|
+
*/
|
|
1646
|
+
function createLazyOptions(context, compile) {
|
|
1647
|
+
let cached = null;
|
|
1648
|
+
return () => {
|
|
1649
|
+
if (cached) return cached.value;
|
|
1650
|
+
cached = { value: compile(safeOptions(context)) };
|
|
1651
|
+
return cached.value;
|
|
1652
|
+
};
|
|
1653
|
+
}
|
|
1654
|
+
//#endregion
|
|
1435
1655
|
//#region src/design-system/loader.ts
|
|
1436
1656
|
const dsCache = /* @__PURE__ */ new Map();
|
|
1437
|
-
|
|
1438
|
-
|
|
1657
|
+
function isEntryPointMapping(v) {
|
|
1658
|
+
if (typeof v !== "object" || v === null) return false;
|
|
1659
|
+
const m = v;
|
|
1660
|
+
return (typeof m.files === "string" || Array.isArray(m.files) && m.files.every((s) => typeof s === "string")) && typeof m.use === "string";
|
|
1661
|
+
}
|
|
1439
1662
|
/**
|
|
1440
|
-
*
|
|
1441
|
-
*
|
|
1663
|
+
* Read `settings.tailwindcss.entryPoint` and normalize it to the v1 shape.
|
|
1664
|
+
*
|
|
1665
|
+
* Throws `DeprecatedEntryPointShapeError` if the legacy `string[]` shape is
|
|
1666
|
+
* still in use (removed in v1; the user must migrate to `{ files, use }[]`).
|
|
1442
1667
|
*/
|
|
1443
1668
|
function entryPointFromSettings(settings) {
|
|
1444
1669
|
const tw = settings?.tailwindcss;
|
|
1445
|
-
if (tw
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1670
|
+
if (!tw || typeof tw !== "object" || !("entryPoint" in tw)) return void 0;
|
|
1671
|
+
const ep = tw.entryPoint;
|
|
1672
|
+
if (typeof ep === "string") return ep;
|
|
1673
|
+
if (Array.isArray(ep)) {
|
|
1674
|
+
if (ep.length === 0) return void 0;
|
|
1675
|
+
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");
|
|
1676
|
+
if (ep.every(isEntryPointMapping)) return ep;
|
|
1677
|
+
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).");
|
|
1449
1678
|
}
|
|
1450
1679
|
}
|
|
1451
1680
|
/**
|
|
1452
|
-
*
|
|
1453
|
-
*
|
|
1681
|
+
* Compile a minimal glob pattern to a regular expression.
|
|
1682
|
+
*
|
|
1683
|
+
* Supported syntax: `**` (any depth), `*` (any chars except `/`), `?` (one
|
|
1684
|
+
* char except `/`), plus literal segments. Anchored on both ends.
|
|
1685
|
+
*
|
|
1686
|
+
* Memoized across calls because `resolveByGlobMapping` runs per lint visit;
|
|
1687
|
+
* recompiling the same pattern per AST node would be wasted work.
|
|
1454
1688
|
*/
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
const
|
|
1458
|
-
|
|
1459
|
-
let
|
|
1460
|
-
for (const ep of entryPoints) {
|
|
1461
|
-
const len = commonPrefixLength(fileDir, dirname(resolve(ep)));
|
|
1462
|
-
if (len > bestLen) {
|
|
1463
|
-
bestLen = len;
|
|
1464
|
-
best = ep;
|
|
1465
|
-
}
|
|
1466
|
-
}
|
|
1467
|
-
return best;
|
|
1468
|
-
}
|
|
1469
|
-
function commonPrefixLength(a, b) {
|
|
1470
|
-
const len = Math.min(a.length, b.length);
|
|
1689
|
+
const globRegexCache = /* @__PURE__ */ new Map();
|
|
1690
|
+
function globToRegExp(glob) {
|
|
1691
|
+
const cached = globRegexCache.get(glob);
|
|
1692
|
+
if (cached) return cached;
|
|
1693
|
+
let re = "";
|
|
1471
1694
|
let i = 0;
|
|
1472
|
-
while (i <
|
|
1473
|
-
|
|
1695
|
+
while (i < glob.length) {
|
|
1696
|
+
const c = glob[i];
|
|
1697
|
+
if (c === "*" && glob[i + 1] === "*") {
|
|
1698
|
+
re += ".*";
|
|
1699
|
+
i += 2;
|
|
1700
|
+
if (glob[i] === "/") i++;
|
|
1701
|
+
} else if (c === "*") {
|
|
1702
|
+
re += "[^/]*";
|
|
1703
|
+
i++;
|
|
1704
|
+
} else if (c === "?") {
|
|
1705
|
+
re += "[^/]";
|
|
1706
|
+
i++;
|
|
1707
|
+
} else if ("+()|.\\$^{}[]".includes(c)) {
|
|
1708
|
+
re += "\\" + c;
|
|
1709
|
+
i++;
|
|
1710
|
+
} else {
|
|
1711
|
+
re += c;
|
|
1712
|
+
i++;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
const compiled = new RegExp("^" + re + "$");
|
|
1716
|
+
globRegexCache.set(glob, compiled);
|
|
1717
|
+
return compiled;
|
|
1718
|
+
}
|
|
1719
|
+
function matchesAnyGlob(filePath, globs) {
|
|
1720
|
+
return (typeof globs === "string" ? [globs] : globs).some((g) => globToRegExp(g).test(filePath));
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
* Resolve a file path to its entry-point CSS via a mapping array.
|
|
1724
|
+
* First-match-wins. Paths are matched relative to `baseDir` (typically the
|
|
1725
|
+
* process CWD where oxlint runs), with forward-slash normalization for
|
|
1726
|
+
* cross-platform consistency. Returns the absolute CSS path, or `null` if
|
|
1727
|
+
* no mapping matches.
|
|
1728
|
+
*/
|
|
1729
|
+
function resolveByGlobMapping(mappings, filePath, baseDir) {
|
|
1730
|
+
const relPath = relative(baseDir, resolve(filePath)).split("\\").join("/");
|
|
1731
|
+
for (const m of mappings) if (matchesAnyGlob(relPath, m.files)) return resolve(baseDir, m.use);
|
|
1732
|
+
return null;
|
|
1474
1733
|
}
|
|
1475
1734
|
/**
|
|
1476
|
-
*
|
|
1735
|
+
* Extract `rootFontSize` from `settings.tailwindcss` (default: 16).
|
|
1477
1736
|
*/
|
|
1478
1737
|
function rootFontSizeFromSettings(settings) {
|
|
1479
1738
|
const tw = settings?.tailwindcss;
|
|
@@ -1483,9 +1742,6 @@ function rootFontSizeFromSettings(settings) {
|
|
|
1483
1742
|
}
|
|
1484
1743
|
return 16;
|
|
1485
1744
|
}
|
|
1486
|
-
/**
|
|
1487
|
-
* Extracts `timeout` from `context.settings.tailwindcss`.
|
|
1488
|
-
*/
|
|
1489
1745
|
function timeoutFromSettings(settings) {
|
|
1490
1746
|
const tw = settings?.tailwindcss;
|
|
1491
1747
|
if (tw && typeof tw === "object" && "timeout" in tw) {
|
|
@@ -1494,147 +1750,157 @@ function timeoutFromSettings(settings) {
|
|
|
1494
1750
|
}
|
|
1495
1751
|
}
|
|
1496
1752
|
/**
|
|
1497
|
-
*
|
|
1498
|
-
* resolve to the same entry point. Avoids repeated filesystem walks.
|
|
1499
|
-
*/
|
|
1500
|
-
function cachedAutoDetect(filePath) {
|
|
1501
|
-
if (!filePath) return autoDetectEntryPoint(filePath);
|
|
1502
|
-
const dir = dirname(resolve(filePath));
|
|
1503
|
-
const cached = autoDetectCache.get(dir);
|
|
1504
|
-
if (cached !== void 0) return cached;
|
|
1505
|
-
const result = autoDetectEntryPoint(filePath);
|
|
1506
|
-
autoDetectCache.set(dir, result);
|
|
1507
|
-
return result;
|
|
1508
|
-
}
|
|
1509
|
-
/**
|
|
1510
|
-
* Returns the design system cache, loading synchronously on first call.
|
|
1511
|
-
* Uses execFileSync internally to bridge the async Tailwind API.
|
|
1753
|
+
* Load the design system for a specific CSS entry point.
|
|
1512
1754
|
*
|
|
1513
|
-
*
|
|
1514
|
-
*
|
|
1515
|
-
*
|
|
1755
|
+
* Throws `DesignSystemLoadError` if the load fails for any reason. Callers
|
|
1756
|
+
* (rules via `createLazyLoader`) catch via `reportFatalDsError` and surface
|
|
1757
|
+
* the failure as a single Program-level diagnostic.
|
|
1516
1758
|
*
|
|
1517
|
-
*
|
|
1759
|
+
* Cached per absolute entry-point path, keyed by mtime. In monorepos each
|
|
1760
|
+
* unique CSS gets its own cache entry.
|
|
1518
1761
|
*/
|
|
1519
|
-
function getLoadedDesignSystem(
|
|
1520
|
-
const settingsEntry = entryPointFromSettings(settings);
|
|
1521
|
-
const explicitEntry = entryPoint ?? (Array.isArray(settingsEntry) ? resolveClosestEntryPoint(settingsEntry, filePath) : settingsEntry);
|
|
1522
|
-
const cssPath = explicitEntry ?? cachedAutoDetect(filePath) ?? lastLoadedPath;
|
|
1523
|
-
if (!cssPath) return null;
|
|
1762
|
+
function getLoadedDesignSystem(cssPath, settings) {
|
|
1524
1763
|
const resolvedPath = resolve(cssPath);
|
|
1764
|
+
let mtime;
|
|
1525
1765
|
try {
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
entryPoint: resolvedPath
|
|
1547
|
-
};
|
|
1548
|
-
} catch {
|
|
1549
|
-
return null;
|
|
1550
|
-
}
|
|
1766
|
+
mtime = statSync(resolvedPath).mtimeMs;
|
|
1767
|
+
} catch (cause) {
|
|
1768
|
+
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 });
|
|
1769
|
+
}
|
|
1770
|
+
const cached = dsCache.get(resolvedPath);
|
|
1771
|
+
if (cached && cached.mtime === mtime) return {
|
|
1772
|
+
cache: cached.cache,
|
|
1773
|
+
entryPoint: resolvedPath
|
|
1774
|
+
};
|
|
1775
|
+
const data = loadDesignSystemSync(resolvedPath, timeoutFromSettings(settings));
|
|
1776
|
+
const cache = DesignSystemCache.fromPrecomputed(data);
|
|
1777
|
+
dsCache.set(resolvedPath, {
|
|
1778
|
+
cache,
|
|
1779
|
+
mtime
|
|
1780
|
+
});
|
|
1781
|
+
debugLog(`Loaded design system from "${resolvedPath}"`);
|
|
1782
|
+
return {
|
|
1783
|
+
cache,
|
|
1784
|
+
entryPoint: resolvedPath
|
|
1785
|
+
};
|
|
1551
1786
|
}
|
|
1552
1787
|
/**
|
|
1553
|
-
*
|
|
1788
|
+
* Lazy DS loader for a rule. Resolves the correct design system for each
|
|
1789
|
+
* file being linted, using (in priority order):
|
|
1790
|
+
*
|
|
1791
|
+
* 1. Rule option `entryPoint` (a string path).
|
|
1792
|
+
* 2. `settings.tailwindcss.entryPoint` as a string.
|
|
1793
|
+
* 3. `settings.tailwindcss.entryPoint` as an `EntryPointMapping[]` —
|
|
1794
|
+
* first matching glob (relative to CWD) wins.
|
|
1554
1795
|
*
|
|
1555
|
-
*
|
|
1556
|
-
*
|
|
1557
|
-
*
|
|
1796
|
+
* If none of those produce an entry point for the current file, throws
|
|
1797
|
+
* `MissingEntryPointError`. If the resolved CSS fails to load, throws
|
|
1798
|
+
* `DesignSystemLoadError`. Both extend `OxlintTailwindError` and are
|
|
1799
|
+
* meant to be caught by `reportFatalDsError` in the rule's `check()`.
|
|
1558
1800
|
*
|
|
1559
|
-
*
|
|
1560
|
-
*
|
|
1801
|
+
* `context.settings` and `context.filename` are unavailable in `createOnce`,
|
|
1802
|
+
* so the actual work is deferred until the returned thunk is invoked from
|
|
1803
|
+
* inside a visitor.
|
|
1561
1804
|
*/
|
|
1562
1805
|
function createLazyLoader(context) {
|
|
1563
|
-
let lastFilePath;
|
|
1564
|
-
let lastResult = null;
|
|
1565
|
-
let fixedEntryResolved = false;
|
|
1566
|
-
let fixedResult = null;
|
|
1567
1806
|
let debugInitialized = false;
|
|
1807
|
+
let lastFilePath;
|
|
1808
|
+
let lastResult;
|
|
1568
1809
|
return () => {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
let settings;
|
|
1574
|
-
try {
|
|
1575
|
-
settings = context.settings;
|
|
1576
|
-
} catch {}
|
|
1577
|
-
let filePath;
|
|
1578
|
-
try {
|
|
1579
|
-
filePath = context.filename;
|
|
1580
|
-
} catch {}
|
|
1810
|
+
const filePath = safeFilename(context);
|
|
1811
|
+
if (filePath !== void 0 && filePath === lastFilePath && lastResult) return lastResult;
|
|
1812
|
+
const ruleOptionEntry = safeOptions(context)?.entryPoint;
|
|
1813
|
+
const settings = safeSettings(context);
|
|
1581
1814
|
if (!debugInitialized && settings) {
|
|
1582
1815
|
debugInitialized = true;
|
|
1583
1816
|
setDebugEnabled(isDebugEnabled(settings));
|
|
1584
1817
|
}
|
|
1585
|
-
const
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
fixedEntryResolved = true;
|
|
1590
|
-
fixedResult = getLoadedDesignSystem(fixedEntry, settings, filePath);
|
|
1591
|
-
if (fixedResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), fixedResult.entryPoint)}`);
|
|
1592
|
-
return fixedResult;
|
|
1593
|
-
}
|
|
1594
|
-
if (Array.isArray(settingsEntry)) {
|
|
1595
|
-
const closest = resolveClosestEntryPoint(settingsEntry, filePath);
|
|
1596
|
-
if (filePath && filePath === lastFilePath) return lastResult;
|
|
1597
|
-
if (filePath) lastFilePath = filePath;
|
|
1598
|
-
lastResult = getLoadedDesignSystem(closest, settings, filePath);
|
|
1599
|
-
if (lastResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
|
|
1600
|
-
return lastResult;
|
|
1601
|
-
}
|
|
1602
|
-
if (filePath && filePath === lastFilePath) return lastResult;
|
|
1603
|
-
if (filePath) lastFilePath = filePath;
|
|
1604
|
-
lastResult = getLoadedDesignSystem(void 0, settings, filePath);
|
|
1605
|
-
if (lastResult && filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
|
|
1818
|
+
const cssPath = resolveEntryPointForFile(ruleOptionEntry, entryPointFromSettings(settings), filePath);
|
|
1819
|
+
lastFilePath = filePath;
|
|
1820
|
+
lastResult = getLoadedDesignSystem(cssPath, settings);
|
|
1821
|
+
if (filePath) debugLog(`${relative(process.cwd(), filePath)} → ${relative(process.cwd(), lastResult.entryPoint)}`);
|
|
1606
1822
|
return lastResult;
|
|
1607
1823
|
};
|
|
1608
1824
|
}
|
|
1609
|
-
//#endregion
|
|
1610
|
-
//#region src/types.ts
|
|
1611
1825
|
/**
|
|
1612
|
-
*
|
|
1613
|
-
*
|
|
1614
|
-
* In oxlint, `context.options` is `null` inside `createOnce()` — options are
|
|
1615
|
-
* only populated when visitors run. Call this lazily (inside `check()` or a
|
|
1616
|
-
* visitor) to get the user's configured options.
|
|
1826
|
+
* Pure resolution from (rule option, settings entry, file path) to a CSS
|
|
1827
|
+
* entry-point path. Exported for testability.
|
|
1617
1828
|
*
|
|
1618
|
-
*
|
|
1829
|
+
* Throws `MissingEntryPointError` if nothing resolves. The error message
|
|
1830
|
+
* names the file so the user knows which one tripped the configuration.
|
|
1619
1831
|
*/
|
|
1620
|
-
function
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1832
|
+
function resolveEntryPointForFile(ruleOptionEntry, settingsEntry, filePath) {
|
|
1833
|
+
if (ruleOptionEntry) return ruleOptionEntry;
|
|
1834
|
+
if (typeof settingsEntry === "string") return settingsEntry;
|
|
1835
|
+
if (Array.isArray(settingsEntry)) {
|
|
1836
|
+
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.");
|
|
1837
|
+
const matched = resolveByGlobMapping(settingsEntry, filePath, process.cwd());
|
|
1838
|
+
if (matched) return matched;
|
|
1839
|
+
throw new MissingEntryPointError(`No \`entryPoint\` mapping matched \`${filePath}\`.`, "Add a mapping covering this file to `settings.tailwindcss.entryPoint`, or use a `\"**\"` fallback entry.");
|
|
1625
1840
|
}
|
|
1841
|
+
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).");
|
|
1626
1842
|
}
|
|
1843
|
+
//#endregion
|
|
1844
|
+
//#region src/utils/report.ts
|
|
1627
1845
|
/**
|
|
1628
|
-
*
|
|
1846
|
+
* Shared reporter for the "first-as-fix, rest-as-suggestion" pattern.
|
|
1847
|
+
*
|
|
1848
|
+
* Nine rules (enforce-canonical, enforce-logical, enforce-physical,
|
|
1849
|
+
* enforce-consistent-variable-syntax, enforce-consistent-important-position,
|
|
1850
|
+
* enforce-negative-arbitrary-values, no-deprecated-classes,
|
|
1851
|
+
* no-unnecessary-arbitrary-value, prefer-theme-tokens) all emit
|
|
1852
|
+
* replacements via the same shape:
|
|
1853
|
+
*
|
|
1854
|
+
* 1. Collect `{ cls, replacement }` entries from a class string.
|
|
1855
|
+
* 2. Rebuild the full class string with replacements applied.
|
|
1856
|
+
* 3. Emit ONE diagnostic with the autofix attached.
|
|
1857
|
+
* 4. Emit one MORE diagnostic per remaining offender, each carrying
|
|
1858
|
+
* the same autofix as a `suggest`.
|
|
1629
1859
|
*
|
|
1630
|
-
*
|
|
1631
|
-
*
|
|
1860
|
+
* oxlint's fixer is single-shot per AST node, so multiple offenders in the
|
|
1861
|
+
* same class string can only get one mechanical fix; the rest stay as
|
|
1862
|
+
* suggestions the IDE can offer interactively.
|
|
1632
1863
|
*/
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1864
|
+
/**
|
|
1865
|
+
* Emit autofix + suggestion diagnostics for a list of class replacements.
|
|
1866
|
+
*
|
|
1867
|
+
* If `offending` is empty, no-op. The fixer's `replaceTextRange` is shared
|
|
1868
|
+
* across all diagnostics — the first one is the primary fix, the rest carry
|
|
1869
|
+
* the same fix as a suggestion (oxlint can only apply one autofix per node).
|
|
1870
|
+
*/
|
|
1871
|
+
function reportClassReplacements(context, loc, split, classes, offending, options) {
|
|
1872
|
+
if (offending.length === 0) return;
|
|
1873
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1874
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => replacements.get(c) ?? c));
|
|
1875
|
+
const replacementKey = options.replacementKey ?? "replacement";
|
|
1876
|
+
const suggestMessageId = options.suggestMessageId ?? "suggestReplace";
|
|
1877
|
+
const applyFix = (fixer) => fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1878
|
+
const report = context.report;
|
|
1879
|
+
for (let i = 0; i < offending.length; i++) {
|
|
1880
|
+
const { cls, replacement } = offending[i];
|
|
1881
|
+
const primaryData = {
|
|
1882
|
+
className: cls,
|
|
1883
|
+
[replacementKey]: replacement
|
|
1884
|
+
};
|
|
1885
|
+
if (i === 0) report({
|
|
1886
|
+
node: loc.node,
|
|
1887
|
+
messageId: options.messageId,
|
|
1888
|
+
data: primaryData,
|
|
1889
|
+
fix: applyFix
|
|
1890
|
+
});
|
|
1891
|
+
else report({
|
|
1892
|
+
node: loc.node,
|
|
1893
|
+
messageId: options.messageId,
|
|
1894
|
+
data: primaryData,
|
|
1895
|
+
suggest: [{
|
|
1896
|
+
messageId: suggestMessageId,
|
|
1897
|
+
data: {
|
|
1898
|
+
className: cls,
|
|
1899
|
+
replacement
|
|
1900
|
+
},
|
|
1901
|
+
fix: applyFix
|
|
1902
|
+
}]
|
|
1903
|
+
});
|
|
1638
1904
|
}
|
|
1639
1905
|
}
|
|
1640
1906
|
//#endregion
|
|
@@ -1667,71 +1933,30 @@ const noDeprecatedClasses = defineRule({
|
|
|
1667
1933
|
additionalProperties: false
|
|
1668
1934
|
}],
|
|
1669
1935
|
hasSuggestions: true,
|
|
1936
|
+
defaultOptions: [{}],
|
|
1670
1937
|
messages: {
|
|
1671
1938
|
deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead.",
|
|
1672
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
1939
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
1940
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
1673
1941
|
}
|
|
1674
1942
|
},
|
|
1675
1943
|
createOnce(context) {
|
|
1676
1944
|
const getDS = createLazyLoader(context);
|
|
1677
1945
|
function check(locations) {
|
|
1678
|
-
|
|
1946
|
+
if (locations.length === 0) return;
|
|
1947
|
+
if (!safeGetDS(getDS, context, locations[0].node)) return;
|
|
1679
1948
|
for (const loc of locations) {
|
|
1680
1949
|
const split = splitClassesWithSeparators(loc.value);
|
|
1681
|
-
const
|
|
1682
|
-
const offending = [];
|
|
1683
|
-
for (const cls of classes) {
|
|
1950
|
+
const offending = split.classes.flatMap((cls) => {
|
|
1684
1951
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1685
|
-
const
|
|
1686
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
1687
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
1952
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
1688
1953
|
const replacement = DEPRECATED_MAP[bareUtility];
|
|
1689
|
-
|
|
1690
|
-
if (dsResult) {
|
|
1691
|
-
const canonical = dsResult.cache.canonicalize(bareUtility);
|
|
1692
|
-
if (canonical !== bareUtility && canonical === replacement) {}
|
|
1693
|
-
}
|
|
1694
|
-
const fullReplacement = variant + (hasImportantPrefix ? "!" : "") + replacement + (hasImportantSuffix ? "!" : "");
|
|
1695
|
-
offending.push({
|
|
1954
|
+
return replacement ? [{
|
|
1696
1955
|
cls,
|
|
1697
|
-
replacement:
|
|
1698
|
-
}
|
|
1699
|
-
}
|
|
1700
|
-
|
|
1701
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1702
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
1703
|
-
for (let i = 0; i < offending.length; i++) {
|
|
1704
|
-
const { cls, replacement } = offending[i];
|
|
1705
|
-
if (i === 0) context.report({
|
|
1706
|
-
node: loc.node,
|
|
1707
|
-
messageId: "deprecated",
|
|
1708
|
-
data: {
|
|
1709
|
-
className: cls,
|
|
1710
|
-
replacement
|
|
1711
|
-
},
|
|
1712
|
-
fix(fixer) {
|
|
1713
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1714
|
-
}
|
|
1715
|
-
});
|
|
1716
|
-
else context.report({
|
|
1717
|
-
node: loc.node,
|
|
1718
|
-
messageId: "deprecated",
|
|
1719
|
-
data: {
|
|
1720
|
-
className: cls,
|
|
1721
|
-
replacement
|
|
1722
|
-
},
|
|
1723
|
-
suggest: [{
|
|
1724
|
-
messageId: "suggestReplace",
|
|
1725
|
-
data: {
|
|
1726
|
-
className: cls,
|
|
1727
|
-
replacement
|
|
1728
|
-
},
|
|
1729
|
-
fix(fixer) {
|
|
1730
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1731
|
-
}
|
|
1732
|
-
}]
|
|
1733
|
-
});
|
|
1734
|
-
}
|
|
1956
|
+
replacement: variant + reattachImportant(replacement, position)
|
|
1957
|
+
}] : [];
|
|
1958
|
+
});
|
|
1959
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "deprecated" });
|
|
1735
1960
|
}
|
|
1736
1961
|
}
|
|
1737
1962
|
return createExtractorVisitors(context, check);
|
|
@@ -1766,37 +1991,27 @@ const noUnknownClasses = defineRule({
|
|
|
1766
1991
|
messages: {
|
|
1767
1992
|
unknown: "\"{{className}}\" is not a valid Tailwind class.",
|
|
1768
1993
|
unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
|
|
1769
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
1994
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
1995
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
1770
1996
|
}
|
|
1771
1997
|
},
|
|
1772
1998
|
createOnce(context) {
|
|
1773
1999
|
const getDS = createLazyLoader(context);
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
const opts = safeOptions(context);
|
|
1779
|
-
_allowlist = new Set(opts?.allowlist ?? []);
|
|
1780
|
-
_ignorePrefixes = opts?.ignorePrefixes ?? [];
|
|
1781
|
-
}
|
|
1782
|
-
return {
|
|
1783
|
-
allowlist: _allowlist,
|
|
1784
|
-
ignorePrefixes: _ignorePrefixes
|
|
1785
|
-
};
|
|
1786
|
-
}
|
|
2000
|
+
const getLazyOptions = createLazyOptions(context, (o) => ({
|
|
2001
|
+
allowlist: new Set(o?.allowlist ?? []),
|
|
2002
|
+
ignorePrefixes: o?.ignorePrefixes ?? []
|
|
2003
|
+
}));
|
|
1787
2004
|
function shouldIgnore(className) {
|
|
1788
2005
|
const { allowlist, ignorePrefixes } = getLazyOptions();
|
|
1789
2006
|
if (allowlist.has(className)) return true;
|
|
1790
2007
|
return ignorePrefixes.some((prefix) => className.startsWith(prefix));
|
|
1791
2008
|
}
|
|
1792
2009
|
function stripModifiers(className) {
|
|
1793
|
-
|
|
1794
|
-
if (stripped.startsWith("!")) stripped = stripped.slice(1);
|
|
1795
|
-
if (stripped.endsWith("!")) stripped = stripped.slice(0, -1);
|
|
1796
|
-
return stripped;
|
|
2010
|
+
return splitImportant(className).bare;
|
|
1797
2011
|
}
|
|
1798
2012
|
function check(locations) {
|
|
1799
|
-
|
|
2013
|
+
if (locations.length === 0) return;
|
|
2014
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
1800
2015
|
if (!ds) return;
|
|
1801
2016
|
const { cache } = ds;
|
|
1802
2017
|
for (const loc of locations) {
|
|
@@ -1840,7 +2055,122 @@ const noUnknownClasses = defineRule({
|
|
|
1840
2055
|
}
|
|
1841
2056
|
});
|
|
1842
2057
|
//#endregion
|
|
2058
|
+
//#region src/rules/no-conflicting-classes/spec.ts
|
|
2059
|
+
const COMPLEMENTARY_GROUPS = [
|
|
2060
|
+
{
|
|
2061
|
+
pattern: /^(from|via|to)-/,
|
|
2062
|
+
reason: "Gradient stops (`from-*`, `via-*`, `to-*`) share `--tw-gradient-stops`; different prefixes compose, same prefix conflicts."
|
|
2063
|
+
},
|
|
2064
|
+
{
|
|
2065
|
+
pattern: /^(transition|duration|ease|delay)(?:-|$)/,
|
|
2066
|
+
reason: "`transition-*` composes with `duration-*`, `ease-*`, `delay-*` — `transition-all` itself has no `--tw-*` vars so the heuristic misses it."
|
|
2067
|
+
},
|
|
2068
|
+
{
|
|
2069
|
+
pattern: /^-?(translate|scale|rotate|skew)-/,
|
|
2070
|
+
reason: "Transform axes (translate / scale / rotate / skew) compose into a single `transform`; the overlap is not captured in `cssProps`."
|
|
2071
|
+
},
|
|
2072
|
+
{
|
|
2073
|
+
pattern: /^-?mask-((?:linear|radial|conic|[trblxy])(?:-(?:from|via|to|at))?)(?:-|$)/,
|
|
2074
|
+
reason: "Mask gradients: different families (e.g. `mask-linear-*` vs `mask-radial-*`) or roles (`from`/`via`/`to`/`at`) compose; same family+role conflicts."
|
|
2075
|
+
},
|
|
2076
|
+
{
|
|
2077
|
+
pattern: /^prose(?:-|$)/,
|
|
2078
|
+
reason: "`prose` + `prose-sm`/`prose-lg`/`prose-xl` modify the same scope and are designed to compose."
|
|
2079
|
+
}
|
|
2080
|
+
];
|
|
2081
|
+
const COMPOSITION_PAIRS = [
|
|
2082
|
+
{
|
|
2083
|
+
a: /^text-/,
|
|
2084
|
+
b: /^leading-/,
|
|
2085
|
+
reason: "`text-*` sets line-height via its size token; `leading-*` overrides it."
|
|
2086
|
+
},
|
|
2087
|
+
{
|
|
2088
|
+
a: /^text-/,
|
|
2089
|
+
b: /^tracking-/,
|
|
2090
|
+
reason: "`text-*` may set letter-spacing via its size token; `tracking-*` overrides it."
|
|
2091
|
+
},
|
|
2092
|
+
{
|
|
2093
|
+
a: /^border(?:-[0-9]|$)/,
|
|
2094
|
+
b: /^border-(?:solid|dashed|dotted|double|hidden|none)$/,
|
|
2095
|
+
reason: "`border-*` (width) composes with `border-{solid,dashed,…}` (style)."
|
|
2096
|
+
},
|
|
2097
|
+
{
|
|
2098
|
+
a: /^divide-/,
|
|
2099
|
+
b: /^border(?:-[trblxyse])?-/,
|
|
2100
|
+
reason: "`divide-*` styles direct children; `border-*` styles the element itself."
|
|
2101
|
+
},
|
|
2102
|
+
{
|
|
2103
|
+
a: /^prose(?:-|$)/,
|
|
2104
|
+
b: /^max-w-/,
|
|
2105
|
+
reason: "`prose` sets a default max-width; `max-w-*` overrides it."
|
|
2106
|
+
},
|
|
2107
|
+
{
|
|
2108
|
+
a: /^animate-in$/,
|
|
2109
|
+
b: /^(?:fade|spin|zoom|blur)-in(?:-|$)|^slide-in-from-/,
|
|
2110
|
+
reason: "`animate-in` initializes all `--tw-enter-*` vars; `*-in` modifiers each override one. (`tailwindcss-animate` / `tw-animate-css`.)"
|
|
2111
|
+
},
|
|
2112
|
+
{
|
|
2113
|
+
a: /^animate-out$/,
|
|
2114
|
+
b: /^(?:fade|spin|zoom|blur)-out(?:-|$)|^slide-out-to-/,
|
|
2115
|
+
reason: "`animate-out` initializes all `--tw-exit-*` vars; `*-out` modifiers each override one."
|
|
2116
|
+
},
|
|
2117
|
+
{
|
|
2118
|
+
a: /^mask-(?:add|subtract|intersect|exclude)$/,
|
|
2119
|
+
b: /^-?mask-(?:linear|radial|conic|[trblxy])-/,
|
|
2120
|
+
reason: "`mask-{add,subtract,intersect,exclude}` are mask-composite modes that compose with mask-gradient utilities."
|
|
2121
|
+
}
|
|
2122
|
+
];
|
|
2123
|
+
//#endregion
|
|
1843
2124
|
//#region src/rules/no-conflicting-classes.ts
|
|
2125
|
+
/**
|
|
2126
|
+
* Two utilities compose via CSS custom properties if both define their own
|
|
2127
|
+
* --tw-* properties that don't overlap — they each contribute to a shared
|
|
2128
|
+
* shorthand (e.g. `shadow` and `ring` both end up in `box-shadow` via
|
|
2129
|
+
* different intermediate vars).
|
|
2130
|
+
*/
|
|
2131
|
+
function isCompositionViaCssVars(propsA, propsB) {
|
|
2132
|
+
const customA = propsA.filter((p) => p.startsWith("--"));
|
|
2133
|
+
const customB = propsB.filter((p) => p.startsWith("--"));
|
|
2134
|
+
if (customA.length === 0 || customB.length === 0) return false;
|
|
2135
|
+
return !customA.some((p) => customB.includes(p));
|
|
2136
|
+
}
|
|
2137
|
+
/**
|
|
2138
|
+
* Detect a narrowing override: the later class's CSS properties are a strict
|
|
2139
|
+
* subset of the earlier class's, so the later class refines one of the
|
|
2140
|
+
* shorthand's properties (size-4 h-6, rounded-t-lg rounded-tl-sm, truncate
|
|
2141
|
+
* text-clip). Direction-sensitive: the inverse means the wider later class
|
|
2142
|
+
* clobbers a prior narrower override.
|
|
2143
|
+
*/
|
|
2144
|
+
function isNarrowingOverride(propsEarlier, propsLater) {
|
|
2145
|
+
if (propsLater.length === 0 || propsLater.length >= propsEarlier.length) return false;
|
|
2146
|
+
const setEarlier = new Set(propsEarlier);
|
|
2147
|
+
return propsLater.every((p) => setEarlier.has(p));
|
|
2148
|
+
}
|
|
2149
|
+
/**
|
|
2150
|
+
* Returns true if two classes (within the same variant) should NOT be reported
|
|
2151
|
+
* as conflicting despite sharing CSS properties. Pure: regex tables are passed
|
|
2152
|
+
* in (or default to the module-level constants).
|
|
2153
|
+
*/
|
|
2154
|
+
function shouldSkipPair(a, b, propsA, propsB, rules = {}) {
|
|
2155
|
+
if (isCompositionViaCssVars(propsA, propsB)) return true;
|
|
2156
|
+
if (isNarrowingOverride(propsA, propsB)) return true;
|
|
2157
|
+
const ua = splitImportant(extractUtility(a)).bare;
|
|
2158
|
+
const ub = splitImportant(extractUtility(b)).bare;
|
|
2159
|
+
const groups = rules.complementaryGroups !== void 0 ? rules.complementaryGroups.map((pattern) => ({ pattern })) : COMPLEMENTARY_GROUPS;
|
|
2160
|
+
for (const { pattern: re } of groups) {
|
|
2161
|
+
const ma = ua.match(re);
|
|
2162
|
+
const mb = ub.match(re);
|
|
2163
|
+
if (!ma || !mb) continue;
|
|
2164
|
+
if (ma[1] === void 0) return true;
|
|
2165
|
+
if (ma[1] !== mb[1]) return true;
|
|
2166
|
+
}
|
|
2167
|
+
const pairs = rules.compositionPairs !== void 0 ? rules.compositionPairs.map(([a, b]) => ({
|
|
2168
|
+
a,
|
|
2169
|
+
b
|
|
2170
|
+
})) : COMPOSITION_PAIRS;
|
|
2171
|
+
for (const { a: reA, b: reB } of pairs) if (reA.test(ua) && reB.test(ub) || reA.test(ub) && reB.test(ua)) return true;
|
|
2172
|
+
return false;
|
|
2173
|
+
}
|
|
1844
2174
|
const noConflictingClasses = defineRule({
|
|
1845
2175
|
meta: {
|
|
1846
2176
|
type: "problem",
|
|
@@ -1850,12 +2180,17 @@ const noConflictingClasses = defineRule({
|
|
|
1850
2180
|
properties: { entryPoint: { type: "string" } },
|
|
1851
2181
|
additionalProperties: false
|
|
1852
2182
|
}],
|
|
1853
|
-
|
|
2183
|
+
defaultOptions: [{}],
|
|
2184
|
+
messages: {
|
|
2185
|
+
conflict: "\"{{classA}}\" and \"{{classB}}\" affect {{properties}}. \"{{winner}}\" takes precedence (appears later).",
|
|
2186
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
2187
|
+
}
|
|
1854
2188
|
},
|
|
1855
2189
|
createOnce(context) {
|
|
1856
2190
|
const getDS = createLazyLoader(context);
|
|
1857
2191
|
function check(locations) {
|
|
1858
|
-
|
|
2192
|
+
if (locations.length === 0) return;
|
|
2193
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
1859
2194
|
if (!ds) return;
|
|
1860
2195
|
const { cache } = ds;
|
|
1861
2196
|
for (const loc of locations) {
|
|
@@ -1872,43 +2207,9 @@ const noConflictingClasses = defineRule({
|
|
|
1872
2207
|
if (variantClasses.length < 2) continue;
|
|
1873
2208
|
const propsMap = /* @__PURE__ */ new Map();
|
|
1874
2209
|
for (const cls of variantClasses) {
|
|
1875
|
-
|
|
1876
|
-
if (utility.startsWith("!")) utility = utility.slice(1);
|
|
1877
|
-
else if (utility.endsWith("!")) utility = utility.slice(0, -1);
|
|
1878
|
-
const props = cache.getCssProperties(utility);
|
|
2210
|
+
const props = cache.getCssProperties(splitImportant(extractUtility(cls)).bare);
|
|
1879
2211
|
propsMap.set(cls, props);
|
|
1880
2212
|
}
|
|
1881
|
-
const COMPLEMENTARY_GROUPS = [
|
|
1882
|
-
/^(?:from|via|to)-/,
|
|
1883
|
-
/^(?:transition|duration|ease|delay)(?:-|$)/,
|
|
1884
|
-
/^-?(?:translate|scale|rotate|skew)-/,
|
|
1885
|
-
/^prose(?:-|$)/
|
|
1886
|
-
];
|
|
1887
|
-
const COMPOSITION_PAIRS = [
|
|
1888
|
-
[/^text-/, /^leading-/],
|
|
1889
|
-
[/^text-/, /^tracking-/],
|
|
1890
|
-
[/^border(?:-[0-9]|$)/, /^border-(?:solid|dashed|dotted|double|hidden|none)$/],
|
|
1891
|
-
[/^divide-/, /^border(?:-[trblxyse])?-/],
|
|
1892
|
-
[/^prose(?:-|$)/, /^max-w-/]
|
|
1893
|
-
];
|
|
1894
|
-
function isCompositionViaCssVars(propsA, propsB) {
|
|
1895
|
-
const customA = propsA.filter((p) => p.startsWith("--"));
|
|
1896
|
-
const customB = propsB.filter((p) => p.startsWith("--"));
|
|
1897
|
-
if (customA.length === 0 || customB.length === 0) return false;
|
|
1898
|
-
return !customA.some((p) => customB.includes(p));
|
|
1899
|
-
}
|
|
1900
|
-
function shouldSkipPair(a, b, propsA, propsB) {
|
|
1901
|
-
if (isCompositionViaCssVars(propsA, propsB)) return true;
|
|
1902
|
-
let ua = extractUtility(a);
|
|
1903
|
-
let ub = extractUtility(b);
|
|
1904
|
-
if (ua.startsWith("!")) ua = ua.slice(1);
|
|
1905
|
-
else if (ua.endsWith("!")) ua = ua.slice(0, -1);
|
|
1906
|
-
if (ub.startsWith("!")) ub = ub.slice(1);
|
|
1907
|
-
else if (ub.endsWith("!")) ub = ub.slice(0, -1);
|
|
1908
|
-
for (const re of COMPLEMENTARY_GROUPS) if (re.test(ua) && re.test(ub)) return true;
|
|
1909
|
-
for (const [reA, reB] of COMPOSITION_PAIRS) if (reA.test(ua) && reB.test(ub) || reA.test(ub) && reB.test(ua)) return true;
|
|
1910
|
-
return false;
|
|
1911
|
-
}
|
|
1912
2213
|
for (let i = 0; i < variantClasses.length; i++) {
|
|
1913
2214
|
const classA = variantClasses[i];
|
|
1914
2215
|
const propsA = propsMap.get(classA) ?? [];
|
|
@@ -1939,42 +2240,168 @@ const noConflictingClasses = defineRule({
|
|
|
1939
2240
|
}
|
|
1940
2241
|
});
|
|
1941
2242
|
//#endregion
|
|
1942
|
-
//#region src/design-system/
|
|
2243
|
+
//#region src/design-system/ds-worker.ts
|
|
1943
2244
|
/**
|
|
1944
|
-
*
|
|
1945
|
-
*
|
|
1946
|
-
* Calls `ds.canonicalizeCandidates([cls], { rem })` dynamically via a worker
|
|
1947
|
-
* thread. Same pattern as sort-service.ts — loads the DS once, then accepts
|
|
1948
|
-
* requests synchronously via Atomics.wait().
|
|
2245
|
+
* Generic worker-thread service wrapping `__unstable__loadDesignSystem`.
|
|
1949
2246
|
*
|
|
1950
|
-
*
|
|
1951
|
-
*
|
|
2247
|
+
* `sort-service.ts` and `canonicalize-service.ts` used to be near-byte-identical
|
|
2248
|
+
* (~500 LOC) — both spawned a worker that loaded the design system, then served
|
|
2249
|
+
* synchronous requests over a SharedArrayBuffer using `Atomics.wait`. The only
|
|
2250
|
+
* real difference was the body of the request loop: `getClassOrder` vs
|
|
2251
|
+
* `canonicalizeCandidates`. This class encapsulates the entire shared protocol
|
|
2252
|
+
* — SharedArrayBuffer layout, state machine, lifecycle, fail-loud — and each
|
|
2253
|
+
* service shrinks to ~40 LOC of singleton + public signature.
|
|
1952
2254
|
*
|
|
1953
|
-
*
|
|
2255
|
+
* v1: failures throw `SortServiceError`. The error is sticky for the lifetime
|
|
2256
|
+
* of the process (per cssPath) so subsequent calls don't pay another
|
|
2257
|
+
* init-timeout cost. Callers wrap via `safeGetDS` to surface the failure as a
|
|
2258
|
+
* single `designSystemUnavailable` diagnostic.
|
|
2259
|
+
*/
|
|
2260
|
+
const BUFFER_SIZE = 4 * 1024 * 1024;
|
|
2261
|
+
const HEADER_INTS = 4;
|
|
2262
|
+
const DATA_OFFSET = 20;
|
|
2263
|
+
const INIT_TIMEOUT = 6e4;
|
|
2264
|
+
const REQUEST_TIMEOUT = 3e4;
|
|
2265
|
+
var DesignSystemWorker = class {
|
|
2266
|
+
opts;
|
|
2267
|
+
ready = null;
|
|
2268
|
+
lastError = null;
|
|
2269
|
+
constructor(opts) {
|
|
2270
|
+
this.opts = opts;
|
|
2271
|
+
}
|
|
2272
|
+
/**
|
|
2273
|
+
* Ensure the worker is running and pointed at `cssPath`. Returns the
|
|
2274
|
+
* ready state. Throws `SortServiceError` on any failure (init timeout,
|
|
2275
|
+
* spawn error, DS-load error). Sticky: subsequent calls for the same
|
|
2276
|
+
* cssPath rethrow without retrying.
|
|
2277
|
+
*/
|
|
2278
|
+
ensure(cssPath) {
|
|
2279
|
+
if (this.lastError && this.ready?.cssPath === cssPath) throw this.lastError;
|
|
2280
|
+
if (this.ready && this.ready.cssPath === cssPath) return this.ready;
|
|
2281
|
+
if (this.ready) this.cleanup();
|
|
2282
|
+
this.lastError = null;
|
|
2283
|
+
if (TAILWIND_NODE_PATH === null) {
|
|
2284
|
+
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.");
|
|
2285
|
+
throw this.lastError;
|
|
2286
|
+
}
|
|
2287
|
+
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);
|
|
2288
|
+
const controlArray = new Int32Array(sharedBuffer, 0, HEADER_INTS);
|
|
2289
|
+
const lengthView = new DataView(sharedBuffer, HEADER_INTS * 4, 4);
|
|
2290
|
+
const dataArea = new Uint8Array(sharedBuffer, DATA_OFFSET);
|
|
2291
|
+
let worker;
|
|
2292
|
+
try {
|
|
2293
|
+
worker = new Worker(this.opts.workerScript, {
|
|
2294
|
+
eval: true,
|
|
2295
|
+
workerData: {
|
|
2296
|
+
sharedBuffer,
|
|
2297
|
+
cssPath,
|
|
2298
|
+
tailwindNodePath: TAILWIND_NODE_PATH
|
|
2299
|
+
}
|
|
2300
|
+
});
|
|
2301
|
+
} catch (cause) {
|
|
2302
|
+
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 });
|
|
2303
|
+
throw this.lastError;
|
|
2304
|
+
}
|
|
2305
|
+
worker.unref();
|
|
2306
|
+
worker.on("error", (err) => {
|
|
2307
|
+
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 });
|
|
2308
|
+
});
|
|
2309
|
+
if (Atomics.wait(controlArray, 2, 0, INIT_TIMEOUT) === "timed-out") {
|
|
2310
|
+
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.");
|
|
2311
|
+
this.terminateWorker(worker);
|
|
2312
|
+
throw this.lastError;
|
|
2313
|
+
}
|
|
2314
|
+
if (controlArray[2] === -1) {
|
|
2315
|
+
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.");
|
|
2316
|
+
this.terminateWorker(worker);
|
|
2317
|
+
throw this.lastError;
|
|
2318
|
+
}
|
|
2319
|
+
this.ready = {
|
|
2320
|
+
worker,
|
|
2321
|
+
controlArray,
|
|
2322
|
+
lengthView,
|
|
2323
|
+
dataArea,
|
|
2324
|
+
cssPath
|
|
2325
|
+
};
|
|
2326
|
+
return this.ready;
|
|
2327
|
+
}
|
|
2328
|
+
/**
|
|
2329
|
+
* Send a request to the worker and synchronously wait for the response.
|
|
2330
|
+
* Throws `SortServiceError` on any failure.
|
|
2331
|
+
*/
|
|
2332
|
+
callSync(cssPath, req) {
|
|
2333
|
+
const state = this.ensure(cssPath);
|
|
2334
|
+
const requestBytes = Buffer.from(JSON.stringify(req), "utf-8");
|
|
2335
|
+
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.");
|
|
2336
|
+
state.dataArea.set(requestBytes, 0);
|
|
2337
|
+
state.lengthView.setUint32(0, requestBytes.length);
|
|
2338
|
+
Atomics.store(state.controlArray, 0, 1);
|
|
2339
|
+
Atomics.notify(state.controlArray, 0);
|
|
2340
|
+
if (Atomics.wait(state.controlArray, 1, 0, REQUEST_TIMEOUT) === "timed-out") {
|
|
2341
|
+
this.lastError = new SortServiceError(`${this.opts.serviceName} worker request timed out after ${REQUEST_TIMEOUT}ms.`, "Raise settings.tailwindcss.timeout if your machine is slow.");
|
|
2342
|
+
this.cleanup();
|
|
2343
|
+
throw this.lastError;
|
|
2344
|
+
}
|
|
2345
|
+
const responseLen = state.lengthView.getUint32(0);
|
|
2346
|
+
const responseStr = Buffer.from(state.dataArea.slice(0, responseLen)).toString("utf-8");
|
|
2347
|
+
Atomics.store(state.controlArray, 1, 0);
|
|
2348
|
+
let parsed;
|
|
2349
|
+
try {
|
|
2350
|
+
parsed = JSON.parse(responseStr);
|
|
2351
|
+
} catch (cause) {
|
|
2352
|
+
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 });
|
|
2353
|
+
this.cleanup();
|
|
2354
|
+
throw this.lastError;
|
|
2355
|
+
}
|
|
2356
|
+
if (parsed === null) {
|
|
2357
|
+
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.");
|
|
2358
|
+
this.cleanup();
|
|
2359
|
+
throw this.lastError;
|
|
2360
|
+
}
|
|
2361
|
+
return parsed;
|
|
2362
|
+
}
|
|
2363
|
+
reset() {
|
|
2364
|
+
this.cleanup();
|
|
2365
|
+
this.lastError = null;
|
|
2366
|
+
}
|
|
2367
|
+
cleanup() {
|
|
2368
|
+
if (this.ready) {
|
|
2369
|
+
this.terminateWorker(this.ready.worker);
|
|
2370
|
+
this.ready = null;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
terminateWorker(worker) {
|
|
2374
|
+
try {
|
|
2375
|
+
worker.terminate();
|
|
2376
|
+
} catch {}
|
|
2377
|
+
}
|
|
2378
|
+
};
|
|
2379
|
+
//#endregion
|
|
2380
|
+
//#region src/design-system/canonicalize-service.ts
|
|
2381
|
+
/**
|
|
2382
|
+
* Persistent canonicalize service backed by `DesignSystemWorker`.
|
|
1954
2383
|
*
|
|
1955
|
-
*
|
|
1956
|
-
*
|
|
1957
|
-
*
|
|
2384
|
+
* Calls `ds.canonicalizeCandidates([cls], { rem })` one class at a time
|
|
2385
|
+
* (the design system deduplicates its input, so batching changes the
|
|
2386
|
+
* result shape — see sync-loader.ts). The shared protocol lives in
|
|
2387
|
+
* `./ds-worker.ts`.
|
|
1958
2388
|
*
|
|
1959
|
-
* ## Per-class
|
|
2389
|
+
* ## Per-class cache (process-wide)
|
|
1960
2390
|
*
|
|
1961
|
-
*
|
|
1962
|
-
*
|
|
1963
|
-
*
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
const
|
|
1967
|
-
|
|
1968
|
-
const INIT_TIMEOUT$1 = 3e4;
|
|
1969
|
-
const REQUEST_TIMEOUT$1 = 1e4;
|
|
1970
|
-
const WORKER_SCRIPT$1 = `
|
|
2391
|
+
* Results are cached by `${cssPath}\0${rem}\0${class}`. The worker is
|
|
2392
|
+
* invoked only for cache misses. In practice the cache converges quickly —
|
|
2393
|
+
* after the first few files any given class has been seen, and every
|
|
2394
|
+
* subsequent lookup is O(1).
|
|
2395
|
+
*/
|
|
2396
|
+
const canonWorker = new DesignSystemWorker({
|
|
2397
|
+
workerScript: `
|
|
1971
2398
|
const { workerData } = require('worker_threads');
|
|
1972
2399
|
|
|
1973
2400
|
async function main() {
|
|
1974
2401
|
const { sharedBuffer, cssPath } = workerData;
|
|
1975
|
-
const control = new Int32Array(sharedBuffer, 0,
|
|
1976
|
-
const lengthView = new DataView(sharedBuffer,
|
|
1977
|
-
const dataArea = new Uint8Array(sharedBuffer,
|
|
2402
|
+
const control = new Int32Array(sharedBuffer, 0, 4);
|
|
2403
|
+
const lengthView = new DataView(sharedBuffer, 4 * 4, 4);
|
|
2404
|
+
const dataArea = new Uint8Array(sharedBuffer, 4 * 4 + 4);
|
|
1978
2405
|
|
|
1979
2406
|
let ds;
|
|
1980
2407
|
try {
|
|
@@ -1989,14 +2416,11 @@ async function main() {
|
|
|
1989
2416
|
return;
|
|
1990
2417
|
}
|
|
1991
2418
|
|
|
1992
|
-
// Signal ready
|
|
1993
2419
|
Atomics.store(control, 2, 1);
|
|
1994
2420
|
Atomics.notify(control, 2);
|
|
1995
2421
|
|
|
1996
|
-
// Request loop
|
|
1997
2422
|
while (true) {
|
|
1998
2423
|
Atomics.wait(control, 0, 0);
|
|
1999
|
-
|
|
2000
2424
|
const len = lengthView.getUint32(0);
|
|
2001
2425
|
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
2002
2426
|
Atomics.store(control, 0, 0);
|
|
@@ -2018,117 +2442,32 @@ async function main() {
|
|
|
2018
2442
|
|
|
2019
2443
|
dataArea.set(response, 0);
|
|
2020
2444
|
lengthView.setUint32(0, response.length);
|
|
2021
|
-
|
|
2022
2445
|
Atomics.store(control, 1, 1);
|
|
2023
2446
|
Atomics.notify(control, 1);
|
|
2024
2447
|
}
|
|
2025
2448
|
}
|
|
2026
2449
|
main();
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
let lengthView$1 = null;
|
|
2031
|
-
let dataArea$1 = null;
|
|
2032
|
-
let initialized$1 = false;
|
|
2033
|
-
let available$1 = true;
|
|
2034
|
-
let currentCssPath$1 = null;
|
|
2035
|
-
const canonCache = /* @__PURE__ */ new Map();
|
|
2036
|
-
function ensureService$1(cssPath) {
|
|
2037
|
-
if (initialized$1 && currentCssPath$1 === cssPath) return available$1;
|
|
2038
|
-
if (initialized$1) {
|
|
2039
|
-
cleanup$1();
|
|
2040
|
-
initialized$1 = false;
|
|
2041
|
-
available$1 = true;
|
|
2042
|
-
controlArray$1 = null;
|
|
2043
|
-
lengthView$1 = null;
|
|
2044
|
-
dataArea$1 = null;
|
|
2045
|
-
}
|
|
2046
|
-
initialized$1 = true;
|
|
2047
|
-
currentCssPath$1 = cssPath;
|
|
2048
|
-
try {
|
|
2049
|
-
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
2050
|
-
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE$1);
|
|
2051
|
-
controlArray$1 = new Int32Array(sharedBuffer, 0, HEADER_INTS$1);
|
|
2052
|
-
lengthView$1 = new DataView(sharedBuffer, HEADER_INTS$1 * 4, 4);
|
|
2053
|
-
dataArea$1 = new Uint8Array(sharedBuffer, DATA_OFFSET$1);
|
|
2054
|
-
worker$1 = new Worker(WORKER_SCRIPT$1, {
|
|
2055
|
-
eval: true,
|
|
2056
|
-
workerData: {
|
|
2057
|
-
sharedBuffer,
|
|
2058
|
-
cssPath,
|
|
2059
|
-
tailwindNodePath
|
|
2060
|
-
}
|
|
2061
|
-
});
|
|
2062
|
-
worker$1.unref();
|
|
2063
|
-
worker$1.on("error", () => {
|
|
2064
|
-
available$1 = false;
|
|
2065
|
-
worker$1 = null;
|
|
2066
|
-
});
|
|
2067
|
-
worker$1.on("exit", () => {
|
|
2068
|
-
worker$1 = null;
|
|
2069
|
-
});
|
|
2070
|
-
if (Atomics.wait(controlArray$1, 2, 0, INIT_TIMEOUT$1) === "timed-out" || controlArray$1[2] === -1) {
|
|
2071
|
-
available$1 = false;
|
|
2072
|
-
cleanup$1();
|
|
2073
|
-
return false;
|
|
2074
|
-
}
|
|
2075
|
-
return true;
|
|
2076
|
-
} catch {
|
|
2077
|
-
available$1 = false;
|
|
2078
|
-
cleanup$1();
|
|
2079
|
-
return false;
|
|
2080
|
-
}
|
|
2081
|
-
}
|
|
2082
|
-
function cleanup$1() {
|
|
2083
|
-
if (worker$1) {
|
|
2084
|
-
try {
|
|
2085
|
-
worker$1.terminate();
|
|
2086
|
-
} catch {}
|
|
2087
|
-
worker$1 = null;
|
|
2088
|
-
}
|
|
2089
|
-
}
|
|
2450
|
+
`,
|
|
2451
|
+
serviceName: "canonicalize"
|
|
2452
|
+
});
|
|
2090
2453
|
/**
|
|
2091
|
-
*
|
|
2092
|
-
*
|
|
2093
|
-
*
|
|
2454
|
+
* Process-wide cache of canonicalized classes.
|
|
2455
|
+
* Key: `${cssPath}\0${rem}\0${className}` — isolates monorepos (multiple
|
|
2456
|
+
* DSs) and different rem settings. Value: canonicalized class string.
|
|
2094
2457
|
*/
|
|
2095
|
-
|
|
2096
|
-
if (!controlArray$1 || !dataArea$1 || !lengthView$1) return null;
|
|
2097
|
-
try {
|
|
2098
|
-
const request = Buffer.from(JSON.stringify({
|
|
2099
|
-
classes,
|
|
2100
|
-
rem
|
|
2101
|
-
}), "utf-8");
|
|
2102
|
-
if (request.length > BUFFER_SIZE$1 - DATA_OFFSET$1) return null;
|
|
2103
|
-
dataArea$1.set(request, 0);
|
|
2104
|
-
lengthView$1.setUint32(0, request.length);
|
|
2105
|
-
Atomics.store(controlArray$1, 0, 1);
|
|
2106
|
-
Atomics.notify(controlArray$1, 0);
|
|
2107
|
-
if (Atomics.wait(controlArray$1, 1, 0, REQUEST_TIMEOUT$1) === "timed-out") {
|
|
2108
|
-
available$1 = false;
|
|
2109
|
-
cleanup$1();
|
|
2110
|
-
return null;
|
|
2111
|
-
}
|
|
2112
|
-
const responseLen = lengthView$1.getUint32(0);
|
|
2113
|
-
const responseStr = Buffer.from(dataArea$1.slice(0, responseLen)).toString("utf-8");
|
|
2114
|
-
Atomics.store(controlArray$1, 1, 0);
|
|
2115
|
-
return JSON.parse(responseStr);
|
|
2116
|
-
} catch {
|
|
2117
|
-
available$1 = false;
|
|
2118
|
-
cleanup$1();
|
|
2119
|
-
return null;
|
|
2120
|
-
}
|
|
2121
|
-
}
|
|
2458
|
+
const canonCache = /* @__PURE__ */ new Map();
|
|
2122
2459
|
/**
|
|
2123
|
-
* Canonicalize classes
|
|
2124
|
-
*
|
|
2125
|
-
*
|
|
2460
|
+
* Canonicalize classes via the worker thread.
|
|
2461
|
+
*
|
|
2462
|
+
* Throws `SortServiceError` on any worker failure (init timeout, request
|
|
2463
|
+
* timeout, worker crash). Callers should wrap via `safeGetDS` so the
|
|
2464
|
+
* failure surfaces as a single `designSystemUnavailable` diagnostic.
|
|
2126
2465
|
*
|
|
2127
|
-
*
|
|
2466
|
+
* Returns an array the same length and order as `classes`. Uses a
|
|
2467
|
+
* process-wide per-class cache: the worker is invoked only for classes
|
|
2128
2468
|
* not already seen with this (cssPath, rem) combination.
|
|
2129
2469
|
*/
|
|
2130
2470
|
function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
2131
|
-
if (!ensureService$1(cssPath)) return null;
|
|
2132
2471
|
const out = Array.from({ length: classes.length });
|
|
2133
2472
|
const missingIdx = [];
|
|
2134
2473
|
const missing = [];
|
|
@@ -2144,8 +2483,11 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
|
2144
2483
|
}
|
|
2145
2484
|
if (missing.length === 0) return out;
|
|
2146
2485
|
const uniqueMissing = [...new Set(missing)];
|
|
2147
|
-
const fresh =
|
|
2148
|
-
|
|
2486
|
+
const fresh = canonWorker.callSync(cssPath, {
|
|
2487
|
+
classes: uniqueMissing,
|
|
2488
|
+
rem
|
|
2489
|
+
});
|
|
2490
|
+
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.");
|
|
2149
2491
|
const freshByClass = /* @__PURE__ */ new Map();
|
|
2150
2492
|
for (let k = 0; k < uniqueMissing.length; k++) freshByClass.set(uniqueMissing[k], fresh[k]);
|
|
2151
2493
|
for (let j = 0; j < missing.length; j++) {
|
|
@@ -2159,20 +2501,20 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
|
2159
2501
|
//#endregion
|
|
2160
2502
|
//#region src/rules/enforce-canonical.ts
|
|
2161
2503
|
/**
|
|
2162
|
-
* Preserve the user's
|
|
2163
|
-
*
|
|
2164
|
-
*
|
|
2504
|
+
* Preserve the user's `!` position after canonicalization.
|
|
2505
|
+
*
|
|
2506
|
+
* `canonicalizeCandidates` normalizes `!` to suffix; this restores the
|
|
2507
|
+
* original prefix-or-suffix-or-none position so `enforce-consistent-important-position`
|
|
2508
|
+
* remains the single source of truth for ! placement policy.
|
|
2509
|
+
*
|
|
2510
|
+
* Bracket-aware via `splitUtilityAndVariant` — arbitrary variants like
|
|
2511
|
+
* `[&>svg]:!w-4` round-trip correctly.
|
|
2165
2512
|
*/
|
|
2166
2513
|
function preserveImportantPosition(original, canonicalized) {
|
|
2167
|
-
const
|
|
2168
|
-
const
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
if (origHasPrefix) {
|
|
2172
|
-
const variantPrefix = bare.slice(0, bare.length - bare.replace(/^[a-z0-9[\]*@-]*:/g, "").length);
|
|
2173
|
-
return variantPrefix + "!" + bare.slice(variantPrefix.length);
|
|
2174
|
-
}
|
|
2175
|
-
return bare + "!";
|
|
2514
|
+
const { position } = splitImportant(splitUtilityAndVariant(original).utility);
|
|
2515
|
+
const { utility, variant } = splitUtilityAndVariant(canonicalized);
|
|
2516
|
+
const bare = splitImportant(utility).bare;
|
|
2517
|
+
return variant + reattachImportant(bare, position);
|
|
2176
2518
|
}
|
|
2177
2519
|
const enforceCanonical = defineRule({
|
|
2178
2520
|
meta: {
|
|
@@ -2185,9 +2527,11 @@ const enforceCanonical = defineRule({
|
|
|
2185
2527
|
additionalProperties: false
|
|
2186
2528
|
}],
|
|
2187
2529
|
hasSuggestions: true,
|
|
2530
|
+
defaultOptions: [{}],
|
|
2188
2531
|
messages: {
|
|
2189
2532
|
nonCanonical: "\"{{className}}\" can be written as \"{{canonical}}\". Use the canonical form.",
|
|
2190
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2533
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
2534
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
2191
2535
|
}
|
|
2192
2536
|
},
|
|
2193
2537
|
createOnce(context) {
|
|
@@ -2198,7 +2542,8 @@ const enforceCanonical = defineRule({
|
|
|
2198
2542
|
return _rem;
|
|
2199
2543
|
}
|
|
2200
2544
|
function check(locations) {
|
|
2201
|
-
|
|
2545
|
+
if (locations.length === 0) return;
|
|
2546
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
2202
2547
|
if (!ds) return;
|
|
2203
2548
|
const { cache, entryPoint } = ds;
|
|
2204
2549
|
for (const loc of locations) {
|
|
@@ -2212,51 +2557,18 @@ const enforceCanonical = defineRule({
|
|
|
2212
2557
|
arbitrary.push(classes[i]);
|
|
2213
2558
|
} else canonicals[i] = cache.canonicalize(classes[i]);
|
|
2214
2559
|
if (arbitrary.length > 0) {
|
|
2215
|
-
const
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
let firstNonCanonical = true;
|
|
2220
|
-
for (let i = 0; i < classes.length; i++) {
|
|
2221
|
-
const cls = classes[i];
|
|
2222
|
-
const canonical = canonicals[i];
|
|
2223
|
-
if (canonical === cls) continue;
|
|
2224
|
-
if (firstNonCanonical) {
|
|
2225
|
-
firstNonCanonical = false;
|
|
2226
|
-
const fixedValue = rebuildClassString(split, canonicals);
|
|
2227
|
-
context.report({
|
|
2228
|
-
node: loc.node,
|
|
2229
|
-
messageId: "nonCanonical",
|
|
2230
|
-
data: {
|
|
2231
|
-
className: cls,
|
|
2232
|
-
canonical
|
|
2233
|
-
},
|
|
2234
|
-
fix(fixer) {
|
|
2235
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2236
|
-
}
|
|
2237
|
-
});
|
|
2238
|
-
} else {
|
|
2239
|
-
const fixedValue = rebuildClassString(split, canonicals);
|
|
2240
|
-
context.report({
|
|
2241
|
-
node: loc.node,
|
|
2242
|
-
messageId: "nonCanonical",
|
|
2243
|
-
data: {
|
|
2244
|
-
className: cls,
|
|
2245
|
-
canonical
|
|
2246
|
-
},
|
|
2247
|
-
suggest: [{
|
|
2248
|
-
messageId: "suggestReplace",
|
|
2249
|
-
data: {
|
|
2250
|
-
className: cls,
|
|
2251
|
-
replacement: canonical
|
|
2252
|
-
},
|
|
2253
|
-
fix(fixer) {
|
|
2254
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2255
|
-
}
|
|
2256
|
-
}]
|
|
2257
|
-
});
|
|
2258
|
-
}
|
|
2560
|
+
const rem = getRem();
|
|
2561
|
+
const dynamic = safeGetDS(() => canonicalizeClassesSync(entryPoint, arbitrary, rem), context, loc.node);
|
|
2562
|
+
if (!dynamic) return;
|
|
2563
|
+
for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = preserveImportantPosition(arbitrary[k], dynamic[k]);
|
|
2259
2564
|
}
|
|
2565
|
+
reportClassReplacements(context, loc, split, classes, classes.flatMap((cls, i) => canonicals[i] === cls ? [] : [{
|
|
2566
|
+
cls,
|
|
2567
|
+
replacement: canonicals[i]
|
|
2568
|
+
}]), {
|
|
2569
|
+
messageId: "nonCanonical",
|
|
2570
|
+
replacementKey: "canonical"
|
|
2571
|
+
});
|
|
2260
2572
|
}
|
|
2261
2573
|
}
|
|
2262
2574
|
return createExtractorVisitors(context, check);
|
|
@@ -2265,31 +2577,21 @@ const enforceCanonical = defineRule({
|
|
|
2265
2577
|
//#endregion
|
|
2266
2578
|
//#region src/design-system/sort-service.ts
|
|
2267
2579
|
/**
|
|
2268
|
-
* Persistent sort service
|
|
2269
|
-
*
|
|
2270
|
-
* The problem: `ds.getClassOrder()` is the only way to get the exact official
|
|
2271
|
-
* Tailwind CSS sort order (matching oxfmt/prettier-plugin-tailwindcss), but the
|
|
2272
|
-
* design system is async to load and the oxlint plugin API is sync.
|
|
2580
|
+
* Persistent sort service backed by `DesignSystemWorker`.
|
|
2273
2581
|
*
|
|
2274
|
-
*
|
|
2275
|
-
*
|
|
2276
|
-
*
|
|
2277
|
-
*
|
|
2278
|
-
* Falls back gracefully if the worker fails to initialize within the timeout.
|
|
2582
|
+
* Calls `ds.getClassOrder(classes)` and returns the class names in the
|
|
2583
|
+
* Tailwind-canonical order. The shared protocol (SharedArrayBuffer,
|
|
2584
|
+
* Atomics.wait, fail-loud, sticky lastError) lives in `./ds-worker.ts`.
|
|
2279
2585
|
*/
|
|
2280
|
-
const
|
|
2281
|
-
|
|
2282
|
-
const DATA_OFFSET = HEADER_INTS * 4 + 4;
|
|
2283
|
-
const INIT_TIMEOUT = 3e4;
|
|
2284
|
-
const REQUEST_TIMEOUT = 1e4;
|
|
2285
|
-
const WORKER_SCRIPT = `
|
|
2586
|
+
const sortWorker = new DesignSystemWorker({
|
|
2587
|
+
workerScript: `
|
|
2286
2588
|
const { workerData } = require('worker_threads');
|
|
2287
2589
|
|
|
2288
2590
|
async function main() {
|
|
2289
2591
|
const { sharedBuffer, cssPath } = workerData;
|
|
2290
|
-
const control = new Int32Array(sharedBuffer, 0,
|
|
2291
|
-
const lengthView = new DataView(sharedBuffer,
|
|
2292
|
-
const dataArea = new Uint8Array(sharedBuffer,
|
|
2592
|
+
const control = new Int32Array(sharedBuffer, 0, 4);
|
|
2593
|
+
const lengthView = new DataView(sharedBuffer, 4 * 4, 4);
|
|
2594
|
+
const dataArea = new Uint8Array(sharedBuffer, 4 * 4 + 4);
|
|
2293
2595
|
|
|
2294
2596
|
let ds;
|
|
2295
2597
|
try {
|
|
@@ -2304,17 +2606,14 @@ async function main() {
|
|
|
2304
2606
|
return;
|
|
2305
2607
|
}
|
|
2306
2608
|
|
|
2307
|
-
// Signal ready
|
|
2308
2609
|
Atomics.store(control, 2, 1);
|
|
2309
2610
|
Atomics.notify(control, 2);
|
|
2310
2611
|
|
|
2311
|
-
// Request loop
|
|
2312
2612
|
while (true) {
|
|
2313
|
-
Atomics.wait(control, 0, 0);
|
|
2314
|
-
|
|
2613
|
+
Atomics.wait(control, 0, 0);
|
|
2315
2614
|
const len = lengthView.getUint32(0);
|
|
2316
2615
|
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
2317
|
-
Atomics.store(control, 0, 0);
|
|
2616
|
+
Atomics.store(control, 0, 0);
|
|
2318
2617
|
|
|
2319
2618
|
let response;
|
|
2320
2619
|
try {
|
|
@@ -2335,102 +2634,24 @@ async function main() {
|
|
|
2335
2634
|
|
|
2336
2635
|
dataArea.set(response, 0);
|
|
2337
2636
|
lengthView.setUint32(0, response.length);
|
|
2338
|
-
|
|
2339
|
-
Atomics.store(control, 1, 1); // signal response
|
|
2637
|
+
Atomics.store(control, 1, 1);
|
|
2340
2638
|
Atomics.notify(control, 1);
|
|
2341
2639
|
}
|
|
2342
2640
|
}
|
|
2343
2641
|
main();
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
let lengthView = null;
|
|
2348
|
-
let dataArea = null;
|
|
2349
|
-
let initialized = false;
|
|
2350
|
-
let available = true;
|
|
2351
|
-
let currentCssPath = null;
|
|
2352
|
-
function ensureService(cssPath) {
|
|
2353
|
-
if (initialized && currentCssPath === cssPath) return available;
|
|
2354
|
-
if (initialized) {
|
|
2355
|
-
cleanup();
|
|
2356
|
-
initialized = false;
|
|
2357
|
-
available = true;
|
|
2358
|
-
controlArray = null;
|
|
2359
|
-
lengthView = null;
|
|
2360
|
-
dataArea = null;
|
|
2361
|
-
}
|
|
2362
|
-
initialized = true;
|
|
2363
|
-
currentCssPath = cssPath;
|
|
2364
|
-
try {
|
|
2365
|
-
const tailwindNodePath = __require.resolve("@tailwindcss/node");
|
|
2366
|
-
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);
|
|
2367
|
-
controlArray = new Int32Array(sharedBuffer, 0, HEADER_INTS);
|
|
2368
|
-
lengthView = new DataView(sharedBuffer, HEADER_INTS * 4, 4);
|
|
2369
|
-
dataArea = new Uint8Array(sharedBuffer, DATA_OFFSET);
|
|
2370
|
-
worker = new Worker(WORKER_SCRIPT, {
|
|
2371
|
-
eval: true,
|
|
2372
|
-
workerData: {
|
|
2373
|
-
sharedBuffer,
|
|
2374
|
-
cssPath,
|
|
2375
|
-
tailwindNodePath
|
|
2376
|
-
}
|
|
2377
|
-
});
|
|
2378
|
-
worker.unref();
|
|
2379
|
-
worker.on("error", () => {
|
|
2380
|
-
available = false;
|
|
2381
|
-
worker = null;
|
|
2382
|
-
});
|
|
2383
|
-
worker.on("exit", () => {
|
|
2384
|
-
worker = null;
|
|
2385
|
-
});
|
|
2386
|
-
if (Atomics.wait(controlArray, 2, 0, INIT_TIMEOUT) === "timed-out" || controlArray[2] === -1) {
|
|
2387
|
-
available = false;
|
|
2388
|
-
cleanup();
|
|
2389
|
-
return false;
|
|
2390
|
-
}
|
|
2391
|
-
return true;
|
|
2392
|
-
} catch {
|
|
2393
|
-
available = false;
|
|
2394
|
-
cleanup();
|
|
2395
|
-
return false;
|
|
2396
|
-
}
|
|
2397
|
-
}
|
|
2398
|
-
function cleanup() {
|
|
2399
|
-
if (worker) {
|
|
2400
|
-
try {
|
|
2401
|
-
worker.terminate();
|
|
2402
|
-
} catch {}
|
|
2403
|
-
worker = null;
|
|
2404
|
-
}
|
|
2405
|
-
}
|
|
2642
|
+
`,
|
|
2643
|
+
serviceName: "sort"
|
|
2644
|
+
});
|
|
2406
2645
|
/**
|
|
2407
|
-
* Sort classes using the official Tailwind
|
|
2408
|
-
*
|
|
2646
|
+
* Sort classes via worker thread using the official Tailwind class order.
|
|
2647
|
+
*
|
|
2648
|
+
* Throws `SortServiceError` on any failure (worker init timeout, request
|
|
2649
|
+
* timeout, worker crash, payload too large). Callers should wrap via
|
|
2650
|
+
* `safeGetDS` so the failure surfaces as a single `designSystemUnavailable`
|
|
2651
|
+
* diagnostic instead of crashing the lint.
|
|
2409
2652
|
*/
|
|
2410
2653
|
function sortClassesSync(cssPath, classes) {
|
|
2411
|
-
|
|
2412
|
-
if (!controlArray || !dataArea || !lengthView) return null;
|
|
2413
|
-
try {
|
|
2414
|
-
const request = Buffer.from(JSON.stringify(classes), "utf-8");
|
|
2415
|
-
if (request.length > BUFFER_SIZE - DATA_OFFSET) return null;
|
|
2416
|
-
dataArea.set(request, 0);
|
|
2417
|
-
lengthView.setUint32(0, request.length);
|
|
2418
|
-
Atomics.store(controlArray, 0, 1);
|
|
2419
|
-
Atomics.notify(controlArray, 0);
|
|
2420
|
-
if (Atomics.wait(controlArray, 1, 0, REQUEST_TIMEOUT) === "timed-out") {
|
|
2421
|
-
available = false;
|
|
2422
|
-
cleanup();
|
|
2423
|
-
return null;
|
|
2424
|
-
}
|
|
2425
|
-
const responseLen = lengthView.getUint32(0);
|
|
2426
|
-
const responseStr = Buffer.from(dataArea.slice(0, responseLen)).toString("utf-8");
|
|
2427
|
-
Atomics.store(controlArray, 1, 0);
|
|
2428
|
-
return JSON.parse(responseStr);
|
|
2429
|
-
} catch {
|
|
2430
|
-
available = false;
|
|
2431
|
-
cleanup();
|
|
2432
|
-
return null;
|
|
2433
|
-
}
|
|
2654
|
+
return sortWorker.callSync(cssPath, classes);
|
|
2434
2655
|
}
|
|
2435
2656
|
//#endregion
|
|
2436
2657
|
//#region src/rules/enforce-sort-order.ts
|
|
@@ -2450,31 +2671,23 @@ const enforceSortOrder = defineRule({
|
|
|
2450
2671
|
},
|
|
2451
2672
|
additionalProperties: false
|
|
2452
2673
|
}],
|
|
2453
|
-
|
|
2674
|
+
defaultOptions: [{ mode: "default" }],
|
|
2675
|
+
messages: {
|
|
2676
|
+
unsorted: "Tailwind classes are not in the recommended order.",
|
|
2677
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
2678
|
+
}
|
|
2454
2679
|
},
|
|
2455
2680
|
createOnce(context) {
|
|
2456
2681
|
const getDS = createLazyLoader(context);
|
|
2457
|
-
|
|
2458
|
-
function getMode() {
|
|
2459
|
-
if (_mode === null) _mode = safeOptions(context)?.mode ?? "default";
|
|
2460
|
-
return _mode;
|
|
2461
|
-
}
|
|
2682
|
+
const getMode = createLazyOptions(context, (o) => o?.mode ?? "default");
|
|
2462
2683
|
function check(locations) {
|
|
2463
|
-
|
|
2684
|
+
if (locations.length === 0) return;
|
|
2685
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
2464
2686
|
if (!ds) return;
|
|
2465
2687
|
const { cache, entryPoint } = ds;
|
|
2466
2688
|
const mode = getMode();
|
|
2467
2689
|
function sortDefault(classes) {
|
|
2468
|
-
|
|
2469
|
-
if (dynamic) return dynamic;
|
|
2470
|
-
return [...cache.getClassOrder(classes)].sort((a, b) => {
|
|
2471
|
-
if (a[1] === null && b[1] === null) return 0;
|
|
2472
|
-
if (a[1] === null) return -1;
|
|
2473
|
-
if (b[1] === null) return 1;
|
|
2474
|
-
if (a[1] < b[1]) return -1;
|
|
2475
|
-
if (a[1] > b[1]) return 1;
|
|
2476
|
-
return 0;
|
|
2477
|
-
}).map(([name]) => name);
|
|
2690
|
+
return sortClassesSync(entryPoint, classes);
|
|
2478
2691
|
}
|
|
2479
2692
|
function sortStrict(classes) {
|
|
2480
2693
|
const groups = /* @__PURE__ */ new Map();
|
|
@@ -2518,7 +2731,8 @@ const enforceSortOrder = defineRule({
|
|
|
2518
2731
|
const split = splitClassesWithSeparators(loc.value);
|
|
2519
2732
|
const classes = split.classes;
|
|
2520
2733
|
if (classes.length < 2) continue;
|
|
2521
|
-
const sortedNames = mode === "strict" ? sortStrict(classes) : sortDefault(classes);
|
|
2734
|
+
const sortedNames = mode === "strict" ? sortStrict(classes) : safeGetDS(() => sortDefault(classes), context, loc.node);
|
|
2735
|
+
if (!sortedNames) return;
|
|
2522
2736
|
if (classes.every((name, i) => name === sortedNames[i])) continue;
|
|
2523
2737
|
context.report({
|
|
2524
2738
|
node: loc.node,
|
|
@@ -2616,8 +2830,7 @@ const enforceShorthand = defineRule({
|
|
|
2616
2830
|
const classSet = new Set(classes);
|
|
2617
2831
|
const values = /* @__PURE__ */ new Set();
|
|
2618
2832
|
for (const cls of classes) {
|
|
2619
|
-
const
|
|
2620
|
-
const match = VALUE_RE.exec(bare);
|
|
2833
|
+
const match = VALUE_RE.exec(splitImportant(cls).bare);
|
|
2621
2834
|
if (match) values.add(match[1]);
|
|
2622
2835
|
}
|
|
2623
2836
|
for (const value of values) {
|
|
@@ -2651,180 +2864,234 @@ const enforceShorthand = defineRule({
|
|
|
2651
2864
|
}
|
|
2652
2865
|
});
|
|
2653
2866
|
//#endregion
|
|
2867
|
+
//#region src/utils/allowlist.ts
|
|
2868
|
+
/**
|
|
2869
|
+
* Shared regex-allowlist helpers used by rules that accept an `allowlist`
|
|
2870
|
+
* option of regex source strings (currently enforce-logical / enforce-physical).
|
|
2871
|
+
*
|
|
2872
|
+
* Invalid regex sources are skipped rather than thrown so a typo in one
|
|
2873
|
+
* pattern doesn't blow up the entire lint run.
|
|
2874
|
+
*/
|
|
2875
|
+
function compileRegexList(patterns) {
|
|
2876
|
+
if (!patterns || patterns.length === 0) return [];
|
|
2877
|
+
const compiled = [];
|
|
2878
|
+
for (const p of patterns) try {
|
|
2879
|
+
compiled.push(new RegExp(p));
|
|
2880
|
+
} catch {}
|
|
2881
|
+
return compiled;
|
|
2882
|
+
}
|
|
2883
|
+
function matchesAny(value, list) {
|
|
2884
|
+
return list.some((re) => re.test(value));
|
|
2885
|
+
}
|
|
2886
|
+
//#endregion
|
|
2654
2887
|
//#region src/rules/enforce-logical.ts
|
|
2655
|
-
const
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
"scroll-pl": "scroll-ps",
|
|
2673
|
-
"scroll-pr": "scroll-pe"
|
|
2888
|
+
const LOGICAL_PHYSICAL_SCHEMA = {
|
|
2889
|
+
type: "object",
|
|
2890
|
+
properties: {
|
|
2891
|
+
allowlist: {
|
|
2892
|
+
type: "array",
|
|
2893
|
+
items: { type: "string" }
|
|
2894
|
+
},
|
|
2895
|
+
direction: {
|
|
2896
|
+
type: "string",
|
|
2897
|
+
enum: [
|
|
2898
|
+
"inline",
|
|
2899
|
+
"block",
|
|
2900
|
+
"both"
|
|
2901
|
+
]
|
|
2902
|
+
}
|
|
2903
|
+
},
|
|
2904
|
+
additionalProperties: false
|
|
2674
2905
|
};
|
|
2906
|
+
const PHYSICAL_TO_LOGICAL_MAPPINGS = [
|
|
2907
|
+
{
|
|
2908
|
+
from: "ml",
|
|
2909
|
+
to: "ms",
|
|
2910
|
+
axis: "inline"
|
|
2911
|
+
},
|
|
2912
|
+
{
|
|
2913
|
+
from: "mr",
|
|
2914
|
+
to: "me",
|
|
2915
|
+
axis: "inline"
|
|
2916
|
+
},
|
|
2917
|
+
{
|
|
2918
|
+
from: "pl",
|
|
2919
|
+
to: "ps",
|
|
2920
|
+
axis: "inline"
|
|
2921
|
+
},
|
|
2922
|
+
{
|
|
2923
|
+
from: "pr",
|
|
2924
|
+
to: "pe",
|
|
2925
|
+
axis: "inline"
|
|
2926
|
+
},
|
|
2927
|
+
{
|
|
2928
|
+
from: "left",
|
|
2929
|
+
to: "start",
|
|
2930
|
+
axis: "inline"
|
|
2931
|
+
},
|
|
2932
|
+
{
|
|
2933
|
+
from: "right",
|
|
2934
|
+
to: "end",
|
|
2935
|
+
axis: "inline"
|
|
2936
|
+
},
|
|
2937
|
+
{
|
|
2938
|
+
from: "border-l",
|
|
2939
|
+
to: "border-s",
|
|
2940
|
+
axis: "inline"
|
|
2941
|
+
},
|
|
2942
|
+
{
|
|
2943
|
+
from: "border-r",
|
|
2944
|
+
to: "border-e",
|
|
2945
|
+
axis: "inline"
|
|
2946
|
+
},
|
|
2947
|
+
{
|
|
2948
|
+
from: "rounded-l",
|
|
2949
|
+
to: "rounded-s",
|
|
2950
|
+
axis: "inline"
|
|
2951
|
+
},
|
|
2952
|
+
{
|
|
2953
|
+
from: "rounded-r",
|
|
2954
|
+
to: "rounded-e",
|
|
2955
|
+
axis: "inline"
|
|
2956
|
+
},
|
|
2957
|
+
{
|
|
2958
|
+
from: "rounded-tl",
|
|
2959
|
+
to: "rounded-ss",
|
|
2960
|
+
axis: "inline"
|
|
2961
|
+
},
|
|
2962
|
+
{
|
|
2963
|
+
from: "rounded-tr",
|
|
2964
|
+
to: "rounded-se",
|
|
2965
|
+
axis: "inline"
|
|
2966
|
+
},
|
|
2967
|
+
{
|
|
2968
|
+
from: "rounded-bl",
|
|
2969
|
+
to: "rounded-es",
|
|
2970
|
+
axis: "inline"
|
|
2971
|
+
},
|
|
2972
|
+
{
|
|
2973
|
+
from: "rounded-br",
|
|
2974
|
+
to: "rounded-ee",
|
|
2975
|
+
axis: "inline"
|
|
2976
|
+
},
|
|
2977
|
+
{
|
|
2978
|
+
from: "scroll-ml",
|
|
2979
|
+
to: "scroll-ms",
|
|
2980
|
+
axis: "inline"
|
|
2981
|
+
},
|
|
2982
|
+
{
|
|
2983
|
+
from: "scroll-mr",
|
|
2984
|
+
to: "scroll-me",
|
|
2985
|
+
axis: "inline"
|
|
2986
|
+
},
|
|
2987
|
+
{
|
|
2988
|
+
from: "scroll-pl",
|
|
2989
|
+
to: "scroll-ps",
|
|
2990
|
+
axis: "inline"
|
|
2991
|
+
},
|
|
2992
|
+
{
|
|
2993
|
+
from: "scroll-pr",
|
|
2994
|
+
to: "scroll-pe",
|
|
2995
|
+
axis: "inline"
|
|
2996
|
+
}
|
|
2997
|
+
];
|
|
2998
|
+
/** Invert a directional table. enforce-physical consumes this. */
|
|
2999
|
+
function invertAxisMappings(mappings) {
|
|
3000
|
+
return mappings.map((m) => ({
|
|
3001
|
+
from: m.to,
|
|
3002
|
+
to: m.from,
|
|
3003
|
+
axis: m.axis
|
|
3004
|
+
}));
|
|
3005
|
+
}
|
|
3006
|
+
Object.fromEntries(PHYSICAL_TO_LOGICAL_MAPPINGS.map((m) => [m.from, m.to]));
|
|
3007
|
+
/**
|
|
3008
|
+
* Build the `check` callback for a directional-rewrite rule. Both
|
|
3009
|
+
* enforce-logical and enforce-physical instantiate this with their own
|
|
3010
|
+
* mapping table and messageId; the rule's `createOnce` wires it through
|
|
3011
|
+
* `createExtractorVisitors`.
|
|
3012
|
+
*
|
|
3013
|
+
* Owns: option compilation (allowlist + direction), per-class conversion
|
|
3014
|
+
* (linear scan of the mapping table respecting `!`-prefix/suffix), and
|
|
3015
|
+
* the `reportClassReplacements` dispatch.
|
|
3016
|
+
*/
|
|
3017
|
+
function createDirectionalMapper(context, opts) {
|
|
3018
|
+
const getState = createLazyOptions(context, (o) => ({
|
|
3019
|
+
allowlist: compileRegexList(o?.allowlist),
|
|
3020
|
+
direction: o?.direction ?? "both"
|
|
3021
|
+
}));
|
|
3022
|
+
function convertClass(cls) {
|
|
3023
|
+
const { allowlist, direction } = getState();
|
|
3024
|
+
if (matchesAny(cls, allowlist)) return null;
|
|
3025
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3026
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3027
|
+
for (const { from, to, axis } of opts.mappings) {
|
|
3028
|
+
if (direction !== "both" && direction !== axis) continue;
|
|
3029
|
+
if (bareUtility === from || bareUtility.startsWith(`${from}-`)) return `${variant}${reattachImportant(to + bareUtility.slice(from.length), position)}`;
|
|
3030
|
+
}
|
|
3031
|
+
return null;
|
|
3032
|
+
}
|
|
3033
|
+
function check(locations) {
|
|
3034
|
+
for (const loc of locations) {
|
|
3035
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
3036
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3037
|
+
const converted = convertClass(cls);
|
|
3038
|
+
return converted ? [{
|
|
3039
|
+
cls,
|
|
3040
|
+
replacement: converted
|
|
3041
|
+
}] : [];
|
|
3042
|
+
});
|
|
3043
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: opts.messageId });
|
|
3044
|
+
}
|
|
3045
|
+
}
|
|
3046
|
+
return { check };
|
|
3047
|
+
}
|
|
2675
3048
|
const enforceLogical = defineRule({
|
|
2676
3049
|
meta: {
|
|
2677
3050
|
type: "suggestion",
|
|
2678
3051
|
docs: { description: "Enforce logical (RTL-friendly) Tailwind CSS properties instead of physical ones" },
|
|
2679
3052
|
fixable: "code",
|
|
2680
|
-
schema: [],
|
|
3053
|
+
schema: [LOGICAL_PHYSICAL_SCHEMA],
|
|
2681
3054
|
hasSuggestions: true,
|
|
3055
|
+
defaultOptions: [{
|
|
3056
|
+
allowlist: [],
|
|
3057
|
+
direction: "both"
|
|
3058
|
+
}],
|
|
2682
3059
|
messages: {
|
|
2683
3060
|
useLogical: "\"{{className}}\" uses a physical property. Use \"{{replacement}}\" for LTR/RTL support.",
|
|
2684
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2685
|
-
}
|
|
2686
|
-
},
|
|
2687
|
-
createOnce(context) {
|
|
2688
|
-
function convertClass(cls) {
|
|
2689
|
-
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
2690
|
-
const hasImportantPrefix = utility.startsWith("!");
|
|
2691
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
2692
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
2693
|
-
for (const [physical, logical] of Object.entries(PHYSICAL_TO_LOGICAL)) if (bareUtility === physical || bareUtility.startsWith(`${physical}-`)) {
|
|
2694
|
-
const suffix = bareUtility.slice(physical.length);
|
|
2695
|
-
return `${variant}${hasImportantPrefix ? "!" : ""}${logical}${suffix}${hasImportantSuffix ? "!" : ""}`;
|
|
2696
|
-
}
|
|
2697
|
-
return null;
|
|
2698
|
-
}
|
|
2699
|
-
function check(locations) {
|
|
2700
|
-
for (const loc of locations) {
|
|
2701
|
-
const split = splitClassesWithSeparators(loc.value);
|
|
2702
|
-
const classes = split.classes;
|
|
2703
|
-
const offending = [];
|
|
2704
|
-
for (const cls of classes) {
|
|
2705
|
-
const converted = convertClass(cls);
|
|
2706
|
-
if (converted) offending.push({
|
|
2707
|
-
cls,
|
|
2708
|
-
replacement: converted
|
|
2709
|
-
});
|
|
2710
|
-
}
|
|
2711
|
-
if (offending.length === 0) continue;
|
|
2712
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2713
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2714
|
-
for (let i = 0; i < offending.length; i++) {
|
|
2715
|
-
const { cls, replacement } = offending[i];
|
|
2716
|
-
if (i === 0) context.report({
|
|
2717
|
-
node: loc.node,
|
|
2718
|
-
messageId: "useLogical",
|
|
2719
|
-
data: {
|
|
2720
|
-
className: cls,
|
|
2721
|
-
replacement
|
|
2722
|
-
},
|
|
2723
|
-
fix(fixer) {
|
|
2724
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2725
|
-
}
|
|
2726
|
-
});
|
|
2727
|
-
else context.report({
|
|
2728
|
-
node: loc.node,
|
|
2729
|
-
messageId: "useLogical",
|
|
2730
|
-
data: {
|
|
2731
|
-
className: cls,
|
|
2732
|
-
replacement
|
|
2733
|
-
},
|
|
2734
|
-
suggest: [{
|
|
2735
|
-
messageId: "suggestReplace",
|
|
2736
|
-
data: {
|
|
2737
|
-
className: cls,
|
|
2738
|
-
replacement
|
|
2739
|
-
},
|
|
2740
|
-
fix(fixer) {
|
|
2741
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2742
|
-
}
|
|
2743
|
-
}]
|
|
2744
|
-
});
|
|
2745
|
-
}
|
|
2746
|
-
}
|
|
3061
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2747
3062
|
}
|
|
3063
|
+
},
|
|
3064
|
+
createOnce(context) {
|
|
3065
|
+
const { check } = createDirectionalMapper(context, {
|
|
3066
|
+
mappings: PHYSICAL_TO_LOGICAL_MAPPINGS,
|
|
3067
|
+
messageId: "useLogical"
|
|
3068
|
+
});
|
|
2748
3069
|
return createExtractorVisitors(context, check);
|
|
2749
3070
|
}
|
|
2750
3071
|
});
|
|
2751
3072
|
//#endregion
|
|
2752
3073
|
//#region src/rules/enforce-physical.ts
|
|
2753
|
-
const LOGICAL_TO_PHYSICAL = {};
|
|
2754
|
-
for (const [physical, logical] of Object.entries(PHYSICAL_TO_LOGICAL)) LOGICAL_TO_PHYSICAL[logical] = physical;
|
|
2755
3074
|
const enforcePhysical = defineRule({
|
|
2756
3075
|
meta: {
|
|
2757
3076
|
type: "suggestion",
|
|
2758
3077
|
docs: { description: "Enforce physical Tailwind CSS properties instead of logical ones for consistency in LTR-only projects" },
|
|
2759
3078
|
fixable: "code",
|
|
2760
|
-
schema: [],
|
|
3079
|
+
schema: [LOGICAL_PHYSICAL_SCHEMA],
|
|
2761
3080
|
hasSuggestions: true,
|
|
3081
|
+
defaultOptions: [{
|
|
3082
|
+
allowlist: [],
|
|
3083
|
+
direction: "both"
|
|
3084
|
+
}],
|
|
2762
3085
|
messages: {
|
|
2763
3086
|
usePhysical: "\"{{className}}\" uses a logical property. Use \"{{replacement}}\" for consistency.",
|
|
2764
3087
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
2765
3088
|
}
|
|
2766
3089
|
},
|
|
2767
3090
|
createOnce(context) {
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
2773
|
-
for (const [logical, physical] of Object.entries(LOGICAL_TO_PHYSICAL)) if (bareUtility === logical || bareUtility.startsWith(`${logical}-`)) {
|
|
2774
|
-
const suffix = bareUtility.slice(logical.length);
|
|
2775
|
-
return `${variant}${hasImportantPrefix ? "!" : ""}${physical}${suffix}${hasImportantSuffix ? "!" : ""}`;
|
|
2776
|
-
}
|
|
2777
|
-
return null;
|
|
2778
|
-
}
|
|
2779
|
-
function check(locations) {
|
|
2780
|
-
for (const loc of locations) {
|
|
2781
|
-
const split = splitClassesWithSeparators(loc.value);
|
|
2782
|
-
const classes = split.classes;
|
|
2783
|
-
const offending = [];
|
|
2784
|
-
for (const cls of classes) {
|
|
2785
|
-
const converted = convertClass(cls);
|
|
2786
|
-
if (converted) offending.push({
|
|
2787
|
-
cls,
|
|
2788
|
-
replacement: converted
|
|
2789
|
-
});
|
|
2790
|
-
}
|
|
2791
|
-
if (offending.length === 0) continue;
|
|
2792
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2793
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
2794
|
-
for (let i = 0; i < offending.length; i++) {
|
|
2795
|
-
const { cls, replacement } = offending[i];
|
|
2796
|
-
if (i === 0) context.report({
|
|
2797
|
-
node: loc.node,
|
|
2798
|
-
messageId: "usePhysical",
|
|
2799
|
-
data: {
|
|
2800
|
-
className: cls,
|
|
2801
|
-
replacement
|
|
2802
|
-
},
|
|
2803
|
-
fix(fixer) {
|
|
2804
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2805
|
-
}
|
|
2806
|
-
});
|
|
2807
|
-
else context.report({
|
|
2808
|
-
node: loc.node,
|
|
2809
|
-
messageId: "usePhysical",
|
|
2810
|
-
data: {
|
|
2811
|
-
className: cls,
|
|
2812
|
-
replacement
|
|
2813
|
-
},
|
|
2814
|
-
suggest: [{
|
|
2815
|
-
messageId: "suggestReplace",
|
|
2816
|
-
data: {
|
|
2817
|
-
className: cls,
|
|
2818
|
-
replacement
|
|
2819
|
-
},
|
|
2820
|
-
fix(fixer) {
|
|
2821
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2822
|
-
}
|
|
2823
|
-
}]
|
|
2824
|
-
});
|
|
2825
|
-
}
|
|
2826
|
-
}
|
|
2827
|
-
}
|
|
3091
|
+
const { check } = createDirectionalMapper(context, {
|
|
3092
|
+
mappings: invertAxisMappings(PHYSICAL_TO_LOGICAL_MAPPINGS),
|
|
3093
|
+
messageId: "usePhysical"
|
|
3094
|
+
});
|
|
2828
3095
|
return createExtractorVisitors(context, check);
|
|
2829
3096
|
}
|
|
2830
3097
|
});
|
|
@@ -2844,11 +3111,7 @@ const maxClassCount = defineRule({
|
|
|
2844
3111
|
messages: { tooMany: "Too many Tailwind classes ({{count}}). Maximum allowed is {{max}}. Consider extracting into a component or utility." }
|
|
2845
3112
|
},
|
|
2846
3113
|
createOnce(context) {
|
|
2847
|
-
|
|
2848
|
-
function getMax() {
|
|
2849
|
-
if (_max === null) _max = safeOptions(context)?.max ?? DEFAULT_MAX;
|
|
2850
|
-
return _max;
|
|
2851
|
-
}
|
|
3114
|
+
const getMax = createLazyOptions(context, (o) => o?.max ?? DEFAULT_MAX);
|
|
2852
3115
|
function check(locations) {
|
|
2853
3116
|
const max = getMax();
|
|
2854
3117
|
for (const loc of locations) {
|
|
@@ -2894,26 +3157,23 @@ const noRestrictedClasses = defineRule({
|
|
|
2894
3157
|
},
|
|
2895
3158
|
additionalProperties: false
|
|
2896
3159
|
}],
|
|
3160
|
+
defaultOptions: [{
|
|
3161
|
+
classes: [],
|
|
3162
|
+
patterns: []
|
|
3163
|
+
}],
|
|
2897
3164
|
messages: {
|
|
2898
3165
|
restricted: "\"{{className}}\" is restricted.",
|
|
2899
3166
|
restrictedWithMessage: "\"{{className}}\" is restricted: {{message}}"
|
|
2900
3167
|
}
|
|
2901
3168
|
},
|
|
2902
3169
|
createOnce(context) {
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
regex: new RegExp(p.pattern),
|
|
2911
|
-
message: p.message
|
|
2912
|
-
}))
|
|
2913
|
-
};
|
|
2914
|
-
}
|
|
2915
|
-
return _config;
|
|
2916
|
-
}
|
|
3170
|
+
const getConfig = createLazyOptions(context, (o) => ({
|
|
3171
|
+
restrictedClasses: new Set(o?.classes ?? []),
|
|
3172
|
+
patterns: (o?.patterns ?? []).map((p) => ({
|
|
3173
|
+
regex: new RegExp(p.pattern),
|
|
3174
|
+
message: p.message
|
|
3175
|
+
}))
|
|
3176
|
+
}));
|
|
2917
3177
|
function check(locations) {
|
|
2918
3178
|
const { restrictedClasses, patterns } = getConfig();
|
|
2919
3179
|
if (restrictedClasses.size === 0 && patterns.length === 0) return;
|
|
@@ -2964,14 +3224,11 @@ const noArbitraryValue = defineRule({
|
|
|
2964
3224
|
} },
|
|
2965
3225
|
additionalProperties: false
|
|
2966
3226
|
}],
|
|
3227
|
+
defaultOptions: [{ allow: [] }],
|
|
2967
3228
|
messages: { noArbitrary: "\"{{className}}\" uses an arbitrary value. Use a design token or extend your theme instead." }
|
|
2968
3229
|
},
|
|
2969
3230
|
createOnce(context) {
|
|
2970
|
-
|
|
2971
|
-
function getAllowPrefixes() {
|
|
2972
|
-
if (_allowPrefixes === null) _allowPrefixes = safeOptions(context)?.allow ?? [];
|
|
2973
|
-
return _allowPrefixes;
|
|
2974
|
-
}
|
|
3231
|
+
const getAllowPrefixes = createLazyOptions(context, (o) => o?.allow ?? []);
|
|
2975
3232
|
function isAllowed(utility) {
|
|
2976
3233
|
return getAllowPrefixes().some((prefix) => utility.startsWith(prefix));
|
|
2977
3234
|
}
|
|
@@ -2980,9 +3237,7 @@ const noArbitraryValue = defineRule({
|
|
|
2980
3237
|
const classes = splitClasses(loc.value);
|
|
2981
3238
|
for (const cls of classes) {
|
|
2982
3239
|
if (!hasArbitraryValue(cls)) continue;
|
|
2983
|
-
|
|
2984
|
-
if (utility.startsWith("!")) utility = utility.slice(1);
|
|
2985
|
-
else if (utility.endsWith("!")) utility = utility.slice(0, -1);
|
|
3240
|
+
const utility = splitImportant(extractUtility(cls)).bare;
|
|
2986
3241
|
if (isAllowed(utility)) continue;
|
|
2987
3242
|
context.report({
|
|
2988
3243
|
node: loc.node,
|
|
@@ -3019,11 +3274,7 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
3019
3274
|
}
|
|
3020
3275
|
},
|
|
3021
3276
|
createOnce(context) {
|
|
3022
|
-
|
|
3023
|
-
function getPosition() {
|
|
3024
|
-
if (_position === null) _position = safeOptions(context)?.position ?? "suffix";
|
|
3025
|
-
return _position;
|
|
3026
|
-
}
|
|
3277
|
+
const getPosition = createLazyOptions(context, (o) => o?.position ?? "suffix");
|
|
3027
3278
|
function fixUtility(utility, variantPfx) {
|
|
3028
3279
|
const position = getPosition();
|
|
3029
3280
|
if (position === "prefix" && utility.endsWith("!") && !utility.startsWith("!")) return `${variantPfx}!${utility.slice(0, -1)}`;
|
|
@@ -3031,54 +3282,18 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
3031
3282
|
return null;
|
|
3032
3283
|
}
|
|
3033
3284
|
function check(locations) {
|
|
3034
|
-
const
|
|
3285
|
+
const messageId = getPosition() === "prefix" ? "usePrefix" : "useSuffix";
|
|
3035
3286
|
for (const loc of locations) {
|
|
3036
3287
|
const split = splitClassesWithSeparators(loc.value);
|
|
3037
|
-
const
|
|
3038
|
-
const offending = [];
|
|
3039
|
-
for (const cls of classes) {
|
|
3288
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3040
3289
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3041
3290
|
const fixed = fixUtility(utility, variant);
|
|
3042
|
-
|
|
3291
|
+
return fixed ? [{
|
|
3043
3292
|
cls,
|
|
3044
3293
|
replacement: fixed
|
|
3045
|
-
}
|
|
3046
|
-
}
|
|
3047
|
-
|
|
3048
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3049
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3050
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3051
|
-
const { cls, replacement } = offending[i];
|
|
3052
|
-
if (i === 0) context.report({
|
|
3053
|
-
node: loc.node,
|
|
3054
|
-
messageId: position === "prefix" ? "usePrefix" : "useSuffix",
|
|
3055
|
-
data: {
|
|
3056
|
-
className: cls,
|
|
3057
|
-
replacement
|
|
3058
|
-
},
|
|
3059
|
-
fix(fixer) {
|
|
3060
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3061
|
-
}
|
|
3062
|
-
});
|
|
3063
|
-
else context.report({
|
|
3064
|
-
node: loc.node,
|
|
3065
|
-
messageId: position === "prefix" ? "usePrefix" : "useSuffix",
|
|
3066
|
-
data: {
|
|
3067
|
-
className: cls,
|
|
3068
|
-
replacement
|
|
3069
|
-
},
|
|
3070
|
-
suggest: [{
|
|
3071
|
-
messageId: "suggestReplace",
|
|
3072
|
-
data: {
|
|
3073
|
-
className: cls,
|
|
3074
|
-
replacement
|
|
3075
|
-
},
|
|
3076
|
-
fix(fixer) {
|
|
3077
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3078
|
-
}
|
|
3079
|
-
}]
|
|
3080
|
-
});
|
|
3081
|
-
}
|
|
3294
|
+
}] : [];
|
|
3295
|
+
});
|
|
3296
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId });
|
|
3082
3297
|
}
|
|
3083
3298
|
}
|
|
3084
3299
|
return createExtractorVisitors(context, check);
|
|
@@ -3121,50 +3336,14 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
3121
3336
|
function check(locations) {
|
|
3122
3337
|
for (const loc of locations) {
|
|
3123
3338
|
const split = splitClassesWithSeparators(loc.value);
|
|
3124
|
-
const
|
|
3125
|
-
const offending = [];
|
|
3126
|
-
for (const cls of classes) {
|
|
3339
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3127
3340
|
const fixed = fixClass(cls);
|
|
3128
|
-
|
|
3341
|
+
return fixed ? [{
|
|
3129
3342
|
cls,
|
|
3130
3343
|
replacement: fixed
|
|
3131
|
-
}
|
|
3132
|
-
}
|
|
3133
|
-
|
|
3134
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3135
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3136
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3137
|
-
const { cls, replacement } = offending[i];
|
|
3138
|
-
if (i === 0) context.report({
|
|
3139
|
-
node: loc.node,
|
|
3140
|
-
messageId: "moveNegative",
|
|
3141
|
-
data: {
|
|
3142
|
-
className: cls,
|
|
3143
|
-
replacement
|
|
3144
|
-
},
|
|
3145
|
-
fix(fixer) {
|
|
3146
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3147
|
-
}
|
|
3148
|
-
});
|
|
3149
|
-
else context.report({
|
|
3150
|
-
node: loc.node,
|
|
3151
|
-
messageId: "moveNegative",
|
|
3152
|
-
data: {
|
|
3153
|
-
className: cls,
|
|
3154
|
-
replacement
|
|
3155
|
-
},
|
|
3156
|
-
suggest: [{
|
|
3157
|
-
messageId: "suggestReplace",
|
|
3158
|
-
data: {
|
|
3159
|
-
className: cls,
|
|
3160
|
-
replacement
|
|
3161
|
-
},
|
|
3162
|
-
fix(fixer) {
|
|
3163
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3164
|
-
}
|
|
3165
|
-
}]
|
|
3166
|
-
});
|
|
3167
|
-
}
|
|
3344
|
+
}] : [];
|
|
3345
|
+
});
|
|
3346
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "moveNegative" });
|
|
3168
3347
|
}
|
|
3169
3348
|
}
|
|
3170
3349
|
return createExtractorVisitors(context, check);
|
|
@@ -3176,22 +3355,18 @@ const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-
|
|
|
3176
3355
|
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
3177
3356
|
function convertClass(cls, syntax) {
|
|
3178
3357
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3179
|
-
const
|
|
3180
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3181
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3182
|
-
const importantStart = hasImportantPrefix ? "!" : "";
|
|
3183
|
-
const importantEnd = hasImportantSuffix ? "!" : "";
|
|
3358
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3184
3359
|
if (syntax === "shorthand") {
|
|
3185
3360
|
const match = EXPLICIT_VAR_RE.exec(bareUtility);
|
|
3186
3361
|
if (match) {
|
|
3187
3362
|
const [, prefix, varName] = match;
|
|
3188
|
-
return `${variant}${
|
|
3363
|
+
return `${variant}${reattachImportant(`${prefix}-(${varName})`, position)}`;
|
|
3189
3364
|
}
|
|
3190
3365
|
} else {
|
|
3191
3366
|
const match = SHORTHAND_VAR_RE.exec(bareUtility);
|
|
3192
3367
|
if (match) {
|
|
3193
3368
|
const [, prefix, varName] = match;
|
|
3194
|
-
return `${variant}${
|
|
3369
|
+
return `${variant}${reattachImportant(`${prefix}-[var(${varName})]`, position)}`;
|
|
3195
3370
|
}
|
|
3196
3371
|
}
|
|
3197
3372
|
return null;
|
|
@@ -3218,60 +3393,20 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
3218
3393
|
}
|
|
3219
3394
|
},
|
|
3220
3395
|
createOnce(context) {
|
|
3221
|
-
|
|
3222
|
-
function getSyntax() {
|
|
3223
|
-
if (_syntax === null) _syntax = safeOptions(context)?.syntax ?? "shorthand";
|
|
3224
|
-
return _syntax;
|
|
3225
|
-
}
|
|
3396
|
+
const getSyntax = createLazyOptions(context, (o) => o?.syntax ?? "shorthand");
|
|
3226
3397
|
function check(locations) {
|
|
3227
3398
|
const syntax = getSyntax();
|
|
3399
|
+
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
3228
3400
|
for (const loc of locations) {
|
|
3229
3401
|
const split = splitClassesWithSeparators(loc.value);
|
|
3230
|
-
const
|
|
3231
|
-
const offending = [];
|
|
3232
|
-
for (const cls of classes) {
|
|
3402
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3233
3403
|
const converted = convertClass(cls, syntax);
|
|
3234
|
-
|
|
3404
|
+
return converted ? [{
|
|
3235
3405
|
cls,
|
|
3236
3406
|
replacement: converted
|
|
3237
|
-
}
|
|
3238
|
-
}
|
|
3239
|
-
|
|
3240
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3241
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3242
|
-
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
3243
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3244
|
-
const { cls, replacement } = offending[i];
|
|
3245
|
-
if (i === 0) context.report({
|
|
3246
|
-
node: loc.node,
|
|
3247
|
-
messageId,
|
|
3248
|
-
data: {
|
|
3249
|
-
className: cls,
|
|
3250
|
-
replacement
|
|
3251
|
-
},
|
|
3252
|
-
fix(fixer) {
|
|
3253
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3254
|
-
}
|
|
3255
|
-
});
|
|
3256
|
-
else context.report({
|
|
3257
|
-
node: loc.node,
|
|
3258
|
-
messageId,
|
|
3259
|
-
data: {
|
|
3260
|
-
className: cls,
|
|
3261
|
-
replacement
|
|
3262
|
-
},
|
|
3263
|
-
suggest: [{
|
|
3264
|
-
messageId: "suggestReplace",
|
|
3265
|
-
data: {
|
|
3266
|
-
className: cls,
|
|
3267
|
-
replacement
|
|
3268
|
-
},
|
|
3269
|
-
fix(fixer) {
|
|
3270
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3271
|
-
}
|
|
3272
|
-
}]
|
|
3273
|
-
});
|
|
3274
|
-
}
|
|
3407
|
+
}] : [];
|
|
3408
|
+
});
|
|
3409
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId });
|
|
3275
3410
|
}
|
|
3276
3411
|
}
|
|
3277
3412
|
return createExtractorVisitors(context, check);
|
|
@@ -3317,9 +3452,7 @@ const KNOWN_PREFIXES = [
|
|
|
3317
3452
|
* Used to group utilities by their property type.
|
|
3318
3453
|
*/
|
|
3319
3454
|
function getUtilityPrefix(utility) {
|
|
3320
|
-
let u = utility;
|
|
3321
|
-
if (u.startsWith("!")) u = u.slice(1);
|
|
3322
|
-
else if (u.endsWith("!")) u = u.slice(0, -1);
|
|
3455
|
+
let u = splitImportant(utility).bare;
|
|
3323
3456
|
if (u.startsWith("-")) u = u.slice(1);
|
|
3324
3457
|
for (const prefix of KNOWN_PREFIXES) if (u === prefix || u.startsWith(`${prefix}-`)) return prefix;
|
|
3325
3458
|
const dashIdx = u.indexOf("-");
|
|
@@ -3341,14 +3474,7 @@ const noDarkWithoutLight = defineRule({
|
|
|
3341
3474
|
messages: { missingBase: "\"{{className}}\" uses the {{variant}} variant, but there is no base \"{{prefix}}-*\" class on this element." }
|
|
3342
3475
|
},
|
|
3343
3476
|
createOnce(context) {
|
|
3344
|
-
|
|
3345
|
-
function getWatchedVariants() {
|
|
3346
|
-
if (_watchedVariants === null) {
|
|
3347
|
-
const options = safeOptions(context);
|
|
3348
|
-
_watchedVariants = new Set(options?.variants ?? DEFAULT_VARIANTS);
|
|
3349
|
-
}
|
|
3350
|
-
return _watchedVariants;
|
|
3351
|
-
}
|
|
3477
|
+
const getWatchedVariants = createLazyOptions(context, (o) => new Set(o?.variants ?? DEFAULT_VARIANTS));
|
|
3352
3478
|
function check(locations) {
|
|
3353
3479
|
const watchedVariants = getWatchedVariants();
|
|
3354
3480
|
for (const loc of locations) {
|
|
@@ -3419,8 +3545,10 @@ const LCH_RE = /^lch\s*\(/;
|
|
|
3419
3545
|
const LAB_RE = /^lab\s*\(/;
|
|
3420
3546
|
const HWB_RE = /^hwb\s*\(/;
|
|
3421
3547
|
const COLOR_RE = /^color\s*\(/;
|
|
3548
|
+
const VAR_REF_RE = /var\(\s*--[\w-]+/i;
|
|
3422
3549
|
function isHardcodedColor(value) {
|
|
3423
3550
|
const trimmed = value.trim();
|
|
3551
|
+
if (VAR_REF_RE.test(trimmed)) return false;
|
|
3424
3552
|
return HEX_RE.test(trimmed) || RGB_RE.test(trimmed) || HSL_RE.test(trimmed) || OKLCH_RE.test(trimmed) || OKLAB_RE.test(trimmed) || LCH_RE.test(trimmed) || LAB_RE.test(trimmed) || HWB_RE.test(trimmed) || COLOR_RE.test(trimmed);
|
|
3425
3553
|
}
|
|
3426
3554
|
function isColorUtility(utility) {
|
|
@@ -3442,17 +3570,11 @@ const noHardcodedColors = defineRule({
|
|
|
3442
3570
|
},
|
|
3443
3571
|
additionalProperties: false
|
|
3444
3572
|
}],
|
|
3573
|
+
defaultOptions: [{ allow: [] }],
|
|
3445
3574
|
messages: { noHardcoded: "\"{{className}}\" uses a hardcoded color value. Use a design token from your theme instead." }
|
|
3446
3575
|
},
|
|
3447
3576
|
createOnce(context) {
|
|
3448
|
-
|
|
3449
|
-
function getAllowlist() {
|
|
3450
|
-
if (_allowlist === null) {
|
|
3451
|
-
const options = safeOptions(context);
|
|
3452
|
-
_allowlist = new Set(options?.allow ?? []);
|
|
3453
|
-
}
|
|
3454
|
-
return _allowlist;
|
|
3455
|
-
}
|
|
3577
|
+
const getAllowlist = createLazyOptions(context, (o) => new Set(o?.allow ?? []));
|
|
3456
3578
|
function check(locations) {
|
|
3457
3579
|
const allowlist = getAllowlist();
|
|
3458
3580
|
for (const loc of locations) {
|
|
@@ -3460,9 +3582,7 @@ const noHardcodedColors = defineRule({
|
|
|
3460
3582
|
for (const cls of classes) {
|
|
3461
3583
|
if (allowlist.has(cls)) continue;
|
|
3462
3584
|
if (!hasArbitraryValue(cls)) continue;
|
|
3463
|
-
|
|
3464
|
-
if (utility.startsWith("!")) utility = utility.slice(1);
|
|
3465
|
-
else if (utility.endsWith("!")) utility = utility.slice(0, -1);
|
|
3585
|
+
const utility = splitImportant(extractUtility(cls)).bare;
|
|
3466
3586
|
if (!isColorUtility(utility)) continue;
|
|
3467
3587
|
const value = getArbitraryValue(cls);
|
|
3468
3588
|
if (!value) continue;
|
|
@@ -3633,6 +3753,7 @@ const consistentVariantOrder = defineRule({
|
|
|
3633
3753
|
additionalProperties: false
|
|
3634
3754
|
}],
|
|
3635
3755
|
hasSuggestions: true,
|
|
3756
|
+
defaultOptions: [{}],
|
|
3636
3757
|
messages: {
|
|
3637
3758
|
wrongOrder: "\"{{className}}\" has variants in wrong order. Use \"{{replacement}}\" instead.",
|
|
3638
3759
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
@@ -3640,54 +3761,33 @@ const consistentVariantOrder = defineRule({
|
|
|
3640
3761
|
},
|
|
3641
3762
|
createOnce(context) {
|
|
3642
3763
|
const getDS = createLazyLoader(context);
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
};
|
|
3656
|
-
} else if (dsCache && dsCache.hasVariantOrder()) {
|
|
3657
|
-
for (const variant of DEFAULT_VARIANT_ORDER) {
|
|
3658
|
-
const p = dsCache.getVariantPriority(variant);
|
|
3659
|
-
if (p !== null) priorityMap.set(variant, p);
|
|
3660
|
-
}
|
|
3661
|
-
_config = {
|
|
3662
|
-
priorityMap,
|
|
3663
|
-
fallbackPriority: Number.MAX_SAFE_INTEGER,
|
|
3664
|
-
dsCache
|
|
3665
|
-
};
|
|
3666
|
-
} else {
|
|
3667
|
-
for (let i = 0; i < DEFAULT_VARIANT_ORDER.length; i++) priorityMap.set(DEFAULT_VARIANT_ORDER[i], i);
|
|
3668
|
-
_config = {
|
|
3669
|
-
priorityMap,
|
|
3670
|
-
fallbackPriority: DEFAULT_VARIANT_ORDER.length,
|
|
3671
|
-
dsCache
|
|
3672
|
-
};
|
|
3673
|
-
}
|
|
3764
|
+
const getPriority = createLazyOptions(context, (o) => {
|
|
3765
|
+
let dsCache = null;
|
|
3766
|
+
try {
|
|
3767
|
+
dsCache = getDS().cache;
|
|
3768
|
+
} catch (err) {
|
|
3769
|
+
if (!isFatalError(err)) throw err;
|
|
3770
|
+
}
|
|
3771
|
+
if (o?.order) {
|
|
3772
|
+
const map = /* @__PURE__ */ new Map();
|
|
3773
|
+
for (let i = 0; i < o.order.length; i++) map.set(o.order[i], i);
|
|
3774
|
+
const fallback = o.order.length;
|
|
3775
|
+
return (v) => map.get(v) ?? fallback;
|
|
3674
3776
|
}
|
|
3675
|
-
return _config;
|
|
3676
|
-
}
|
|
3677
|
-
function getVariantPriority(variant) {
|
|
3678
|
-
const { priorityMap, fallbackPriority, dsCache } = getConfig();
|
|
3679
|
-
const priority = priorityMap.get(variant);
|
|
3680
|
-
if (priority !== void 0) return priority;
|
|
3681
3777
|
if (dsCache && dsCache.hasVariantOrder()) {
|
|
3682
|
-
const
|
|
3683
|
-
|
|
3778
|
+
const ds = dsCache;
|
|
3779
|
+
return (v) => ds.getVariantPriority(v) ?? Number.MAX_SAFE_INTEGER;
|
|
3684
3780
|
}
|
|
3685
|
-
|
|
3686
|
-
|
|
3781
|
+
const map = /* @__PURE__ */ new Map();
|
|
3782
|
+
for (let i = 0; i < DEFAULT_VARIANT_ORDER.length; i++) map.set(DEFAULT_VARIANT_ORDER[i], i);
|
|
3783
|
+
const fallback = DEFAULT_VARIANT_ORDER.length;
|
|
3784
|
+
return (v) => map.get(v) ?? fallback;
|
|
3785
|
+
});
|
|
3687
3786
|
function reorderClass(cls) {
|
|
3688
3787
|
const variants = extractVariants(cls);
|
|
3689
3788
|
if (variants.length < 2) return null;
|
|
3690
|
-
const
|
|
3789
|
+
const priorityOf = getPriority();
|
|
3790
|
+
const sorted = [...variants].sort((a, b) => priorityOf(a) - priorityOf(b));
|
|
3691
3791
|
const nonPseudo = [];
|
|
3692
3792
|
const pseudo = [];
|
|
3693
3793
|
for (const v of sorted) if (PSEUDO_ELEMENTS.has(v)) pseudo.push(v);
|
|
@@ -3772,16 +3872,8 @@ const enforceConsistentLineWrapping = defineRule({
|
|
|
3772
3872
|
}
|
|
3773
3873
|
},
|
|
3774
3874
|
createOnce(context) {
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
|
|
3778
|
-
return _printWidth;
|
|
3779
|
-
}
|
|
3780
|
-
let _classesPerLine = null;
|
|
3781
|
-
function getClassesPerLine() {
|
|
3782
|
-
if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
|
|
3783
|
-
return _classesPerLine;
|
|
3784
|
-
}
|
|
3875
|
+
const getPrintWidth = createLazyOptions(context, (o) => o?.printWidth ?? DEFAULT_PRINT_WIDTH);
|
|
3876
|
+
const getClassesPerLine = createLazyOptions(context, (o) => o?.classesPerLine);
|
|
3785
3877
|
function check(locations) {
|
|
3786
3878
|
const printWidth = getPrintWidth();
|
|
3787
3879
|
const classesPerLine = getClassesPerLine();
|
|
@@ -3840,69 +3932,33 @@ const noUnnecessaryArbitraryValue = defineRule({
|
|
|
3840
3932
|
additionalProperties: false
|
|
3841
3933
|
}],
|
|
3842
3934
|
hasSuggestions: true,
|
|
3935
|
+
defaultOptions: [{}],
|
|
3843
3936
|
messages: {
|
|
3844
3937
|
unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead.",
|
|
3845
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3938
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
3939
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
3846
3940
|
}
|
|
3847
3941
|
},
|
|
3848
3942
|
createOnce(context) {
|
|
3849
3943
|
const getDS = createLazyLoader(context);
|
|
3850
3944
|
function check(locations) {
|
|
3851
|
-
|
|
3945
|
+
if (locations.length === 0) return;
|
|
3946
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
3852
3947
|
if (!ds) return;
|
|
3853
3948
|
const { cache } = ds;
|
|
3854
3949
|
for (const loc of locations) {
|
|
3855
3950
|
const split = splitClassesWithSeparators(loc.value);
|
|
3856
|
-
const
|
|
3857
|
-
|
|
3858
|
-
for (const cls of classes) {
|
|
3859
|
-
if (!hasArbitraryValue(cls)) continue;
|
|
3951
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3952
|
+
if (!hasArbitraryValue(cls)) return [];
|
|
3860
3953
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3861
|
-
const
|
|
3862
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3863
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
3954
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3864
3955
|
const named = cache.getNamedEquivalent(bareUtility);
|
|
3865
|
-
|
|
3866
|
-
offending.push({
|
|
3956
|
+
return named ? [{
|
|
3867
3957
|
cls,
|
|
3868
|
-
replacement: variant + (
|
|
3869
|
-
}
|
|
3870
|
-
}
|
|
3871
|
-
|
|
3872
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
3873
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
3874
|
-
for (let i = 0; i < offending.length; i++) {
|
|
3875
|
-
const { cls, replacement } = offending[i];
|
|
3876
|
-
if (i === 0) context.report({
|
|
3877
|
-
node: loc.node,
|
|
3878
|
-
messageId: "unnecessaryArbitrary",
|
|
3879
|
-
data: {
|
|
3880
|
-
className: cls,
|
|
3881
|
-
replacement
|
|
3882
|
-
},
|
|
3883
|
-
fix(fixer) {
|
|
3884
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3885
|
-
}
|
|
3886
|
-
});
|
|
3887
|
-
else context.report({
|
|
3888
|
-
node: loc.node,
|
|
3889
|
-
messageId: "unnecessaryArbitrary",
|
|
3890
|
-
data: {
|
|
3891
|
-
className: cls,
|
|
3892
|
-
replacement
|
|
3893
|
-
},
|
|
3894
|
-
suggest: [{
|
|
3895
|
-
messageId: "suggestReplace",
|
|
3896
|
-
data: {
|
|
3897
|
-
className: cls,
|
|
3898
|
-
replacement
|
|
3899
|
-
},
|
|
3900
|
-
fix(fixer) {
|
|
3901
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3902
|
-
}
|
|
3903
|
-
}]
|
|
3904
|
-
});
|
|
3905
|
-
}
|
|
3958
|
+
replacement: variant + reattachImportant(named, position)
|
|
3959
|
+
}] : [];
|
|
3960
|
+
});
|
|
3961
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "unnecessaryArbitrary" });
|
|
3906
3962
|
}
|
|
3907
3963
|
}
|
|
3908
3964
|
return createExtractorVisitors(context, check);
|
|
@@ -3973,73 +4029,37 @@ const plugin = definePlugin({
|
|
|
3973
4029
|
additionalProperties: false
|
|
3974
4030
|
}],
|
|
3975
4031
|
hasSuggestions: true,
|
|
4032
|
+
defaultOptions: [{}],
|
|
3976
4033
|
messages: {
|
|
3977
4034
|
preferNamed: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named theme token.",
|
|
3978
|
-
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
4035
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
4036
|
+
...DS_UNAVAILABLE_MESSAGE
|
|
3979
4037
|
}
|
|
3980
4038
|
},
|
|
3981
4039
|
createOnce(context) {
|
|
3982
4040
|
const getDS = createLazyLoader(context);
|
|
3983
4041
|
function check(locations) {
|
|
3984
|
-
|
|
4042
|
+
if (locations.length === 0) return;
|
|
4043
|
+
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
3985
4044
|
if (!ds) return;
|
|
3986
4045
|
const { cache } = ds;
|
|
3987
4046
|
for (const loc of locations) {
|
|
3988
4047
|
const split = splitClassesWithSeparators(loc.value);
|
|
3989
|
-
const
|
|
3990
|
-
const offending = [];
|
|
3991
|
-
for (const cls of classes) {
|
|
4048
|
+
const offending = split.classes.flatMap((cls) => {
|
|
3992
4049
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3993
|
-
const
|
|
3994
|
-
const hasImportantSuffix = !hasImportantPrefix && utility.endsWith("!");
|
|
3995
|
-
const bareUtility = hasImportantPrefix ? utility.slice(1) : hasImportantSuffix ? utility.slice(0, -1) : utility;
|
|
4050
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3996
4051
|
const match = detectRawVariable(bareUtility);
|
|
3997
|
-
if (!match)
|
|
4052
|
+
if (!match) return [];
|
|
3998
4053
|
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
3999
|
-
if (!cache.isValid(candidate))
|
|
4054
|
+
if (!cache.isValid(candidate)) return [];
|
|
4000
4055
|
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
4001
|
-
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`)
|
|
4002
|
-
|
|
4003
|
-
offending.push({
|
|
4056
|
+
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|
|
4057
|
+
return [{
|
|
4004
4058
|
cls,
|
|
4005
|
-
replacement
|
|
4006
|
-
}
|
|
4007
|
-
}
|
|
4008
|
-
|
|
4009
|
-
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
4010
|
-
const fixedValue = rebuildClassString(split, classes.map((cls) => replacements.get(cls) ?? cls));
|
|
4011
|
-
for (let i = 0; i < offending.length; i++) {
|
|
4012
|
-
const { cls, replacement } = offending[i];
|
|
4013
|
-
if (i === 0) context.report({
|
|
4014
|
-
node: loc.node,
|
|
4015
|
-
messageId: "preferNamed",
|
|
4016
|
-
data: {
|
|
4017
|
-
className: cls,
|
|
4018
|
-
replacement
|
|
4019
|
-
},
|
|
4020
|
-
fix(fixer) {
|
|
4021
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4022
|
-
}
|
|
4023
|
-
});
|
|
4024
|
-
else context.report({
|
|
4025
|
-
node: loc.node,
|
|
4026
|
-
messageId: "preferNamed",
|
|
4027
|
-
data: {
|
|
4028
|
-
className: cls,
|
|
4029
|
-
replacement
|
|
4030
|
-
},
|
|
4031
|
-
suggest: [{
|
|
4032
|
-
messageId: "suggestReplace",
|
|
4033
|
-
data: {
|
|
4034
|
-
className: cls,
|
|
4035
|
-
replacement
|
|
4036
|
-
},
|
|
4037
|
-
fix(fixer) {
|
|
4038
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4039
|
-
}
|
|
4040
|
-
}]
|
|
4041
|
-
});
|
|
4042
|
-
}
|
|
4059
|
+
replacement: variant + reattachImportant(candidate, position)
|
|
4060
|
+
}];
|
|
4061
|
+
});
|
|
4062
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "preferNamed" });
|
|
4043
4063
|
}
|
|
4044
4064
|
}
|
|
4045
4065
|
return createExtractorVisitors(context, check);
|