@economic/taco-rules 0.0.0-bootstrap.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +427 -0
- package/dist/oxlint/configs/all.d.ts +4 -0
- package/dist/oxlint/configs/all.js +5 -0
- package/dist/oxlint/configs/recommended.d.ts +4 -0
- package/dist/oxlint/configs/recommended.js +10 -0
- package/dist/oxlint/configs/styles.d.ts +4 -0
- package/dist/oxlint/configs/styles.js +12 -0
- package/dist/oxlint/index.d.ts +37 -0
- package/dist/oxlint/index.js +18 -0
- package/dist/oxlint/lib/create-class-policy-visitors.d.ts +9 -0
- package/dist/oxlint/lib/create-class-policy-visitors.js +17 -0
- package/dist/oxlint/lib/visit-tailwind-class-sources.d.ts +16 -0
- package/dist/oxlint/lib/visit-tailwind-class-sources.js +21 -0
- package/dist/oxlint/rules/consistent-clsx-import/consistent-clsx-import.d.ts +2 -0
- package/dist/oxlint/rules/consistent-clsx-import/consistent-clsx-import.js +66 -0
- package/dist/oxlint/rules/data-taco-kebab-case/data-taco-kebab-case.d.ts +2 -0
- package/dist/oxlint/rules/data-taco-kebab-case/data-taco-kebab-case.js +73 -0
- package/dist/oxlint/rules/index.d.ts +2 -0
- package/dist/oxlint/rules/index.js +30 -0
- package/dist/oxlint/rules/no-component-margin/no-component-margin.d.ts +2 -0
- package/dist/oxlint/rules/no-component-margin/no-component-margin.js +93 -0
- package/dist/oxlint/rules/no-cross-boundary-relative-imports/no-cross-boundary-relative-imports.d.ts +2 -0
- package/dist/oxlint/rules/no-cross-boundary-relative-imports/no-cross-boundary-relative-imports.js +131 -0
- package/dist/oxlint/rules/no-data-taco-attribute/no-data-taco-attribute.d.ts +2 -0
- package/dist/oxlint/rules/no-data-taco-attribute/no-data-taco-attribute.js +28 -0
- package/dist/oxlint/rules/no-disallowed-color-classes/no-disallowed-color-classes.d.ts +2 -0
- package/dist/oxlint/rules/no-disallowed-color-classes/no-disallowed-color-classes.js +23 -0
- package/dist/oxlint/rules/no-disallowed-radius-classes/no-disallowed-radius-classes.d.ts +2 -0
- package/dist/oxlint/rules/no-disallowed-radius-classes/no-disallowed-radius-classes.js +19 -0
- package/dist/oxlint/rules/no-disallowed-shadow-classes/no-disallowed-shadow-classes.d.ts +2 -0
- package/dist/oxlint/rules/no-disallowed-shadow-classes/no-disallowed-shadow-classes.js +20 -0
- package/dist/oxlint/rules/no-disallowed-spacing-classes/no-disallowed-spacing-classes.d.ts +2 -0
- package/dist/oxlint/rules/no-disallowed-spacing-classes/no-disallowed-spacing-classes.js +21 -0
- package/dist/oxlint/rules/no-display-value-key/no-display-value-key.d.ts +2 -0
- package/dist/oxlint/rules/no-display-value-key/no-display-value-key.js +114 -0
- package/dist/oxlint/rules/no-leaked-render/no-leaked-render.d.ts +2 -0
- package/dist/oxlint/rules/no-leaked-render/no-leaked-render.js +72 -0
- package/dist/oxlint/rules/no-react-useid/no-react-useid.d.ts +2 -0
- package/dist/oxlint/rules/no-react-useid/no-react-useid.js +56 -0
- package/dist/oxlint/rules/no-separate-size-classes/no-separate-size-classes.d.ts +2 -0
- package/dist/oxlint/rules/no-separate-size-classes/no-separate-size-classes.js +218 -0
- package/dist/oxlint/rules/no-unnecessary-template-literal/no-unnecessary-template-literal.d.ts +2 -0
- package/dist/oxlint/rules/no-unnecessary-template-literal/no-unnecessary-template-literal.js +63 -0
- package/dist/policies/classes.d.ts +12 -0
- package/dist/policies/classes.js +36 -0
- package/dist/policies/color.d.ts +11 -0
- package/dist/policies/color.js +217 -0
- package/dist/policies/radius.d.ts +4 -0
- package/dist/policies/radius.js +30 -0
- package/dist/policies/shadow.d.ts +2 -0
- package/dist/policies/shadow.js +23 -0
- package/dist/policies/spacing.d.ts +4 -0
- package/dist/policies/spacing.js +32 -0
- package/dist/policies/theme.d.ts +6 -0
- package/dist/policies/theme.js +14 -0
- package/dist/stylelint/index.d.ts +3 -0
- package/dist/stylelint/index.js +36 -0
- package/dist/stylelint/lib/create-policy-plugin.d.ts +16 -0
- package/dist/stylelint/lib/create-policy-plugin.js +36 -0
- package/dist/stylelint/lib/walk-css-sources.d.ts +15 -0
- package/dist/stylelint/lib/walk-css-sources.js +28 -0
- package/dist/stylelint/rules/color-token/color-token.d.ts +2 -0
- package/dist/stylelint/rules/color-token/color-token.js +4 -0
- package/dist/stylelint/rules/color-value/color-value.d.ts +2 -0
- package/dist/stylelint/rules/color-value/color-value.js +19 -0
- package/dist/stylelint/rules/radius/radius.d.ts +2 -0
- package/dist/stylelint/rules/radius/radius.js +8 -0
- package/dist/stylelint/rules/shadow/shadow.d.ts +2 -0
- package/dist/stylelint/rules/shadow/shadow.js +4 -0
- package/dist/stylelint/rules/spacing/spacing.d.ts +2 -0
- package/dist/stylelint/rules/spacing/spacing.js +8 -0
- package/oxlint/styles.json +11 -0
- package/package.json +67 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
// Flags `key` props derived purely from display values (label, name, title, text,
|
|
3
|
+
// value, dataKey, …). Those are user- or data-facing strings with no uniqueness
|
|
4
|
+
// guarantee — two chart segments labelled "Jan", two rows named "Total" — so React
|
|
5
|
+
// silently reconciles them as the same element. A stable id, or the map index mixed
|
|
6
|
+
// into the key, is what actually makes siblings distinguishable.
|
|
7
|
+
const MESSAGE = "Don't key by a display value — labels/names/titles aren't guaranteed unique across siblings. Use a stable id, or mix in the map index (e.g. `${index}-${item.label}`).";
|
|
8
|
+
const DISPLAY_NAMES = new Set([
|
|
9
|
+
'caption',
|
|
10
|
+
'content',
|
|
11
|
+
'datakey',
|
|
12
|
+
'description',
|
|
13
|
+
'header',
|
|
14
|
+
'heading',
|
|
15
|
+
'label',
|
|
16
|
+
'name',
|
|
17
|
+
'text',
|
|
18
|
+
'title',
|
|
19
|
+
]);
|
|
20
|
+
const IDENTIFIER_NAMES = new Set(['guid', 'href', 'id', 'key', 'path', 'slug', 'uid', 'url', 'uuid']);
|
|
21
|
+
const INDEX_NAMES = new Set(['i', 'idx', 'index', 'position']);
|
|
22
|
+
// `columnName` / `dataKey` / `rowIndex` all classify by their trailing word, so a
|
|
23
|
+
// camelCase name is matched on its last segment as well as in full.
|
|
24
|
+
function lastSegment(name) {
|
|
25
|
+
const segments = name.split(/(?=[A-Z])/u);
|
|
26
|
+
return (segments.at(-1) ?? name).toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
const GROUPS = [
|
|
29
|
+
['display', DISPLAY_NAMES],
|
|
30
|
+
['identifier', IDENTIFIER_NAMES],
|
|
31
|
+
['index', INDEX_NAMES],
|
|
32
|
+
];
|
|
33
|
+
// A whole-name match always beats a trailing-word one, so `dataKey` stays a display
|
|
34
|
+
// value rather than being read as an identifier because it ends in "key".
|
|
35
|
+
function classify(name) {
|
|
36
|
+
return (GROUPS.find(([, group]) => group.has(name.toLowerCase()))?.[0] ??
|
|
37
|
+
GROUPS.find(([, group]) => group.has(lastSegment(name)))?.[0] ??
|
|
38
|
+
null);
|
|
39
|
+
}
|
|
40
|
+
const NO_NAMES = [[]];
|
|
41
|
+
function combine(left, right) {
|
|
42
|
+
return left.flatMap(a => right.map(b => [...a, ...b]));
|
|
43
|
+
}
|
|
44
|
+
// For member expressions only the accessed property counts (`item.label` → `label`),
|
|
45
|
+
// since the object is just the carrier. Computed access keeps both sides, so
|
|
46
|
+
// `item['label']` reads like `item.label` and `rows[index]` still yields `index`.
|
|
47
|
+
function collectBranches(node) {
|
|
48
|
+
if (!node)
|
|
49
|
+
return NO_NAMES;
|
|
50
|
+
switch (node.type) {
|
|
51
|
+
case 'Identifier': {
|
|
52
|
+
return [[node.name]];
|
|
53
|
+
}
|
|
54
|
+
case 'MemberExpression': {
|
|
55
|
+
if (!node.computed) {
|
|
56
|
+
return node.property.type === 'Identifier' ? [[node.property.name]] : NO_NAMES;
|
|
57
|
+
}
|
|
58
|
+
const property = node.property.type === 'Literal' && typeof node.property.value === 'string'
|
|
59
|
+
? [[node.property.value]]
|
|
60
|
+
: collectBranches(node.property);
|
|
61
|
+
return combine(collectBranches(node.object), property);
|
|
62
|
+
}
|
|
63
|
+
case 'TemplateLiteral': {
|
|
64
|
+
return node.expressions.reduce((branches, expression) => combine(branches, collectBranches(expression)), NO_NAMES);
|
|
65
|
+
}
|
|
66
|
+
case 'BinaryExpression': {
|
|
67
|
+
return combine(collectBranches(node.left), collectBranches(node.right));
|
|
68
|
+
}
|
|
69
|
+
case 'CallExpression': {
|
|
70
|
+
const callee = node.callee.type === 'MemberExpression' && !node.callee.computed ? collectBranches(node.callee.object) : NO_NAMES;
|
|
71
|
+
return node.arguments.reduce((branches, argument) => combine(branches, collectBranches(argument)), callee);
|
|
72
|
+
}
|
|
73
|
+
case 'ConditionalExpression': {
|
|
74
|
+
return [...collectBranches(node.consequent), ...collectBranches(node.alternate)];
|
|
75
|
+
}
|
|
76
|
+
case 'LogicalExpression': {
|
|
77
|
+
return [...collectBranches(node.left), ...collectBranches(node.right)];
|
|
78
|
+
}
|
|
79
|
+
default: {
|
|
80
|
+
return NO_NAMES;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export default defineRule({
|
|
85
|
+
meta: {
|
|
86
|
+
type: 'problem',
|
|
87
|
+
docs: {
|
|
88
|
+
description: 'Disallow React keys derived solely from display values such as labels, names or titles.',
|
|
89
|
+
recommended: false,
|
|
90
|
+
},
|
|
91
|
+
schema: [],
|
|
92
|
+
},
|
|
93
|
+
createOnce(context) {
|
|
94
|
+
return {
|
|
95
|
+
JSXAttribute(node) {
|
|
96
|
+
if (node.name.type !== 'JSXIdentifier' || node.name.name !== 'key')
|
|
97
|
+
return;
|
|
98
|
+
if (!node.value || node.value.type !== 'JSXExpressionContainer')
|
|
99
|
+
return;
|
|
100
|
+
const branches = collectBranches(node.value.expression);
|
|
101
|
+
// every branch must stand on its own: an id or an index anywhere in
|
|
102
|
+
// that branch makes it unique, otherwise a display value sinks it
|
|
103
|
+
const unsafe = branches.some(names => {
|
|
104
|
+
const categories = names.map(classify);
|
|
105
|
+
if (categories.some(category => category === 'identifier' || category === 'index'))
|
|
106
|
+
return false;
|
|
107
|
+
return categories.includes('display');
|
|
108
|
+
});
|
|
109
|
+
if (unsafe)
|
|
110
|
+
context.report({ node, message: MESSAGE });
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
// Prevents leaked renders: `{items.length && <span />}` renders `0` when the array
|
|
3
|
+
// is empty, because `&&` returns its falsy left operand and React renders numbers.
|
|
4
|
+
//
|
|
5
|
+
// Ported from eslint-plugin-oesterkilde's type-aware no-leaked-render. oxlint JS
|
|
6
|
+
// plugins expose no parser services / type checker, so detection is syntactic:
|
|
7
|
+
// only expressions that are numeric by construction are flagged — `.length`
|
|
8
|
+
// access, numeric literals, arithmetic and bitwise operators. A bare identifier
|
|
9
|
+
// that merely has a number type cannot be detected without type information.
|
|
10
|
+
const MESSAGE = 'Potential leaked value that might cause unintentionally rendered values or rendering crashes. Coerce to boolean (e.g. `items.length > 0`) or use a ternary.';
|
|
11
|
+
// `+` is excluded: it may be string concatenation.
|
|
12
|
+
const NUMERIC_BINARY_OPERATORS = new Set(['-', '*', '/', '%', '**', '&', '|', '^', '<<', '>>', '>>>']);
|
|
13
|
+
const NUMERIC_UNARY_OPERATORS = new Set(['-', '+', '~']);
|
|
14
|
+
function isLengthAccess(node) {
|
|
15
|
+
if (node.computed) {
|
|
16
|
+
return node.property.type === 'Literal' && node.property.value === 'length';
|
|
17
|
+
}
|
|
18
|
+
return node.property.type === 'Identifier' && node.property.name === 'length';
|
|
19
|
+
}
|
|
20
|
+
// Whether the expression's value is numeric by construction (no type info needed).
|
|
21
|
+
function isNumericByConstruction(node) {
|
|
22
|
+
switch (node.type) {
|
|
23
|
+
case 'Literal':
|
|
24
|
+
return typeof node.value === 'number' || typeof node.value === 'bigint';
|
|
25
|
+
case 'MemberExpression':
|
|
26
|
+
return isLengthAccess(node);
|
|
27
|
+
case 'ChainExpression':
|
|
28
|
+
// optional chaining: `items?.length`
|
|
29
|
+
return node.expression.type === 'MemberExpression' && isLengthAccess(node.expression);
|
|
30
|
+
case 'BinaryExpression':
|
|
31
|
+
return NUMERIC_BINARY_OPERATORS.has(node.operator);
|
|
32
|
+
case 'UnaryExpression':
|
|
33
|
+
return NUMERIC_UNARY_OPERATORS.has(node.operator);
|
|
34
|
+
case 'ParenthesizedExpression':
|
|
35
|
+
return isNumericByConstruction(node.expression);
|
|
36
|
+
case 'ConditionalExpression':
|
|
37
|
+
// `(a ? x : y) && …` leaks whichever branch was chosen
|
|
38
|
+
return isNumericByConstruction(node.consequent) || isNumericByConstruction(node.alternate);
|
|
39
|
+
default:
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// In `a && b && <jsx />` (parsed as `(a && b) && <jsx />`) every operand except the
|
|
44
|
+
// final one is a condition whose falsy value leaks into the render output.
|
|
45
|
+
function checkConditions(context, node) {
|
|
46
|
+
if (node.type === 'LogicalExpression' && node.operator === '&&') {
|
|
47
|
+
checkConditions(context, node.left);
|
|
48
|
+
checkConditions(context, node.right);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (isNumericByConstruction(node)) {
|
|
52
|
+
context.report({ node, message: MESSAGE });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export default defineRule({
|
|
56
|
+
meta: {
|
|
57
|
+
type: 'problem',
|
|
58
|
+
docs: {
|
|
59
|
+
description: 'Prevent problematic leaked values from being rendered in JSX.',
|
|
60
|
+
recommended: true,
|
|
61
|
+
},
|
|
62
|
+
schema: [],
|
|
63
|
+
},
|
|
64
|
+
createOnce(context) {
|
|
65
|
+
return {
|
|
66
|
+
'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) {
|
|
67
|
+
// Only the left side leaks — the right side is the intended render output.
|
|
68
|
+
checkConditions(context, node.left);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
// Disallows React's useId in favour of the design-system's own id utilities:
|
|
3
|
+
// `useId` from '@/hooks/useId' in components, `generateId` from '@/utils/generateId'
|
|
4
|
+
// elsewhere. Covers both the named import (`import { useId } from 'react'`) and
|
|
5
|
+
// member access through any local binding of the react module (`React.useId()`),
|
|
6
|
+
// without flagging react imports that never touch useId.
|
|
7
|
+
const MESSAGE = "Use `useId` from '@/hooks/useId' instead, which supports a native id fallback and a prefix.";
|
|
8
|
+
function isUseIdImportName(imported) {
|
|
9
|
+
return imported.type === 'Identifier' ? imported.name === 'useId' : imported.value === 'useId';
|
|
10
|
+
}
|
|
11
|
+
function isUseIdProperty(node) {
|
|
12
|
+
if (node.computed)
|
|
13
|
+
return node.property.type === 'Literal' && node.property.value === 'useId';
|
|
14
|
+
return node.property.type === 'Identifier' && node.property.name === 'useId';
|
|
15
|
+
}
|
|
16
|
+
export default defineRule({
|
|
17
|
+
meta: {
|
|
18
|
+
type: 'problem',
|
|
19
|
+
docs: {
|
|
20
|
+
description: "Disallow React's useId in favour of `useId` from '@/hooks/useId'.",
|
|
21
|
+
recommended: false,
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
},
|
|
25
|
+
createOnce(context) {
|
|
26
|
+
// local names bound to the react module's default/namespace export;
|
|
27
|
+
// reset on Program because the createOnce closure is reused across files
|
|
28
|
+
let reactBindings = new Set();
|
|
29
|
+
return {
|
|
30
|
+
Program() {
|
|
31
|
+
reactBindings = new Set();
|
|
32
|
+
},
|
|
33
|
+
ImportDeclaration(node) {
|
|
34
|
+
if (node.source.value !== 'react')
|
|
35
|
+
return;
|
|
36
|
+
for (const specifier of node.specifiers) {
|
|
37
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
38
|
+
if (isUseIdImportName(specifier.imported)) {
|
|
39
|
+
context.report({ node: specifier, message: MESSAGE });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
reactBindings.add(specifier.local.name);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
MemberExpression(node) {
|
|
48
|
+
if (!isUseIdProperty(node))
|
|
49
|
+
return;
|
|
50
|
+
if (node.object.type !== 'Identifier' || !reactBindings.has(node.object.name))
|
|
51
|
+
return;
|
|
52
|
+
context.report({ node, message: MESSAGE });
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
});
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
import { createTailwindClassSourceVisitors } from "../../lib/visit-tailwind-class-sources.js";
|
|
3
|
+
// Bans separate width + height utilities that share the same value and modifiers.
|
|
4
|
+
// Use the combined size-* utility instead:
|
|
5
|
+
// w-5 h-5 -> size-5
|
|
6
|
+
// !w-10 !h-10 -> !size-10
|
|
7
|
+
// hover:w-4 hover:h-4 -> hover:size-4
|
|
8
|
+
// Also detects pairs split across arguments of class-combining callees:
|
|
9
|
+
// cn('foo w-5', 'bar h-5') -> cn('foo size-5', 'bar')
|
|
10
|
+
// See https://v3.tailwindcss.com/docs/size
|
|
11
|
+
//
|
|
12
|
+
// Values without a size-* equivalent (e.g. w-screen/h-screen map to different
|
|
13
|
+
// viewport axes) are left alone.
|
|
14
|
+
const WIDTH_HEIGHT = /^((?:[\w-]+:)*)(!?)(w|h)-(.+)$/;
|
|
15
|
+
// Viewport-relative values differ per axis, so there is no size-* equivalent.
|
|
16
|
+
const NO_SIZE_EQUIVALENT = /screen|^\d*(?:v[wh]|[sld]v[wh])$/;
|
|
17
|
+
const MESSAGE = 'Separate equal width and height utilities. Use the combined size-* utility instead (e.g. size-5).';
|
|
18
|
+
function tokenize(chunk) {
|
|
19
|
+
const tokens = [];
|
|
20
|
+
const re = /\S+/g;
|
|
21
|
+
let match;
|
|
22
|
+
while ((match = re.exec(chunk)) !== null) {
|
|
23
|
+
tokens.push({ text: match[0], start: match.index, end: match.index + match[0].length });
|
|
24
|
+
}
|
|
25
|
+
return tokens;
|
|
26
|
+
}
|
|
27
|
+
// Pair up w-/h- tokens sharing modifiers + value. `entry.order` decides which of a pair is
|
|
28
|
+
// kept (the earlier one); defaults to the token index.
|
|
29
|
+
function findPairs(entries) {
|
|
30
|
+
const groups = new Map();
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const match = WIDTH_HEIGHT.exec(entry.token.text);
|
|
33
|
+
if (match === null)
|
|
34
|
+
continue;
|
|
35
|
+
const [, variants, important, axis, sizeValue] = match;
|
|
36
|
+
if (variants === undefined || important === undefined || sizeValue === undefined)
|
|
37
|
+
continue;
|
|
38
|
+
if (axis !== 'w' && axis !== 'h')
|
|
39
|
+
continue;
|
|
40
|
+
if (NO_SIZE_EQUIVALENT.test(sizeValue))
|
|
41
|
+
continue;
|
|
42
|
+
const key = `${variants}${important}${sizeValue}`;
|
|
43
|
+
let group = groups.get(key);
|
|
44
|
+
if (group === undefined) {
|
|
45
|
+
group = { w: [], h: [], sizeToken: `${variants}${important}size-${sizeValue}` };
|
|
46
|
+
groups.set(key, group);
|
|
47
|
+
}
|
|
48
|
+
group[axis].push(entry);
|
|
49
|
+
}
|
|
50
|
+
const pairs = [];
|
|
51
|
+
for (const group of groups.values()) {
|
|
52
|
+
const count = Math.min(group.w.length, group.h.length);
|
|
53
|
+
for (let p = 0; p < count; p++) {
|
|
54
|
+
// `p < count = min(w.length, h.length)`, so both indices are always in bounds.
|
|
55
|
+
const wEntry = group.w[p];
|
|
56
|
+
const hEntry = group.h[p];
|
|
57
|
+
const [keep, drop] = wEntry.order <= hEntry.order ? [wEntry, hEntry] : [hEntry, wEntry];
|
|
58
|
+
pairs.push({ keep, drop, sizeToken: group.sizeToken });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return pairs;
|
|
62
|
+
}
|
|
63
|
+
function intraPairs(tokens) {
|
|
64
|
+
return findPairs(tokens.map((token, i) => ({ token, tokens, index: i, order: i })));
|
|
65
|
+
}
|
|
66
|
+
// Token-level fixes: replace each kept token with the size-* utility and remove
|
|
67
|
+
// each dropped token plus the whitespace joining it to a neighbour. Two drops
|
|
68
|
+
// from the same token list can both claim the whitespace between them (a drop
|
|
69
|
+
// at index 0 absorbs trailing whitespace, its successor absorbs leading), so
|
|
70
|
+
// removal ranges are computed per token list in index order, clamped against
|
|
71
|
+
// the previous removal's end. Fixes stay within their tokens' spans, so fixes
|
|
72
|
+
// from separate reports (intra-literal and cross-argument) never overlap and
|
|
73
|
+
// oxlint can apply them all in one pass.
|
|
74
|
+
function buildFixes(fixer, pairs) {
|
|
75
|
+
const fixes = pairs.map(({ keep, sizeToken }) => {
|
|
76
|
+
const base = keep.base ?? 0;
|
|
77
|
+
return fixer.replaceTextRange([base + keep.token.start, base + keep.token.end], sizeToken);
|
|
78
|
+
});
|
|
79
|
+
const dropsByTokenList = new Map();
|
|
80
|
+
for (const { drop } of pairs) {
|
|
81
|
+
const group = dropsByTokenList.get(drop.tokens);
|
|
82
|
+
if (group === undefined) {
|
|
83
|
+
dropsByTokenList.set(drop.tokens, [drop]);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
group.push(drop);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const drops of dropsByTokenList.values()) {
|
|
90
|
+
drops.sort((a, b) => a.index - b.index);
|
|
91
|
+
let previousEnd = 0;
|
|
92
|
+
for (const drop of drops) {
|
|
93
|
+
const dropToken = drop.token;
|
|
94
|
+
// Guarded by `drop.index > 0` / `drop.index + 1 < drop.tokens.length` below, so
|
|
95
|
+
// the neighbouring index is always in bounds.
|
|
96
|
+
const removeStart = drop.index > 0 ? drop.tokens[drop.index - 1].end : dropToken.start;
|
|
97
|
+
const removeEnd = drop.index > 0
|
|
98
|
+
? dropToken.end
|
|
99
|
+
: drop.index + 1 < drop.tokens.length
|
|
100
|
+
? drop.tokens[drop.index + 1].start
|
|
101
|
+
: dropToken.end;
|
|
102
|
+
const base = drop.base ?? 0;
|
|
103
|
+
fixes.push(fixer.removeRange([base + Math.max(removeStart, previousEnd), base + removeEnd]));
|
|
104
|
+
previousEnd = removeEnd;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return fixes;
|
|
108
|
+
}
|
|
109
|
+
// The exact raw source chunk to lint/rewrite. For a Literal that's the cooked
|
|
110
|
+
// string (Tailwind classes contain no escapes, so cooked === inner source); for
|
|
111
|
+
// a TemplateElement it's the raw quasi text.
|
|
112
|
+
function rawChunk(node, value) {
|
|
113
|
+
return node.type === 'TemplateElement' ? (node.value?.raw ?? node.value?.cooked ?? '') : value;
|
|
114
|
+
}
|
|
115
|
+
function check(context, node, value) {
|
|
116
|
+
if (typeof value !== 'string' || value.length === 0)
|
|
117
|
+
return;
|
|
118
|
+
const chunk = rawChunk(node, value);
|
|
119
|
+
const tokens = tokenize(chunk);
|
|
120
|
+
// Token offsets are relative to the chunk, which starts one char after the
|
|
121
|
+
// node: past the opening quote for a Literal, past the backtick or `}` for
|
|
122
|
+
// a TemplateElement.
|
|
123
|
+
const base = node.range[0] + 1;
|
|
124
|
+
const pairs = intraPairs(tokens).map(({ keep, drop, sizeToken }) => ({
|
|
125
|
+
keep: { ...keep, base },
|
|
126
|
+
drop: { ...drop, base },
|
|
127
|
+
sizeToken,
|
|
128
|
+
}));
|
|
129
|
+
if (pairs.length === 0)
|
|
130
|
+
return;
|
|
131
|
+
context.report({
|
|
132
|
+
node,
|
|
133
|
+
message: MESSAGE,
|
|
134
|
+
fix: fixer => buildFixes(fixer, pairs),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
// Class-combining callees whose string arguments are joined at runtime, so a
|
|
138
|
+
// w-/h- pair may be split across arguments: cn('foo w-5', 'bar h-5').
|
|
139
|
+
const CLASS_FN_CALLEES = new Set(['cn', 'clsx']);
|
|
140
|
+
const CROSS_ARG_MESSAGE = 'Separate equal width and height utilities split across arguments. Use the combined size-* utility instead (e.g. size-5).';
|
|
141
|
+
function isStringLiteralArgument(node) {
|
|
142
|
+
return node.type === 'Literal' && typeof node.value === 'string';
|
|
143
|
+
}
|
|
144
|
+
// Dropping tokens from a call's arguments is only safe when the callee actually
|
|
145
|
+
// combines class strings. An unresolved `cn`/`clsx` (a global) or one bound by an
|
|
146
|
+
// import is trusted — project convention makes `cn` the clsx default import — but a
|
|
147
|
+
// local binding (parameter, variable, function) merely shadowing the name is not.
|
|
148
|
+
function isShadowingBinding(context, callee) {
|
|
149
|
+
if (callee.type !== 'Identifier')
|
|
150
|
+
return false;
|
|
151
|
+
for (const scope of context.sourceCode.scopeManager.scopes) {
|
|
152
|
+
const variable = scope.set.get(callee.name);
|
|
153
|
+
if (variable === undefined)
|
|
154
|
+
continue;
|
|
155
|
+
if (!variable.references.some(reference => reference.identifier === callee))
|
|
156
|
+
continue;
|
|
157
|
+
return variable.defs.some(definition => definition.type !== 'ImportBinding');
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
function checkCallArguments(context, node) {
|
|
162
|
+
if (node.callee.type !== 'Identifier' || !CLASS_FN_CALLEES.has(node.callee.name))
|
|
163
|
+
return;
|
|
164
|
+
if (isShadowingBinding(context, node.callee))
|
|
165
|
+
return;
|
|
166
|
+
// Only unconditional direct string arguments — a token inside a conditional
|
|
167
|
+
// argument (isActive && 'h-5') is not always applied, so pairing it with an
|
|
168
|
+
// unconditional one would change behaviour.
|
|
169
|
+
const args = node.arguments.filter(isStringLiteralArgument);
|
|
170
|
+
if (args.length < 2)
|
|
171
|
+
return;
|
|
172
|
+
// Tokens consumed by intra-string pairs are reported (and fixed) by the
|
|
173
|
+
// Literal visitor; only the leftovers participate here. Per key, one
|
|
174
|
+
// argument's leftovers are all on one axis, so any pair found below
|
|
175
|
+
// necessarily spans two arguments.
|
|
176
|
+
const entries = [];
|
|
177
|
+
for (const [a, arg] of args.entries()) {
|
|
178
|
+
const tokens = tokenize(arg.value);
|
|
179
|
+
const consumed = new Set();
|
|
180
|
+
for (const { keep, drop } of intraPairs(tokens)) {
|
|
181
|
+
consumed.add(keep.index);
|
|
182
|
+
consumed.add(drop.index);
|
|
183
|
+
}
|
|
184
|
+
const base = arg.range[0] + 1;
|
|
185
|
+
for (const [i, token] of tokens.entries()) {
|
|
186
|
+
if (consumed.has(i))
|
|
187
|
+
continue;
|
|
188
|
+
entries.push({ token, tokens, index: i, base, order: a * 1e6 + i });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const pairs = findPairs(entries);
|
|
192
|
+
if (pairs.length === 0)
|
|
193
|
+
return;
|
|
194
|
+
context.report({
|
|
195
|
+
node,
|
|
196
|
+
message: CROSS_ARG_MESSAGE,
|
|
197
|
+
fix: fixer => buildFixes(fixer, pairs),
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
export default defineRule({
|
|
201
|
+
meta: {
|
|
202
|
+
type: 'problem',
|
|
203
|
+
fixable: 'code',
|
|
204
|
+
docs: {
|
|
205
|
+
description: 'Disallow separate equal width/height Tailwind utilities in favour of size-*.',
|
|
206
|
+
recommended: false,
|
|
207
|
+
},
|
|
208
|
+
schema: [],
|
|
209
|
+
},
|
|
210
|
+
createOnce(context) {
|
|
211
|
+
return {
|
|
212
|
+
...createTailwindClassSourceVisitors(context, check),
|
|
213
|
+
CallExpression(node) {
|
|
214
|
+
checkCallArguments(context, node);
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
// Template literals without interpolations are just strings with extra syntax:
|
|
3
|
+
// `` const a = `hello`; `` → `const a = 'hello';`
|
|
4
|
+
//
|
|
5
|
+
// Left alone:
|
|
6
|
+
// - templates with `${…}` expressions (the reason backticks exist)
|
|
7
|
+
// - tagged templates (`tag`x`` — the tag function requires a template)
|
|
8
|
+
// - multiline templates (backticks are the cleanest multiline syntax)
|
|
9
|
+
// - content containing BOTH quote styles — any quoted form would need escaping,
|
|
10
|
+
// so backticks remain the cleanest way to write it
|
|
11
|
+
const MESSAGE = 'Template literal has no dynamic parts — use a regular string literal instead.';
|
|
12
|
+
// Rebuild the string from the cooked value rather than the raw text so existing
|
|
13
|
+
// escape sequences (\`, \\, …) are regenerated correctly for the new quote
|
|
14
|
+
// style instead of being copied through. Content with both quote styles never
|
|
15
|
+
// reaches here, so the chosen quote never needs escaping.
|
|
16
|
+
function requote(cooked) {
|
|
17
|
+
const quote = cooked.includes("'") ? '"' : "'";
|
|
18
|
+
const escaped = cooked
|
|
19
|
+
.replace(/\\/g, '\\\\')
|
|
20
|
+
.replace(/\n/g, '\\n')
|
|
21
|
+
.replace(/\r/g, '\\r')
|
|
22
|
+
.replace(/\t/g, '\\t')
|
|
23
|
+
.replace(/\u2028/g, '\\u2028')
|
|
24
|
+
.replace(/\u2029/g, '\\u2029');
|
|
25
|
+
return `${quote}${escaped}${quote}`;
|
|
26
|
+
}
|
|
27
|
+
export default defineRule({
|
|
28
|
+
meta: {
|
|
29
|
+
type: 'problem',
|
|
30
|
+
fixable: 'code',
|
|
31
|
+
docs: {
|
|
32
|
+
description: 'Disallow template literals without dynamic parts in favour of regular string literals.',
|
|
33
|
+
recommended: false,
|
|
34
|
+
},
|
|
35
|
+
schema: [],
|
|
36
|
+
},
|
|
37
|
+
createOnce(context) {
|
|
38
|
+
return {
|
|
39
|
+
TemplateLiteral(node) {
|
|
40
|
+
if (node.expressions.length > 0)
|
|
41
|
+
return;
|
|
42
|
+
if (node.parent.type === 'TaggedTemplateExpression')
|
|
43
|
+
return;
|
|
44
|
+
const [quasi] = node.quasis;
|
|
45
|
+
const cooked = quasi?.value.cooked;
|
|
46
|
+
// cooked is null when the template contains an invalid escape
|
|
47
|
+
// sequence — no equivalent string literal exists.
|
|
48
|
+
if (cooked === undefined || cooked === null)
|
|
49
|
+
return;
|
|
50
|
+
if (cooked.includes('\n'))
|
|
51
|
+
return;
|
|
52
|
+
// both quote styles present — backticks avoid escaping either
|
|
53
|
+
if (cooked.includes("'") && cooked.includes('"'))
|
|
54
|
+
return;
|
|
55
|
+
context.report({
|
|
56
|
+
node,
|
|
57
|
+
message: MESSAGE,
|
|
58
|
+
fix: fixer => fixer.replaceTextRange(node.range, requote(cooked)),
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type ClassCheck = (className: string) => string | undefined;
|
|
2
|
+
export interface ClassToken {
|
|
3
|
+
className: string;
|
|
4
|
+
index: number;
|
|
5
|
+
}
|
|
6
|
+
export interface ClassViolation extends ClassToken {
|
|
7
|
+
message: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function splitClasses(text: string): ClassToken[];
|
|
10
|
+
export declare function findViolations(text: string, check: ClassCheck): ClassViolation[];
|
|
11
|
+
export declare function stripVariants(className: string): string;
|
|
12
|
+
export declare function orList(items: readonly string[]): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Class-string parsing shared by every family's oxlint rule and stylelint plugin, so both linters
|
|
2
|
+
// split and read classes the same way.
|
|
3
|
+
// Splits text into separate classes. Text can be CSS (`@apply p-2;`) or a sentence (`use rounded-sm,
|
|
4
|
+
// rounded or …`), so a trailing `;`, `,` or `.` is dropped: no real class ends in one.
|
|
5
|
+
export function splitClasses(text) {
|
|
6
|
+
return [...text.matchAll(/\S+/gu)].map(match => ({ className: match[0].replace(/[;,.]+$/u, ''), index: match.index }));
|
|
7
|
+
}
|
|
8
|
+
export function findViolations(text, check) {
|
|
9
|
+
return splitClasses(text).flatMap(token => {
|
|
10
|
+
const message = check(token.className);
|
|
11
|
+
return message === undefined ? [] : [{ ...token, message }];
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
// Removes conditions like `hover:` or `md:` so only the style itself is checked (`hover:bg-blue-500`
|
|
15
|
+
// → `bg-blue-500`). A `:` inside `[…]` is part of a custom condition (`[&:hover]:`), not a separator,
|
|
16
|
+
// so it's skipped. A leading `!` ("always win") is dropped too.
|
|
17
|
+
export function stripVariants(className) {
|
|
18
|
+
let depth = 0;
|
|
19
|
+
let start = 0;
|
|
20
|
+
for (let i = 0; i < className.length; i++) {
|
|
21
|
+
const char = className[i];
|
|
22
|
+
if (char === '[')
|
|
23
|
+
depth++;
|
|
24
|
+
else if (char === ']')
|
|
25
|
+
depth--;
|
|
26
|
+
else if (char === ':' && depth === 0)
|
|
27
|
+
start = i + 1;
|
|
28
|
+
}
|
|
29
|
+
return className.slice(start).replace(/^!/u, '');
|
|
30
|
+
}
|
|
31
|
+
// Joins words into a readable phrase for error messages, e.g. "instead use rounded, rounded-sm or rounded-lg".
|
|
32
|
+
// Every word but the last is joined with commas, and the last is added with "or":
|
|
33
|
+
// [] → "", ['a'] → "a", ['a', 'b'] → "a or b", ['a', 'b', 'c'] → "a, b or c".
|
|
34
|
+
export function orList(items) {
|
|
35
|
+
return items.length < 2 ? items.join('') : `${items.slice(0, -1).join(', ')} or ${items.at(-1)}`;
|
|
36
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const COLOR_FUNCTIONS: readonly string[];
|
|
2
|
+
/**
|
|
3
|
+
* Whether a single class is a color utility whose color isn't defined in taco's tailwind.theme.colors.js.
|
|
4
|
+
* The theme replaces Tailwind's palette, so such classes generate no CSS.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isUndefinedThemeColor(className: string): boolean;
|
|
7
|
+
/** Whether a single class is an arbitrary `text-[…]` / `bg-[…]` color literal. */
|
|
8
|
+
export declare function isArbitraryColor(className: string): boolean;
|
|
9
|
+
export declare function checkColorClass(className: string): string | undefined;
|
|
10
|
+
export declare const COLOR_VARIABLES: ReadonlySet<string>;
|
|
11
|
+
export declare function checkColorVariable(property: string, name: string): string | undefined;
|