@antebudimir/eslint-plugin-vanilla-extract 1.14.0 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -3
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +8 -2
- package/dist/css-rules/no-empty-blocks/recipe-processor.js +23 -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/{no-px-unit/visitor-creator.js → prefer-theme-tokens/theme-token-visitor-creator.js} +36 -10
- 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/css-rules/shared-utils/empty-object-processor.d.ts +5 -0
- package/dist/css-rules/shared-utils/empty-object-processor.js +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/package.json +6 -1
- package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.d.ts +0 -2
- package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.js +0 -55
- package/dist/css-rules/no-px-unit/processor.d.ts +0 -3
- package/dist/css-rules/no-px-unit/processor.js +0 -93
- package/dist/css-rules/no-px-unit/visitor-creator.d.ts +0 -2
- package/dist/sample.css.d.ts +0 -75
- package/dist/sample.css.js +0 -540
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { ValueEvaluator } from './value-evaluator.js';
|
|
4
|
+
/**
|
|
5
|
+
* Analyzes theme contract files to extract token values and their paths
|
|
6
|
+
*/
|
|
7
|
+
export class ThemeContractAnalyzer {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.contracts = new Map();
|
|
10
|
+
this.evaluator = new ValueEvaluator();
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Set rem base for evaluation (default: 16)
|
|
14
|
+
*/
|
|
15
|
+
setRemBase(base) {
|
|
16
|
+
this.evaluator.setRemBase(base);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Load and analyze a theme contract file
|
|
20
|
+
*/
|
|
21
|
+
loadThemeContract(filePath, baseDir) {
|
|
22
|
+
const absolutePath = path.resolve(baseDir, filePath);
|
|
23
|
+
if (!fs.existsSync(absolutePath)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const content = fs.readFileSync(absolutePath, 'utf-8');
|
|
28
|
+
const contract = this.parseThemeContract(content);
|
|
29
|
+
if (contract) {
|
|
30
|
+
this.contracts.set(filePath, contract);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Silently fail - theme file might not be available during linting
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse theme contract from source code
|
|
39
|
+
*/
|
|
40
|
+
parseThemeContract(content) {
|
|
41
|
+
const tokens = new Map();
|
|
42
|
+
let variableName = 'theme';
|
|
43
|
+
// Try to extract theme variable name from exports
|
|
44
|
+
// Prefer the second identifier in destructuring like: export const [themeClass, vars] = createTheme(...)
|
|
45
|
+
const destructureMatch = content.match(/export\s+(?:const|let)\s*\[\s*(\w+)\s*,\s*(\w+)\s*\]\s*=/);
|
|
46
|
+
if (destructureMatch?.[2]) {
|
|
47
|
+
variableName = destructureMatch[2];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// Fallback: capture single identifier export
|
|
51
|
+
const exportMatch = content.match(/export\s+(?:const|let)\s+(\w+)\s*=|export\s+(?:const|let)\s*\[\s*(\w+)\s*\]/);
|
|
52
|
+
const candidate = exportMatch?.[1] || exportMatch?.[2];
|
|
53
|
+
if (candidate) {
|
|
54
|
+
variableName = candidate;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Parse createTheme (one-arg), createTheme(contract, values), createGlobalTheme, or contract definitions
|
|
58
|
+
const createThemeTwoArg = content.match(/createTheme\s*\(\s*([A-Za-z_$][\w$]*)\s*,\s*({[\s\S]*?})\s*\)/);
|
|
59
|
+
const themeObjectMatch = content.match(/createTheme\s*\(\s*({[\s\S]*?})\s*\)/);
|
|
60
|
+
const globalThemeMatch = content.match(/createGlobalTheme\s*\([^,]+,\s*({[\s\S]*?})\s*\)/);
|
|
61
|
+
const contractMatch = content.match(/createThemeContract\s*\(\s*({[\s\S]*?})\s*\)/);
|
|
62
|
+
const themeContent = createThemeTwoArg?.[2] || themeObjectMatch?.[1] || globalThemeMatch?.[1] || contractMatch?.[1];
|
|
63
|
+
if (!themeContent) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// If using createTheme(contractIdentifier, values), prefer the contract identifier for variable name (e.g., 'theme')
|
|
67
|
+
if (createThemeTwoArg?.[1]) {
|
|
68
|
+
variableName = createThemeTwoArg[1];
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Try to extract theme variable name from exports
|
|
72
|
+
// Prefer the second identifier in destructuring like: export const [themeClass, vars] = createTheme(...)
|
|
73
|
+
const destructureMatch = content.match(/export\s+(?:const|let)\s*\[\s*(\w+)\s*,\s*(\w+)\s*\]\s*=/);
|
|
74
|
+
if (destructureMatch?.[2]) {
|
|
75
|
+
variableName = destructureMatch[2];
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// Fallback: capture single identifier export
|
|
79
|
+
const exportMatch = content.match(/export\s+(?:const|let)\s+(\w+)\s*=|export\s+(?:const|let)\s*\[\s*(\w+)\s*\]/);
|
|
80
|
+
const candidate = exportMatch?.[1] || exportMatch?.[2];
|
|
81
|
+
if (candidate) {
|
|
82
|
+
variableName = candidate;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Parse the theme object structure
|
|
87
|
+
this.parseThemeObject(themeContent, variableName, tokens);
|
|
88
|
+
return { tokens, variableName };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Parse theme object and extract token values
|
|
92
|
+
*/
|
|
93
|
+
parseThemeObject(content, variableName, tokens, pathPrefix = '') {
|
|
94
|
+
// Remove comments
|
|
95
|
+
const cleaned = content.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '');
|
|
96
|
+
// First, extract and process nested objects to avoid matching their contents
|
|
97
|
+
const objectRegex = /['"]?(\w+)['"]?\s*:\s*{([^{}]*(?:{[^{}]*}[^{}]*)*?)}/g;
|
|
98
|
+
const nestedObjects = [];
|
|
99
|
+
let objectMatch;
|
|
100
|
+
while ((objectMatch = objectRegex.exec(cleaned)) !== null) {
|
|
101
|
+
const key = objectMatch[1];
|
|
102
|
+
const nestedContent = objectMatch[2];
|
|
103
|
+
if (key && nestedContent) {
|
|
104
|
+
nestedObjects.push({ key, content: nestedContent });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Remove nested objects from content to avoid double-matching
|
|
108
|
+
let contentWithoutNested = cleaned;
|
|
109
|
+
nestedObjects.forEach(({ content }) => {
|
|
110
|
+
contentWithoutNested = contentWithoutNested.replace(content, '');
|
|
111
|
+
});
|
|
112
|
+
// Match key-value pairs - both string literals and expressions
|
|
113
|
+
// String literals: key: 'value' or key: "value" or key: `value`
|
|
114
|
+
const stringLiteralRegex = /['"]?(\w+)['"]?\s*:\s*(['"`])(.*?)\2/g;
|
|
115
|
+
// Expressions: key: rem(16) or key: clsx(...)
|
|
116
|
+
const expressionRegex = /['"]?(\w+)['"]?\s*:\s*([^,}\n]+?)(?=[,}\n])/g;
|
|
117
|
+
let match;
|
|
118
|
+
// First pass: extract string literals (only from content without nested objects)
|
|
119
|
+
while ((match = stringLiteralRegex.exec(contentWithoutNested)) !== null) {
|
|
120
|
+
const key = match[1];
|
|
121
|
+
const value = match[3];
|
|
122
|
+
if (!key || !value)
|
|
123
|
+
continue;
|
|
124
|
+
const tokenPath = pathPrefix ? `${pathPrefix}.${key}` : key;
|
|
125
|
+
const fullPath = `${variableName}.${tokenPath}`;
|
|
126
|
+
// Determine category based on key name and value
|
|
127
|
+
const category = this.categorizeToken(tokenPath, value);
|
|
128
|
+
// Normalize the value
|
|
129
|
+
const normalizedValue = this.normalizeValue(value);
|
|
130
|
+
if (normalizedValue) {
|
|
131
|
+
const existing = tokens.get(normalizedValue) || [];
|
|
132
|
+
existing.push({ value: normalizedValue, tokenPath: fullPath, category });
|
|
133
|
+
tokens.set(normalizedValue, existing);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Second pass: extract and evaluate expressions (only from content without nested objects)
|
|
137
|
+
let exprMatch;
|
|
138
|
+
while ((exprMatch = expressionRegex.exec(contentWithoutNested)) !== null) {
|
|
139
|
+
const key = exprMatch[1];
|
|
140
|
+
let value = exprMatch[2];
|
|
141
|
+
if (!key || !value)
|
|
142
|
+
continue;
|
|
143
|
+
value = value.trim();
|
|
144
|
+
// Skip if it's a string literal (already processed)
|
|
145
|
+
if (value.startsWith('"') || value.startsWith("'") || value.startsWith('`')) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
// Skip nested objects
|
|
149
|
+
if (value.startsWith('{')) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const tokenPath = pathPrefix ? `${pathPrefix}.${key}` : key;
|
|
153
|
+
const fullPath = `${variableName}.${tokenPath}`;
|
|
154
|
+
// Try to evaluate the expression
|
|
155
|
+
const evaluatedValue = this.evaluator.evaluate(value);
|
|
156
|
+
if (!evaluatedValue)
|
|
157
|
+
continue;
|
|
158
|
+
// Determine category based on key name and evaluated value
|
|
159
|
+
const category = this.categorizeToken(tokenPath, evaluatedValue);
|
|
160
|
+
// Normalize the evaluated value
|
|
161
|
+
const normalizedValue = this.normalizeValue(evaluatedValue);
|
|
162
|
+
if (normalizedValue) {
|
|
163
|
+
const existing = tokens.get(normalizedValue) || [];
|
|
164
|
+
existing.push({ value: normalizedValue, tokenPath: fullPath, category });
|
|
165
|
+
tokens.set(normalizedValue, existing);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Process nested objects (already extracted earlier)
|
|
169
|
+
nestedObjects.forEach(({ key, content }) => {
|
|
170
|
+
const newPrefix = pathPrefix ? `${pathPrefix}.${key}` : key;
|
|
171
|
+
this.parseThemeObject(content, variableName, tokens, newPrefix);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Categorize a token based on its path and value
|
|
176
|
+
*/
|
|
177
|
+
categorizeToken(tokenPath, value) {
|
|
178
|
+
const lowerPath = tokenPath.toLowerCase();
|
|
179
|
+
// Check by path
|
|
180
|
+
if (lowerPath.includes('color') || lowerPath.includes('bg') || lowerPath.includes('background')) {
|
|
181
|
+
return 'color';
|
|
182
|
+
}
|
|
183
|
+
if (lowerPath.includes('spacing') || lowerPath.includes('space') || lowerPath.includes('gap')) {
|
|
184
|
+
return 'spacing';
|
|
185
|
+
}
|
|
186
|
+
if (lowerPath.includes('fontsize') || lowerPath.includes('font') && lowerPath.includes('size')) {
|
|
187
|
+
return 'fontSize';
|
|
188
|
+
}
|
|
189
|
+
if (lowerPath.includes('radius') || lowerPath.includes('radii')) {
|
|
190
|
+
return 'borderRadius';
|
|
191
|
+
}
|
|
192
|
+
if (lowerPath.includes('borderwidth') || lowerPath.includes('border') && lowerPath.includes('width')) {
|
|
193
|
+
return 'borderWidth';
|
|
194
|
+
}
|
|
195
|
+
if (lowerPath.includes('shadow')) {
|
|
196
|
+
return 'shadow';
|
|
197
|
+
}
|
|
198
|
+
if (lowerPath.includes('zindex') || lowerPath.includes('z-index') || lowerPath.includes('z')) {
|
|
199
|
+
return 'zIndex';
|
|
200
|
+
}
|
|
201
|
+
if (lowerPath.includes('opacity')) {
|
|
202
|
+
return 'opacity';
|
|
203
|
+
}
|
|
204
|
+
if (lowerPath.includes('fontweight') || lowerPath.includes('font') && lowerPath.includes('weight') || lowerPath.includes('weight')) {
|
|
205
|
+
return 'fontWeight';
|
|
206
|
+
}
|
|
207
|
+
if (lowerPath.includes('transition') || lowerPath.includes('animation') || lowerPath.includes('duration') || lowerPath.includes('delay')) {
|
|
208
|
+
return 'transition';
|
|
209
|
+
}
|
|
210
|
+
// Check by value pattern
|
|
211
|
+
if (this.isColor(value)) {
|
|
212
|
+
return 'color';
|
|
213
|
+
}
|
|
214
|
+
if (/^\d+(\.\d+)?(px|rem|em)$/.test(value)) {
|
|
215
|
+
if (/font|size/.test(lowerPath)) {
|
|
216
|
+
return 'fontSize';
|
|
217
|
+
}
|
|
218
|
+
if (/radius/.test(lowerPath)) {
|
|
219
|
+
return 'borderRadius';
|
|
220
|
+
}
|
|
221
|
+
if (/border.*width|width/.test(lowerPath)) {
|
|
222
|
+
return 'borderWidth';
|
|
223
|
+
}
|
|
224
|
+
return 'spacing';
|
|
225
|
+
}
|
|
226
|
+
if (/^-?\d+$/.test(value)) {
|
|
227
|
+
return 'zIndex';
|
|
228
|
+
}
|
|
229
|
+
if (/^(0?\.\d+|1(\.0+)?)$/.test(value)) {
|
|
230
|
+
return 'opacity';
|
|
231
|
+
}
|
|
232
|
+
if (/^[1-9]00$/.test(value) || /^(normal|bold|bolder|lighter)$/i.test(value)) {
|
|
233
|
+
return 'fontWeight';
|
|
234
|
+
}
|
|
235
|
+
if (/^\d+(\.\d+)?(s|ms)$/.test(value) || /(ease|linear|cubic-bezier|steps)/i.test(value)) {
|
|
236
|
+
return 'transition';
|
|
237
|
+
}
|
|
238
|
+
return 'other';
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Check if a value is a color
|
|
242
|
+
*/
|
|
243
|
+
isColor(value) {
|
|
244
|
+
return /^#[0-9a-f]{3,8}$/i.test(value) ||
|
|
245
|
+
/^rgba?\(/.test(value) ||
|
|
246
|
+
/^hsla?\(/.test(value);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Normalize a value for comparison
|
|
250
|
+
*/
|
|
251
|
+
normalizeValue(value) {
|
|
252
|
+
const trimmed = value.trim();
|
|
253
|
+
// Normalize hex colors
|
|
254
|
+
if (/^#[0-9a-f]{3}$/i.test(trimmed)) {
|
|
255
|
+
// Expand 3-digit hex to 6-digit
|
|
256
|
+
const r = trimmed[1];
|
|
257
|
+
const g = trimmed[2];
|
|
258
|
+
const b = trimmed[3];
|
|
259
|
+
return `#${r}${r}${g}${g}${b}${b}`.toLowerCase();
|
|
260
|
+
}
|
|
261
|
+
if (/^#[0-9a-f]{6}$/i.test(trimmed)) {
|
|
262
|
+
return trimmed.toLowerCase();
|
|
263
|
+
}
|
|
264
|
+
// Normalize RGB/RGBA
|
|
265
|
+
const rgbMatch = trimmed.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*([\d.]+))?\s*\)$/i);
|
|
266
|
+
if (rgbMatch?.[1] && rgbMatch[2] && rgbMatch[3]) {
|
|
267
|
+
const r = parseInt(rgbMatch[1]);
|
|
268
|
+
const g = parseInt(rgbMatch[2]);
|
|
269
|
+
const b = parseInt(rgbMatch[3]);
|
|
270
|
+
const a = rgbMatch[4];
|
|
271
|
+
if (a && a !== '1') {
|
|
272
|
+
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
273
|
+
}
|
|
274
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
275
|
+
}
|
|
276
|
+
// Normalize spacing values
|
|
277
|
+
const spacingMatch = trimmed.match(/^(\d+(\.\d+)?)(px|rem|em|%)$/);
|
|
278
|
+
if (spacingMatch) {
|
|
279
|
+
return trimmed;
|
|
280
|
+
}
|
|
281
|
+
// Return as-is for other values
|
|
282
|
+
return trimmed || null;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Find matching tokens for a given value and category
|
|
286
|
+
*/
|
|
287
|
+
findMatchingTokens(value, category) {
|
|
288
|
+
const normalized = this.normalizeValue(value);
|
|
289
|
+
if (!normalized) {
|
|
290
|
+
return [];
|
|
291
|
+
}
|
|
292
|
+
const allMatches = [];
|
|
293
|
+
for (const contract of this.contracts.values()) {
|
|
294
|
+
const matches = contract.tokens.get(normalized) || [];
|
|
295
|
+
allMatches.push(...matches);
|
|
296
|
+
}
|
|
297
|
+
// Filter by category if specified
|
|
298
|
+
if (category) {
|
|
299
|
+
return allMatches.filter((token) => token.category === category);
|
|
300
|
+
}
|
|
301
|
+
return allMatches;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Get the primary variable name from loaded contracts
|
|
305
|
+
*/
|
|
306
|
+
getVariableName() {
|
|
307
|
+
const firstContract = Array.from(this.contracts.values())[0];
|
|
308
|
+
return firstContract?.variableName || 'theme';
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Check if any contracts are loaded
|
|
312
|
+
*/
|
|
313
|
+
hasContracts() {
|
|
314
|
+
return this.contracts.size > 0;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Clear all loaded contracts
|
|
318
|
+
*/
|
|
319
|
+
clear() {
|
|
320
|
+
this.contracts.clear();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
import type { ThemeContractAnalyzer } from './theme-contract-analyzer.js';
|
|
4
|
+
export interface ThemeTokenOptions {
|
|
5
|
+
themeContracts?: string[];
|
|
6
|
+
checkColors?: boolean;
|
|
7
|
+
checkSpacing?: boolean;
|
|
8
|
+
checkFontSizes?: boolean;
|
|
9
|
+
checkBorderRadius?: boolean;
|
|
10
|
+
checkBorderWidths?: boolean;
|
|
11
|
+
checkShadows?: boolean;
|
|
12
|
+
checkZIndex?: boolean;
|
|
13
|
+
checkOpacity?: boolean;
|
|
14
|
+
checkFontWeights?: boolean;
|
|
15
|
+
checkTransitions?: boolean;
|
|
16
|
+
allowedValues?: string[];
|
|
17
|
+
allowedProperties?: string[];
|
|
18
|
+
autoFix?: boolean;
|
|
19
|
+
remBase?: number;
|
|
20
|
+
checkHelperFunctions?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Recursively processes a vanilla-extract style object and reports hard-coded values
|
|
24
|
+
* that should use theme tokens instead.
|
|
25
|
+
*/
|
|
26
|
+
export declare const processThemeTokensInStyleObject: (context: Rule.RuleContext, node: TSESTree.ObjectExpression, options: ThemeTokenOptions, analyzer: ThemeContractAnalyzer) => void;
|