@antebudimir/eslint-plugin-vanilla-extract 1.12.0 → 1.14.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 +90 -14
- package/dist/css-rules/no-px-unit/_tests_/no-px-unit.test.d.ts +1 -0
- package/dist/css-rules/no-px-unit/_tests_/no-px-unit.test.js +163 -0
- package/dist/css-rules/no-px-unit/index.d.ts +2 -0
- package/dist/css-rules/no-px-unit/index.js +2 -0
- 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/px-unit-processor.d.ts +14 -0
- package/dist/css-rules/no-px-unit/px-unit-processor.js +104 -0
- package/dist/css-rules/no-px-unit/px-unit-visitor-creator.d.ts +11 -0
- package/dist/css-rules/no-px-unit/px-unit-visitor-creator.js +64 -0
- package/dist/css-rules/no-px-unit/rule-definition.d.ts +3 -0
- package/dist/css-rules/no-px-unit/rule-definition.js +37 -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/prefer-logical-properties/_tests_/prefer-logical-properties.test.d.ts +1 -0
- package/dist/css-rules/prefer-logical-properties/_tests_/prefer-logical-properties.test.js +572 -0
- package/dist/css-rules/prefer-logical-properties/index.d.ts +2 -0
- package/dist/css-rules/prefer-logical-properties/index.js +2 -0
- package/dist/css-rules/prefer-logical-properties/logical-properties-processor.d.ts +19 -0
- package/dist/css-rules/prefer-logical-properties/logical-properties-processor.js +202 -0
- package/dist/css-rules/prefer-logical-properties/logical-properties-visitor-creator.d.ts +14 -0
- package/dist/css-rules/prefer-logical-properties/logical-properties-visitor-creator.js +67 -0
- package/dist/css-rules/prefer-logical-properties/property-mappings.d.ts +44 -0
- package/dist/css-rules/prefer-logical-properties/property-mappings.js +179 -0
- package/dist/css-rules/prefer-logical-properties/rule-definition.d.ts +3 -0
- package/dist/css-rules/prefer-logical-properties/rule-definition.js +35 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/sample.css.d.ts +75 -0
- package/dist/sample.css.js +540 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,11 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
[vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract) CSS styles, including CSS property ordering
|
|
20
|
-
and additional linting rules. Available presets are for alphabetical and
|
|
21
|
-
[concentric](https://rhodesmill.org/brandon/2011/concentric-css/) CSS ordering. The plugin also supports a custom group
|
|
22
|
-
ordering option based on groups available in [concentric CSS](src/css-rules/concentric-order/concentric-groups.ts).
|
|
18
|
+
Comprehensive ESLint plugin for vanilla-extract that enforces best practices in [vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract) CSS styles. Includes support for CSS property ordering (alphabetical, [concentric](https://rhodesmill.org/brandon/2011/concentric-css/), and custom group ordering), advanced style linting rules, auto-fixing, and validation of style patterns specific to vanilla-extract. Ensures zero-runtime safety and integrates with multiple vanilla-extract APIs to promote maintainable, consistent code across projects
|
|
23
19
|
|
|
24
20
|
## Demo
|
|
25
21
|
|
|
@@ -257,7 +253,7 @@ For VS Code users, add these settings to your `.vscode/settings.json`:
|
|
|
257
253
|
|
|
258
254
|
The recommended configuration enables the following rules with error severity:
|
|
259
255
|
|
|
260
|
-
- `vanilla-extract/concentric-order`: Enforces concentric CSS property ordering
|
|
256
|
+
- `vanilla-extract/concentric-order`: Enforces [concentric CSS](#concentric-css-model) property ordering
|
|
261
257
|
- `vanilla-extract/no-empty-style-blocks`: Prevents empty style blocks
|
|
262
258
|
- `vanilla-extract/no-trailing-zero`: Disallows trailing zeros in numeric CSS values
|
|
263
259
|
- `vanilla-extract/no-unknown-unit`: Prohibits usage of unrecognized CSS units
|
|
@@ -267,6 +263,8 @@ The recommended configuration enables the following rules with error severity:
|
|
|
267
263
|
|
|
268
264
|
- `vanilla-extract/alphabetical-order`: Alternative ordering rule (alphabetical sorting)
|
|
269
265
|
- `vanilla-extract/custom-order`: Alternative ordering rule (custom group-based sorting)
|
|
266
|
+
- `vanilla-extract/no-px-unit`: Disallows px units with an optional allowlist
|
|
267
|
+
- `vanilla-extract/prefer-logical-properties`: Enforces logical CSS properties over physical directional properties
|
|
270
268
|
|
|
271
269
|
You can use the recommended configuration as a starting point and override rules as needed for your project. See the configuration examples above for how to switch between ordering rules.
|
|
272
270
|
|
|
@@ -338,8 +336,7 @@ export const myStyle = style({
|
|
|
338
336
|
|
|
339
337
|
### vanilla-extract/concentric-order
|
|
340
338
|
|
|
341
|
-
This rule enforces that CSS properties in vanilla-extract style objects follow the concentric CSS ordering pattern,
|
|
342
|
-
which organizes properties from outside to inside.
|
|
339
|
+
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.
|
|
343
340
|
|
|
344
341
|
```typescript
|
|
345
342
|
// ❌ Incorrect
|
|
@@ -369,7 +366,7 @@ export const myStyle = style({
|
|
|
369
366
|
|
|
370
367
|
The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your
|
|
371
368
|
vanilla-extract styles. You can specify an array of property groups in your preferred order, and the rule will ensure
|
|
372
|
-
that properties within these groups are sorted according to their position in the concentric CSS model.
|
|
369
|
+
that properties within these groups are sorted according to their position in the [concentric CSS model](https://rhodesmill.org/brandon/2011/concentric-css/).
|
|
373
370
|
|
|
374
371
|
Key features of this rule include:
|
|
375
372
|
|
|
@@ -465,7 +462,49 @@ export const recipeWithEmptyVariants = recipe({
|
|
|
465
462
|
});
|
|
466
463
|
```
|
|
467
464
|
|
|
468
|
-
|
|
465
|
+
### vanilla-extract/no-px-unit
|
|
466
|
+
|
|
467
|
+
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.
|
|
468
|
+
|
|
469
|
+
Configuration with an allowlist:
|
|
470
|
+
|
|
471
|
+
```json
|
|
472
|
+
{
|
|
473
|
+
"rules": {
|
|
474
|
+
"vanilla-extract/no-px-unit": ["error", { "allow": ["borderWidth", "outline-offset"] }]
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
Before:
|
|
480
|
+
|
|
481
|
+
```typescript
|
|
482
|
+
import { style } from '@vanilla-extract/css';
|
|
483
|
+
|
|
484
|
+
export const box = style({
|
|
485
|
+
marginTop: '8px',
|
|
486
|
+
padding: '16px',
|
|
487
|
+
selectors: {
|
|
488
|
+
'&:hover': { gap: '4px' },
|
|
489
|
+
},
|
|
490
|
+
});
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
After (suggested fix shown using rem):
|
|
494
|
+
|
|
495
|
+
```typescript
|
|
496
|
+
import { style } from '@vanilla-extract/css';
|
|
497
|
+
|
|
498
|
+
export const box = style({
|
|
499
|
+
marginTop: '8rem',
|
|
500
|
+
padding: '16rem',
|
|
501
|
+
selectors: {
|
|
502
|
+
'&:hover': { gap: '4rem' },
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### vanilla-extract/no-trailing-zero
|
|
469
508
|
|
|
470
509
|
This rule disallows trailing zeros in numeric CSS values within vanilla-extract style objects. It helps maintain cleaner
|
|
471
510
|
and more consistent CSS by removing unnecessary trailing zeros from decimal numbers.
|
|
@@ -496,7 +535,7 @@ export const myStyle = style({
|
|
|
496
535
|
});
|
|
497
536
|
```
|
|
498
537
|
|
|
499
|
-
|
|
538
|
+
### vanilla-extract/no-unknown-unit
|
|
500
539
|
|
|
501
540
|
This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units
|
|
502
541
|
that could cause styling issues or browser compatibility problems.
|
|
@@ -535,7 +574,7 @@ export const myRecipe = recipe({
|
|
|
535
574
|
});
|
|
536
575
|
```
|
|
537
576
|
|
|
538
|
-
|
|
577
|
+
### vanilla-extract/no-zero-unit
|
|
539
578
|
|
|
540
579
|
This rule enforces the removal of unnecessary units for zero values in vanilla-extract style objects. It helps maintain
|
|
541
580
|
cleaner and more consistent CSS by eliminating redundant units when the value is zero.
|
|
@@ -564,6 +603,43 @@ export const myStyle = style({
|
|
|
564
603
|
});
|
|
565
604
|
```
|
|
566
605
|
|
|
606
|
+
### vanilla-extract/prefer-logical-properties
|
|
607
|
+
|
|
608
|
+
This rule enforces the use of CSS logical properties instead of physical (directional) properties in vanilla-extract style declarations. Logical properties adapt to different writing directions (LTR/RTL) and writing modes, making your styles more internationalization-friendly. Supports 140+ property mappings across margin, padding, border, inset, size, overflow, and scroll properties. Configurable allowlist lets you permit specific properties via the `allow` option (supports both camelCase and kebab-case).
|
|
609
|
+
|
|
610
|
+
Configuration with an allowlist:
|
|
611
|
+
|
|
612
|
+
```json
|
|
613
|
+
{
|
|
614
|
+
"rules": {
|
|
615
|
+
"vanilla-extract/prefer-logical-properties": ["error", { "allow": ["top", "left"] }]
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
```typescript
|
|
621
|
+
// ❌ Incorrect
|
|
622
|
+
import { style } from '@vanilla-extract/css';
|
|
623
|
+
|
|
624
|
+
export const box = style({
|
|
625
|
+
marginLeft: '1rem',
|
|
626
|
+
paddingTop: '2rem',
|
|
627
|
+
width: '100%',
|
|
628
|
+
borderRight: '1px solid',
|
|
629
|
+
textAlign: 'left',
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
// ✅ Correct
|
|
633
|
+
import { style } from '@vanilla-extract/css';
|
|
634
|
+
|
|
635
|
+
export const box = style({
|
|
636
|
+
marginInlineStart: '1rem',
|
|
637
|
+
paddingBlockStart: '2rem',
|
|
638
|
+
inlineSize: '100%',
|
|
639
|
+
borderInlineEnd: '1px solid',
|
|
640
|
+
textAlign: 'start',
|
|
641
|
+
});
|
|
642
|
+
|
|
567
643
|
## Font Face Declarations
|
|
568
644
|
|
|
569
645
|
For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce
|
|
@@ -648,14 +724,14 @@ The roadmap outlines the project's current status and future plans:
|
|
|
648
724
|
- Support for using the plugin's recommended config via the extends field (as discussed in
|
|
649
725
|
[issue #3](https://github.com/antebudimir/eslint-plugin-vanilla-extract/issues/3))
|
|
650
726
|
- Comprehensive rule testing.
|
|
727
|
+
- `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
|
|
651
728
|
|
|
652
729
|
### Current Work
|
|
653
730
|
|
|
654
|
-
-
|
|
731
|
+
- TBA
|
|
655
732
|
|
|
656
733
|
### Upcoming Features
|
|
657
734
|
|
|
658
|
-
- `prefer-logical-properties` rule to enforce use of logical properties.
|
|
659
735
|
- `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
|
|
660
736
|
- `no-global-style` rule to disallow use of `globalStyle` function.
|
|
661
737
|
- `property-unit-match` rule to enforce valid units per CSS property specs. **Note**: This feature will only be
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import tsParser from '@typescript-eslint/parser';
|
|
2
|
+
import { run } from 'eslint-vitest-rule-tester';
|
|
3
|
+
import noPxUnitRule from '../rule-definition.js';
|
|
4
|
+
run({
|
|
5
|
+
name: 'vanilla-extract/no-px-unit',
|
|
6
|
+
rule: noPxUnitRule,
|
|
7
|
+
languageOptions: {
|
|
8
|
+
parser: tsParser,
|
|
9
|
+
parserOptions: {
|
|
10
|
+
ecmaVersion: 2022,
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
valid: [
|
|
15
|
+
{
|
|
16
|
+
code: `
|
|
17
|
+
import { style } from '@vanilla-extract/css';
|
|
18
|
+
const myStyle = style({
|
|
19
|
+
margin: '1rem',
|
|
20
|
+
padding: 8,
|
|
21
|
+
width: '100%',
|
|
22
|
+
color: vars.color.primary,
|
|
23
|
+
});
|
|
24
|
+
`,
|
|
25
|
+
name: 'allows rem, numbers, and token references',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
code: `
|
|
29
|
+
import { style } from '@vanilla-extract/css';
|
|
30
|
+
const myStyle = style({
|
|
31
|
+
borderWidth: '1px',
|
|
32
|
+
outlineOffset: '2px',
|
|
33
|
+
});
|
|
34
|
+
`,
|
|
35
|
+
options: [{ allow: ['border-width', 'outline-offset', 'borderWidth', 'outlineOffset'] }],
|
|
36
|
+
name: 'respects allowlist for kebab and camelCase',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
code: `
|
|
40
|
+
import { recipe } from '@vanilla-extract/css';
|
|
41
|
+
const myRecipe = recipe({
|
|
42
|
+
base: { margin: '1rem' },
|
|
43
|
+
variants: {
|
|
44
|
+
size: {
|
|
45
|
+
sm: { padding: '0.5rem' },
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
`,
|
|
50
|
+
name: 'passes when no px in recipe base/variants',
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
code: `
|
|
54
|
+
import { style } from '@vanilla-extract/css';
|
|
55
|
+
const s = style({
|
|
56
|
+
border: '1px solid',
|
|
57
|
+
borderWidth: '2px'
|
|
58
|
+
});
|
|
59
|
+
`,
|
|
60
|
+
options: [{ allow: ['borderWidth', 'border'] }],
|
|
61
|
+
name: 'does not report whitelisted properties',
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
invalid: [
|
|
65
|
+
{
|
|
66
|
+
code: `
|
|
67
|
+
import { style } from '@vanilla-extract/css';
|
|
68
|
+
const myStyle = style({
|
|
69
|
+
margin: '10px',
|
|
70
|
+
padding: '2px',
|
|
71
|
+
});
|
|
72
|
+
`,
|
|
73
|
+
errors: [{ messageId: 'noPxUnit' }, { messageId: 'noPxUnit' }],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
code: `
|
|
77
|
+
import { style } from '@vanilla-extract/css';
|
|
78
|
+
const s = style({
|
|
79
|
+
'@media': {
|
|
80
|
+
'(min-width: 768px)': {
|
|
81
|
+
lineHeight: '12px',
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
selectors: {
|
|
85
|
+
'&:hover': { gap: '4px' }
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
`,
|
|
89
|
+
errors: [{ messageId: 'noPxUnit' }, { messageId: 'noPxUnit' }],
|
|
90
|
+
name: 'reports within nested @media and selectors',
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
code: `
|
|
94
|
+
import { recipe } from '@vanilla-extract/css';
|
|
95
|
+
const r = recipe({
|
|
96
|
+
base: { marginTop: '3px' },
|
|
97
|
+
variants: { size: { md: { paddingBottom: '6px' } } }
|
|
98
|
+
});
|
|
99
|
+
`,
|
|
100
|
+
errors: [{ messageId: 'noPxUnit' }, { messageId: 'noPxUnit' }],
|
|
101
|
+
name: 'reports within recipe base and variants',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
code: `
|
|
105
|
+
import { fontFace } from '@vanilla-extract/css';
|
|
106
|
+
fontFace({ sizeAdjust: '10px' });
|
|
107
|
+
`,
|
|
108
|
+
errors: [{ messageId: 'noPxUnit' }],
|
|
109
|
+
name: 'reports in fontFace() first-arg object (covers fontFace branch)',
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
code: `
|
|
113
|
+
import { globalFontFace } from '@vanilla-extract/css';
|
|
114
|
+
globalFontFace('MyFont', { lineGapOverride: '12px' });
|
|
115
|
+
`,
|
|
116
|
+
errors: [{ messageId: 'noPxUnit' }],
|
|
117
|
+
name: 'reports in globalFontFace() second-arg object (covers globalFontFace branch)',
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
code: `
|
|
121
|
+
import { globalStyle } from '@vanilla-extract/css';
|
|
122
|
+
globalStyle('html', { boxShadow: '0 1px 2px black' });
|
|
123
|
+
`,
|
|
124
|
+
errors: [{ messageId: 'noPxUnit' }],
|
|
125
|
+
name: 'reports in globalStyle() second-arg object (covers globalStyle branch)',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
code: `
|
|
129
|
+
import { globalKeyframes } from '@vanilla-extract/css';
|
|
130
|
+
globalKeyframes('fade', {
|
|
131
|
+
from: { margin: '5px' },
|
|
132
|
+
to: { padding: '3px' }
|
|
133
|
+
});
|
|
134
|
+
`,
|
|
135
|
+
errors: [{ messageId: 'noPxUnit' }, { messageId: 'noPxUnit' }],
|
|
136
|
+
name: 'reports in globalKeyframes() frames (covers globalKeyframes branch)',
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
code: `
|
|
140
|
+
import { style } from '@vanilla-extract/css';
|
|
141
|
+
const s = style({
|
|
142
|
+
margin: ` +
|
|
143
|
+
'`10px`' +
|
|
144
|
+
`,
|
|
145
|
+
});
|
|
146
|
+
`,
|
|
147
|
+
errors: [{ messageId: 'noPxUnit' }],
|
|
148
|
+
name: 'reports for simple template literal with px',
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
code: `
|
|
152
|
+
import { style } from '@vanilla-extract/css';
|
|
153
|
+
const s = style({
|
|
154
|
+
margin: ` +
|
|
155
|
+
'`${token}px`' +
|
|
156
|
+
`,
|
|
157
|
+
});
|
|
158
|
+
`,
|
|
159
|
+
errors: [{ messageId: 'noPxUnit' }],
|
|
160
|
+
name: 'reports for complex template literals with expressions containing px',
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
});
|
|
@@ -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,14 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Recursively processes a vanilla-extract style object and reports occurrences of 'px' units.
|
|
5
|
+
*
|
|
6
|
+
* - Skips properties present in the allow list (supports camelCase and kebab-case).
|
|
7
|
+
* - Traverses nested object values and delegates deeper traversal to callers for arrays/at-rules/selectors.
|
|
8
|
+
* - Provides fix suggestions for string literals and simple template literals (no expressions).
|
|
9
|
+
*
|
|
10
|
+
* @param context ESLint rule context used to report diagnostics and apply suggestions.
|
|
11
|
+
* @param node The ObjectExpression node representing the style object to inspect.
|
|
12
|
+
* @param allowSet Set of property names (camelCase or kebab-case) that are allowed to contain 'px'.
|
|
13
|
+
*/
|
|
14
|
+
export declare const processNoPxUnitInStyleObject: (context: Rule.RuleContext, node: TSESTree.ObjectExpression, allowSet: Set<string>) => void;
|
|
@@ -0,0 +1,104 @@
|
|
|
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, (match) => `-${match.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((quasi) => quasi.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
|
+
/**
|
|
24
|
+
* Recursively processes a vanilla-extract style object and reports occurrences of 'px' units.
|
|
25
|
+
*
|
|
26
|
+
* - Skips properties present in the allow list (supports camelCase and kebab-case).
|
|
27
|
+
* - Traverses nested object values and delegates deeper traversal to callers for arrays/at-rules/selectors.
|
|
28
|
+
* - Provides fix suggestions for string literals and simple template literals (no expressions).
|
|
29
|
+
*
|
|
30
|
+
* @param context ESLint rule context used to report diagnostics and apply suggestions.
|
|
31
|
+
* @param node The ObjectExpression node representing the style object to inspect.
|
|
32
|
+
* @param allowSet Set of property names (camelCase or kebab-case) that are allowed to contain 'px'.
|
|
33
|
+
*/
|
|
34
|
+
export const processNoPxUnitInStyleObject = (context, node, allowSet) => {
|
|
35
|
+
for (const property of node.properties) {
|
|
36
|
+
if (property.type !== AST_NODE_TYPES.Property)
|
|
37
|
+
continue;
|
|
38
|
+
// Determine property name when possible
|
|
39
|
+
let propertyName = null;
|
|
40
|
+
if (property.key.type === AST_NODE_TYPES.Identifier) {
|
|
41
|
+
propertyName = property.key.name;
|
|
42
|
+
}
|
|
43
|
+
else if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
|
|
44
|
+
propertyName = property.key.value;
|
|
45
|
+
}
|
|
46
|
+
// Recurse into known nested containers
|
|
47
|
+
if (propertyName === '@media' || propertyName === 'selectors') {
|
|
48
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
49
|
+
for (const nested of property.value.properties) {
|
|
50
|
+
if (nested.type === AST_NODE_TYPES.Property && nested.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
51
|
+
processNoPxUnitInStyleObject(context, nested.value, allowSet);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
// Traverse any nested object
|
|
58
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
59
|
+
processNoPxUnitInStyleObject(context, property.value, allowSet);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
// Skip if property is whitelisted (supports both camelCase and kebab-case)
|
|
63
|
+
if (propertyName) {
|
|
64
|
+
const kebab = toKebab(propertyName);
|
|
65
|
+
if (allowSet.has(propertyName) || allowSet.has(kebab)) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Check string or template literal values
|
|
70
|
+
const text = getValueText(property.value);
|
|
71
|
+
if (text && containsPx(text)) {
|
|
72
|
+
const fixability = canSuggestFix(property.value);
|
|
73
|
+
context.report({
|
|
74
|
+
node: property.value,
|
|
75
|
+
messageId: 'noPxUnit',
|
|
76
|
+
suggest: fixability
|
|
77
|
+
? [
|
|
78
|
+
{
|
|
79
|
+
messageId: 'removePx',
|
|
80
|
+
fix: (fixer) => {
|
|
81
|
+
const newText = replacePxWith(text, '');
|
|
82
|
+
if (fixability === 'literal') {
|
|
83
|
+
return fixer.replaceText(property.value, `'${newText}'`);
|
|
84
|
+
}
|
|
85
|
+
// simple template with no expressions
|
|
86
|
+
return fixer.replaceText(property.value, `\`${newText}\``);
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
messageId: 'replaceWithRem',
|
|
91
|
+
fix: (fixer) => {
|
|
92
|
+
const newText = replacePxWith(text, 'rem');
|
|
93
|
+
if (fixability === 'literal') {
|
|
94
|
+
return fixer.replaceText(property.value, `'${newText}'`);
|
|
95
|
+
}
|
|
96
|
+
return fixer.replaceText(property.value, `\`${newText}\``);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
]
|
|
100
|
+
: undefined,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* Creates ESLint rule visitors for detecting and reporting 'px' units in vanilla-extract style objects.
|
|
4
|
+
* - Tracks calls to vanilla-extract APIs (style, recipe, keyframes, globalStyle, fontFace, etc.).
|
|
5
|
+
* This visitor only orchestrates traversal; the actual reporting and suggestion logic lives in the processor.
|
|
6
|
+
* - Respects the `allow` option (camelCase or kebab-case) by passing it as a Set to the processor.
|
|
7
|
+
*
|
|
8
|
+
* @param context ESLint rule context used to read options and report diagnostics.
|
|
9
|
+
* @returns Rule listener that inspects vanilla-extract call expressions and processes style objects.
|
|
10
|
+
*/
|
|
11
|
+
export declare const createNoPxUnitVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
|