oxlint-tailwindcss 0.1.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/LICENSE +21 -0
- package/README.md +701 -0
- package/dist/index.cjs +2547 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2549 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2549 @@
|
|
|
1
|
+
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
const DEFAULT_EXTRACTOR_CONFIG = {
|
|
6
|
+
attributes: ["className", "class"],
|
|
7
|
+
callees: [
|
|
8
|
+
"cn",
|
|
9
|
+
"clsx",
|
|
10
|
+
"cva",
|
|
11
|
+
"twMerge",
|
|
12
|
+
"tv",
|
|
13
|
+
"cx",
|
|
14
|
+
"classnames",
|
|
15
|
+
"ctl",
|
|
16
|
+
"twJoin",
|
|
17
|
+
"cc",
|
|
18
|
+
"clb",
|
|
19
|
+
"cnb",
|
|
20
|
+
"objstr"
|
|
21
|
+
],
|
|
22
|
+
tags: ["tw"],
|
|
23
|
+
variablePatterns: [
|
|
24
|
+
/^classNames?$/,
|
|
25
|
+
/^classes$/,
|
|
26
|
+
/^styles?$/
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Extracts class locations from a JSXAttribute node.
|
|
31
|
+
* Handles: className="...", className={`...`}, className={cond ? "..." : "..."}
|
|
32
|
+
*/
|
|
33
|
+
function extractFromJSXAttribute(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
34
|
+
const name = node.name.type === "JSXIdentifier" ? node.name.name : void 0;
|
|
35
|
+
if (!name || !config.attributes.includes(name)) return [];
|
|
36
|
+
if (!node.value) return [];
|
|
37
|
+
if (node.value.type === "Literal" && typeof node.value.value === "string") return [{
|
|
38
|
+
value: node.value.value,
|
|
39
|
+
node: node.value,
|
|
40
|
+
range: [node.value.range[0] + 1, node.value.range[1] - 1]
|
|
41
|
+
}];
|
|
42
|
+
if (node.value.type === "JSXExpressionContainer") return extractFromExpression(node.value.expression);
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Extracts class locations from a CallExpression (cn, clsx, cva, tv, etc).
|
|
47
|
+
* For cva() and tv(), uses dedicated extractors that understand their config structure.
|
|
48
|
+
*/
|
|
49
|
+
function extractFromCallExpression(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
50
|
+
const calleeName = getCalleeName(node.callee);
|
|
51
|
+
if (!calleeName || !config.callees.includes(calleeName)) return [];
|
|
52
|
+
if (calleeName === "cva") return extractFromCvaCall(node);
|
|
53
|
+
if (calleeName === "tv") return extractFromTvCall(node);
|
|
54
|
+
const results = [];
|
|
55
|
+
for (const arg of node.arguments) results.push(...extractFromExpression(arg));
|
|
56
|
+
return results;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Extracts class locations from a TaggedTemplateExpression (tw`...`).
|
|
60
|
+
*/
|
|
61
|
+
function extractFromTaggedTemplate(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
62
|
+
const tagName = getCalleeName(node.tag);
|
|
63
|
+
if (!tagName || !config.tags.includes(tagName)) return [];
|
|
64
|
+
return extractFromTemplateLiteral(node.quasi);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Extracts class locations from a cva() call.
|
|
68
|
+
* Handles: base string, variants config, compoundVariants.
|
|
69
|
+
*/
|
|
70
|
+
function extractFromCvaCall(node) {
|
|
71
|
+
const results = [];
|
|
72
|
+
for (const arg of node.arguments) if (arg.type === "ObjectExpression") results.push(...extractFromCvaConfig(arg));
|
|
73
|
+
else results.push(...extractFromExpression(arg));
|
|
74
|
+
return results;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Extracts class locations from a cva config object.
|
|
78
|
+
* Properties: variants (nested category→value objects), compoundVariants (array with class/className).
|
|
79
|
+
* Ignores: defaultVariants.
|
|
80
|
+
*/
|
|
81
|
+
function extractFromCvaConfig(node) {
|
|
82
|
+
const results = [];
|
|
83
|
+
for (const prop of node.properties) {
|
|
84
|
+
if (prop.type !== "Property") continue;
|
|
85
|
+
const name = getPropertyName(prop.key);
|
|
86
|
+
if (name === "variants" && prop.value.type === "ObjectExpression") for (const category of prop.value.properties) {
|
|
87
|
+
if (category.type !== "Property" || category.value.type !== "ObjectExpression") continue;
|
|
88
|
+
for (const variant of category.value.properties) if (variant.type === "Property") results.push(...extractFromExpression(variant.value));
|
|
89
|
+
}
|
|
90
|
+
else if (name === "compoundVariants" && prop.value.type === "ArrayExpression") results.push(...extractClassFromCompoundEntries(prop.value));
|
|
91
|
+
else if (name === "defaultVariants") {} else results.push(...extractFromExpression(prop.value));
|
|
92
|
+
}
|
|
93
|
+
return results;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Extracts class locations from a tv() call.
|
|
97
|
+
* Handles: base, slots, variants (with slot objects), compoundVariants, compoundSlots.
|
|
98
|
+
*/
|
|
99
|
+
function extractFromTvCall(node) {
|
|
100
|
+
const results = [];
|
|
101
|
+
for (const arg of node.arguments) if (arg.type === "ObjectExpression") results.push(...extractFromTvConfig(arg));
|
|
102
|
+
else results.push(...extractFromExpression(arg));
|
|
103
|
+
return results;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Extracts class locations from a tv config object.
|
|
107
|
+
* Properties: base, slots, variants (may have slot sub-objects), compoundVariants, compoundSlots.
|
|
108
|
+
* Ignores: defaultVariants.
|
|
109
|
+
*/
|
|
110
|
+
function extractFromTvConfig(node) {
|
|
111
|
+
const results = [];
|
|
112
|
+
for (const prop of node.properties) {
|
|
113
|
+
if (prop.type !== "Property") continue;
|
|
114
|
+
const name = getPropertyName(prop.key);
|
|
115
|
+
if (name === "base") results.push(...extractFromExpression(prop.value));
|
|
116
|
+
else if (name === "slots" && prop.value.type === "ObjectExpression") {
|
|
117
|
+
for (const slot of prop.value.properties) if (slot.type === "Property") results.push(...extractFromExpression(slot.value));
|
|
118
|
+
} else if (name === "variants" && prop.value.type === "ObjectExpression") for (const category of prop.value.properties) {
|
|
119
|
+
if (category.type !== "Property" || category.value.type !== "ObjectExpression") continue;
|
|
120
|
+
for (const variant of category.value.properties) {
|
|
121
|
+
if (variant.type !== "Property") continue;
|
|
122
|
+
if (variant.value.type === "ObjectExpression") {
|
|
123
|
+
for (const slotProp of variant.value.properties) if (slotProp.type === "Property") results.push(...extractFromExpression(slotProp.value));
|
|
124
|
+
} else results.push(...extractFromExpression(variant.value));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if ((name === "compoundVariants" || name === "compoundSlots") && prop.value.type === "ArrayExpression") results.push(...extractClassFromCompoundEntries(prop.value));
|
|
128
|
+
else if (name === "defaultVariants") {} else results.push(...extractFromExpression(prop.value));
|
|
129
|
+
}
|
|
130
|
+
return results;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Extracts class/className values from an array of compound variant/slot entries.
|
|
134
|
+
* Each entry is an ObjectExpression; we look for "class" or "className" properties.
|
|
135
|
+
*/
|
|
136
|
+
function extractClassFromCompoundEntries(node) {
|
|
137
|
+
const results = [];
|
|
138
|
+
for (const element of node.elements) {
|
|
139
|
+
if (!element || element.type !== "ObjectExpression") continue;
|
|
140
|
+
for (const prop of element.properties) {
|
|
141
|
+
if (prop.type !== "Property") continue;
|
|
142
|
+
const name = getPropertyName(prop.key);
|
|
143
|
+
if (name === "class" || name === "className") results.push(...extractFromExpression(prop.value));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return results;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Gets the property name from an object key node.
|
|
150
|
+
*/
|
|
151
|
+
function getPropertyName(key) {
|
|
152
|
+
if (key.type === "Identifier") return key.name;
|
|
153
|
+
if (key.type === "Literal" && typeof key.value === "string") return key.value;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Extracts class locations from a VariableDeclarator whose name matches variablePatterns.
|
|
157
|
+
* e.g. const className = "flex items-center"
|
|
158
|
+
*/
|
|
159
|
+
function extractFromVariableDeclarator(node, config = DEFAULT_EXTRACTOR_CONFIG) {
|
|
160
|
+
if (node.id.type !== "Identifier") return [];
|
|
161
|
+
if (!config.variablePatterns.some((p) => p.test(node.id.name))) return [];
|
|
162
|
+
if (!node.init) return [];
|
|
163
|
+
return extractFromExpression(node.init);
|
|
164
|
+
}
|
|
165
|
+
function extractFromExpression(node) {
|
|
166
|
+
if (node.type === "Literal" && typeof node.value === "string") return [{
|
|
167
|
+
value: node.value,
|
|
168
|
+
node,
|
|
169
|
+
range: [node.range[0] + 1, node.range[1] - 1]
|
|
170
|
+
}];
|
|
171
|
+
if (node.type === "TemplateLiteral") return extractFromTemplateLiteral(node);
|
|
172
|
+
if (node.type === "ConditionalExpression") return [...extractFromExpression(node.consequent), ...extractFromExpression(node.alternate)];
|
|
173
|
+
if (node.type === "LogicalExpression") return extractFromExpression(node.right);
|
|
174
|
+
if (node.type === "ObjectExpression") {
|
|
175
|
+
const results = [];
|
|
176
|
+
for (const prop of node.properties) if (prop.type === "Property" && prop.key.type === "Literal" && typeof prop.key.value === "string") results.push({
|
|
177
|
+
value: prop.key.value,
|
|
178
|
+
node: prop.key,
|
|
179
|
+
range: [prop.key.range[0] + 1, prop.key.range[1] - 1]
|
|
180
|
+
});
|
|
181
|
+
return results;
|
|
182
|
+
}
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
function extractFromTemplateLiteral(node) {
|
|
186
|
+
const results = [];
|
|
187
|
+
for (let i = 0; i < node.quasis.length; i++) {
|
|
188
|
+
const quasi = node.quasis[i];
|
|
189
|
+
const value = quasi.value.raw;
|
|
190
|
+
if (value.trim().length > 0) {
|
|
191
|
+
const start = quasi.range[0] + 1;
|
|
192
|
+
results.push({
|
|
193
|
+
value,
|
|
194
|
+
node: quasi,
|
|
195
|
+
range: [start, start + value.length],
|
|
196
|
+
preserveLeadingSpace: i > 0,
|
|
197
|
+
preserveTrailingSpace: i < node.quasis.length - 1
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return results;
|
|
202
|
+
}
|
|
203
|
+
function getCalleeName(node) {
|
|
204
|
+
if (node.type === "Identifier") return node.name;
|
|
205
|
+
if (node.type === "MemberExpression") {
|
|
206
|
+
const prop = node.property;
|
|
207
|
+
if (prop.type === "Identifier") return prop.name;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/utils/class-splitter.ts
|
|
212
|
+
/**
|
|
213
|
+
* Splits a Tailwind class string respecting:
|
|
214
|
+
* - Nested brackets: bg-[url('...')], h-[calc(100%+2rem)]
|
|
215
|
+
* - Arbitrary variants: [&>svg]:w-4
|
|
216
|
+
* - Negative values: -translate-x-1
|
|
217
|
+
* - Important modifier: !font-bold
|
|
218
|
+
*/
|
|
219
|
+
function splitClasses(classString) {
|
|
220
|
+
const classes = [];
|
|
221
|
+
let current = "";
|
|
222
|
+
let bracketDepth = 0;
|
|
223
|
+
let inSingleQuote = false;
|
|
224
|
+
let inDoubleQuote = false;
|
|
225
|
+
for (let i = 0; i < classString.length; i++) {
|
|
226
|
+
const char = classString[i];
|
|
227
|
+
const prev = classString[i - 1];
|
|
228
|
+
if (bracketDepth > 0 && prev !== "\\") {
|
|
229
|
+
if (char === "'" && !inDoubleQuote) inSingleQuote = !inSingleQuote;
|
|
230
|
+
if (char === "\"" && !inSingleQuote) inDoubleQuote = !inDoubleQuote;
|
|
231
|
+
}
|
|
232
|
+
if (!inSingleQuote && !inDoubleQuote) {
|
|
233
|
+
if (char === "[") bracketDepth++;
|
|
234
|
+
if (char === "]") bracketDepth--;
|
|
235
|
+
}
|
|
236
|
+
if (/\s/.test(char) && bracketDepth === 0) {
|
|
237
|
+
if (current.length > 0) {
|
|
238
|
+
classes.push(current);
|
|
239
|
+
current = "";
|
|
240
|
+
}
|
|
241
|
+
} else current += char;
|
|
242
|
+
}
|
|
243
|
+
if (current.length > 0) classes.push(current);
|
|
244
|
+
return classes;
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/rules/no-duplicate-classes.ts
|
|
248
|
+
const noDuplicateClasses = defineRule({
|
|
249
|
+
meta: {
|
|
250
|
+
type: "problem",
|
|
251
|
+
docs: { description: "Disallow duplicate Tailwind CSS classes" },
|
|
252
|
+
fixable: "code",
|
|
253
|
+
schema: [],
|
|
254
|
+
messages: { duplicate: "Duplicate class: \"{{className}}\". Removing the second occurrence." }
|
|
255
|
+
},
|
|
256
|
+
createOnce(context) {
|
|
257
|
+
function check(locations) {
|
|
258
|
+
for (const loc of locations) {
|
|
259
|
+
const classes = splitClasses(loc.value);
|
|
260
|
+
const seen = /* @__PURE__ */ new Set();
|
|
261
|
+
const duplicates = [];
|
|
262
|
+
for (const cls of classes) if (seen.has(cls)) duplicates.push(cls);
|
|
263
|
+
else seen.add(cls);
|
|
264
|
+
if (duplicates.length > 0) {
|
|
265
|
+
const fixed = [...new Set(classes)].join(" ");
|
|
266
|
+
for (const dup of duplicates) context.report({
|
|
267
|
+
node: loc.node,
|
|
268
|
+
messageId: "duplicate",
|
|
269
|
+
data: { className: dup },
|
|
270
|
+
fix(fixer) {
|
|
271
|
+
return fixer.replaceTextRange(loc.range, fixed);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
JSXAttribute(node) {
|
|
279
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
280
|
+
},
|
|
281
|
+
CallExpression(node) {
|
|
282
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
283
|
+
},
|
|
284
|
+
TaggedTemplateExpression(node) {
|
|
285
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
286
|
+
},
|
|
287
|
+
VariableDeclarator(node) {
|
|
288
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/rules/no-unnecessary-whitespace.ts
|
|
295
|
+
const noUnnecessaryWhitespace = defineRule({
|
|
296
|
+
meta: {
|
|
297
|
+
type: "suggestion",
|
|
298
|
+
docs: { description: "Disallow unnecessary whitespace in Tailwind CSS class strings" },
|
|
299
|
+
fixable: "code",
|
|
300
|
+
schema: [],
|
|
301
|
+
messages: { unnecessaryWhitespace: "Unnecessary whitespace in Tailwind classes. Can be normalized." }
|
|
302
|
+
},
|
|
303
|
+
createOnce(context) {
|
|
304
|
+
function check(locations) {
|
|
305
|
+
for (const loc of locations) {
|
|
306
|
+
let normalized = loc.value.replace(/\s+/g, " ");
|
|
307
|
+
if (normalized.startsWith(" ") && !loc.preserveLeadingSpace) normalized = normalized.slice(1);
|
|
308
|
+
if (normalized.endsWith(" ") && !loc.preserveTrailingSpace) normalized = normalized.slice(0, -1);
|
|
309
|
+
if (normalized !== loc.value) context.report({
|
|
310
|
+
node: loc.node,
|
|
311
|
+
messageId: "unnecessaryWhitespace",
|
|
312
|
+
fix(fixer) {
|
|
313
|
+
return fixer.replaceTextRange(loc.range, normalized);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
JSXAttribute(node) {
|
|
320
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
321
|
+
},
|
|
322
|
+
CallExpression(node) {
|
|
323
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
324
|
+
},
|
|
325
|
+
TaggedTemplateExpression(node) {
|
|
326
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
327
|
+
},
|
|
328
|
+
VariableDeclarator(node) {
|
|
329
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
//#endregion
|
|
335
|
+
//#region src/utils/levenshtein.ts
|
|
336
|
+
/**
|
|
337
|
+
* Calculates the Levenshtein distance between two strings.
|
|
338
|
+
*/
|
|
339
|
+
function levenshtein(a, b) {
|
|
340
|
+
if (a.length === 0) return b.length;
|
|
341
|
+
if (b.length === 0) return a.length;
|
|
342
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
343
|
+
for (let i = 1; i <= a.length; i++) {
|
|
344
|
+
let prev = i;
|
|
345
|
+
for (let j = 1; j <= b.length; j++) {
|
|
346
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
347
|
+
const current = Math.min(row[j] + 1, prev + 1, row[j - 1] + cost);
|
|
348
|
+
row[j - 1] = prev;
|
|
349
|
+
prev = current;
|
|
350
|
+
}
|
|
351
|
+
row[b.length] = prev;
|
|
352
|
+
}
|
|
353
|
+
return row[b.length];
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Finds the best suggestion for a given string within a list of candidates.
|
|
357
|
+
* Returns null if no suggestion is close enough.
|
|
358
|
+
*/
|
|
359
|
+
function findBestSuggestion(input, candidates, maxDistance = 3) {
|
|
360
|
+
let best = null;
|
|
361
|
+
let bestDistance = maxDistance + 1;
|
|
362
|
+
for (const candidate of candidates) {
|
|
363
|
+
if (Math.abs(input.length - candidate.length) > maxDistance) continue;
|
|
364
|
+
const dist = levenshtein(input, candidate);
|
|
365
|
+
if (dist < bestDistance) {
|
|
366
|
+
bestDistance = dist;
|
|
367
|
+
best = candidate;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return best;
|
|
371
|
+
}
|
|
372
|
+
//#endregion
|
|
373
|
+
//#region src/utils/floating-point.ts
|
|
374
|
+
/**
|
|
375
|
+
* Rounds rem/em/px values in class strings to avoid
|
|
376
|
+
* JavaScript floating point issues.
|
|
377
|
+
*
|
|
378
|
+
* Example: "text-lg/[2.4000000000000004rem]" -> "text-lg/[2.4rem]"
|
|
379
|
+
*
|
|
380
|
+
* Regression test for: better-tailwindcss#320
|
|
381
|
+
*/
|
|
382
|
+
function roundRemValue(className) {
|
|
383
|
+
return className.replace(/\[(-?\d+\.\d{7,})(rem|em|px|%)\]/g, (_, num, unit) => {
|
|
384
|
+
return `[${Number.parseFloat(Number.parseFloat(num).toFixed(6))}${unit}]`;
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
//#endregion
|
|
388
|
+
//#region src/utils/class-parser.ts
|
|
389
|
+
/**
|
|
390
|
+
* Extracts variants from a class string, respecting bracket depth.
|
|
391
|
+
* e.g. "[&>svg]:hover:w-4" → ["[&>svg]", "hover"]
|
|
392
|
+
* "dark:hover:flex" → ["dark", "hover"]
|
|
393
|
+
* "w-4" → []
|
|
394
|
+
*/
|
|
395
|
+
function extractVariants(cls) {
|
|
396
|
+
const variants = [];
|
|
397
|
+
let depth = 0;
|
|
398
|
+
let segmentStart = 0;
|
|
399
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
400
|
+
else if (cls[i] === "]") depth--;
|
|
401
|
+
else if (cls[i] === ":" && depth === 0) {
|
|
402
|
+
variants.push(cls.slice(segmentStart, i));
|
|
403
|
+
segmentStart = i + 1;
|
|
404
|
+
}
|
|
405
|
+
if (variants.length === 0) return [];
|
|
406
|
+
return variants;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Extracts the utility part from a potentially variant-prefixed class.
|
|
410
|
+
* Bracket-aware: respects arbitrary variants like [&>svg]:w-4.
|
|
411
|
+
*
|
|
412
|
+
* e.g. "hover:bg-blue-500" → "bg-blue-500"
|
|
413
|
+
* "dark:hover:flex" → "flex"
|
|
414
|
+
* "[&>svg]:w-4" → "w-4"
|
|
415
|
+
* "w-4" → "w-4"
|
|
416
|
+
*/
|
|
417
|
+
function extractUtility(cls) {
|
|
418
|
+
let depth = 0;
|
|
419
|
+
let lastColon = -1;
|
|
420
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
421
|
+
else if (cls[i] === "]") depth--;
|
|
422
|
+
else if (cls[i] === ":" && depth === 0) lastColon = i;
|
|
423
|
+
return lastColon >= 0 ? cls.slice(lastColon + 1) : cls;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Gets the variant prefix (everything before the utility).
|
|
427
|
+
* e.g. "hover:sm:flex" → "hover:sm:"
|
|
428
|
+
* "flex" → ""
|
|
429
|
+
*/
|
|
430
|
+
function getVariantPrefix(cls) {
|
|
431
|
+
let depth = 0;
|
|
432
|
+
let lastColon = -1;
|
|
433
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
434
|
+
else if (cls[i] === "]") depth--;
|
|
435
|
+
else if (cls[i] === ":" && depth === 0) lastColon = i;
|
|
436
|
+
return lastColon >= 0 ? cls.slice(0, lastColon + 1) : "";
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Splits a class into variant prefix and utility in a single pass.
|
|
440
|
+
* More efficient than calling extractUtility() + getVariantPrefix() separately.
|
|
441
|
+
*/
|
|
442
|
+
function splitUtilityAndVariant(cls) {
|
|
443
|
+
let depth = 0;
|
|
444
|
+
let lastColon = -1;
|
|
445
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
446
|
+
else if (cls[i] === "]") depth--;
|
|
447
|
+
else if (cls[i] === ":" && depth === 0) lastColon = i;
|
|
448
|
+
return {
|
|
449
|
+
utility: lastColon >= 0 ? cls.slice(lastColon + 1) : cls,
|
|
450
|
+
variant: lastColon >= 0 ? cls.slice(0, lastColon + 1) : ""
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Checks if a class has an arbitrary value (bracket syntax in the utility part).
|
|
455
|
+
* Distinguishes from arbitrary variants: [&>svg]:w-4 does NOT have an arbitrary value,
|
|
456
|
+
* but w-[200px] does.
|
|
457
|
+
*/
|
|
458
|
+
function hasArbitraryValue(cls) {
|
|
459
|
+
const utility = extractUtility(cls);
|
|
460
|
+
return utility.includes("[") && utility.includes("]");
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Gets the arbitrary value from a class, or null if none.
|
|
464
|
+
* e.g. "w-[200px]" → "200px"
|
|
465
|
+
* "bg-[#ff0000]" → "#ff0000"
|
|
466
|
+
* "flex" → null
|
|
467
|
+
*/
|
|
468
|
+
function getArbitraryValue(cls) {
|
|
469
|
+
const utility = extractUtility(cls);
|
|
470
|
+
const openBracket = utility.indexOf("[");
|
|
471
|
+
if (openBracket === -1) return null;
|
|
472
|
+
let depth = 0;
|
|
473
|
+
for (let i = openBracket; i < utility.length; i++) if (utility[i] === "[") depth++;
|
|
474
|
+
else if (utility[i] === "]") {
|
|
475
|
+
depth--;
|
|
476
|
+
if (depth === 0) return utility.slice(openBracket + 1, i);
|
|
477
|
+
}
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/design-system/cache.ts
|
|
482
|
+
var DesignSystemCache = class DesignSystemCache {
|
|
483
|
+
canonicalMap = /* @__PURE__ */ new Map();
|
|
484
|
+
validitySet = /* @__PURE__ */ new Set();
|
|
485
|
+
orderMap = /* @__PURE__ */ new Map();
|
|
486
|
+
cssPropsMap = /* @__PURE__ */ new Map();
|
|
487
|
+
variantOrderMap = /* @__PURE__ */ new Map();
|
|
488
|
+
arbitraryEquivMap = /* @__PURE__ */ new Map();
|
|
489
|
+
_validClasses = [];
|
|
490
|
+
static fromPrecomputed(data) {
|
|
491
|
+
const cache = new DesignSystemCache();
|
|
492
|
+
cache._validClasses = data.validClasses;
|
|
493
|
+
for (const cls of data.validClasses) cache.validitySet.add(cls);
|
|
494
|
+
for (const [from, to] of Object.entries(data.canonical)) cache.canonicalMap.set(from, roundRemValue(to));
|
|
495
|
+
for (const [cls, val] of Object.entries(data.order)) cache.orderMap.set(cls, BigInt(val));
|
|
496
|
+
for (const [cls, props] of Object.entries(data.cssProps)) cache.cssPropsMap.set(cls, props);
|
|
497
|
+
if (data.variantOrder) for (const [name, index] of Object.entries(data.variantOrder)) cache.variantOrderMap.set(name, index);
|
|
498
|
+
if (data.componentClasses) for (const cls of data.componentClasses) cache.validitySet.add(cls);
|
|
499
|
+
if (data.arbitraryEquivalents) for (const [arb, named] of Object.entries(data.arbitraryEquivalents)) cache.arbitraryEquivMap.set(arb, named);
|
|
500
|
+
return cache;
|
|
501
|
+
}
|
|
502
|
+
get validClasses() {
|
|
503
|
+
return this._validClasses;
|
|
504
|
+
}
|
|
505
|
+
canonicalize(className) {
|
|
506
|
+
const cached = this.canonicalMap.get(className);
|
|
507
|
+
if (cached !== void 0) return cached;
|
|
508
|
+
const utility = extractUtility(className);
|
|
509
|
+
if (utility !== className) {
|
|
510
|
+
const canonicalUtility = this.canonicalMap.get(utility);
|
|
511
|
+
if (canonicalUtility !== void 0) return className.slice(0, className.length - utility.length) + canonicalUtility;
|
|
512
|
+
}
|
|
513
|
+
return className;
|
|
514
|
+
}
|
|
515
|
+
isValid(className) {
|
|
516
|
+
if (this.validitySet.has(className)) return true;
|
|
517
|
+
const utility = extractUtility(className);
|
|
518
|
+
if (utility !== className && this.validitySet.has(utility)) return true;
|
|
519
|
+
if (hasArbitraryValue(className)) return true;
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
getOrder(className) {
|
|
523
|
+
const cached = this.orderMap.get(className);
|
|
524
|
+
if (cached !== void 0) return cached;
|
|
525
|
+
const utility = extractUtility(className);
|
|
526
|
+
if (utility !== className) {
|
|
527
|
+
const baseOrder = this.orderMap.get(utility);
|
|
528
|
+
if (baseOrder !== void 0) return baseOrder;
|
|
529
|
+
}
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
getClassOrder(classes) {
|
|
533
|
+
return classes.map((cls) => [cls, this.getOrder(cls)]);
|
|
534
|
+
}
|
|
535
|
+
getCssProperties(className) {
|
|
536
|
+
return this.cssPropsMap.get(className) ?? [];
|
|
537
|
+
}
|
|
538
|
+
getVariantPriority(variant) {
|
|
539
|
+
return this.variantOrderMap.get(variant) ?? null;
|
|
540
|
+
}
|
|
541
|
+
hasVariantOrder() {
|
|
542
|
+
return this.variantOrderMap.size > 0;
|
|
543
|
+
}
|
|
544
|
+
getNamedEquivalent(className) {
|
|
545
|
+
return this.arbitraryEquivMap.get(className) ?? null;
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/design-system/sync-loader.ts
|
|
550
|
+
/**
|
|
551
|
+
* Synchronous design system loader using execFileSync.
|
|
552
|
+
*
|
|
553
|
+
* The problem: __unstable__loadDesignSystem is async, but oxlint's createOnce is sync.
|
|
554
|
+
* The solution: spawn a child process that loads the design system, pre-computes all
|
|
555
|
+
* data we need, and returns it as JSON via stdout. This runs ONCE at plugin init time.
|
|
556
|
+
*
|
|
557
|
+
* For arbitrary values (bg-[#123]) that aren't in the class list, we use heuristics.
|
|
558
|
+
*/
|
|
559
|
+
const PRECOMPUTE_SCRIPT = `
|
|
560
|
+
const { __unstable__loadDesignSystem } = require('@tailwindcss/node');
|
|
561
|
+
const { readFileSync } = require('fs');
|
|
562
|
+
const { dirname, resolve } = require('path');
|
|
563
|
+
|
|
564
|
+
function extractComponentClasses(cssPath, baseDir) {
|
|
565
|
+
let css;
|
|
566
|
+
try { css = readFileSync(cssPath, 'utf-8'); } catch { return []; }
|
|
567
|
+
const files = [css];
|
|
568
|
+
const importRe = /@import\\s+['"](\\.[\\/][^'"]+)['"]/g;
|
|
569
|
+
let m;
|
|
570
|
+
while ((m = importRe.exec(css)) !== null) {
|
|
571
|
+
try { files.push(readFileSync(resolve(baseDir, m[1]), 'utf-8')); } catch {}
|
|
572
|
+
}
|
|
573
|
+
const result = [];
|
|
574
|
+
for (const content of files) {
|
|
575
|
+
const layerRe = /@layer\\s+components\\s*\\{/g;
|
|
576
|
+
let lm;
|
|
577
|
+
while ((lm = layerRe.exec(content)) !== null) {
|
|
578
|
+
let depth = 1, i = lm.index + lm[0].length;
|
|
579
|
+
while (i < content.length && depth > 0) {
|
|
580
|
+
if (content[i] === '{') depth++;
|
|
581
|
+
if (content[i] === '}') depth--;
|
|
582
|
+
i++;
|
|
583
|
+
}
|
|
584
|
+
const block = content.slice(lm.index + lm[0].length, i - 1);
|
|
585
|
+
const selRe = /\\.([\\w-]+)/g;
|
|
586
|
+
let sm;
|
|
587
|
+
while ((sm = selRe.exec(block)) !== null) result.push(sm[1]);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return [...new Set(result)];
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
async function main() {
|
|
594
|
+
const cssPath = process.env.TAILWIND_CSS_PATH;
|
|
595
|
+
const css = readFileSync(cssPath, 'utf-8');
|
|
596
|
+
const base = dirname(cssPath);
|
|
597
|
+
const ds = await __unstable__loadDesignSystem(css, { base });
|
|
598
|
+
|
|
599
|
+
const entries = ds.getClassList();
|
|
600
|
+
const classNames = entries.map(e => e[0]);
|
|
601
|
+
|
|
602
|
+
// Validity: which classes produce CSS
|
|
603
|
+
const cssResults = ds.candidatesToCss(classNames);
|
|
604
|
+
const validClasses = classNames.filter((_, i) => cssResults[i] != null);
|
|
605
|
+
|
|
606
|
+
// Canonical forms (only store diffs)
|
|
607
|
+
// NOTE: canonicalizeCandidates deduplicates, so we must call it one class at a time
|
|
608
|
+
const canonical = {};
|
|
609
|
+
for (const cls of classNames) {
|
|
610
|
+
const result = ds.canonicalizeCandidates([cls]);
|
|
611
|
+
if (result[0] && result[0] !== cls) {
|
|
612
|
+
canonical[cls] = result[0];
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Sort order
|
|
617
|
+
const order = {};
|
|
618
|
+
const orderResults = ds.getClassOrder(classNames);
|
|
619
|
+
for (const [name, val] of orderResults) {
|
|
620
|
+
if (val !== null) order[name] = val.toString();
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// CSS properties per class
|
|
624
|
+
const cssProps = {};
|
|
625
|
+
const propRegex = /^\\s+([\\w-]+)\\s*:/gm;
|
|
626
|
+
for (let i = 0; i < classNames.length; i++) {
|
|
627
|
+
if (cssResults[i]) {
|
|
628
|
+
const props = [];
|
|
629
|
+
let match;
|
|
630
|
+
propRegex.lastIndex = 0;
|
|
631
|
+
while ((match = propRegex.exec(cssResults[i])) !== null) {
|
|
632
|
+
props.push(match[1]);
|
|
633
|
+
}
|
|
634
|
+
if (props.length > 0) cssProps[classNames[i]] = props;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// Variant ordering from the design system
|
|
639
|
+
const variantOrder = {};
|
|
640
|
+
const variants = ds.getVariants();
|
|
641
|
+
for (let i = 0; i < variants.length; i++) {
|
|
642
|
+
if (!variants[i].isArbitrary) {
|
|
643
|
+
variantOrder[variants[i].name] = i;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Component classes from @layer components
|
|
648
|
+
const componentClasses = extractComponentClasses(cssPath, base);
|
|
649
|
+
|
|
650
|
+
// Arbitrary equivalents: map arbitrary forms to named equivalents
|
|
651
|
+
const arbitraryEquivalents = {};
|
|
652
|
+
const candidates = [];
|
|
653
|
+
for (const cls of validClasses) {
|
|
654
|
+
if (cls.includes('[') || cls.includes('/')) continue;
|
|
655
|
+
const idx = classNames.indexOf(cls);
|
|
656
|
+
const cssText = cssResults[idx];
|
|
657
|
+
if (!cssText) continue;
|
|
658
|
+
const pvMatch = cssText.match(/^\\s+([\\w-]+)\\s*:\\s*(.+?)\\s*;?\\s*$/m);
|
|
659
|
+
if (!pvMatch) continue;
|
|
660
|
+
const value = pvMatch[2].trim().replace(/;$/, '');
|
|
661
|
+
const lastDash = cls.lastIndexOf('-');
|
|
662
|
+
if (lastDash <= 0) continue;
|
|
663
|
+
const prefix = cls.slice(0, lastDash);
|
|
664
|
+
candidates.push({ arbitraryForm: prefix + '-[' + value + ']', namedCls: cls, namedCss: cssText });
|
|
665
|
+
}
|
|
666
|
+
function extractDeclarations(css) {
|
|
667
|
+
const openBrace = css.indexOf('{');
|
|
668
|
+
const closeBrace = css.lastIndexOf('}');
|
|
669
|
+
if (openBrace === -1 || closeBrace === -1) return css;
|
|
670
|
+
return css.slice(openBrace + 1, closeBrace).replace(/\\s+/g, ' ').trim();
|
|
671
|
+
}
|
|
672
|
+
if (candidates.length > 0) {
|
|
673
|
+
const arbForms = candidates.map(c => c.arbitraryForm);
|
|
674
|
+
const arbResults = ds.candidatesToCss(arbForms);
|
|
675
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
676
|
+
if (!arbResults[i]) continue;
|
|
677
|
+
if (extractDeclarations(arbResults[i]) === extractDeclarations(candidates[i].namedCss)) {
|
|
678
|
+
arbitraryEquivalents[candidates[i].arbitraryForm] = candidates[i].namedCls;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
process.stdout.write(JSON.stringify({ validClasses, canonical, order, cssProps, variantOrder, componentClasses, arbitraryEquivalents }));
|
|
684
|
+
}
|
|
685
|
+
main().catch(e => { process.stderr.write(e.message); process.exit(1); });
|
|
686
|
+
`;
|
|
687
|
+
function loadDesignSystemSync(cssPath) {
|
|
688
|
+
const resolvedPath = resolve(cssPath);
|
|
689
|
+
try {
|
|
690
|
+
const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
691
|
+
encoding: "utf-8",
|
|
692
|
+
timeout: 3e4,
|
|
693
|
+
maxBuffer: 50 * 1024 * 1024,
|
|
694
|
+
env: {
|
|
695
|
+
...process.env,
|
|
696
|
+
TAILWIND_CSS_PATH: resolvedPath
|
|
697
|
+
},
|
|
698
|
+
cwd: dirname(resolvedPath)
|
|
699
|
+
});
|
|
700
|
+
return JSON.parse(stdout);
|
|
701
|
+
} catch (error) {
|
|
702
|
+
console.error(`[oxlint-plugin-tailwindcss] Failed to load design system from "${resolvedPath}":`, error instanceof Error ? error.message : error);
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
//#endregion
|
|
707
|
+
//#region src/design-system/auto-detect.ts
|
|
708
|
+
const CANDIDATE_PATHS = [
|
|
709
|
+
"src/app.css",
|
|
710
|
+
"src/globals.css",
|
|
711
|
+
"src/global.css",
|
|
712
|
+
"src/style.css",
|
|
713
|
+
"src/styles.css",
|
|
714
|
+
"src/index.css",
|
|
715
|
+
"app.css",
|
|
716
|
+
"main.css",
|
|
717
|
+
"globals.css",
|
|
718
|
+
"style.css",
|
|
719
|
+
"styles/globals.css",
|
|
720
|
+
"app/globals.css",
|
|
721
|
+
"assets/css/tailwind.css"
|
|
722
|
+
];
|
|
723
|
+
const TAILWIND_SIGNALS = [
|
|
724
|
+
"@import \"tailwindcss\"",
|
|
725
|
+
"@import 'tailwindcss'",
|
|
726
|
+
"@tailwind base",
|
|
727
|
+
"@import tailwindcss"
|
|
728
|
+
];
|
|
729
|
+
/**
|
|
730
|
+
* Searches for the Tailwind CSS entry point by walking UP from the file
|
|
731
|
+
* being linted. Monorepo-aware: stops at package.json boundaries.
|
|
732
|
+
*/
|
|
733
|
+
function autoDetectEntryPoint(filePath) {
|
|
734
|
+
let currentDir = filePath ? dirname(resolve(filePath)) : process.cwd();
|
|
735
|
+
for (let depth = 0; depth < 20; depth++) {
|
|
736
|
+
for (const candidate of CANDIDATE_PATHS) {
|
|
737
|
+
const fullPath = join(currentDir, candidate);
|
|
738
|
+
if (!existsSync(fullPath)) continue;
|
|
739
|
+
try {
|
|
740
|
+
const content = readFileSync(fullPath, "utf-8");
|
|
741
|
+
if (TAILWIND_SIGNALS.some((signal) => content.includes(signal))) return fullPath;
|
|
742
|
+
} catch {
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
const parentDir = dirname(currentDir);
|
|
747
|
+
if (parentDir === currentDir) break;
|
|
748
|
+
if (depth > 0 && existsSync(join(parentDir, "package.json"))) {
|
|
749
|
+
for (const candidate of CANDIDATE_PATHS) {
|
|
750
|
+
const fullPath = join(parentDir, candidate);
|
|
751
|
+
if (!existsSync(fullPath)) continue;
|
|
752
|
+
try {
|
|
753
|
+
const content = readFileSync(fullPath, "utf-8");
|
|
754
|
+
if (TAILWIND_SIGNALS.some((signal) => content.includes(signal))) return fullPath;
|
|
755
|
+
} catch {
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
break;
|
|
760
|
+
}
|
|
761
|
+
currentDir = parentDir;
|
|
762
|
+
}
|
|
763
|
+
return null;
|
|
764
|
+
}
|
|
765
|
+
//#endregion
|
|
766
|
+
//#region src/design-system/loader.ts
|
|
767
|
+
let singleton = null;
|
|
768
|
+
/**
|
|
769
|
+
* Returns the design system cache, loading synchronously on first call.
|
|
770
|
+
* Uses execFileSync internally to bridge the async Tailwind API.
|
|
771
|
+
* All rules receive the SAME instance.
|
|
772
|
+
*/
|
|
773
|
+
function getLoadedDesignSystem(entryPoint) {
|
|
774
|
+
const cssPath = entryPoint ?? singleton?.path ?? autoDetectEntryPoint();
|
|
775
|
+
if (!cssPath) return null;
|
|
776
|
+
const resolvedPath = resolve(cssPath);
|
|
777
|
+
try {
|
|
778
|
+
const mtime = statSync(resolvedPath).mtimeMs;
|
|
779
|
+
if (singleton !== null && singleton.path === resolvedPath && singleton.mtime === mtime) return {
|
|
780
|
+
cache: singleton.cache,
|
|
781
|
+
entryPoint: resolvedPath
|
|
782
|
+
};
|
|
783
|
+
const data = loadDesignSystemSync(resolvedPath);
|
|
784
|
+
if (!data) return null;
|
|
785
|
+
const cache = DesignSystemCache.fromPrecomputed(data);
|
|
786
|
+
singleton = {
|
|
787
|
+
cache,
|
|
788
|
+
path: resolvedPath,
|
|
789
|
+
mtime
|
|
790
|
+
};
|
|
791
|
+
return {
|
|
792
|
+
cache,
|
|
793
|
+
entryPoint: resolvedPath
|
|
794
|
+
};
|
|
795
|
+
} catch {
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
//#endregion
|
|
800
|
+
//#region src/types.ts
|
|
801
|
+
/**
|
|
802
|
+
* Safely read context.options.
|
|
803
|
+
*
|
|
804
|
+
* In oxlint, `context.options` is `null` inside `createOnce()` — options are
|
|
805
|
+
* only populated when visitors run. Call this lazily (inside `check()` or a
|
|
806
|
+
* visitor) to get the user's configured options.
|
|
807
|
+
*
|
|
808
|
+
* In oxlint <1.31.0 the getter may throw — the try/catch handles that.
|
|
809
|
+
*/
|
|
810
|
+
function safeOptions(context) {
|
|
811
|
+
try {
|
|
812
|
+
return context.options?.[0] ?? void 0;
|
|
813
|
+
} catch {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
//#endregion
|
|
818
|
+
//#region src/rules/no-unknown-classes.ts
|
|
819
|
+
const noUnknownClasses = defineRule({
|
|
820
|
+
meta: {
|
|
821
|
+
type: "problem",
|
|
822
|
+
docs: { description: "Disallow classes that are not defined in the Tailwind CSS design system" },
|
|
823
|
+
schema: [{
|
|
824
|
+
type: "object",
|
|
825
|
+
properties: {
|
|
826
|
+
entryPoint: { type: "string" },
|
|
827
|
+
allowlist: {
|
|
828
|
+
type: "array",
|
|
829
|
+
items: { type: "string" }
|
|
830
|
+
},
|
|
831
|
+
ignorePrefixes: {
|
|
832
|
+
type: "array",
|
|
833
|
+
items: { type: "string" }
|
|
834
|
+
}
|
|
835
|
+
},
|
|
836
|
+
additionalProperties: false
|
|
837
|
+
}],
|
|
838
|
+
messages: {
|
|
839
|
+
unknown: "\"{{className}}\" is not a valid Tailwind class.",
|
|
840
|
+
unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?"
|
|
841
|
+
}
|
|
842
|
+
},
|
|
843
|
+
createOnce(context) {
|
|
844
|
+
const options = safeOptions(context);
|
|
845
|
+
const result = getLoadedDesignSystem(options?.entryPoint);
|
|
846
|
+
if (!result) return {};
|
|
847
|
+
const { cache } = result;
|
|
848
|
+
const allowlist = new Set(options?.allowlist ?? []);
|
|
849
|
+
const ignorePrefixes = options?.ignorePrefixes ?? [];
|
|
850
|
+
function shouldIgnore(className) {
|
|
851
|
+
if (allowlist.has(className)) return true;
|
|
852
|
+
return ignorePrefixes.some((prefix) => className.startsWith(prefix));
|
|
853
|
+
}
|
|
854
|
+
function stripModifiers(className) {
|
|
855
|
+
let stripped = className;
|
|
856
|
+
if (stripped.startsWith("!")) stripped = stripped.slice(1);
|
|
857
|
+
return stripped;
|
|
858
|
+
}
|
|
859
|
+
function check(locations) {
|
|
860
|
+
for (const loc of locations) {
|
|
861
|
+
const classes = splitClasses(loc.value);
|
|
862
|
+
for (const cls of classes) {
|
|
863
|
+
if (shouldIgnore(cls)) continue;
|
|
864
|
+
const stripped = stripModifiers(cls);
|
|
865
|
+
if (cache.isValid(stripped)) continue;
|
|
866
|
+
const suggestion = findBestSuggestion(stripped, cache.validClasses);
|
|
867
|
+
if (suggestion) context.report({
|
|
868
|
+
node: loc.node,
|
|
869
|
+
messageId: "unknownWithSuggestion",
|
|
870
|
+
data: {
|
|
871
|
+
className: cls,
|
|
872
|
+
suggestion
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
else context.report({
|
|
876
|
+
node: loc.node,
|
|
877
|
+
messageId: "unknown",
|
|
878
|
+
data: { className: cls }
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return {
|
|
884
|
+
JSXAttribute(node) {
|
|
885
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
886
|
+
},
|
|
887
|
+
CallExpression(node) {
|
|
888
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
889
|
+
},
|
|
890
|
+
TaggedTemplateExpression(node) {
|
|
891
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
892
|
+
},
|
|
893
|
+
VariableDeclarator(node) {
|
|
894
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
//#endregion
|
|
900
|
+
//#region src/rules/no-conflicting-classes.ts
|
|
901
|
+
const noConflictingClasses = defineRule({
|
|
902
|
+
meta: {
|
|
903
|
+
type: "problem",
|
|
904
|
+
docs: { description: "Disallow Tailwind CSS classes that generate conflicting CSS properties" },
|
|
905
|
+
schema: [{
|
|
906
|
+
type: "object",
|
|
907
|
+
properties: { entryPoint: { type: "string" } },
|
|
908
|
+
additionalProperties: false
|
|
909
|
+
}],
|
|
910
|
+
messages: { conflict: "\"{{classA}}\" and \"{{classB}}\" affect {{properties}}. \"{{winner}}\" takes precedence (appears later)." }
|
|
911
|
+
},
|
|
912
|
+
createOnce(context) {
|
|
913
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
914
|
+
if (!result) return {};
|
|
915
|
+
const { cache } = result;
|
|
916
|
+
function check(locations) {
|
|
917
|
+
for (const loc of locations) {
|
|
918
|
+
const classes = splitClasses(loc.value);
|
|
919
|
+
if (classes.length < 2) continue;
|
|
920
|
+
const byVariant = /* @__PURE__ */ new Map();
|
|
921
|
+
for (const cls of classes) {
|
|
922
|
+
const variant = getVariantPrefix(cls);
|
|
923
|
+
const existing = byVariant.get(variant) ?? [];
|
|
924
|
+
existing.push(cls);
|
|
925
|
+
byVariant.set(variant, existing);
|
|
926
|
+
}
|
|
927
|
+
for (const [, variantClasses] of byVariant) {
|
|
928
|
+
if (variantClasses.length < 2) continue;
|
|
929
|
+
const propsMap = /* @__PURE__ */ new Map();
|
|
930
|
+
for (const cls of variantClasses) {
|
|
931
|
+
const utility = extractUtility(cls);
|
|
932
|
+
const props = cache.getCssProperties(utility);
|
|
933
|
+
propsMap.set(cls, props);
|
|
934
|
+
}
|
|
935
|
+
for (let i = 0; i < variantClasses.length; i++) {
|
|
936
|
+
const classA = variantClasses[i];
|
|
937
|
+
const propsA = propsMap.get(classA) ?? [];
|
|
938
|
+
for (let j = i + 1; j < variantClasses.length; j++) {
|
|
939
|
+
const classB = variantClasses[j];
|
|
940
|
+
const propsB = propsMap.get(classB) ?? [];
|
|
941
|
+
const overlap = propsA.filter((p) => propsB.includes(p));
|
|
942
|
+
if (overlap.length > 0) {
|
|
943
|
+
const propList = overlap.length <= 3 ? `"${overlap.join("\", \"")}"` : `${overlap.length} CSS properties`;
|
|
944
|
+
context.report({
|
|
945
|
+
node: loc.node,
|
|
946
|
+
messageId: "conflict",
|
|
947
|
+
data: {
|
|
948
|
+
classA,
|
|
949
|
+
classB,
|
|
950
|
+
properties: propList,
|
|
951
|
+
winner: classB
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
return {
|
|
961
|
+
JSXAttribute(node) {
|
|
962
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
963
|
+
},
|
|
964
|
+
CallExpression(node) {
|
|
965
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
966
|
+
},
|
|
967
|
+
TaggedTemplateExpression(node) {
|
|
968
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
969
|
+
},
|
|
970
|
+
VariableDeclarator(node) {
|
|
971
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
//#endregion
|
|
977
|
+
//#region src/rules/no-deprecated-classes.ts
|
|
978
|
+
const DEPRECATED_MAP = {
|
|
979
|
+
"flex-grow": "grow",
|
|
980
|
+
"flex-grow-0": "grow-0",
|
|
981
|
+
"flex-shrink": "shrink",
|
|
982
|
+
"flex-shrink-0": "shrink-0",
|
|
983
|
+
"overflow-ellipsis": "text-ellipsis",
|
|
984
|
+
"decoration-slice": "box-decoration-slice",
|
|
985
|
+
"decoration-clone": "box-decoration-clone"
|
|
986
|
+
};
|
|
987
|
+
const noDeprecatedClasses = defineRule({
|
|
988
|
+
meta: {
|
|
989
|
+
type: "problem",
|
|
990
|
+
docs: { description: "Disallow deprecated Tailwind CSS v4 classes" },
|
|
991
|
+
fixable: "code",
|
|
992
|
+
schema: [{
|
|
993
|
+
type: "object",
|
|
994
|
+
properties: { entryPoint: { type: "string" } },
|
|
995
|
+
additionalProperties: false
|
|
996
|
+
}],
|
|
997
|
+
messages: { deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead." }
|
|
998
|
+
},
|
|
999
|
+
createOnce(context) {
|
|
1000
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
1001
|
+
function check(locations) {
|
|
1002
|
+
for (const loc of locations) {
|
|
1003
|
+
const classes = splitClasses(loc.value);
|
|
1004
|
+
for (const cls of classes) {
|
|
1005
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1006
|
+
const replacement = DEPRECATED_MAP[utility];
|
|
1007
|
+
if (!replacement) continue;
|
|
1008
|
+
if (result) {
|
|
1009
|
+
const canonical = result.cache.canonicalize(utility);
|
|
1010
|
+
if (canonical !== utility && canonical === replacement) {}
|
|
1011
|
+
}
|
|
1012
|
+
const fullReplacement = variant + replacement;
|
|
1013
|
+
context.report({
|
|
1014
|
+
node: loc.node,
|
|
1015
|
+
messageId: "deprecated",
|
|
1016
|
+
data: {
|
|
1017
|
+
className: cls,
|
|
1018
|
+
replacement: fullReplacement
|
|
1019
|
+
},
|
|
1020
|
+
fix(fixer) {
|
|
1021
|
+
const fixed = loc.value.replace(cls, fullReplacement);
|
|
1022
|
+
return fixer.replaceTextRange(loc.range, fixed);
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
return {
|
|
1029
|
+
JSXAttribute(node) {
|
|
1030
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1031
|
+
},
|
|
1032
|
+
CallExpression(node) {
|
|
1033
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1034
|
+
},
|
|
1035
|
+
TaggedTemplateExpression(node) {
|
|
1036
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1037
|
+
},
|
|
1038
|
+
VariableDeclarator(node) {
|
|
1039
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
//#endregion
|
|
1045
|
+
//#region src/rules/enforce-canonical.ts
|
|
1046
|
+
const enforceCanonical = defineRule({
|
|
1047
|
+
meta: {
|
|
1048
|
+
type: "suggestion",
|
|
1049
|
+
docs: { description: "Enforce canonical Tailwind CSS class names using canonicalizeCandidates()" },
|
|
1050
|
+
fixable: "code",
|
|
1051
|
+
schema: [{
|
|
1052
|
+
type: "object",
|
|
1053
|
+
properties: { entryPoint: { type: "string" } },
|
|
1054
|
+
additionalProperties: false
|
|
1055
|
+
}],
|
|
1056
|
+
messages: { nonCanonical: "\"{{className}}\" can be written as \"{{canonical}}\". Use the canonical form." }
|
|
1057
|
+
},
|
|
1058
|
+
createOnce(context) {
|
|
1059
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
1060
|
+
if (!result) return {};
|
|
1061
|
+
const { cache } = result;
|
|
1062
|
+
function check(locations) {
|
|
1063
|
+
for (const loc of locations) {
|
|
1064
|
+
const classes = splitClasses(loc.value);
|
|
1065
|
+
const canonicals = classes.map((cls) => cache.canonicalize(cls));
|
|
1066
|
+
let firstNonCanonical = true;
|
|
1067
|
+
for (let i = 0; i < classes.length; i++) {
|
|
1068
|
+
const cls = classes[i];
|
|
1069
|
+
const canonical = canonicals[i];
|
|
1070
|
+
if (canonical === cls) continue;
|
|
1071
|
+
if (firstNonCanonical) {
|
|
1072
|
+
firstNonCanonical = false;
|
|
1073
|
+
const fixedValue = canonicals.join(" ");
|
|
1074
|
+
context.report({
|
|
1075
|
+
node: loc.node,
|
|
1076
|
+
messageId: "nonCanonical",
|
|
1077
|
+
data: {
|
|
1078
|
+
className: cls,
|
|
1079
|
+
canonical
|
|
1080
|
+
},
|
|
1081
|
+
fix(fixer) {
|
|
1082
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1083
|
+
}
|
|
1084
|
+
});
|
|
1085
|
+
} else context.report({
|
|
1086
|
+
node: loc.node,
|
|
1087
|
+
messageId: "nonCanonical",
|
|
1088
|
+
data: {
|
|
1089
|
+
className: cls,
|
|
1090
|
+
canonical
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
return {
|
|
1097
|
+
JSXAttribute(node) {
|
|
1098
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1099
|
+
},
|
|
1100
|
+
CallExpression(node) {
|
|
1101
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1102
|
+
},
|
|
1103
|
+
TaggedTemplateExpression(node) {
|
|
1104
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1105
|
+
},
|
|
1106
|
+
VariableDeclarator(node) {
|
|
1107
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1108
|
+
}
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
//#endregion
|
|
1113
|
+
//#region src/rules/enforce-sort-order.ts
|
|
1114
|
+
const enforceSortOrder = defineRule({
|
|
1115
|
+
meta: {
|
|
1116
|
+
type: "suggestion",
|
|
1117
|
+
docs: { description: "Enforce consistent sort order of Tailwind CSS classes using the official class order" },
|
|
1118
|
+
fixable: "code",
|
|
1119
|
+
schema: [{
|
|
1120
|
+
type: "object",
|
|
1121
|
+
properties: {
|
|
1122
|
+
entryPoint: { type: "string" },
|
|
1123
|
+
mode: {
|
|
1124
|
+
type: "string",
|
|
1125
|
+
enum: ["default", "strict"]
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
additionalProperties: false
|
|
1129
|
+
}],
|
|
1130
|
+
messages: { unsorted: "Tailwind classes are not in the recommended order." }
|
|
1131
|
+
},
|
|
1132
|
+
createOnce(context) {
|
|
1133
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
1134
|
+
if (!result) return {};
|
|
1135
|
+
const { cache } = result;
|
|
1136
|
+
let _mode = null;
|
|
1137
|
+
function getMode() {
|
|
1138
|
+
if (_mode === null) _mode = safeOptions(context)?.mode ?? "default";
|
|
1139
|
+
return _mode;
|
|
1140
|
+
}
|
|
1141
|
+
function sortDefault(classes) {
|
|
1142
|
+
return [...cache.getClassOrder(classes)].sort((a, b) => {
|
|
1143
|
+
const orderA = a[1] ?? 0n;
|
|
1144
|
+
const orderB = b[1] ?? 0n;
|
|
1145
|
+
if (orderA < orderB) return -1;
|
|
1146
|
+
if (orderA > orderB) return 1;
|
|
1147
|
+
return 0;
|
|
1148
|
+
}).map(([name]) => name);
|
|
1149
|
+
}
|
|
1150
|
+
function sortStrict(classes) {
|
|
1151
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1152
|
+
const groupOrder = [];
|
|
1153
|
+
for (const cls of classes) {
|
|
1154
|
+
const { variant } = splitUtilityAndVariant(cls);
|
|
1155
|
+
if (!groups.has(variant)) {
|
|
1156
|
+
groups.set(variant, []);
|
|
1157
|
+
groupOrder.push(variant);
|
|
1158
|
+
}
|
|
1159
|
+
groups.get(variant).push(cls);
|
|
1160
|
+
}
|
|
1161
|
+
for (const [, groupClasses] of groups) {
|
|
1162
|
+
const ordered = cache.getClassOrder(groupClasses);
|
|
1163
|
+
ordered.sort((a, b) => {
|
|
1164
|
+
const orderA = a[1] ?? 0n;
|
|
1165
|
+
const orderB = b[1] ?? 0n;
|
|
1166
|
+
if (orderA < orderB) return -1;
|
|
1167
|
+
if (orderA > orderB) return 1;
|
|
1168
|
+
return 0;
|
|
1169
|
+
});
|
|
1170
|
+
groupClasses.length = 0;
|
|
1171
|
+
for (const [name] of ordered) groupClasses.push(name);
|
|
1172
|
+
}
|
|
1173
|
+
const sortedGroupKeys = [...groups.keys()].sort((a, b) => {
|
|
1174
|
+
if (a === "" && b !== "") return -1;
|
|
1175
|
+
if (a !== "" && b === "") return 1;
|
|
1176
|
+
if (a === "" && b === "") return 0;
|
|
1177
|
+
const variantA = a.slice(0, -1);
|
|
1178
|
+
const variantB = b.slice(0, -1);
|
|
1179
|
+
return (cache.getVariantPriority(variantA) ?? Number.MAX_SAFE_INTEGER) - (cache.getVariantPriority(variantB) ?? Number.MAX_SAFE_INTEGER);
|
|
1180
|
+
});
|
|
1181
|
+
const result = [];
|
|
1182
|
+
for (const key of sortedGroupKeys) result.push(...groups.get(key));
|
|
1183
|
+
return result;
|
|
1184
|
+
}
|
|
1185
|
+
function check(locations) {
|
|
1186
|
+
const mode = getMode();
|
|
1187
|
+
for (const loc of locations) {
|
|
1188
|
+
const classes = splitClasses(loc.value);
|
|
1189
|
+
if (classes.length < 2) continue;
|
|
1190
|
+
const sortedNames = mode === "strict" ? sortStrict(classes) : sortDefault(classes);
|
|
1191
|
+
if (classes.every((name, i) => name === sortedNames[i])) continue;
|
|
1192
|
+
context.report({
|
|
1193
|
+
node: loc.node,
|
|
1194
|
+
messageId: "unsorted",
|
|
1195
|
+
fix(fixer) {
|
|
1196
|
+
return fixer.replaceTextRange(loc.range, sortedNames.join(" "));
|
|
1197
|
+
}
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
return {
|
|
1202
|
+
JSXAttribute(node) {
|
|
1203
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1204
|
+
},
|
|
1205
|
+
CallExpression(node) {
|
|
1206
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1207
|
+
},
|
|
1208
|
+
TaggedTemplateExpression(node) {
|
|
1209
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1210
|
+
},
|
|
1211
|
+
VariableDeclarator(node) {
|
|
1212
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
});
|
|
1217
|
+
//#endregion
|
|
1218
|
+
//#region src/rules/enforce-shorthand.ts
|
|
1219
|
+
const VALUE_RE = /^(?:m[trbl]|p[trbl]|[wh]|rounded-t[lr]|rounded-b[lr]|min-[wh]|max-[wh])-(.+)$/;
|
|
1220
|
+
/**
|
|
1221
|
+
* Generates shorthand rules parameterized by value.
|
|
1222
|
+
* E.g.: mt-2 + mr-2 + mb-2 + ml-2 -> m-2
|
|
1223
|
+
*/
|
|
1224
|
+
function createShorthandRules(value) {
|
|
1225
|
+
return [
|
|
1226
|
+
{
|
|
1227
|
+
parts: [
|
|
1228
|
+
`mt-${value}`,
|
|
1229
|
+
`mr-${value}`,
|
|
1230
|
+
`mb-${value}`,
|
|
1231
|
+
`ml-${value}`
|
|
1232
|
+
],
|
|
1233
|
+
replacement: `m-${value}`
|
|
1234
|
+
},
|
|
1235
|
+
{
|
|
1236
|
+
parts: [`mt-${value}`, `mb-${value}`],
|
|
1237
|
+
replacement: `my-${value}`
|
|
1238
|
+
},
|
|
1239
|
+
{
|
|
1240
|
+
parts: [`ml-${value}`, `mr-${value}`],
|
|
1241
|
+
replacement: `mx-${value}`
|
|
1242
|
+
},
|
|
1243
|
+
{
|
|
1244
|
+
parts: [
|
|
1245
|
+
`pt-${value}`,
|
|
1246
|
+
`pr-${value}`,
|
|
1247
|
+
`pb-${value}`,
|
|
1248
|
+
`pl-${value}`
|
|
1249
|
+
],
|
|
1250
|
+
replacement: `p-${value}`
|
|
1251
|
+
},
|
|
1252
|
+
{
|
|
1253
|
+
parts: [`pt-${value}`, `pb-${value}`],
|
|
1254
|
+
replacement: `py-${value}`
|
|
1255
|
+
},
|
|
1256
|
+
{
|
|
1257
|
+
parts: [`pl-${value}`, `pr-${value}`],
|
|
1258
|
+
replacement: `px-${value}`
|
|
1259
|
+
},
|
|
1260
|
+
{
|
|
1261
|
+
parts: [`w-${value}`, `h-${value}`],
|
|
1262
|
+
replacement: `size-${value}`
|
|
1263
|
+
},
|
|
1264
|
+
{
|
|
1265
|
+
parts: [
|
|
1266
|
+
`rounded-tl-${value}`,
|
|
1267
|
+
`rounded-tr-${value}`,
|
|
1268
|
+
`rounded-br-${value}`,
|
|
1269
|
+
`rounded-bl-${value}`
|
|
1270
|
+
],
|
|
1271
|
+
replacement: `rounded-${value}`
|
|
1272
|
+
}
|
|
1273
|
+
];
|
|
1274
|
+
}
|
|
1275
|
+
const enforceShorthand = defineRule({
|
|
1276
|
+
meta: {
|
|
1277
|
+
type: "suggestion",
|
|
1278
|
+
docs: { description: "Enforce shorthand Tailwind CSS classes when all axes have the same value" },
|
|
1279
|
+
fixable: "code",
|
|
1280
|
+
schema: [],
|
|
1281
|
+
messages: { shorthand: "{{parts}} can be simplified to \"{{replacement}}\"." }
|
|
1282
|
+
},
|
|
1283
|
+
createOnce(context) {
|
|
1284
|
+
function check(locations) {
|
|
1285
|
+
for (const loc of locations) {
|
|
1286
|
+
const classes = splitClasses(loc.value);
|
|
1287
|
+
if (classes.length < 2) continue;
|
|
1288
|
+
const classSet = new Set(classes);
|
|
1289
|
+
const values = /* @__PURE__ */ new Set();
|
|
1290
|
+
for (const cls of classes) {
|
|
1291
|
+
const match = VALUE_RE.exec(cls);
|
|
1292
|
+
if (match) values.add(match[1]);
|
|
1293
|
+
}
|
|
1294
|
+
for (const value of values) {
|
|
1295
|
+
const rules = createShorthandRules(value);
|
|
1296
|
+
for (const rule of rules) if (rule.parts.every((p) => classSet.has(p))) {
|
|
1297
|
+
const remaining = classes.filter((c) => !rule.parts.includes(c));
|
|
1298
|
+
remaining.push(rule.replacement);
|
|
1299
|
+
context.report({
|
|
1300
|
+
node: loc.node,
|
|
1301
|
+
messageId: "shorthand",
|
|
1302
|
+
data: {
|
|
1303
|
+
parts: rule.parts.map((p) => `"${p}"`).join(", "),
|
|
1304
|
+
replacement: rule.replacement
|
|
1305
|
+
},
|
|
1306
|
+
fix(fixer) {
|
|
1307
|
+
return fixer.replaceTextRange(loc.range, remaining.join(" "));
|
|
1308
|
+
}
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
return {
|
|
1315
|
+
JSXAttribute(node) {
|
|
1316
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1317
|
+
},
|
|
1318
|
+
CallExpression(node) {
|
|
1319
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1320
|
+
},
|
|
1321
|
+
TaggedTemplateExpression(node) {
|
|
1322
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1323
|
+
},
|
|
1324
|
+
VariableDeclarator(node) {
|
|
1325
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
});
|
|
1330
|
+
//#endregion
|
|
1331
|
+
//#region src/rules/enforce-logical.ts
|
|
1332
|
+
const PHYSICAL_TO_LOGICAL = {
|
|
1333
|
+
ml: "ms",
|
|
1334
|
+
mr: "me",
|
|
1335
|
+
pl: "ps",
|
|
1336
|
+
pr: "pe",
|
|
1337
|
+
left: "start",
|
|
1338
|
+
right: "end",
|
|
1339
|
+
"border-l": "border-s",
|
|
1340
|
+
"border-r": "border-e",
|
|
1341
|
+
"rounded-l": "rounded-s",
|
|
1342
|
+
"rounded-r": "rounded-e",
|
|
1343
|
+
"rounded-tl": "rounded-ss",
|
|
1344
|
+
"rounded-tr": "rounded-se",
|
|
1345
|
+
"rounded-bl": "rounded-es",
|
|
1346
|
+
"rounded-br": "rounded-ee",
|
|
1347
|
+
"scroll-ml": "scroll-ms",
|
|
1348
|
+
"scroll-mr": "scroll-me",
|
|
1349
|
+
"scroll-pl": "scroll-ps",
|
|
1350
|
+
"scroll-pr": "scroll-pe"
|
|
1351
|
+
};
|
|
1352
|
+
const enforceLogical = defineRule({
|
|
1353
|
+
meta: {
|
|
1354
|
+
type: "suggestion",
|
|
1355
|
+
docs: { description: "Enforce logical (RTL-friendly) Tailwind CSS properties instead of physical ones" },
|
|
1356
|
+
fixable: "code",
|
|
1357
|
+
schema: [],
|
|
1358
|
+
messages: { useLogical: "\"{{className}}\" uses a physical property. Use \"{{replacement}}\" for LTR/RTL support." }
|
|
1359
|
+
},
|
|
1360
|
+
createOnce(context) {
|
|
1361
|
+
function convertClass(cls) {
|
|
1362
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1363
|
+
for (const [physical, logical] of Object.entries(PHYSICAL_TO_LOGICAL)) if (utility === physical || utility.startsWith(`${physical}-`)) return `${variant}${logical}${utility.slice(physical.length)}`;
|
|
1364
|
+
return null;
|
|
1365
|
+
}
|
|
1366
|
+
function check(locations) {
|
|
1367
|
+
for (const loc of locations) {
|
|
1368
|
+
const classes = splitClasses(loc.value);
|
|
1369
|
+
const offending = [];
|
|
1370
|
+
for (const cls of classes) {
|
|
1371
|
+
const converted = convertClass(cls);
|
|
1372
|
+
if (converted) offending.push({
|
|
1373
|
+
cls,
|
|
1374
|
+
replacement: converted
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
if (offending.length === 0) continue;
|
|
1378
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1379
|
+
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
|
|
1380
|
+
for (let i = 0; i < offending.length; i++) {
|
|
1381
|
+
const { cls, replacement } = offending[i];
|
|
1382
|
+
if (i === 0) context.report({
|
|
1383
|
+
node: loc.node,
|
|
1384
|
+
messageId: "useLogical",
|
|
1385
|
+
data: {
|
|
1386
|
+
className: cls,
|
|
1387
|
+
replacement
|
|
1388
|
+
},
|
|
1389
|
+
fix(fixer) {
|
|
1390
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1391
|
+
}
|
|
1392
|
+
});
|
|
1393
|
+
else context.report({
|
|
1394
|
+
node: loc.node,
|
|
1395
|
+
messageId: "useLogical",
|
|
1396
|
+
data: {
|
|
1397
|
+
className: cls,
|
|
1398
|
+
replacement
|
|
1399
|
+
}
|
|
1400
|
+
});
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
return {
|
|
1405
|
+
JSXAttribute(node) {
|
|
1406
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1407
|
+
},
|
|
1408
|
+
CallExpression(node) {
|
|
1409
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1410
|
+
},
|
|
1411
|
+
TaggedTemplateExpression(node) {
|
|
1412
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1413
|
+
},
|
|
1414
|
+
VariableDeclarator(node) {
|
|
1415
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1416
|
+
}
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1419
|
+
});
|
|
1420
|
+
//#endregion
|
|
1421
|
+
//#region src/rules/max-class-count.ts
|
|
1422
|
+
const DEFAULT_MAX = 20;
|
|
1423
|
+
const maxClassCount = defineRule({
|
|
1424
|
+
meta: {
|
|
1425
|
+
type: "suggestion",
|
|
1426
|
+
docs: { description: "Enforce a maximum number of Tailwind CSS classes per element" },
|
|
1427
|
+
schema: [{
|
|
1428
|
+
type: "object",
|
|
1429
|
+
properties: { max: { type: "number" } },
|
|
1430
|
+
additionalProperties: false
|
|
1431
|
+
}],
|
|
1432
|
+
messages: { tooMany: "Too many Tailwind classes ({{count}}). Maximum allowed is {{max}}. Consider extracting into a component or utility." }
|
|
1433
|
+
},
|
|
1434
|
+
createOnce(context) {
|
|
1435
|
+
let _max = null;
|
|
1436
|
+
function getMax() {
|
|
1437
|
+
if (_max === null) _max = safeOptions(context)?.max ?? DEFAULT_MAX;
|
|
1438
|
+
return _max;
|
|
1439
|
+
}
|
|
1440
|
+
function check(locations) {
|
|
1441
|
+
const max = getMax();
|
|
1442
|
+
for (const loc of locations) {
|
|
1443
|
+
const classes = splitClasses(loc.value);
|
|
1444
|
+
if (classes.length > max) context.report({
|
|
1445
|
+
node: loc.node,
|
|
1446
|
+
messageId: "tooMany",
|
|
1447
|
+
data: {
|
|
1448
|
+
count: String(classes.length),
|
|
1449
|
+
max: String(max)
|
|
1450
|
+
}
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
return {
|
|
1455
|
+
JSXAttribute(node) {
|
|
1456
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1457
|
+
},
|
|
1458
|
+
CallExpression(node) {
|
|
1459
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1460
|
+
},
|
|
1461
|
+
TaggedTemplateExpression(node) {
|
|
1462
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1463
|
+
},
|
|
1464
|
+
VariableDeclarator(node) {
|
|
1465
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1466
|
+
}
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
});
|
|
1470
|
+
//#endregion
|
|
1471
|
+
//#region src/rules/no-restricted-classes.ts
|
|
1472
|
+
const noRestrictedClasses = defineRule({
|
|
1473
|
+
meta: {
|
|
1474
|
+
type: "problem",
|
|
1475
|
+
docs: { description: "Disallow specific Tailwind CSS classes" },
|
|
1476
|
+
schema: [{
|
|
1477
|
+
type: "object",
|
|
1478
|
+
properties: {
|
|
1479
|
+
classes: {
|
|
1480
|
+
type: "array",
|
|
1481
|
+
items: { type: "string" }
|
|
1482
|
+
},
|
|
1483
|
+
patterns: {
|
|
1484
|
+
type: "array",
|
|
1485
|
+
items: {
|
|
1486
|
+
type: "object",
|
|
1487
|
+
properties: {
|
|
1488
|
+
pattern: { type: "string" },
|
|
1489
|
+
message: { type: "string" }
|
|
1490
|
+
},
|
|
1491
|
+
required: ["pattern"],
|
|
1492
|
+
additionalProperties: false
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
},
|
|
1496
|
+
additionalProperties: false
|
|
1497
|
+
}],
|
|
1498
|
+
messages: {
|
|
1499
|
+
restricted: "\"{{className}}\" is restricted.",
|
|
1500
|
+
restrictedWithMessage: "\"{{className}}\" is restricted: {{message}}"
|
|
1501
|
+
}
|
|
1502
|
+
},
|
|
1503
|
+
createOnce(context) {
|
|
1504
|
+
let _config = null;
|
|
1505
|
+
function getConfig() {
|
|
1506
|
+
if (_config === null) {
|
|
1507
|
+
const options = safeOptions(context);
|
|
1508
|
+
_config = {
|
|
1509
|
+
restrictedClasses: new Set(options?.classes ?? []),
|
|
1510
|
+
patterns: (options?.patterns ?? []).map((p) => ({
|
|
1511
|
+
regex: new RegExp(p.pattern),
|
|
1512
|
+
message: p.message
|
|
1513
|
+
}))
|
|
1514
|
+
};
|
|
1515
|
+
}
|
|
1516
|
+
return _config;
|
|
1517
|
+
}
|
|
1518
|
+
function check(locations) {
|
|
1519
|
+
const { restrictedClasses, patterns } = getConfig();
|
|
1520
|
+
if (restrictedClasses.size === 0 && patterns.length === 0) return;
|
|
1521
|
+
for (const loc of locations) {
|
|
1522
|
+
const classes = splitClasses(loc.value);
|
|
1523
|
+
for (const cls of classes) {
|
|
1524
|
+
if (restrictedClasses.has(cls)) {
|
|
1525
|
+
context.report({
|
|
1526
|
+
node: loc.node,
|
|
1527
|
+
messageId: "restricted",
|
|
1528
|
+
data: { className: cls }
|
|
1529
|
+
});
|
|
1530
|
+
continue;
|
|
1531
|
+
}
|
|
1532
|
+
for (const { regex, message } of patterns) if (regex.test(cls)) {
|
|
1533
|
+
if (message) context.report({
|
|
1534
|
+
node: loc.node,
|
|
1535
|
+
messageId: "restrictedWithMessage",
|
|
1536
|
+
data: {
|
|
1537
|
+
className: cls,
|
|
1538
|
+
message
|
|
1539
|
+
}
|
|
1540
|
+
});
|
|
1541
|
+
else context.report({
|
|
1542
|
+
node: loc.node,
|
|
1543
|
+
messageId: "restricted",
|
|
1544
|
+
data: { className: cls }
|
|
1545
|
+
});
|
|
1546
|
+
break;
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
return {
|
|
1552
|
+
JSXAttribute(node) {
|
|
1553
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1554
|
+
},
|
|
1555
|
+
CallExpression(node) {
|
|
1556
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1557
|
+
},
|
|
1558
|
+
TaggedTemplateExpression(node) {
|
|
1559
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1560
|
+
},
|
|
1561
|
+
VariableDeclarator(node) {
|
|
1562
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1563
|
+
}
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
});
|
|
1567
|
+
//#endregion
|
|
1568
|
+
//#region src/rules/no-arbitrary-value.ts
|
|
1569
|
+
const noArbitraryValue = defineRule({
|
|
1570
|
+
meta: {
|
|
1571
|
+
type: "suggestion",
|
|
1572
|
+
docs: { description: "Disallow arbitrary values in Tailwind CSS classes" },
|
|
1573
|
+
schema: [{
|
|
1574
|
+
type: "object",
|
|
1575
|
+
properties: { allow: {
|
|
1576
|
+
type: "array",
|
|
1577
|
+
items: { type: "string" }
|
|
1578
|
+
} },
|
|
1579
|
+
additionalProperties: false
|
|
1580
|
+
}],
|
|
1581
|
+
messages: { noArbitrary: "\"{{className}}\" uses an arbitrary value. Use a design token or extend your theme instead." }
|
|
1582
|
+
},
|
|
1583
|
+
createOnce(context) {
|
|
1584
|
+
let _allowPrefixes = null;
|
|
1585
|
+
function getAllowPrefixes() {
|
|
1586
|
+
if (_allowPrefixes === null) _allowPrefixes = safeOptions(context)?.allow ?? [];
|
|
1587
|
+
return _allowPrefixes;
|
|
1588
|
+
}
|
|
1589
|
+
function isAllowed(utility) {
|
|
1590
|
+
return getAllowPrefixes().some((prefix) => utility.startsWith(prefix));
|
|
1591
|
+
}
|
|
1592
|
+
function check(locations) {
|
|
1593
|
+
for (const loc of locations) {
|
|
1594
|
+
const classes = splitClasses(loc.value);
|
|
1595
|
+
for (const cls of classes) {
|
|
1596
|
+
if (!hasArbitraryValue(cls)) continue;
|
|
1597
|
+
if (isAllowed(extractUtility(cls))) continue;
|
|
1598
|
+
context.report({
|
|
1599
|
+
node: loc.node,
|
|
1600
|
+
messageId: "noArbitrary",
|
|
1601
|
+
data: { className: cls }
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
return {
|
|
1607
|
+
JSXAttribute(node) {
|
|
1608
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1609
|
+
},
|
|
1610
|
+
CallExpression(node) {
|
|
1611
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1612
|
+
},
|
|
1613
|
+
TaggedTemplateExpression(node) {
|
|
1614
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1615
|
+
},
|
|
1616
|
+
VariableDeclarator(node) {
|
|
1617
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1618
|
+
}
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
});
|
|
1622
|
+
//#endregion
|
|
1623
|
+
//#region src/rules/enforce-consistent-important-position.ts
|
|
1624
|
+
const enforceConsistentImportantPosition = defineRule({
|
|
1625
|
+
meta: {
|
|
1626
|
+
type: "suggestion",
|
|
1627
|
+
docs: { description: "Enforce consistent position of the important (!) modifier in Tailwind CSS classes" },
|
|
1628
|
+
fixable: "code",
|
|
1629
|
+
schema: [{
|
|
1630
|
+
type: "object",
|
|
1631
|
+
properties: { position: {
|
|
1632
|
+
type: "string",
|
|
1633
|
+
enum: ["prefix", "suffix"]
|
|
1634
|
+
} },
|
|
1635
|
+
additionalProperties: false
|
|
1636
|
+
}],
|
|
1637
|
+
messages: {
|
|
1638
|
+
usePrefix: "\"{{className}}\" uses suffix important. Use \"{{replacement}}\" (prefix) instead.",
|
|
1639
|
+
useSuffix: "\"{{className}}\" uses prefix important. Use \"{{replacement}}\" (suffix) instead."
|
|
1640
|
+
}
|
|
1641
|
+
},
|
|
1642
|
+
createOnce(context) {
|
|
1643
|
+
let _position = null;
|
|
1644
|
+
function getPosition() {
|
|
1645
|
+
if (_position === null) _position = safeOptions(context)?.position ?? "prefix";
|
|
1646
|
+
return _position;
|
|
1647
|
+
}
|
|
1648
|
+
function fixUtility(utility, variantPfx) {
|
|
1649
|
+
const position = getPosition();
|
|
1650
|
+
if (position === "prefix" && utility.endsWith("!") && !utility.startsWith("!")) return `${variantPfx}!${utility.slice(0, -1)}`;
|
|
1651
|
+
if (position === "suffix" && utility.startsWith("!") && !utility.endsWith("!")) return `${variantPfx}${utility.slice(1)}!`;
|
|
1652
|
+
return null;
|
|
1653
|
+
}
|
|
1654
|
+
function check(locations) {
|
|
1655
|
+
const position = getPosition();
|
|
1656
|
+
for (const loc of locations) {
|
|
1657
|
+
const classes = splitClasses(loc.value);
|
|
1658
|
+
const offending = [];
|
|
1659
|
+
for (const cls of classes) {
|
|
1660
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1661
|
+
const fixed = fixUtility(utility, variant);
|
|
1662
|
+
if (fixed) offending.push({
|
|
1663
|
+
cls,
|
|
1664
|
+
replacement: fixed
|
|
1665
|
+
});
|
|
1666
|
+
}
|
|
1667
|
+
if (offending.length === 0) continue;
|
|
1668
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1669
|
+
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
|
|
1670
|
+
for (let i = 0; i < offending.length; i++) {
|
|
1671
|
+
const { cls, replacement } = offending[i];
|
|
1672
|
+
if (i === 0) context.report({
|
|
1673
|
+
node: loc.node,
|
|
1674
|
+
messageId: position === "prefix" ? "usePrefix" : "useSuffix",
|
|
1675
|
+
data: {
|
|
1676
|
+
className: cls,
|
|
1677
|
+
replacement
|
|
1678
|
+
},
|
|
1679
|
+
fix(fixer) {
|
|
1680
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1681
|
+
}
|
|
1682
|
+
});
|
|
1683
|
+
else context.report({
|
|
1684
|
+
node: loc.node,
|
|
1685
|
+
messageId: position === "prefix" ? "usePrefix" : "useSuffix",
|
|
1686
|
+
data: {
|
|
1687
|
+
className: cls,
|
|
1688
|
+
replacement
|
|
1689
|
+
}
|
|
1690
|
+
});
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
return {
|
|
1695
|
+
JSXAttribute(node) {
|
|
1696
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1697
|
+
},
|
|
1698
|
+
CallExpression(node) {
|
|
1699
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1700
|
+
},
|
|
1701
|
+
TaggedTemplateExpression(node) {
|
|
1702
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1703
|
+
},
|
|
1704
|
+
VariableDeclarator(node) {
|
|
1705
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1706
|
+
}
|
|
1707
|
+
};
|
|
1708
|
+
}
|
|
1709
|
+
});
|
|
1710
|
+
//#endregion
|
|
1711
|
+
//#region src/rules/enforce-negative-arbitrary-values.ts
|
|
1712
|
+
function fixClass(cls) {
|
|
1713
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1714
|
+
if (!utility.startsWith("-")) return null;
|
|
1715
|
+
const bracketOpen = utility.indexOf("[");
|
|
1716
|
+
if (bracketOpen === -1) return null;
|
|
1717
|
+
const bracketClose = utility.lastIndexOf("]");
|
|
1718
|
+
if (bracketClose === -1 || bracketClose < bracketOpen) return null;
|
|
1719
|
+
const innerValue = utility.slice(bracketOpen + 1, bracketClose);
|
|
1720
|
+
if (innerValue.startsWith("-")) return null;
|
|
1721
|
+
return `${variant}${utility.slice(1, bracketOpen)}[-${innerValue}]`;
|
|
1722
|
+
}
|
|
1723
|
+
/**
|
|
1724
|
+
* Detects `-utility-[value]` and suggests `utility-[-value]`.
|
|
1725
|
+
* e.g. `-top-[5px]` → `top-[-5px]`
|
|
1726
|
+
*/
|
|
1727
|
+
const enforceNegativeArbitraryValues = defineRule({
|
|
1728
|
+
meta: {
|
|
1729
|
+
type: "suggestion",
|
|
1730
|
+
docs: { description: "Enforce moving the negative sign inside arbitrary value brackets: -top-[5px] → top-[-5px]" },
|
|
1731
|
+
fixable: "code",
|
|
1732
|
+
schema: [],
|
|
1733
|
+
messages: { moveNegative: "\"{{className}}\" has the negative outside brackets. Use \"{{replacement}}\" instead." }
|
|
1734
|
+
},
|
|
1735
|
+
createOnce(context) {
|
|
1736
|
+
function check(locations) {
|
|
1737
|
+
for (const loc of locations) {
|
|
1738
|
+
const classes = splitClasses(loc.value);
|
|
1739
|
+
const offending = [];
|
|
1740
|
+
for (const cls of classes) {
|
|
1741
|
+
const fixed = fixClass(cls);
|
|
1742
|
+
if (fixed) offending.push({
|
|
1743
|
+
cls,
|
|
1744
|
+
replacement: fixed
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1747
|
+
if (offending.length === 0) continue;
|
|
1748
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1749
|
+
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
|
|
1750
|
+
for (let i = 0; i < offending.length; i++) {
|
|
1751
|
+
const { cls, replacement } = offending[i];
|
|
1752
|
+
if (i === 0) context.report({
|
|
1753
|
+
node: loc.node,
|
|
1754
|
+
messageId: "moveNegative",
|
|
1755
|
+
data: {
|
|
1756
|
+
className: cls,
|
|
1757
|
+
replacement
|
|
1758
|
+
},
|
|
1759
|
+
fix(fixer) {
|
|
1760
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1761
|
+
}
|
|
1762
|
+
});
|
|
1763
|
+
else context.report({
|
|
1764
|
+
node: loc.node,
|
|
1765
|
+
messageId: "moveNegative",
|
|
1766
|
+
data: {
|
|
1767
|
+
className: cls,
|
|
1768
|
+
replacement
|
|
1769
|
+
}
|
|
1770
|
+
});
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
return {
|
|
1775
|
+
JSXAttribute(node) {
|
|
1776
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1777
|
+
},
|
|
1778
|
+
CallExpression(node) {
|
|
1779
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1780
|
+
},
|
|
1781
|
+
TaggedTemplateExpression(node) {
|
|
1782
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1783
|
+
},
|
|
1784
|
+
VariableDeclarator(node) {
|
|
1785
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1786
|
+
}
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
});
|
|
1790
|
+
//#endregion
|
|
1791
|
+
//#region src/rules/enforce-consistent-variable-syntax.ts
|
|
1792
|
+
const EXPLICIT_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\[var\((--[a-zA-Z0-9-]+)\)\]$/;
|
|
1793
|
+
const SHORTHAND_VAR_RE = /^([a-z][a-z0-9-]*(?:-[a-z0-9]+)*)-\((--[a-zA-Z0-9-]+)\)$/;
|
|
1794
|
+
function convertClass(cls, syntax) {
|
|
1795
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
1796
|
+
if (syntax === "shorthand") {
|
|
1797
|
+
const match = EXPLICIT_VAR_RE.exec(utility);
|
|
1798
|
+
if (match) {
|
|
1799
|
+
const [, prefix, varName] = match;
|
|
1800
|
+
return `${variant}${prefix}-(${varName})`;
|
|
1801
|
+
}
|
|
1802
|
+
} else {
|
|
1803
|
+
const match = SHORTHAND_VAR_RE.exec(utility);
|
|
1804
|
+
if (match) {
|
|
1805
|
+
const [, prefix, varName] = match;
|
|
1806
|
+
return `${variant}${prefix}-[var(${varName})]`;
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
return null;
|
|
1810
|
+
}
|
|
1811
|
+
const enforceConsistentVariableSyntax = defineRule({
|
|
1812
|
+
meta: {
|
|
1813
|
+
type: "suggestion",
|
|
1814
|
+
docs: { description: "Enforce consistent CSS variable syntax: bg-[var(--color)] ↔ bg-(--color)" },
|
|
1815
|
+
fixable: "code",
|
|
1816
|
+
schema: [{
|
|
1817
|
+
type: "object",
|
|
1818
|
+
properties: { syntax: {
|
|
1819
|
+
type: "string",
|
|
1820
|
+
enum: ["shorthand", "explicit"]
|
|
1821
|
+
} },
|
|
1822
|
+
additionalProperties: false
|
|
1823
|
+
}],
|
|
1824
|
+
messages: {
|
|
1825
|
+
useShorthand: "\"{{className}}\" uses explicit var() syntax. Use \"{{replacement}}\" (shorthand) instead.",
|
|
1826
|
+
useExplicit: "\"{{className}}\" uses shorthand syntax. Use \"{{replacement}}\" (explicit) instead."
|
|
1827
|
+
}
|
|
1828
|
+
},
|
|
1829
|
+
createOnce(context) {
|
|
1830
|
+
let _syntax = null;
|
|
1831
|
+
function getSyntax() {
|
|
1832
|
+
if (_syntax === null) _syntax = safeOptions(context)?.syntax ?? "shorthand";
|
|
1833
|
+
return _syntax;
|
|
1834
|
+
}
|
|
1835
|
+
function check(locations) {
|
|
1836
|
+
const syntax = getSyntax();
|
|
1837
|
+
for (const loc of locations) {
|
|
1838
|
+
const classes = splitClasses(loc.value);
|
|
1839
|
+
const offending = [];
|
|
1840
|
+
for (const cls of classes) {
|
|
1841
|
+
const converted = convertClass(cls, syntax);
|
|
1842
|
+
if (converted) offending.push({
|
|
1843
|
+
cls,
|
|
1844
|
+
replacement: converted
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
if (offending.length === 0) continue;
|
|
1848
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
1849
|
+
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
|
|
1850
|
+
const messageId = syntax === "shorthand" ? "useShorthand" : "useExplicit";
|
|
1851
|
+
for (let i = 0; i < offending.length; i++) {
|
|
1852
|
+
const { cls, replacement } = offending[i];
|
|
1853
|
+
if (i === 0) context.report({
|
|
1854
|
+
node: loc.node,
|
|
1855
|
+
messageId,
|
|
1856
|
+
data: {
|
|
1857
|
+
className: cls,
|
|
1858
|
+
replacement
|
|
1859
|
+
},
|
|
1860
|
+
fix(fixer) {
|
|
1861
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1862
|
+
}
|
|
1863
|
+
});
|
|
1864
|
+
else context.report({
|
|
1865
|
+
node: loc.node,
|
|
1866
|
+
messageId,
|
|
1867
|
+
data: {
|
|
1868
|
+
className: cls,
|
|
1869
|
+
replacement
|
|
1870
|
+
}
|
|
1871
|
+
});
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return {
|
|
1876
|
+
JSXAttribute(node) {
|
|
1877
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1878
|
+
},
|
|
1879
|
+
CallExpression(node) {
|
|
1880
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1881
|
+
},
|
|
1882
|
+
TaggedTemplateExpression(node) {
|
|
1883
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1884
|
+
},
|
|
1885
|
+
VariableDeclarator(node) {
|
|
1886
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1887
|
+
}
|
|
1888
|
+
};
|
|
1889
|
+
}
|
|
1890
|
+
});
|
|
1891
|
+
//#endregion
|
|
1892
|
+
//#region src/rules/no-dark-without-light.ts
|
|
1893
|
+
const DEFAULT_VARIANTS = ["dark"];
|
|
1894
|
+
const KNOWN_PREFIXES = [
|
|
1895
|
+
"rounded-tl",
|
|
1896
|
+
"rounded-tr",
|
|
1897
|
+
"rounded-bl",
|
|
1898
|
+
"rounded-br",
|
|
1899
|
+
"rounded-ss",
|
|
1900
|
+
"rounded-se",
|
|
1901
|
+
"rounded-es",
|
|
1902
|
+
"rounded-ee",
|
|
1903
|
+
"ring-offset",
|
|
1904
|
+
"border-t",
|
|
1905
|
+
"border-b",
|
|
1906
|
+
"border-l",
|
|
1907
|
+
"border-r",
|
|
1908
|
+
"border-s",
|
|
1909
|
+
"border-e",
|
|
1910
|
+
"border-x",
|
|
1911
|
+
"border-y",
|
|
1912
|
+
"rounded-t",
|
|
1913
|
+
"rounded-b",
|
|
1914
|
+
"rounded-l",
|
|
1915
|
+
"rounded-r",
|
|
1916
|
+
"rounded-s",
|
|
1917
|
+
"rounded-e",
|
|
1918
|
+
"divide-x",
|
|
1919
|
+
"divide-y",
|
|
1920
|
+
"scroll-m",
|
|
1921
|
+
"scroll-p",
|
|
1922
|
+
"from",
|
|
1923
|
+
"via",
|
|
1924
|
+
"to"
|
|
1925
|
+
];
|
|
1926
|
+
/**
|
|
1927
|
+
* Extracts the utility prefix (e.g. "bg" from "bg-gray-900", "text" from "text-white").
|
|
1928
|
+
* Used to group utilities by their property type.
|
|
1929
|
+
*/
|
|
1930
|
+
function getUtilityPrefix(utility) {
|
|
1931
|
+
let u = utility;
|
|
1932
|
+
if (u.startsWith("!")) u = u.slice(1);
|
|
1933
|
+
if (u.startsWith("-")) u = u.slice(1);
|
|
1934
|
+
for (const prefix of KNOWN_PREFIXES) if (u === prefix || u.startsWith(`${prefix}-`)) return prefix;
|
|
1935
|
+
const dashIdx = u.indexOf("-");
|
|
1936
|
+
return dashIdx >= 0 ? u.slice(0, dashIdx) : u;
|
|
1937
|
+
}
|
|
1938
|
+
const noDarkWithoutLight = defineRule({
|
|
1939
|
+
meta: {
|
|
1940
|
+
type: "problem",
|
|
1941
|
+
docs: { description: "Require a base (light) utility when using dark: (or other scheme) variant" },
|
|
1942
|
+
schema: [{
|
|
1943
|
+
type: "object",
|
|
1944
|
+
properties: { variants: {
|
|
1945
|
+
type: "array",
|
|
1946
|
+
items: { type: "string" }
|
|
1947
|
+
} },
|
|
1948
|
+
additionalProperties: false
|
|
1949
|
+
}],
|
|
1950
|
+
messages: { missingBase: "\"{{className}}\" uses the {{variant}} variant, but there is no base \"{{prefix}}-*\" class on this element." }
|
|
1951
|
+
},
|
|
1952
|
+
createOnce(context) {
|
|
1953
|
+
let _watchedVariants = null;
|
|
1954
|
+
function getWatchedVariants() {
|
|
1955
|
+
if (_watchedVariants === null) {
|
|
1956
|
+
const options = safeOptions(context);
|
|
1957
|
+
_watchedVariants = new Set(options?.variants ?? DEFAULT_VARIANTS);
|
|
1958
|
+
}
|
|
1959
|
+
return _watchedVariants;
|
|
1960
|
+
}
|
|
1961
|
+
function check(locations) {
|
|
1962
|
+
const watchedVariants = getWatchedVariants();
|
|
1963
|
+
for (const loc of locations) {
|
|
1964
|
+
const classes = splitClasses(loc.value);
|
|
1965
|
+
const basePrefixes = /* @__PURE__ */ new Set();
|
|
1966
|
+
const variantClasses = [];
|
|
1967
|
+
for (const cls of classes) {
|
|
1968
|
+
const variants = extractVariants(cls);
|
|
1969
|
+
const prefix = getUtilityPrefix(extractUtility(cls));
|
|
1970
|
+
if (variants.some((v) => watchedVariants.has(v))) {
|
|
1971
|
+
const variant = variants.find((v) => watchedVariants.has(v));
|
|
1972
|
+
variantClasses.push({
|
|
1973
|
+
cls,
|
|
1974
|
+
variant,
|
|
1975
|
+
prefix
|
|
1976
|
+
});
|
|
1977
|
+
} else basePrefixes.add(prefix);
|
|
1978
|
+
}
|
|
1979
|
+
for (const { cls, variant, prefix } of variantClasses) if (!basePrefixes.has(prefix)) context.report({
|
|
1980
|
+
node: loc.node,
|
|
1981
|
+
messageId: "missingBase",
|
|
1982
|
+
data: {
|
|
1983
|
+
className: cls,
|
|
1984
|
+
variant,
|
|
1985
|
+
prefix
|
|
1986
|
+
}
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
return {
|
|
1991
|
+
JSXAttribute(node) {
|
|
1992
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1993
|
+
},
|
|
1994
|
+
CallExpression(node) {
|
|
1995
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1996
|
+
},
|
|
1997
|
+
TaggedTemplateExpression(node) {
|
|
1998
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
1999
|
+
},
|
|
2000
|
+
VariableDeclarator(node) {
|
|
2001
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2002
|
+
}
|
|
2003
|
+
};
|
|
2004
|
+
}
|
|
2005
|
+
});
|
|
2006
|
+
//#endregion
|
|
2007
|
+
//#region src/rules/no-hardcoded-colors.ts
|
|
2008
|
+
const COLOR_UTILITY_PREFIXES = [
|
|
2009
|
+
"bg",
|
|
2010
|
+
"text",
|
|
2011
|
+
"border",
|
|
2012
|
+
"border-t",
|
|
2013
|
+
"border-b",
|
|
2014
|
+
"border-l",
|
|
2015
|
+
"border-r",
|
|
2016
|
+
"border-s",
|
|
2017
|
+
"border-e",
|
|
2018
|
+
"border-x",
|
|
2019
|
+
"border-y",
|
|
2020
|
+
"outline",
|
|
2021
|
+
"ring",
|
|
2022
|
+
"ring-offset",
|
|
2023
|
+
"shadow",
|
|
2024
|
+
"accent",
|
|
2025
|
+
"caret",
|
|
2026
|
+
"fill",
|
|
2027
|
+
"stroke",
|
|
2028
|
+
"decoration",
|
|
2029
|
+
"divide",
|
|
2030
|
+
"placeholder",
|
|
2031
|
+
"from",
|
|
2032
|
+
"via",
|
|
2033
|
+
"to"
|
|
2034
|
+
];
|
|
2035
|
+
const HEX_RE = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
2036
|
+
const RGB_RE = /^rgba?\s*\(/;
|
|
2037
|
+
const HSL_RE = /^hsla?\s*\(/;
|
|
2038
|
+
const OKLCH_RE = /^oklch\s*\(/;
|
|
2039
|
+
const OKLAB_RE = /^oklab\s*\(/;
|
|
2040
|
+
const LCH_RE = /^lch\s*\(/;
|
|
2041
|
+
const LAB_RE = /^lab\s*\(/;
|
|
2042
|
+
const HWB_RE = /^hwb\s*\(/;
|
|
2043
|
+
const COLOR_RE = /^color\s*\(/;
|
|
2044
|
+
function isHardcodedColor(value) {
|
|
2045
|
+
const trimmed = value.trim();
|
|
2046
|
+
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);
|
|
2047
|
+
}
|
|
2048
|
+
function isColorUtility(utility) {
|
|
2049
|
+
for (const prefix of COLOR_UTILITY_PREFIXES) if (utility === prefix || utility.startsWith(`${prefix}-`)) return true;
|
|
2050
|
+
return false;
|
|
2051
|
+
}
|
|
2052
|
+
const noHardcodedColors = defineRule({
|
|
2053
|
+
meta: {
|
|
2054
|
+
type: "suggestion",
|
|
2055
|
+
docs: { description: "Disallow hardcoded color values in Tailwind CSS classes" },
|
|
2056
|
+
schema: [{
|
|
2057
|
+
type: "object",
|
|
2058
|
+
properties: {
|
|
2059
|
+
entryPoint: { type: "string" },
|
|
2060
|
+
allow: {
|
|
2061
|
+
type: "array",
|
|
2062
|
+
items: { type: "string" }
|
|
2063
|
+
}
|
|
2064
|
+
},
|
|
2065
|
+
additionalProperties: false
|
|
2066
|
+
}],
|
|
2067
|
+
messages: { noHardcoded: "\"{{className}}\" uses a hardcoded color value. Use a design token from your theme instead." }
|
|
2068
|
+
},
|
|
2069
|
+
createOnce(context) {
|
|
2070
|
+
let _allowlist = null;
|
|
2071
|
+
function getAllowlist() {
|
|
2072
|
+
if (_allowlist === null) {
|
|
2073
|
+
const options = safeOptions(context);
|
|
2074
|
+
_allowlist = new Set(options?.allow ?? []);
|
|
2075
|
+
}
|
|
2076
|
+
return _allowlist;
|
|
2077
|
+
}
|
|
2078
|
+
function check(locations) {
|
|
2079
|
+
const allowlist = getAllowlist();
|
|
2080
|
+
for (const loc of locations) {
|
|
2081
|
+
const classes = splitClasses(loc.value);
|
|
2082
|
+
for (const cls of classes) {
|
|
2083
|
+
if (allowlist.has(cls)) continue;
|
|
2084
|
+
if (!hasArbitraryValue(cls)) continue;
|
|
2085
|
+
if (!isColorUtility(extractUtility(cls))) continue;
|
|
2086
|
+
const value = getArbitraryValue(cls);
|
|
2087
|
+
if (!value) continue;
|
|
2088
|
+
if (isHardcodedColor(value)) context.report({
|
|
2089
|
+
node: loc.node,
|
|
2090
|
+
messageId: "noHardcoded",
|
|
2091
|
+
data: { className: cls }
|
|
2092
|
+
});
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
return {
|
|
2097
|
+
JSXAttribute(node) {
|
|
2098
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2099
|
+
},
|
|
2100
|
+
CallExpression(node) {
|
|
2101
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2102
|
+
},
|
|
2103
|
+
TaggedTemplateExpression(node) {
|
|
2104
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2105
|
+
},
|
|
2106
|
+
VariableDeclarator(node) {
|
|
2107
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2108
|
+
}
|
|
2109
|
+
};
|
|
2110
|
+
}
|
|
2111
|
+
});
|
|
2112
|
+
//#endregion
|
|
2113
|
+
//#region src/rules/no-contradicting-variants.ts
|
|
2114
|
+
const noContradictingVariants = defineRule({
|
|
2115
|
+
meta: {
|
|
2116
|
+
type: "suggestion",
|
|
2117
|
+
docs: { description: "Disallow variant-prefixed classes that are redundant because the base class already applies unconditionally" },
|
|
2118
|
+
schema: [],
|
|
2119
|
+
messages: { redundantVariant: "\"{{variantClass}}\" is redundant because \"{{baseClass}}\" already applies unconditionally." }
|
|
2120
|
+
},
|
|
2121
|
+
createOnce(context) {
|
|
2122
|
+
function check(locations) {
|
|
2123
|
+
for (const loc of locations) {
|
|
2124
|
+
const classes = splitClasses(loc.value);
|
|
2125
|
+
const baseUtilities = /* @__PURE__ */ new Set();
|
|
2126
|
+
for (const cls of classes) if (extractVariants(cls).length === 0) baseUtilities.add(cls);
|
|
2127
|
+
for (const cls of classes) {
|
|
2128
|
+
if (extractVariants(cls).length === 0) continue;
|
|
2129
|
+
const utility = extractUtility(cls);
|
|
2130
|
+
if (baseUtilities.has(utility)) context.report({
|
|
2131
|
+
node: loc.node,
|
|
2132
|
+
messageId: "redundantVariant",
|
|
2133
|
+
data: {
|
|
2134
|
+
variantClass: cls,
|
|
2135
|
+
baseClass: utility
|
|
2136
|
+
}
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
return {
|
|
2142
|
+
JSXAttribute(node) {
|
|
2143
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2144
|
+
},
|
|
2145
|
+
CallExpression(node) {
|
|
2146
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2147
|
+
},
|
|
2148
|
+
TaggedTemplateExpression(node) {
|
|
2149
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2150
|
+
},
|
|
2151
|
+
VariableDeclarator(node) {
|
|
2152
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2153
|
+
}
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
});
|
|
2157
|
+
//#endregion
|
|
2158
|
+
//#region src/rules/consistent-variant-order.ts
|
|
2159
|
+
const DEFAULT_VARIANT_ORDER = [
|
|
2160
|
+
"sm",
|
|
2161
|
+
"md",
|
|
2162
|
+
"lg",
|
|
2163
|
+
"xl",
|
|
2164
|
+
"2xl",
|
|
2165
|
+
"supports",
|
|
2166
|
+
"motion-safe",
|
|
2167
|
+
"motion-reduce",
|
|
2168
|
+
"contrast-more",
|
|
2169
|
+
"contrast-less",
|
|
2170
|
+
"forced-colors",
|
|
2171
|
+
"dark",
|
|
2172
|
+
"light",
|
|
2173
|
+
"@sm",
|
|
2174
|
+
"@md",
|
|
2175
|
+
"@lg",
|
|
2176
|
+
"@xl",
|
|
2177
|
+
"@2xl",
|
|
2178
|
+
"print",
|
|
2179
|
+
"group-hover",
|
|
2180
|
+
"group-focus",
|
|
2181
|
+
"group-active",
|
|
2182
|
+
"group-first",
|
|
2183
|
+
"group-last",
|
|
2184
|
+
"peer-hover",
|
|
2185
|
+
"peer-focus",
|
|
2186
|
+
"peer-checked",
|
|
2187
|
+
"peer-invalid",
|
|
2188
|
+
"peer-disabled",
|
|
2189
|
+
"hover",
|
|
2190
|
+
"focus",
|
|
2191
|
+
"focus-within",
|
|
2192
|
+
"focus-visible",
|
|
2193
|
+
"active",
|
|
2194
|
+
"enabled",
|
|
2195
|
+
"disabled",
|
|
2196
|
+
"checked",
|
|
2197
|
+
"indeterminate",
|
|
2198
|
+
"default",
|
|
2199
|
+
"required",
|
|
2200
|
+
"valid",
|
|
2201
|
+
"invalid",
|
|
2202
|
+
"in-range",
|
|
2203
|
+
"out-of-range",
|
|
2204
|
+
"placeholder-shown",
|
|
2205
|
+
"autofill",
|
|
2206
|
+
"read-only",
|
|
2207
|
+
"first",
|
|
2208
|
+
"last",
|
|
2209
|
+
"only",
|
|
2210
|
+
"odd",
|
|
2211
|
+
"even",
|
|
2212
|
+
"first-of-type",
|
|
2213
|
+
"last-of-type",
|
|
2214
|
+
"only-of-type",
|
|
2215
|
+
"empty",
|
|
2216
|
+
"has",
|
|
2217
|
+
"before",
|
|
2218
|
+
"after",
|
|
2219
|
+
"file",
|
|
2220
|
+
"placeholder",
|
|
2221
|
+
"selection",
|
|
2222
|
+
"marker",
|
|
2223
|
+
"backdrop",
|
|
2224
|
+
"first-line",
|
|
2225
|
+
"first-letter",
|
|
2226
|
+
"ltr",
|
|
2227
|
+
"rtl",
|
|
2228
|
+
"open"
|
|
2229
|
+
];
|
|
2230
|
+
const consistentVariantOrder = defineRule({
|
|
2231
|
+
meta: {
|
|
2232
|
+
type: "suggestion",
|
|
2233
|
+
docs: { description: "Enforce a consistent order for Tailwind CSS variant prefixes" },
|
|
2234
|
+
fixable: "code",
|
|
2235
|
+
schema: [{
|
|
2236
|
+
type: "object",
|
|
2237
|
+
properties: {
|
|
2238
|
+
entryPoint: { type: "string" },
|
|
2239
|
+
order: {
|
|
2240
|
+
type: "array",
|
|
2241
|
+
items: { type: "string" }
|
|
2242
|
+
}
|
|
2243
|
+
},
|
|
2244
|
+
additionalProperties: false
|
|
2245
|
+
}],
|
|
2246
|
+
messages: { wrongOrder: "\"{{className}}\" has variants in wrong order. Use \"{{replacement}}\" instead." }
|
|
2247
|
+
},
|
|
2248
|
+
createOnce(context) {
|
|
2249
|
+
const dsCache = getLoadedDesignSystem()?.cache ?? null;
|
|
2250
|
+
let _config = null;
|
|
2251
|
+
function getConfig() {
|
|
2252
|
+
if (_config === null) {
|
|
2253
|
+
const options = safeOptions(context);
|
|
2254
|
+
const priorityMap = /* @__PURE__ */ new Map();
|
|
2255
|
+
if (options?.order) {
|
|
2256
|
+
for (let i = 0; i < options.order.length; i++) priorityMap.set(options.order[i], i);
|
|
2257
|
+
_config = {
|
|
2258
|
+
priorityMap,
|
|
2259
|
+
fallbackPriority: options.order.length
|
|
2260
|
+
};
|
|
2261
|
+
} else if (dsCache && dsCache.hasVariantOrder()) {
|
|
2262
|
+
for (const variant of DEFAULT_VARIANT_ORDER) {
|
|
2263
|
+
const p = dsCache.getVariantPriority(variant);
|
|
2264
|
+
if (p !== null) priorityMap.set(variant, p);
|
|
2265
|
+
}
|
|
2266
|
+
_config = {
|
|
2267
|
+
priorityMap,
|
|
2268
|
+
fallbackPriority: Number.MAX_SAFE_INTEGER
|
|
2269
|
+
};
|
|
2270
|
+
} else {
|
|
2271
|
+
for (let i = 0; i < DEFAULT_VARIANT_ORDER.length; i++) priorityMap.set(DEFAULT_VARIANT_ORDER[i], i);
|
|
2272
|
+
_config = {
|
|
2273
|
+
priorityMap,
|
|
2274
|
+
fallbackPriority: DEFAULT_VARIANT_ORDER.length
|
|
2275
|
+
};
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
return _config;
|
|
2279
|
+
}
|
|
2280
|
+
function getVariantPriority(variant) {
|
|
2281
|
+
const { priorityMap, fallbackPriority } = getConfig();
|
|
2282
|
+
const priority = priorityMap.get(variant);
|
|
2283
|
+
if (priority !== void 0) return priority;
|
|
2284
|
+
if (dsCache && dsCache.hasVariantOrder()) {
|
|
2285
|
+
const dsPriority = dsCache.getVariantPriority(variant);
|
|
2286
|
+
if (dsPriority !== null) return dsPriority;
|
|
2287
|
+
}
|
|
2288
|
+
if (variant.startsWith("[")) return -1;
|
|
2289
|
+
return fallbackPriority;
|
|
2290
|
+
}
|
|
2291
|
+
function reorderClass(cls) {
|
|
2292
|
+
const variants = extractVariants(cls);
|
|
2293
|
+
if (variants.length < 2) return null;
|
|
2294
|
+
const sorted = [...variants].sort((a, b) => getVariantPriority(a) - getVariantPriority(b));
|
|
2295
|
+
if (variants.every((v, i) => v === sorted[i])) return null;
|
|
2296
|
+
const utility = extractUtility(cls);
|
|
2297
|
+
return sorted.join(":") + ":" + utility;
|
|
2298
|
+
}
|
|
2299
|
+
function check(locations) {
|
|
2300
|
+
for (const loc of locations) {
|
|
2301
|
+
const classes = splitClasses(loc.value);
|
|
2302
|
+
const offending = [];
|
|
2303
|
+
for (const cls of classes) {
|
|
2304
|
+
const fixed = reorderClass(cls);
|
|
2305
|
+
if (fixed) offending.push({
|
|
2306
|
+
cls,
|
|
2307
|
+
replacement: fixed
|
|
2308
|
+
});
|
|
2309
|
+
}
|
|
2310
|
+
if (offending.length === 0) continue;
|
|
2311
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2312
|
+
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
|
|
2313
|
+
for (let i = 0; i < offending.length; i++) {
|
|
2314
|
+
const { cls, replacement } = offending[i];
|
|
2315
|
+
if (i === 0) context.report({
|
|
2316
|
+
node: loc.node,
|
|
2317
|
+
messageId: "wrongOrder",
|
|
2318
|
+
data: {
|
|
2319
|
+
className: cls,
|
|
2320
|
+
replacement
|
|
2321
|
+
},
|
|
2322
|
+
fix(fixer) {
|
|
2323
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
2324
|
+
}
|
|
2325
|
+
});
|
|
2326
|
+
else context.report({
|
|
2327
|
+
node: loc.node,
|
|
2328
|
+
messageId: "wrongOrder",
|
|
2329
|
+
data: {
|
|
2330
|
+
className: cls,
|
|
2331
|
+
replacement
|
|
2332
|
+
}
|
|
2333
|
+
});
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
return {
|
|
2338
|
+
JSXAttribute(node) {
|
|
2339
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2340
|
+
},
|
|
2341
|
+
CallExpression(node) {
|
|
2342
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2343
|
+
},
|
|
2344
|
+
TaggedTemplateExpression(node) {
|
|
2345
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2346
|
+
},
|
|
2347
|
+
VariableDeclarator(node) {
|
|
2348
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2349
|
+
}
|
|
2350
|
+
};
|
|
2351
|
+
}
|
|
2352
|
+
});
|
|
2353
|
+
//#endregion
|
|
2354
|
+
//#region src/rules/enforce-consistent-line-wrapping.ts
|
|
2355
|
+
const DEFAULT_PRINT_WIDTH = 80;
|
|
2356
|
+
const enforceConsistentLineWrapping = defineRule({
|
|
2357
|
+
meta: {
|
|
2358
|
+
type: "suggestion",
|
|
2359
|
+
docs: { description: "Warn when a class string exceeds the configured print width" },
|
|
2360
|
+
fixable: "whitespace",
|
|
2361
|
+
schema: [{
|
|
2362
|
+
type: "object",
|
|
2363
|
+
properties: {
|
|
2364
|
+
printWidth: { type: "number" },
|
|
2365
|
+
classesPerLine: { type: "number" }
|
|
2366
|
+
},
|
|
2367
|
+
additionalProperties: false
|
|
2368
|
+
}],
|
|
2369
|
+
messages: {
|
|
2370
|
+
tooLong: "Class string is {{length}} characters long, exceeding the print width of {{printWidth}}. Consider splitting into multiple lines or extracting into a component.",
|
|
2371
|
+
tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
|
|
2372
|
+
}
|
|
2373
|
+
},
|
|
2374
|
+
createOnce(context) {
|
|
2375
|
+
let _printWidth = null;
|
|
2376
|
+
function getPrintWidth() {
|
|
2377
|
+
if (_printWidth === null) _printWidth = safeOptions(context)?.printWidth ?? DEFAULT_PRINT_WIDTH;
|
|
2378
|
+
return _printWidth;
|
|
2379
|
+
}
|
|
2380
|
+
let _classesPerLine = null;
|
|
2381
|
+
function getClassesPerLine() {
|
|
2382
|
+
if (_classesPerLine === null) _classesPerLine = safeOptions(context)?.classesPerLine ?? void 0;
|
|
2383
|
+
return _classesPerLine;
|
|
2384
|
+
}
|
|
2385
|
+
function check(locations) {
|
|
2386
|
+
const printWidth = getPrintWidth();
|
|
2387
|
+
const classesPerLine = getClassesPerLine();
|
|
2388
|
+
for (const loc of locations) {
|
|
2389
|
+
if (loc.value.length > printWidth) context.report({
|
|
2390
|
+
node: loc.node,
|
|
2391
|
+
messageId: "tooLong",
|
|
2392
|
+
data: {
|
|
2393
|
+
length: String(loc.value.length),
|
|
2394
|
+
printWidth: String(printWidth)
|
|
2395
|
+
}
|
|
2396
|
+
});
|
|
2397
|
+
if (classesPerLine !== void 0) {
|
|
2398
|
+
const classes = splitClasses(loc.value);
|
|
2399
|
+
if (classes.length > classesPerLine) if (loc.node.type === "TemplateElement") {
|
|
2400
|
+
const chunks = [];
|
|
2401
|
+
for (let i = 0; i < classes.length; i += classesPerLine) chunks.push(classes.slice(i, i + classesPerLine).join(" "));
|
|
2402
|
+
const startCol = loc.node.loc?.start.column ?? 0;
|
|
2403
|
+
const indent = " ".repeat(startCol);
|
|
2404
|
+
const fixedValue = chunks.join("\n" + indent);
|
|
2405
|
+
context.report({
|
|
2406
|
+
node: loc.node,
|
|
2407
|
+
messageId: "tooManyPerLine",
|
|
2408
|
+
data: {
|
|
2409
|
+
count: String(classes.length),
|
|
2410
|
+
max: String(classesPerLine)
|
|
2411
|
+
},
|
|
2412
|
+
fix(fixer) {
|
|
2413
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
2414
|
+
}
|
|
2415
|
+
});
|
|
2416
|
+
} else context.report({
|
|
2417
|
+
node: loc.node,
|
|
2418
|
+
messageId: "tooManyPerLine",
|
|
2419
|
+
data: {
|
|
2420
|
+
count: String(classes.length),
|
|
2421
|
+
max: String(classesPerLine)
|
|
2422
|
+
}
|
|
2423
|
+
});
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
return {
|
|
2428
|
+
JSXAttribute(node) {
|
|
2429
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2430
|
+
},
|
|
2431
|
+
CallExpression(node) {
|
|
2432
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2433
|
+
},
|
|
2434
|
+
TaggedTemplateExpression(node) {
|
|
2435
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2436
|
+
},
|
|
2437
|
+
VariableDeclarator(node) {
|
|
2438
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
2441
|
+
}
|
|
2442
|
+
});
|
|
2443
|
+
//#endregion
|
|
2444
|
+
//#region src/rules/no-unnecessary-arbitrary-value.ts
|
|
2445
|
+
const noUnnecessaryArbitraryValue = defineRule({
|
|
2446
|
+
meta: {
|
|
2447
|
+
type: "suggestion",
|
|
2448
|
+
docs: { description: "Disallow arbitrary values when a named Tailwind class produces the same CSS" },
|
|
2449
|
+
fixable: "code",
|
|
2450
|
+
schema: [{
|
|
2451
|
+
type: "object",
|
|
2452
|
+
properties: { entryPoint: { type: "string" } },
|
|
2453
|
+
additionalProperties: false
|
|
2454
|
+
}],
|
|
2455
|
+
messages: { unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead." }
|
|
2456
|
+
},
|
|
2457
|
+
createOnce(context) {
|
|
2458
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
2459
|
+
if (!result) return {};
|
|
2460
|
+
const { cache } = result;
|
|
2461
|
+
function check(locations) {
|
|
2462
|
+
for (const loc of locations) {
|
|
2463
|
+
const classes = splitClasses(loc.value);
|
|
2464
|
+
const offending = [];
|
|
2465
|
+
for (const cls of classes) {
|
|
2466
|
+
if (!hasArbitraryValue(cls)) continue;
|
|
2467
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
2468
|
+
const named = cache.getNamedEquivalent(utility);
|
|
2469
|
+
if (!named) continue;
|
|
2470
|
+
offending.push({
|
|
2471
|
+
cls,
|
|
2472
|
+
replacement: variant + named
|
|
2473
|
+
});
|
|
2474
|
+
}
|
|
2475
|
+
if (offending.length === 0) continue;
|
|
2476
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
2477
|
+
const fixedValue = classes.map((cls) => replacements.get(cls) ?? cls).join(" ");
|
|
2478
|
+
for (let i = 0; i < offending.length; i++) {
|
|
2479
|
+
const { cls, replacement } = offending[i];
|
|
2480
|
+
if (i === 0) context.report({
|
|
2481
|
+
node: loc.node,
|
|
2482
|
+
messageId: "unnecessaryArbitrary",
|
|
2483
|
+
data: {
|
|
2484
|
+
className: cls,
|
|
2485
|
+
replacement
|
|
2486
|
+
},
|
|
2487
|
+
fix(fixer) {
|
|
2488
|
+
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
2489
|
+
}
|
|
2490
|
+
});
|
|
2491
|
+
else context.report({
|
|
2492
|
+
node: loc.node,
|
|
2493
|
+
messageId: "unnecessaryArbitrary",
|
|
2494
|
+
data: {
|
|
2495
|
+
className: cls,
|
|
2496
|
+
replacement
|
|
2497
|
+
}
|
|
2498
|
+
});
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
return {
|
|
2503
|
+
JSXAttribute(node) {
|
|
2504
|
+
check(extractFromJSXAttribute(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2505
|
+
},
|
|
2506
|
+
CallExpression(node) {
|
|
2507
|
+
check(extractFromCallExpression(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2508
|
+
},
|
|
2509
|
+
TaggedTemplateExpression(node) {
|
|
2510
|
+
check(extractFromTaggedTemplate(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2511
|
+
},
|
|
2512
|
+
VariableDeclarator(node) {
|
|
2513
|
+
check(extractFromVariableDeclarator(node, DEFAULT_EXTRACTOR_CONFIG));
|
|
2514
|
+
}
|
|
2515
|
+
};
|
|
2516
|
+
}
|
|
2517
|
+
});
|
|
2518
|
+
//#endregion
|
|
2519
|
+
//#region src/index.ts
|
|
2520
|
+
const plugin = definePlugin({
|
|
2521
|
+
meta: { name: "tailwindcss" },
|
|
2522
|
+
rules: {
|
|
2523
|
+
"no-duplicate-classes": noDuplicateClasses,
|
|
2524
|
+
"no-unnecessary-whitespace": noUnnecessaryWhitespace,
|
|
2525
|
+
"no-unknown-classes": noUnknownClasses,
|
|
2526
|
+
"no-conflicting-classes": noConflictingClasses,
|
|
2527
|
+
"no-deprecated-classes": noDeprecatedClasses,
|
|
2528
|
+
"enforce-canonical": enforceCanonical,
|
|
2529
|
+
"enforce-sort-order": enforceSortOrder,
|
|
2530
|
+
"enforce-shorthand": enforceShorthand,
|
|
2531
|
+
"enforce-logical": enforceLogical,
|
|
2532
|
+
"max-class-count": maxClassCount,
|
|
2533
|
+
"no-restricted-classes": noRestrictedClasses,
|
|
2534
|
+
"no-arbitrary-value": noArbitraryValue,
|
|
2535
|
+
"enforce-consistent-important-position": enforceConsistentImportantPosition,
|
|
2536
|
+
"enforce-negative-arbitrary-values": enforceNegativeArbitraryValues,
|
|
2537
|
+
"enforce-consistent-variable-syntax": enforceConsistentVariableSyntax,
|
|
2538
|
+
"no-dark-without-light": noDarkWithoutLight,
|
|
2539
|
+
"no-hardcoded-colors": noHardcodedColors,
|
|
2540
|
+
"no-contradicting-variants": noContradictingVariants,
|
|
2541
|
+
"consistent-variant-order": consistentVariantOrder,
|
|
2542
|
+
"enforce-consistent-line-wrapping": enforceConsistentLineWrapping,
|
|
2543
|
+
"no-unnecessary-arbitrary-value": noUnnecessaryArbitraryValue
|
|
2544
|
+
}
|
|
2545
|
+
});
|
|
2546
|
+
//#endregion
|
|
2547
|
+
export { plugin as default };
|
|
2548
|
+
|
|
2549
|
+
//# sourceMappingURL=index.mjs.map
|