@antebudimir/eslint-plugin-vanilla-extract 1.14.0 → 1.15.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 +111 -3
- package/dist/css-rules/prefer-theme-tokens/index.d.ts +2 -0
- package/dist/css-rules/prefer-theme-tokens/index.js +2 -0
- package/dist/css-rules/prefer-theme-tokens/rule-definition.d.ts +3 -0
- package/dist/css-rules/prefer-theme-tokens/rule-definition.js +119 -0
- package/dist/css-rules/prefer-theme-tokens/theme-contract-analyzer.d.ts +60 -0
- package/dist/css-rules/prefer-theme-tokens/theme-contract-analyzer.js +322 -0
- package/dist/css-rules/prefer-theme-tokens/theme-token-processor.d.ts +26 -0
- package/dist/css-rules/prefer-theme-tokens/theme-token-processor.js +562 -0
- package/dist/css-rules/prefer-theme-tokens/theme-token-visitor-creator.d.ts +6 -0
- package/dist/css-rules/prefer-theme-tokens/theme-token-visitor-creator.js +81 -0
- package/dist/css-rules/prefer-theme-tokens/value-evaluator.d.ts +43 -0
- package/dist/css-rules/prefer-theme-tokens/value-evaluator.js +190 -0
- package/dist/demo-bad-example.css.d.ts +4 -0
- package/dist/demo-bad-example.css.js +45 -0
- package/dist/demo-good-example.css.d.ts +5 -0
- package/dist/demo-good-example.css.js +48 -0
- package/dist/demo-theme.css.d.ts +37 -0
- package/dist/demo-theme.css.js +39 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/sample-bad.css.d.ts +26 -0
- package/dist/sample-bad.css.js +85 -0
- package/dist/sample-good.css.d.ts +27 -0
- package/dist/sample-good.css.js +94 -0
- package/dist/sample-theme.css.d.ts +37 -0
- package/dist/sample-theme.css.js +38 -0
- package/dist/sample.css.d.ts +18 -18
- package/dist/sample.css.js +90 -100
- package/package.json +5 -1
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { ValueEvaluator } from './value-evaluator.js';
|
|
3
|
+
// Color detection patterns
|
|
4
|
+
const HEX_COLOR = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
5
|
+
const RGB_COLOR = /^rgba?\s*\(/i;
|
|
6
|
+
const HSL_COLOR = /^hsla?\s*\(/i;
|
|
7
|
+
const NAMED_COLORS = new Set([
|
|
8
|
+
'black',
|
|
9
|
+
'white',
|
|
10
|
+
'red',
|
|
11
|
+
'green',
|
|
12
|
+
'blue',
|
|
13
|
+
'yellow',
|
|
14
|
+
'orange',
|
|
15
|
+
'purple',
|
|
16
|
+
'pink',
|
|
17
|
+
'gray',
|
|
18
|
+
'grey',
|
|
19
|
+
'brown',
|
|
20
|
+
'cyan',
|
|
21
|
+
'magenta',
|
|
22
|
+
'lime',
|
|
23
|
+
'navy',
|
|
24
|
+
'teal',
|
|
25
|
+
'olive',
|
|
26
|
+
'maroon',
|
|
27
|
+
'aqua',
|
|
28
|
+
'fuchsia',
|
|
29
|
+
'silver',
|
|
30
|
+
'gold',
|
|
31
|
+
'indigo',
|
|
32
|
+
'violet',
|
|
33
|
+
'tan',
|
|
34
|
+
]);
|
|
35
|
+
// CSS keywords that should be allowed
|
|
36
|
+
const ALLOWED_KEYWORDS = new Set(['transparent', 'currentcolor', 'inherit', 'initial', 'unset', 'revert']);
|
|
37
|
+
// Spacing-related properties
|
|
38
|
+
const SPACING_PROPERTIES = new Set([
|
|
39
|
+
'margin',
|
|
40
|
+
'marginTop',
|
|
41
|
+
'marginRight',
|
|
42
|
+
'marginBottom',
|
|
43
|
+
'marginLeft',
|
|
44
|
+
'marginBlock',
|
|
45
|
+
'marginBlockStart',
|
|
46
|
+
'marginBlockEnd',
|
|
47
|
+
'marginInline',
|
|
48
|
+
'marginInlineStart',
|
|
49
|
+
'marginInlineEnd',
|
|
50
|
+
'padding',
|
|
51
|
+
'paddingTop',
|
|
52
|
+
'paddingRight',
|
|
53
|
+
'paddingBottom',
|
|
54
|
+
'paddingLeft',
|
|
55
|
+
'paddingBlock',
|
|
56
|
+
'paddingBlockStart',
|
|
57
|
+
'paddingBlockEnd',
|
|
58
|
+
'paddingInline',
|
|
59
|
+
'paddingInlineStart',
|
|
60
|
+
'paddingInlineEnd',
|
|
61
|
+
'gap',
|
|
62
|
+
'rowGap',
|
|
63
|
+
'columnGap',
|
|
64
|
+
'gridGap',
|
|
65
|
+
'gridRowGap',
|
|
66
|
+
'gridColumnGap',
|
|
67
|
+
'inset',
|
|
68
|
+
'insetBlock',
|
|
69
|
+
'insetBlockStart',
|
|
70
|
+
'insetBlockEnd',
|
|
71
|
+
'insetInline',
|
|
72
|
+
'insetInlineStart',
|
|
73
|
+
'insetInlineEnd',
|
|
74
|
+
'top',
|
|
75
|
+
'right',
|
|
76
|
+
'bottom',
|
|
77
|
+
'left',
|
|
78
|
+
'width',
|
|
79
|
+
'height',
|
|
80
|
+
'minWidth',
|
|
81
|
+
'minHeight',
|
|
82
|
+
'maxWidth',
|
|
83
|
+
'maxHeight',
|
|
84
|
+
'blockSize',
|
|
85
|
+
'inlineSize',
|
|
86
|
+
'minBlockSize',
|
|
87
|
+
'minInlineSize',
|
|
88
|
+
'maxBlockSize',
|
|
89
|
+
'maxInlineSize',
|
|
90
|
+
]);
|
|
91
|
+
// Font size properties
|
|
92
|
+
const FONT_SIZE_PROPERTIES = new Set(['fontSize', 'lineHeight']);
|
|
93
|
+
// Border radius properties
|
|
94
|
+
const BORDER_RADIUS_PROPERTIES = new Set([
|
|
95
|
+
'borderRadius',
|
|
96
|
+
'borderTopLeftRadius',
|
|
97
|
+
'borderTopRightRadius',
|
|
98
|
+
'borderBottomLeftRadius',
|
|
99
|
+
'borderBottomRightRadius',
|
|
100
|
+
'borderStartStartRadius',
|
|
101
|
+
'borderStartEndRadius',
|
|
102
|
+
'borderEndStartRadius',
|
|
103
|
+
'borderEndRadius',
|
|
104
|
+
]);
|
|
105
|
+
// Border width properties
|
|
106
|
+
const BORDER_WIDTH_PROPERTIES = new Set([
|
|
107
|
+
'borderWidth',
|
|
108
|
+
'borderTopWidth',
|
|
109
|
+
'borderRightWidth',
|
|
110
|
+
'borderBottomWidth',
|
|
111
|
+
'borderLeftWidth',
|
|
112
|
+
'borderBlockWidth',
|
|
113
|
+
'borderBlockStartWidth',
|
|
114
|
+
'borderBlockEndWidth',
|
|
115
|
+
'borderInlineWidth',
|
|
116
|
+
'borderInlineStartWidth',
|
|
117
|
+
'borderInlineEndWidth',
|
|
118
|
+
'outlineWidth',
|
|
119
|
+
'columnRuleWidth',
|
|
120
|
+
// Shorthands that include width
|
|
121
|
+
'border',
|
|
122
|
+
'borderTop',
|
|
123
|
+
'borderRight',
|
|
124
|
+
'borderBottom',
|
|
125
|
+
'borderLeft',
|
|
126
|
+
'borderBlock',
|
|
127
|
+
'borderBlockStart',
|
|
128
|
+
'borderBlockEnd',
|
|
129
|
+
'borderInline',
|
|
130
|
+
'borderInlineStart',
|
|
131
|
+
'borderInlineEnd',
|
|
132
|
+
'outline',
|
|
133
|
+
]);
|
|
134
|
+
// Shadow properties
|
|
135
|
+
const SHADOW_PROPERTIES = new Set(['boxShadow', 'textShadow', 'filter', 'backdropFilter']);
|
|
136
|
+
// Z-index property
|
|
137
|
+
const Z_INDEX_PROPERTIES = new Set(['zIndex']);
|
|
138
|
+
// Opacity property
|
|
139
|
+
const OPACITY_PROPERTIES = new Set(['opacity']);
|
|
140
|
+
// Font weight properties
|
|
141
|
+
const FONT_WEIGHT_PROPERTIES = new Set(['fontWeight']);
|
|
142
|
+
// Transition and animation properties
|
|
143
|
+
const TRANSITION_PROPERTIES = new Set([
|
|
144
|
+
'transition',
|
|
145
|
+
'transitionDelay',
|
|
146
|
+
'transitionDuration',
|
|
147
|
+
'transitionTimingFunction',
|
|
148
|
+
'animation',
|
|
149
|
+
'animationDelay',
|
|
150
|
+
'animationDuration',
|
|
151
|
+
'animationTimingFunction',
|
|
152
|
+
]);
|
|
153
|
+
// Color properties
|
|
154
|
+
const COLOR_PROPERTIES = new Set([
|
|
155
|
+
'color',
|
|
156
|
+
'backgroundColor',
|
|
157
|
+
'borderColor',
|
|
158
|
+
'borderTopColor',
|
|
159
|
+
'borderRightColor',
|
|
160
|
+
'borderBottomColor',
|
|
161
|
+
'borderLeftColor',
|
|
162
|
+
'borderBlockStartColor',
|
|
163
|
+
'borderBlockEndColor',
|
|
164
|
+
'borderInlineStartColor',
|
|
165
|
+
'borderInlineEndColor',
|
|
166
|
+
'outlineColor',
|
|
167
|
+
'textDecorationColor',
|
|
168
|
+
'caretColor',
|
|
169
|
+
'columnRuleColor',
|
|
170
|
+
'fill',
|
|
171
|
+
'stroke',
|
|
172
|
+
]);
|
|
173
|
+
const isHardCodedColor = (value) => {
|
|
174
|
+
if (ALLOWED_KEYWORDS.has(value.toLowerCase())) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return (HEX_COLOR.test(value) || RGB_COLOR.test(value) || HSL_COLOR.test(value) || NAMED_COLORS.has(value.toLowerCase()));
|
|
178
|
+
};
|
|
179
|
+
const hasNumericValue = (value) => {
|
|
180
|
+
// Match numeric values with units (e.g., 10px, 1rem, 2em, 50%, etc.)
|
|
181
|
+
return /\d+(\.\d+)?(px|rem|em|%|vh|vw|vmin|vmax|ch|ex)/.test(value);
|
|
182
|
+
};
|
|
183
|
+
const hasShadowValue = (value) => {
|
|
184
|
+
// Match shadow values (e.g., "0 4px 6px rgba(...)", "inset 0 1px 2px #000")
|
|
185
|
+
// Also matches filter functions like blur(), drop-shadow()
|
|
186
|
+
return /(\d+px|\d+rem|rgba?\(|hsla?\(|#[0-9a-f]{3,8}|blur\(|drop-shadow\(|brightness\(|contrast\()/.test(value.toLowerCase());
|
|
187
|
+
};
|
|
188
|
+
const hasZIndexValue = (value) => {
|
|
189
|
+
// Match numeric z-index values
|
|
190
|
+
return /^-?\d+$/.test(value.trim());
|
|
191
|
+
};
|
|
192
|
+
const hasOpacityValue = (value) => {
|
|
193
|
+
// Match opacity values (0-1 or percentages)
|
|
194
|
+
return /^(0?\.\d+|1(\.0+)?|\d+%)$/.test(value.trim());
|
|
195
|
+
};
|
|
196
|
+
const hasFontWeightValue = (value) => {
|
|
197
|
+
// Match font weight values (numeric or named)
|
|
198
|
+
const namedWeights = ['normal', 'bold', 'bolder', 'lighter'];
|
|
199
|
+
const trimmed = value.trim().toLowerCase();
|
|
200
|
+
return /^[1-9]00$/.test(trimmed) || namedWeights.includes(trimmed);
|
|
201
|
+
};
|
|
202
|
+
const hasTransitionValue = (value) => {
|
|
203
|
+
// Match transition/animation values (e.g., "0.3s", "200ms", "ease-in-out", "cubic-bezier(...)")
|
|
204
|
+
return /(^\d+(\.\d+)?(s|ms)$|ease|linear|cubic-bezier\(|steps\()/.test(value.toLowerCase());
|
|
205
|
+
};
|
|
206
|
+
const isAllowedValue = (value, allowedValues) => {
|
|
207
|
+
const trimmed = value.trim();
|
|
208
|
+
return (allowedValues.has(trimmed) ||
|
|
209
|
+
allowedValues.has(trimmed.toLowerCase()) ||
|
|
210
|
+
trimmed === '0' ||
|
|
211
|
+
trimmed === 'auto' ||
|
|
212
|
+
trimmed === 'none' ||
|
|
213
|
+
trimmed === 'inherit' ||
|
|
214
|
+
trimmed === 'initial' ||
|
|
215
|
+
trimmed === 'unset' ||
|
|
216
|
+
/^\d+(\.\d+)?%$/.test(trimmed)); // Allow percentages
|
|
217
|
+
};
|
|
218
|
+
const normalizePropertyName = (name) => {
|
|
219
|
+
// Convert kebab-case to camelCase
|
|
220
|
+
return name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* Recursively processes a vanilla-extract style object and reports hard-coded values
|
|
224
|
+
* that should use theme tokens instead.
|
|
225
|
+
*/
|
|
226
|
+
export const processThemeTokensInStyleObject = (context, node, options, analyzer) => {
|
|
227
|
+
const { checkColors = true, checkSpacing = true, checkFontSizes = true, checkBorderRadius = true, checkBorderWidths = true, checkShadows = true, checkZIndex = true, checkOpacity = true, checkFontWeights = true, checkTransitions = true, allowedValues = [], allowedProperties = [], autoFix = false, checkHelperFunctions = false, } = options;
|
|
228
|
+
const evaluator = new ValueEvaluator();
|
|
229
|
+
const allowedValuesSet = new Set(allowedValues);
|
|
230
|
+
const allowedPropertiesSet = new Set([...allowedProperties.map(normalizePropertyName), ...allowedProperties]);
|
|
231
|
+
for (const property of node.properties) {
|
|
232
|
+
if (property.type !== AST_NODE_TYPES.Property)
|
|
233
|
+
continue;
|
|
234
|
+
// Determine property name
|
|
235
|
+
let propertyName = null;
|
|
236
|
+
if (property.key.type === AST_NODE_TYPES.Identifier) {
|
|
237
|
+
propertyName = property.key.name;
|
|
238
|
+
}
|
|
239
|
+
else if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
|
|
240
|
+
propertyName = property.key.value;
|
|
241
|
+
}
|
|
242
|
+
// Recurse into nested containers (@media, selectors, etc.)
|
|
243
|
+
if (propertyName && (propertyName === '@media' || propertyName === 'selectors' || propertyName.startsWith('@'))) {
|
|
244
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
245
|
+
for (const nestedProp of property.value.properties) {
|
|
246
|
+
if (nestedProp.type === AST_NODE_TYPES.Property &&
|
|
247
|
+
nestedProp.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
248
|
+
processThemeTokensInStyleObject(context, nestedProp.value, options, analyzer);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (!propertyName)
|
|
255
|
+
continue;
|
|
256
|
+
// Skip if property is in allowed list
|
|
257
|
+
if (allowedPropertiesSet.has(propertyName) || allowedPropertiesSet.has(normalizePropertyName(propertyName))) {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
// Check if property value is a literal (string or number)
|
|
261
|
+
if (property.value.type === AST_NODE_TYPES.Literal &&
|
|
262
|
+
(typeof property.value.value === 'string' || typeof property.value.value === 'number')) {
|
|
263
|
+
const value = String(property.value.value);
|
|
264
|
+
// Skip if value is in allowed list
|
|
265
|
+
if (isAllowedValue(value, allowedValuesSet)) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
// Check for hard-coded colors
|
|
269
|
+
if (checkColors && COLOR_PROPERTIES.has(propertyName) && isHardCodedColor(value)) {
|
|
270
|
+
reportHardCodedValue(context, property.value, value, 'color', propertyName, analyzer, autoFix);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
// Check for hard-coded spacing
|
|
274
|
+
if (checkSpacing && SPACING_PROPERTIES.has(propertyName) && hasNumericValue(value)) {
|
|
275
|
+
reportHardCodedValue(context, property.value, value, 'spacing', propertyName, analyzer, autoFix);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
// Check for hard-coded font sizes
|
|
279
|
+
if (checkFontSizes && FONT_SIZE_PROPERTIES.has(propertyName) && hasNumericValue(value)) {
|
|
280
|
+
reportHardCodedValue(context, property.value, value, 'fontSize', propertyName, analyzer, autoFix);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
// Check for hard-coded border radius
|
|
284
|
+
if (checkBorderRadius && BORDER_RADIUS_PROPERTIES.has(propertyName) && hasNumericValue(value)) {
|
|
285
|
+
reportHardCodedValue(context, property.value, value, 'borderRadius', propertyName, analyzer, autoFix);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
// Check for hard-coded border widths
|
|
289
|
+
if (checkBorderWidths && BORDER_WIDTH_PROPERTIES.has(propertyName) && hasNumericValue(value)) {
|
|
290
|
+
reportHardCodedValue(context, property.value, value, 'borderWidth', propertyName, analyzer, autoFix);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
// Check for hard-coded shadows
|
|
294
|
+
if (checkShadows && SHADOW_PROPERTIES.has(propertyName) && hasShadowValue(value)) {
|
|
295
|
+
reportHardCodedValue(context, property.value, value, 'shadow', propertyName, analyzer, autoFix);
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
// Check for hard-coded z-index
|
|
299
|
+
if (checkZIndex && Z_INDEX_PROPERTIES.has(propertyName) && hasZIndexValue(value)) {
|
|
300
|
+
reportHardCodedValue(context, property.value, value, 'zIndex', propertyName, analyzer, autoFix);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
// Check for hard-coded opacity
|
|
304
|
+
if (checkOpacity && OPACITY_PROPERTIES.has(propertyName) && hasOpacityValue(value)) {
|
|
305
|
+
reportHardCodedValue(context, property.value, value, 'opacity', propertyName, analyzer, autoFix);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
// Check for hard-coded font weights
|
|
309
|
+
if (checkFontWeights && FONT_WEIGHT_PROPERTIES.has(propertyName) && hasFontWeightValue(value)) {
|
|
310
|
+
reportHardCodedValue(context, property.value, value, 'fontWeight', propertyName, analyzer, autoFix);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
// Check for hard-coded transitions
|
|
314
|
+
if (checkTransitions && TRANSITION_PROPERTIES.has(propertyName) && hasTransitionValue(value)) {
|
|
315
|
+
reportHardCodedValue(context, property.value, value, 'transition', propertyName, analyzer, autoFix);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
// Check if property value is a TemplateLiteral (e.g., `${rem(4)} ${rem(8)}`)
|
|
319
|
+
if (checkHelperFunctions && property.value.type === AST_NODE_TYPES.TemplateLiteral) {
|
|
320
|
+
// Get the source code of the template literal
|
|
321
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
322
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
323
|
+
const templateText = sourceCode.getText(property.value);
|
|
324
|
+
// Try to evaluate it
|
|
325
|
+
const evaluatedValue = evaluator.evaluate(templateText);
|
|
326
|
+
if (evaluatedValue) {
|
|
327
|
+
// Check for hard-coded colors
|
|
328
|
+
if (checkColors && COLOR_PROPERTIES.has(propertyName) && isHardCodedColor(evaluatedValue)) {
|
|
329
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'color', propertyName, analyzer, autoFix);
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
// Check for hard-coded spacing
|
|
333
|
+
if (checkSpacing && SPACING_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
334
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'spacing', propertyName, analyzer, autoFix);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
// Check for hard-coded font sizes
|
|
338
|
+
if (checkFontSizes && FONT_SIZE_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
339
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'fontSize', propertyName, analyzer, autoFix);
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
// Check for hard-coded border radius
|
|
343
|
+
if (checkBorderRadius && BORDER_RADIUS_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
344
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'borderRadius', propertyName, analyzer, autoFix);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
// Check for hard-coded border widths
|
|
348
|
+
if (checkBorderWidths && BORDER_WIDTH_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
349
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'borderWidth', propertyName, analyzer, autoFix);
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
// Check for hard-coded shadows
|
|
353
|
+
if (checkShadows && SHADOW_PROPERTIES.has(propertyName) && hasShadowValue(evaluatedValue)) {
|
|
354
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'shadow', propertyName, analyzer, autoFix);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
// Check for hard-coded z-index
|
|
358
|
+
if (checkZIndex && Z_INDEX_PROPERTIES.has(propertyName) && hasZIndexValue(evaluatedValue)) {
|
|
359
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'zIndex', propertyName, analyzer, autoFix);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
// Check for hard-coded opacity
|
|
363
|
+
if (checkOpacity && OPACITY_PROPERTIES.has(propertyName) && hasOpacityValue(evaluatedValue)) {
|
|
364
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'opacity', propertyName, analyzer, autoFix);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
// Check for hard-coded font weights
|
|
368
|
+
if (checkFontWeights && FONT_WEIGHT_PROPERTIES.has(propertyName) && hasFontWeightValue(evaluatedValue)) {
|
|
369
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'fontWeight', propertyName, analyzer, autoFix);
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
// Check for hard-coded transitions
|
|
373
|
+
if (checkTransitions && TRANSITION_PROPERTIES.has(propertyName) && hasTransitionValue(evaluatedValue)) {
|
|
374
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'transition', propertyName, analyzer, autoFix);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// Check if property value is a CallExpression (e.g., rem(48), clsx(...))
|
|
379
|
+
if (checkHelperFunctions && property.value.type === AST_NODE_TYPES.CallExpression) {
|
|
380
|
+
// Get the source code of the call expression
|
|
381
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
382
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
383
|
+
const callExpressionText = sourceCode.getText(property.value);
|
|
384
|
+
// Try to evaluate it
|
|
385
|
+
const evaluatedValue = evaluator.evaluate(callExpressionText);
|
|
386
|
+
if (evaluatedValue) {
|
|
387
|
+
// Check for hard-coded colors
|
|
388
|
+
if (checkColors && COLOR_PROPERTIES.has(propertyName) && isHardCodedColor(evaluatedValue)) {
|
|
389
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'color', propertyName, analyzer, autoFix);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
// Check for hard-coded spacing
|
|
393
|
+
if (checkSpacing && SPACING_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
394
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'spacing', propertyName, analyzer, autoFix);
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
// Check for hard-coded font sizes
|
|
398
|
+
if (checkFontSizes && FONT_SIZE_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
399
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'fontSize', propertyName, analyzer, autoFix);
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
// Check for hard-coded border radius
|
|
403
|
+
if (checkBorderRadius && BORDER_RADIUS_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
404
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'borderRadius', propertyName, analyzer, autoFix);
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
// Check for hard-coded border widths
|
|
408
|
+
if (checkBorderWidths && BORDER_WIDTH_PROPERTIES.has(propertyName) && hasNumericValue(evaluatedValue)) {
|
|
409
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'borderWidth', propertyName, analyzer, autoFix);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
// Check for hard-coded shadows
|
|
413
|
+
if (checkShadows && SHADOW_PROPERTIES.has(propertyName) && hasShadowValue(evaluatedValue)) {
|
|
414
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'shadow', propertyName, analyzer, autoFix);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
// Check for hard-coded z-index
|
|
418
|
+
if (checkZIndex && Z_INDEX_PROPERTIES.has(propertyName) && hasZIndexValue(evaluatedValue)) {
|
|
419
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'zIndex', propertyName, analyzer, autoFix);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
// Check for hard-coded opacity
|
|
423
|
+
if (checkOpacity && OPACITY_PROPERTIES.has(propertyName) && hasOpacityValue(evaluatedValue)) {
|
|
424
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'opacity', propertyName, analyzer, autoFix);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
// Check for hard-coded font weights
|
|
428
|
+
if (checkFontWeights && FONT_WEIGHT_PROPERTIES.has(propertyName) && hasFontWeightValue(evaluatedValue)) {
|
|
429
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'fontWeight', propertyName, analyzer, autoFix);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
// Check for hard-coded transitions
|
|
433
|
+
if (checkTransitions && TRANSITION_PROPERTIES.has(propertyName) && hasTransitionValue(evaluatedValue)) {
|
|
434
|
+
reportHardCodedValue(context, property.value, evaluatedValue, 'transition', propertyName, analyzer, autoFix);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
// Recurse into nested objects
|
|
439
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
440
|
+
processThemeTokensInStyleObject(context, property.value, options, analyzer);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
/**
|
|
445
|
+
* Report a hard-coded value and suggest theme tokens
|
|
446
|
+
*/
|
|
447
|
+
const reportHardCodedValue = (context, node, value, category, propertyName, analyzer, autoFix) => {
|
|
448
|
+
// Find matching tokens from the theme contract
|
|
449
|
+
const matchingTokens = analyzer.findMatchingTokens(value, category);
|
|
450
|
+
if (matchingTokens.length > 0) {
|
|
451
|
+
// We have exact matches - suggest the specific token(s)
|
|
452
|
+
const primaryToken = matchingTokens[0];
|
|
453
|
+
if (!primaryToken)
|
|
454
|
+
return;
|
|
455
|
+
const tokenPath = primaryToken.tokenPath;
|
|
456
|
+
const suggestions = matchingTokens.map((token) => ({
|
|
457
|
+
messageId: 'replaceWithToken',
|
|
458
|
+
data: { tokenPath: token.tokenPath },
|
|
459
|
+
fix: (fixer) => fixer.replaceText(node, token.tokenPath),
|
|
460
|
+
}));
|
|
461
|
+
const reportDescriptor = {
|
|
462
|
+
node: node,
|
|
463
|
+
messageId: 'hardCodedValueWithToken',
|
|
464
|
+
data: {
|
|
465
|
+
value,
|
|
466
|
+
property: propertyName,
|
|
467
|
+
tokenPath,
|
|
468
|
+
},
|
|
469
|
+
suggest: suggestions,
|
|
470
|
+
};
|
|
471
|
+
// Add fix if autoFix is enabled
|
|
472
|
+
// Only auto-fix when there's exactly one match (unambiguous)
|
|
473
|
+
// For multiple matches, user must manually select from suggestions
|
|
474
|
+
if (autoFix && matchingTokens.length === 1) {
|
|
475
|
+
reportDescriptor.fix = (fixer) => fixer.replaceText(node, tokenPath);
|
|
476
|
+
}
|
|
477
|
+
context.report(reportDescriptor);
|
|
478
|
+
}
|
|
479
|
+
else if (analyzer.hasContracts()) {
|
|
480
|
+
// Theme contract exists but no exact match - give generic suggestion
|
|
481
|
+
const categoryHint = getCategoryHint(category, analyzer.getVariableName());
|
|
482
|
+
context.report({
|
|
483
|
+
node: node,
|
|
484
|
+
messageId: 'hardCodedValueGeneric',
|
|
485
|
+
data: {
|
|
486
|
+
value,
|
|
487
|
+
property: propertyName,
|
|
488
|
+
categoryHint,
|
|
489
|
+
},
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
// No theme contract loaded - give very generic message
|
|
494
|
+
context.report({
|
|
495
|
+
node: node,
|
|
496
|
+
messageId: 'hardCodedValueNoContract',
|
|
497
|
+
data: {
|
|
498
|
+
value,
|
|
499
|
+
property: propertyName,
|
|
500
|
+
category: getCategoryName(category),
|
|
501
|
+
},
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
/**
|
|
506
|
+
* Get a helpful hint for the category
|
|
507
|
+
*/
|
|
508
|
+
const getCategoryHint = (category, variableName) => {
|
|
509
|
+
switch (category) {
|
|
510
|
+
case 'color':
|
|
511
|
+
return `${variableName}.colors.*`;
|
|
512
|
+
case 'spacing':
|
|
513
|
+
return `${variableName}.spacing.*`;
|
|
514
|
+
case 'fontSize':
|
|
515
|
+
return `${variableName}.fontSizes.*`;
|
|
516
|
+
case 'borderRadius':
|
|
517
|
+
return `${variableName}.radii.*`;
|
|
518
|
+
case 'borderWidth':
|
|
519
|
+
return `${variableName}.borderWidths.*`;
|
|
520
|
+
case 'shadow':
|
|
521
|
+
return `${variableName}.shadows.*`;
|
|
522
|
+
case 'zIndex':
|
|
523
|
+
return `${variableName}.zIndex.*`;
|
|
524
|
+
case 'opacity':
|
|
525
|
+
return `${variableName}.opacity.*`;
|
|
526
|
+
case 'fontWeight':
|
|
527
|
+
return `${variableName}.fontWeights.*`;
|
|
528
|
+
case 'transition':
|
|
529
|
+
return `${variableName}.transitions.*`;
|
|
530
|
+
default:
|
|
531
|
+
return `${variableName}.*`;
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
/**
|
|
535
|
+
* Get a readable category name
|
|
536
|
+
*/
|
|
537
|
+
const getCategoryName = (category) => {
|
|
538
|
+
switch (category) {
|
|
539
|
+
case 'color':
|
|
540
|
+
return 'color';
|
|
541
|
+
case 'spacing':
|
|
542
|
+
return 'spacing';
|
|
543
|
+
case 'fontSize':
|
|
544
|
+
return 'font size';
|
|
545
|
+
case 'borderRadius':
|
|
546
|
+
return 'border radius';
|
|
547
|
+
case 'borderWidth':
|
|
548
|
+
return 'border width';
|
|
549
|
+
case 'shadow':
|
|
550
|
+
return 'shadow';
|
|
551
|
+
case 'zIndex':
|
|
552
|
+
return 'z-index';
|
|
553
|
+
case 'opacity':
|
|
554
|
+
return 'opacity';
|
|
555
|
+
case 'fontWeight':
|
|
556
|
+
return 'font weight';
|
|
557
|
+
case 'transition':
|
|
558
|
+
return 'transition';
|
|
559
|
+
default:
|
|
560
|
+
return 'value';
|
|
561
|
+
}
|
|
562
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { type ThemeTokenOptions } from './theme-token-processor.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates ESLint rule visitors for the prefer-theme-tokens rule
|
|
5
|
+
*/
|
|
6
|
+
export declare const createThemeTokenVisitors: (context: Rule.RuleContext, options: ThemeTokenOptions) => Rule.RuleListener;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
|
|
4
|
+
import { ReferenceTracker, createReferenceTrackingVisitor } from '../shared-utils/reference-tracker.js';
|
|
5
|
+
import { processStyleNode } from '../shared-utils/style-node-processor.js';
|
|
6
|
+
import { ThemeContractAnalyzer } from './theme-contract-analyzer.js';
|
|
7
|
+
import { processThemeTokensInStyleObject } from './theme-token-processor.js';
|
|
8
|
+
/**
|
|
9
|
+
* Creates ESLint rule visitors for the prefer-theme-tokens rule
|
|
10
|
+
*/
|
|
11
|
+
export const createThemeTokenVisitors = (context, options) => {
|
|
12
|
+
const tracker = new ReferenceTracker();
|
|
13
|
+
const trackingVisitor = createReferenceTrackingVisitor(tracker);
|
|
14
|
+
// Initialize the theme contract analyzer
|
|
15
|
+
const analyzer = new ThemeContractAnalyzer();
|
|
16
|
+
// Set rem base if provided
|
|
17
|
+
if (options.remBase) {
|
|
18
|
+
analyzer.setRemBase(options.remBase);
|
|
19
|
+
}
|
|
20
|
+
// Load theme contracts if provided
|
|
21
|
+
const themeContracts = options.themeContracts || [];
|
|
22
|
+
if (themeContracts.length > 0) {
|
|
23
|
+
// Use getCwd() to get project root, or fallback to linted file's directory
|
|
24
|
+
const baseDir = context.getCwd ? context.getCwd() : path.dirname(context.filename || context.getFilename());
|
|
25
|
+
themeContracts.forEach((contractPath) => {
|
|
26
|
+
analyzer.loadThemeContract(contractPath, baseDir);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const process = (context, object) => processThemeTokensInStyleObject(context, object, options, analyzer);
|
|
30
|
+
return {
|
|
31
|
+
...trackingVisitor,
|
|
32
|
+
// Call the tracking visitor's ImportDeclaration handler
|
|
33
|
+
ImportDeclaration(node) {
|
|
34
|
+
if (trackingVisitor.ImportDeclaration) {
|
|
35
|
+
trackingVisitor.ImportDeclaration(node);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
CallExpression(node) {
|
|
39
|
+
if (node.callee.type !== AST_NODE_TYPES.Identifier)
|
|
40
|
+
return;
|
|
41
|
+
const functionName = node.callee.name;
|
|
42
|
+
if (!tracker.isTrackedFunction(functionName))
|
|
43
|
+
return;
|
|
44
|
+
const originalName = tracker.getOriginalName(functionName);
|
|
45
|
+
if (!originalName)
|
|
46
|
+
return;
|
|
47
|
+
switch (originalName) {
|
|
48
|
+
case 'fontFace':
|
|
49
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
50
|
+
process(context, node.arguments[0]);
|
|
51
|
+
}
|
|
52
|
+
break;
|
|
53
|
+
case 'globalFontFace':
|
|
54
|
+
if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
55
|
+
process(context, node.arguments[1]);
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case 'style':
|
|
59
|
+
case 'styleVariants':
|
|
60
|
+
case 'keyframes':
|
|
61
|
+
if (node.arguments.length > 0) {
|
|
62
|
+
processStyleNode(context, node.arguments[0], (context, object) => process(context, object));
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case 'globalStyle':
|
|
66
|
+
if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
67
|
+
process(context, node.arguments[1]);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
case 'recipe':
|
|
71
|
+
case 'compoundVariant':
|
|
72
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
73
|
+
processRecipeProperties(context, node.arguments[0], (context, object) => process(context, object));
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
default:
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
};
|