@antebudimir/eslint-plugin-vanilla-extract 1.16.0 → 1.17.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 +142 -1
- package/dist/css-rules/no-empty-blocks/fix-utils.d.ts +4 -0
- package/dist/css-rules/no-empty-blocks/fix-utils.js +71 -22
- package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.d.ts +2 -0
- package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.js +55 -0
- package/dist/css-rules/no-px-unit/processor.d.ts +3 -0
- package/dist/css-rules/no-px-unit/processor.js +93 -0
- package/dist/css-rules/no-px-unit/visitor-creator.d.ts +2 -0
- package/dist/css-rules/no-px-unit/visitor-creator.js +55 -0
- package/dist/css-rules/shared-utils/order-strategy-visitor-creator.js +10 -1
- package/dist/css-rules/shared-utils/recipe-property-processor.d.ts +4 -3
- package/dist/css-rules/shared-utils/recipe-property-processor.js +9 -7
- package/dist/css-rules/shared-utils/reference-tracker.d.ts +7 -1
- package/dist/css-rules/shared-utils/reference-tracker.js +27 -5
- package/dist/css-rules/types.d.ts +4 -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.js +1 -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 +75 -0
- package/dist/sample.css.js +530 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -304,6 +304,36 @@ export default [
|
|
|
304
304
|
|
|
305
305
|
> **Note:** Remember to enable only one ordering rule at a time. See the "Important" section above for details on switching between ordering rules.
|
|
306
306
|
|
|
307
|
+
### Global Settings (Recommended for shared wrappers)
|
|
308
|
+
|
|
309
|
+
If you use imported wrapper functions across multiple ordering rules, configure them once in ESLint `settings`.
|
|
310
|
+
All ordering rules (`alphabetical-order`, `concentric-order`, `custom-order`) read this value.
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
import { defineConfig } from 'eslint/config';
|
|
314
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
315
|
+
|
|
316
|
+
export default defineConfig([
|
|
317
|
+
{
|
|
318
|
+
files: ['**/*.css.ts'],
|
|
319
|
+
plugins: {
|
|
320
|
+
'vanilla-extract': vanillaExtract,
|
|
321
|
+
},
|
|
322
|
+
settings: {
|
|
323
|
+
'vanilla-extract': {
|
|
324
|
+
style: ['componentStyle', 'layerStyle'],
|
|
325
|
+
recipe: ['componentRecipe'],
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
rules: {
|
|
329
|
+
...vanillaExtract.configs.recommended.rules,
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
]);
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Configure wrapper names only via ESLint `settings` (`style`, `recipe`).
|
|
336
|
+
|
|
307
337
|
## Rules
|
|
308
338
|
|
|
309
339
|
### vanilla-extract/alphabetical-order
|
|
@@ -336,6 +366,32 @@ export const myStyle = style({
|
|
|
336
366
|
});
|
|
337
367
|
```
|
|
338
368
|
|
|
369
|
+
#### Global setting for alphabetical-order: `settings['vanilla-extract'].style`
|
|
370
|
+
|
|
371
|
+
`vanilla-extract/alphabetical-order` reads wrapper names from global ESLint settings.
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
import { defineConfig } from 'eslint/config';
|
|
375
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
376
|
+
|
|
377
|
+
export default defineConfig([
|
|
378
|
+
{
|
|
379
|
+
files: ['**/*.css.ts'],
|
|
380
|
+
plugins: {
|
|
381
|
+
'vanilla-extract': vanillaExtract,
|
|
382
|
+
},
|
|
383
|
+
settings: {
|
|
384
|
+
'vanilla-extract': {
|
|
385
|
+
style: ['componentStyle'],
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
rules: {
|
|
389
|
+
'vanilla-extract/alphabetical-order': 'error',
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
]);
|
|
393
|
+
```
|
|
394
|
+
|
|
339
395
|
### vanilla-extract/concentric-order
|
|
340
396
|
|
|
341
397
|
This rule enforces that CSS properties in vanilla-extract style objects follow the [concentric CSS](#concentric-css-model) ordering pattern, which organizes properties from outside to inside.
|
|
@@ -364,6 +420,33 @@ export const myStyle = style({
|
|
|
364
420
|
});
|
|
365
421
|
```
|
|
366
422
|
|
|
423
|
+
#### Global setting for concentric-order: `settings['vanilla-extract'].style`
|
|
424
|
+
|
|
425
|
+
Use this setting when style wrappers are imported from local modules (for example `componentStyle`, `layerStyle`).
|
|
426
|
+
If a wrapper function name is listed, the rule treats calls to that function like `style(...)` calls.
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
import { defineConfig } from 'eslint/config';
|
|
430
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
431
|
+
|
|
432
|
+
export default defineConfig([
|
|
433
|
+
{
|
|
434
|
+
files: ['**/*.css.ts'],
|
|
435
|
+
plugins: {
|
|
436
|
+
'vanilla-extract': vanillaExtract,
|
|
437
|
+
},
|
|
438
|
+
settings: {
|
|
439
|
+
'vanilla-extract': {
|
|
440
|
+
style: ['componentStyle', 'layerStyle'],
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
rules: {
|
|
444
|
+
'vanilla-extract/concentric-order': 'error',
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
]);
|
|
448
|
+
```
|
|
449
|
+
|
|
367
450
|
### vanilla-extract/custom-order
|
|
368
451
|
|
|
369
452
|
The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your
|
|
@@ -388,6 +471,39 @@ To configure the rule, add it to your ESLint configuration file with your desire
|
|
|
388
471
|
`groups` array to include any number of available CSS property groups you want to enforce, with a minimum of one group
|
|
389
472
|
required.
|
|
390
473
|
|
|
474
|
+
#### Global setting for custom-order: `settings['vanilla-extract'].style`
|
|
475
|
+
|
|
476
|
+
`vanilla-extract/custom-order` also reads `style` from global settings together with
|
|
477
|
+
`groupOrder` and `sortRemainingProperties`.
|
|
478
|
+
|
|
479
|
+
```typescript
|
|
480
|
+
import { defineConfig } from 'eslint/config';
|
|
481
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
482
|
+
|
|
483
|
+
export default defineConfig([
|
|
484
|
+
{
|
|
485
|
+
files: ['**/*.css.ts'],
|
|
486
|
+
plugins: {
|
|
487
|
+
'vanilla-extract': vanillaExtract,
|
|
488
|
+
},
|
|
489
|
+
settings: {
|
|
490
|
+
'vanilla-extract': {
|
|
491
|
+
style: ['componentStyle'],
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
rules: {
|
|
495
|
+
'vanilla-extract/custom-order': [
|
|
496
|
+
'error',
|
|
497
|
+
{
|
|
498
|
+
groupOrder: ['font', 'dimensions', 'margin', 'padding', 'position', 'border'],
|
|
499
|
+
sortRemainingProperties: 'alphabetical',
|
|
500
|
+
},
|
|
501
|
+
],
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
]);
|
|
505
|
+
```
|
|
506
|
+
|
|
391
507
|
```typescript
|
|
392
508
|
// ❌ Incorrect (Unordered)
|
|
393
509
|
import { style } from '@vanilla-extract/css';
|
|
@@ -464,6 +580,31 @@ export const recipeWithEmptyVariants = recipe({
|
|
|
464
580
|
});
|
|
465
581
|
```
|
|
466
582
|
|
|
583
|
+
#### Autofix safety
|
|
584
|
+
|
|
585
|
+
Rule only auto-removes a declaration when it's safe to do so. Declarations are **reported without an
|
|
586
|
+
autofix** (you must resolve them manually) when:
|
|
587
|
+
|
|
588
|
+
- the declaration's **exported** (`export const x = style({})`) — exported identifiers can be imported and
|
|
589
|
+
used as a `className` in other files the rule can't see.
|
|
590
|
+
- the identifier's **still referenced elsewhere in the same file** — e.g. used as a class-name hook inside a
|
|
591
|
+
`globalStyle()` selector template literal:
|
|
592
|
+
|
|
593
|
+
```typescript
|
|
594
|
+
// ❌ Reported, but not auto-fixed (the identifier's still referenced)
|
|
595
|
+
import { globalStyle, style } from '@vanilla-extract/css';
|
|
596
|
+
|
|
597
|
+
// used only as a class-name hook, not for its own props
|
|
598
|
+
export const buttonWrapper = style({});
|
|
599
|
+
|
|
600
|
+
globalStyle(`${buttonWrapper} > svg`, {
|
|
601
|
+
color: 'red',
|
|
602
|
+
});
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
Empty blocks that declare no identifier (e.g. `globalStyle('ul', {})`) and unreferenced, non-exported
|
|
606
|
+
declarations remain fully auto-fixable.
|
|
607
|
+
|
|
467
608
|
### vanilla-extract/no-px-unit
|
|
468
609
|
|
|
469
610
|
This rule disallows the use of hard-coded `px` units in vanilla-extract style declarations. Prefer `rem`, `em`, or theme tokens. A configurable allowlist lets you permit specific properties to use `px` where necessary. Allowlist supports both camelCase and kebab-case property names.
|
|
@@ -721,7 +862,7 @@ Enforces theme tokens instead of hard-coded CSS values. Analyzes your theme cont
|
|
|
721
862
|
**Options:**
|
|
722
863
|
|
|
723
864
|
| Option | Type | Default | Description |
|
|
724
|
-
|
|
865
|
+
| -------- | ------ | --------- | ------------- |
|
|
725
866
|
| `themeContracts` | `string[]` | `[]` | Theme contract file paths (relative to project root or absolute) |
|
|
726
867
|
| `checkColors` | `boolean` | `true` | Check colors (hex, rgb, hsl, named) |
|
|
727
868
|
| `checkSpacing` | `boolean` | `true` | Check spacing (margin, padding, gap, width, height) |
|
|
@@ -15,6 +15,10 @@ import { TSESTree } from '@typescript-eslint/utils';
|
|
|
15
15
|
export declare const findTargetDeclarationNode: (callNode: TSESTree.CallExpression, isGlobalApi: boolean) => TSESTree.Node | null;
|
|
16
16
|
/**
|
|
17
17
|
* Reports an issue for an empty style declaration, and provides a fix to remove the declaration.
|
|
18
|
+
*
|
|
19
|
+
* The removal autofix is only provided when the declaration is not exported and its
|
|
20
|
+
* identifier is not referenced anywhere else in the file - otherwise the fix would break
|
|
21
|
+
* those references (e.g. a `globalStyle()` selector or an import in another file).
|
|
18
22
|
* @param ruleContext The ESLint rule context.
|
|
19
23
|
* @param node The node to report the issue on.
|
|
20
24
|
* @param callNode The CallExpression node of the style() function.
|
|
@@ -32,8 +32,58 @@ export const findTargetDeclarationNode = (callNode, isGlobalApi) => {
|
|
|
32
32
|
}
|
|
33
33
|
return null;
|
|
34
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Determines whether the given declaration node can safely be removed by the autofix.
|
|
37
|
+
*
|
|
38
|
+
* The autofix must NOT remove a declaration when doing so would leave dangling
|
|
39
|
+
* references to its identifier:
|
|
40
|
+
*
|
|
41
|
+
* 1. Exported declarations (`export const x = style({})`) are never auto-removed.
|
|
42
|
+
* Exported identifiers can be consumed in other files (e.g. as a `className`)
|
|
43
|
+
* that this rule has no visibility into.
|
|
44
|
+
*
|
|
45
|
+
* 2. Declarations whose identifier is still referenced elsewhere in the same file
|
|
46
|
+
* (e.g. interpolated into a `globalStyle()` selector template literal, or listed
|
|
47
|
+
* in a separate `export { x }` statement) are never auto-removed.
|
|
48
|
+
*
|
|
49
|
+
* In both cases the empty style block is still reported, just without a fix, so the
|
|
50
|
+
* developer can resolve the usages manually.
|
|
51
|
+
*/
|
|
52
|
+
const isDeclarationSafeToAutoRemove = (ruleContext, declarationNode) => {
|
|
53
|
+
// Exported bindings may be used in other files - never auto-remove them.
|
|
54
|
+
if (declarationNode.type === 'ExportNamedDeclaration' || declarationNode.type === 'ExportDefaultDeclaration') {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if (declarationNode.type !== 'VariableDeclaration') {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
const sourceCode = ruleContext.sourceCode;
|
|
61
|
+
const declarationRange = declarationNode.range;
|
|
62
|
+
if (!declarationRange) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
return declarationNode.declarations.every((declarator) => {
|
|
66
|
+
const declaredVariables = sourceCode.getDeclaredVariables(declarator);
|
|
67
|
+
return declaredVariables.every((variable) => {
|
|
68
|
+
return !variable.references.some((reference) => {
|
|
69
|
+
const identifier = reference.identifier;
|
|
70
|
+
if (!identifier || !identifier.range) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
// The binding identifier itself lives inside the declaration - only
|
|
74
|
+
// references outside of it are actual usages of the identifier.
|
|
75
|
+
const isInsideDeclaration = identifier.range[0] >= declarationRange[0] && identifier.range[1] <= declarationRange[1];
|
|
76
|
+
return !isInsideDeclaration;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
};
|
|
35
81
|
/**
|
|
36
82
|
* Reports an issue for an empty style declaration, and provides a fix to remove the declaration.
|
|
83
|
+
*
|
|
84
|
+
* The removal autofix is only provided when the declaration is not exported and its
|
|
85
|
+
* identifier is not referenced anywhere else in the file - otherwise the fix would break
|
|
86
|
+
* those references (e.g. a `globalStyle()` selector or an import in another file).
|
|
37
87
|
* @param ruleContext The ESLint rule context.
|
|
38
88
|
* @param node The node to report the issue on.
|
|
39
89
|
* @param callNode The CallExpression node of the style() function.
|
|
@@ -44,32 +94,31 @@ export const reportEmptyDeclaration = (ruleContext, node, callNode) => {
|
|
|
44
94
|
['globalStyle', 'globalFontFace', 'globalKeyframes'].includes(callNode.callee.name);
|
|
45
95
|
// Find the parent declaration node
|
|
46
96
|
const targetDeclarationNode = findTargetDeclarationNode(callNode, isGlobalApi);
|
|
47
|
-
if (targetDeclarationNode) {
|
|
48
|
-
ruleContext.report({
|
|
49
|
-
node: node,
|
|
50
|
-
messageId: 'emptyStyleDeclaration',
|
|
51
|
-
fix(fixer) {
|
|
52
|
-
const sourceCode = ruleContext.sourceCode;
|
|
53
|
-
const startLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[0]).line;
|
|
54
|
-
const lineStart = sourceCode.getIndexFromLoc({
|
|
55
|
-
line: startLine,
|
|
56
|
-
column: 0,
|
|
57
|
-
});
|
|
58
|
-
// Find next line after the declaration
|
|
59
|
-
const endLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[1]).line;
|
|
60
|
-
const nextLineStart = sourceCode.getIndexFromLoc({
|
|
61
|
-
line: endLine + 1,
|
|
62
|
-
column: 0,
|
|
63
|
-
});
|
|
64
|
-
return fixer.removeRange([lineStart, nextLineStart]);
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
97
|
+
if (!targetDeclarationNode || !isDeclarationSafeToAutoRemove(ruleContext, targetDeclarationNode)) {
|
|
69
98
|
// Report the issue without providing a fix
|
|
70
99
|
ruleContext.report({
|
|
71
100
|
node: node,
|
|
72
101
|
messageId: 'emptyStyleDeclaration',
|
|
73
102
|
});
|
|
103
|
+
return;
|
|
74
104
|
}
|
|
105
|
+
ruleContext.report({
|
|
106
|
+
node: node,
|
|
107
|
+
messageId: 'emptyStyleDeclaration',
|
|
108
|
+
fix(fixer) {
|
|
109
|
+
const sourceCode = ruleContext.sourceCode;
|
|
110
|
+
const startLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[0]).line;
|
|
111
|
+
const lineStart = sourceCode.getIndexFromLoc({
|
|
112
|
+
line: startLine,
|
|
113
|
+
column: 0,
|
|
114
|
+
});
|
|
115
|
+
// Find next line after the declaration
|
|
116
|
+
const endLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[1]).line;
|
|
117
|
+
const nextLineStart = sourceCode.getIndexFromLoc({
|
|
118
|
+
line: endLine + 1,
|
|
119
|
+
column: 0,
|
|
120
|
+
});
|
|
121
|
+
return fixer.removeRange([lineStart, nextLineStart]);
|
|
122
|
+
},
|
|
123
|
+
});
|
|
75
124
|
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
|
|
3
|
+
import { ReferenceTracker, createReferenceTrackingVisitor } from '../shared-utils/reference-tracker.js';
|
|
4
|
+
import { processStyleNode } from '../shared-utils/style-node-processor.js';
|
|
5
|
+
import { processNoPxUnitInStyleObject } from './processor.js';
|
|
6
|
+
export const createNoPxUnitVisitors = (context) => {
|
|
7
|
+
const tracker = new ReferenceTracker();
|
|
8
|
+
const trackingVisitor = createReferenceTrackingVisitor(tracker);
|
|
9
|
+
const options = context.options?.[0] || {};
|
|
10
|
+
const allowSet = new Set((options.allow ?? []).map((s) => s));
|
|
11
|
+
const process = (ctx, obj) => processNoPxUnitInStyleObject(ctx, obj, allowSet);
|
|
12
|
+
return {
|
|
13
|
+
...trackingVisitor,
|
|
14
|
+
CallExpression(node) {
|
|
15
|
+
if (node.callee.type !== AST_NODE_TYPES.Identifier)
|
|
16
|
+
return;
|
|
17
|
+
const functionName = node.callee.name;
|
|
18
|
+
if (!tracker.isTrackedFunction(functionName))
|
|
19
|
+
return;
|
|
20
|
+
const originalName = tracker.getOriginalName(functionName);
|
|
21
|
+
if (!originalName)
|
|
22
|
+
return;
|
|
23
|
+
switch (originalName) {
|
|
24
|
+
case 'fontFace':
|
|
25
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
26
|
+
process(context, node.arguments[0]);
|
|
27
|
+
}
|
|
28
|
+
break;
|
|
29
|
+
case 'globalFontFace':
|
|
30
|
+
if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
31
|
+
process(context, node.arguments[1]);
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
case 'style':
|
|
35
|
+
case 'styleVariants':
|
|
36
|
+
case 'keyframes':
|
|
37
|
+
if (node.arguments.length > 0) {
|
|
38
|
+
processStyleNode(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
case 'globalStyle':
|
|
42
|
+
case 'globalKeyframes':
|
|
43
|
+
if (node.arguments.length >= 2) {
|
|
44
|
+
processStyleNode(context, node.arguments[1], (ctx, obj) => process(ctx, obj));
|
|
45
|
+
}
|
|
46
|
+
break;
|
|
47
|
+
case 'recipe':
|
|
48
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
49
|
+
processRecipeProperties(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
const containsPx = (text) => /(^|\W)-?\d*\.?\d*px(?![a-zA-Z])/i.test(text);
|
|
3
|
+
const replacePxWith = (text, replacement) => text.replace(/px(?![a-zA-Z])/g, replacement);
|
|
4
|
+
const toKebab = (name) => name.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
|
|
5
|
+
const getValueText = (node) => {
|
|
6
|
+
if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string') {
|
|
7
|
+
return node.value;
|
|
8
|
+
}
|
|
9
|
+
if (node.type === AST_NODE_TYPES.TemplateLiteral) {
|
|
10
|
+
// Join all quasis (ignore expressions content)
|
|
11
|
+
const raw = node.quasis.map((q) => q.value.raw ?? '').join('');
|
|
12
|
+
return raw;
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
};
|
|
16
|
+
const canSuggestFix = (node) => {
|
|
17
|
+
if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string')
|
|
18
|
+
return 'literal';
|
|
19
|
+
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0)
|
|
20
|
+
return 'simple-template';
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
export const processNoPxUnitInStyleObject = (context, node, allowSet) => {
|
|
24
|
+
for (const property of node.properties) {
|
|
25
|
+
if (property.type !== AST_NODE_TYPES.Property)
|
|
26
|
+
continue;
|
|
27
|
+
// Determine property name when possible
|
|
28
|
+
let propertyName = null;
|
|
29
|
+
if (property.key.type === AST_NODE_TYPES.Identifier) {
|
|
30
|
+
propertyName = property.key.name;
|
|
31
|
+
}
|
|
32
|
+
else if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
|
|
33
|
+
propertyName = property.key.value;
|
|
34
|
+
}
|
|
35
|
+
// Recurse into known nested containers
|
|
36
|
+
if (propertyName === '@media' || propertyName === 'selectors') {
|
|
37
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
38
|
+
for (const nested of property.value.properties) {
|
|
39
|
+
if (nested.type === AST_NODE_TYPES.Property && nested.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
40
|
+
processNoPxUnitInStyleObject(context, nested.value, allowSet);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// Traverse any nested object
|
|
47
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
48
|
+
processNoPxUnitInStyleObject(context, property.value, allowSet);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
// Skip if property is whitelisted (supports both camelCase and kebab-case)
|
|
52
|
+
if (propertyName) {
|
|
53
|
+
const kebab = toKebab(propertyName);
|
|
54
|
+
if (allowSet.has(propertyName) || allowSet.has(kebab)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Check string or template literal values
|
|
59
|
+
const text = getValueText(property.value);
|
|
60
|
+
if (text && containsPx(text)) {
|
|
61
|
+
const fixability = canSuggestFix(property.value);
|
|
62
|
+
context.report({
|
|
63
|
+
node: property.value,
|
|
64
|
+
messageId: 'noPxUnit',
|
|
65
|
+
suggest: fixability
|
|
66
|
+
? [
|
|
67
|
+
{
|
|
68
|
+
messageId: 'removePx',
|
|
69
|
+
fix: (fixer) => {
|
|
70
|
+
const newText = replacePxWith(text, '');
|
|
71
|
+
if (fixability === 'literal') {
|
|
72
|
+
return fixer.replaceText(property.value, `'${newText}'`);
|
|
73
|
+
}
|
|
74
|
+
// simple template with no expressions
|
|
75
|
+
return fixer.replaceText(property.value, `\`${newText}\``);
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
messageId: 'replaceWithRem',
|
|
80
|
+
fix: (fixer) => {
|
|
81
|
+
const newText = replacePxWith(text, 'rem');
|
|
82
|
+
if (fixability === 'literal') {
|
|
83
|
+
return fixer.replaceText(property.value, `'${newText}'`);
|
|
84
|
+
}
|
|
85
|
+
return fixer.replaceText(property.value, `\`${newText}\``);
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
]
|
|
89
|
+
: undefined,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
|
|
3
|
+
import { ReferenceTracker, createReferenceTrackingVisitor } from '../shared-utils/reference-tracker.js';
|
|
4
|
+
import { processStyleNode } from '../shared-utils/style-node-processor.js';
|
|
5
|
+
import { processNoPxUnitInStyleObject } from './processor.js';
|
|
6
|
+
export const createNoPxUnitVisitors = (context) => {
|
|
7
|
+
const tracker = new ReferenceTracker();
|
|
8
|
+
const trackingVisitor = createReferenceTrackingVisitor(tracker);
|
|
9
|
+
const options = context.options?.[0] || {};
|
|
10
|
+
const allowSet = new Set((options.allow ?? []).map((s) => s));
|
|
11
|
+
const process = (ctx, obj) => processNoPxUnitInStyleObject(ctx, obj, allowSet);
|
|
12
|
+
return {
|
|
13
|
+
...trackingVisitor,
|
|
14
|
+
CallExpression(node) {
|
|
15
|
+
if (node.callee.type !== AST_NODE_TYPES.Identifier)
|
|
16
|
+
return;
|
|
17
|
+
const functionName = node.callee.name;
|
|
18
|
+
if (!tracker.isTrackedFunction(functionName))
|
|
19
|
+
return;
|
|
20
|
+
const originalName = tracker.getOriginalName(functionName);
|
|
21
|
+
if (!originalName)
|
|
22
|
+
return;
|
|
23
|
+
switch (originalName) {
|
|
24
|
+
case 'fontFace':
|
|
25
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
26
|
+
process(context, node.arguments[0]);
|
|
27
|
+
}
|
|
28
|
+
break;
|
|
29
|
+
case 'globalFontFace':
|
|
30
|
+
if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
31
|
+
process(context, node.arguments[1]);
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
case 'style':
|
|
35
|
+
case 'styleVariants':
|
|
36
|
+
case 'keyframes':
|
|
37
|
+
if (node.arguments.length > 0) {
|
|
38
|
+
processStyleNode(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
case 'globalStyle':
|
|
42
|
+
case 'globalKeyframes':
|
|
43
|
+
if (node.arguments.length >= 2) {
|
|
44
|
+
processStyleNode(context, node.arguments[1], (ctx, obj) => process(ctx, obj));
|
|
45
|
+
}
|
|
46
|
+
break;
|
|
47
|
+
case 'recipe':
|
|
48
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
49
|
+
processRecipeProperties(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -19,7 +19,8 @@ import { processStyleNode } from './style-node-processor.js';
|
|
|
19
19
|
* @returns An object with visitor functions for the ESLint rule.
|
|
20
20
|
*/
|
|
21
21
|
export const createNodeVisitors = (ruleContext, orderingStrategy, userDefinedGroupOrder, sortRemainingProperties) => {
|
|
22
|
-
const
|
|
22
|
+
const wrapperSettings = resolveWrapperSettings(ruleContext);
|
|
23
|
+
const tracker = new ReferenceTracker(wrapperSettings);
|
|
23
24
|
const trackingVisitor = createReferenceTrackingVisitor(tracker);
|
|
24
25
|
return {
|
|
25
26
|
// Include the import/variable tracking visitors
|
|
@@ -64,6 +65,14 @@ export const createNodeVisitors = (ruleContext, orderingStrategy, userDefinedGro
|
|
|
64
65
|
},
|
|
65
66
|
};
|
|
66
67
|
};
|
|
68
|
+
const resolveWrapperSettings = (ruleContext) => {
|
|
69
|
+
const pluginSettings = (ruleContext.settings?.['vanilla-extract'] ??
|
|
70
|
+
{});
|
|
71
|
+
return {
|
|
72
|
+
style: pluginSettings.style ?? [],
|
|
73
|
+
recipe: pluginSettings.recipe ?? [],
|
|
74
|
+
};
|
|
75
|
+
};
|
|
67
76
|
/**
|
|
68
77
|
* Helper function to process style ordering for style-related functions
|
|
69
78
|
*/
|
|
@@ -7,10 +7,11 @@ import { TSESTree } from '@typescript-eslint/utils';
|
|
|
7
7
|
* @param processProperty A callback function to process each property object (e.g., for alphabetical or concentric ordering).
|
|
8
8
|
*
|
|
9
9
|
* This function iterates through the properties of the recipe object:
|
|
10
|
-
* - For the `base` property, it
|
|
11
|
-
* - For the `variants` property, it processes each variant
|
|
10
|
+
* - For the `base` property, it processes object or array style nodes.
|
|
11
|
+
* - For the `variants` property, it processes each variant option as object or array style nodes.
|
|
12
12
|
*
|
|
13
13
|
* The function skips any non-Property nodes or nodes without an Identifier key.
|
|
14
|
-
* It
|
|
14
|
+
* It delegates node handling to `processStyleNode`, which safely supports
|
|
15
|
+
* ObjectExpression and ArrayExpression values.
|
|
15
16
|
*/
|
|
16
17
|
export declare const processRecipeProperties: (ruleContext: Rule.RuleContext, recipeNode: TSESTree.ObjectExpression, processProperty: (ruleContext: Rule.RuleContext, value: TSESTree.ObjectExpression) => void) => void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { processStyleNode } from './style-node-processor.js';
|
|
2
3
|
/**
|
|
3
4
|
* Processes the `base` and `variants` properties of a recipe object.
|
|
4
5
|
* @param ruleContext The ESLint rule context.
|
|
@@ -6,11 +7,12 @@ import { TSESTree } from '@typescript-eslint/utils';
|
|
|
6
7
|
* @param processProperty A callback function to process each property object (e.g., for alphabetical or concentric ordering).
|
|
7
8
|
*
|
|
8
9
|
* This function iterates through the properties of the recipe object:
|
|
9
|
-
* - For the `base` property, it
|
|
10
|
-
* - For the `variants` property, it processes each variant
|
|
10
|
+
* - For the `base` property, it processes object or array style nodes.
|
|
11
|
+
* - For the `variants` property, it processes each variant option as object or array style nodes.
|
|
11
12
|
*
|
|
12
13
|
* The function skips any non-Property nodes or nodes without an Identifier key.
|
|
13
|
-
* It
|
|
14
|
+
* It delegates node handling to `processStyleNode`, which safely supports
|
|
15
|
+
* ObjectExpression and ArrayExpression values.
|
|
14
16
|
*/
|
|
15
17
|
export const processRecipeProperties = (ruleContext, recipeNode, processProperty) => {
|
|
16
18
|
recipeNode.properties.forEach((property) => {
|
|
@@ -18,16 +20,16 @@ export const processRecipeProperties = (ruleContext, recipeNode, processProperty
|
|
|
18
20
|
return; // Skip non-property nodes or nodes without an identifier key
|
|
19
21
|
}
|
|
20
22
|
// Process the `base` property
|
|
21
|
-
if (property.key.name === 'base'
|
|
22
|
-
|
|
23
|
+
if (property.key.name === 'base') {
|
|
24
|
+
processStyleNode(ruleContext, property.value, processProperty);
|
|
23
25
|
}
|
|
24
26
|
// Process the `variants` property
|
|
25
27
|
if (property.key.name === 'variants' && property.value.type === 'ObjectExpression') {
|
|
26
28
|
property.value.properties.forEach((variantProperty) => {
|
|
27
29
|
if (variantProperty.type === 'Property' && variantProperty.value.type === 'ObjectExpression') {
|
|
28
30
|
variantProperty.value.properties.forEach((optionProperty) => {
|
|
29
|
-
if (optionProperty.type === 'Property'
|
|
30
|
-
|
|
31
|
+
if (optionProperty.type === 'Property') {
|
|
32
|
+
processStyleNode(ruleContext, optionProperty.value, processProperty);
|
|
31
33
|
}
|
|
32
34
|
});
|
|
33
35
|
}
|
|
@@ -23,7 +23,12 @@ export declare class ReferenceTracker {
|
|
|
23
23
|
private imports;
|
|
24
24
|
private trackedFunctions;
|
|
25
25
|
private wrapperFunctions;
|
|
26
|
-
|
|
26
|
+
private style;
|
|
27
|
+
private recipe;
|
|
28
|
+
constructor(options?: {
|
|
29
|
+
style?: string[];
|
|
30
|
+
recipe?: string[];
|
|
31
|
+
});
|
|
27
32
|
/**
|
|
28
33
|
* Processes import declarations to track vanilla-extract functions
|
|
29
34
|
*/
|
|
@@ -77,6 +82,7 @@ export declare class ReferenceTracker {
|
|
|
77
82
|
*/
|
|
78
83
|
reset(): void;
|
|
79
84
|
private isVanillaExtractSource;
|
|
85
|
+
private getCustomWrapper;
|
|
80
86
|
private categorizeFunction;
|
|
81
87
|
}
|
|
82
88
|
/**
|