@antebudimir/eslint-plugin-vanilla-extract 1.11.0 → 1.12.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 +116 -11
- package/dist/css-rules/no-trailing-zero/_tests_/no-trailing-zero.test.d.ts +1 -0
- package/dist/css-rules/no-trailing-zero/_tests_/no-trailing-zero.test.js +537 -0
- package/dist/css-rules/no-trailing-zero/index.d.ts +1 -0
- package/dist/css-rules/no-trailing-zero/index.js +1 -0
- package/dist/css-rules/no-trailing-zero/rule-definition.d.ts +3 -0
- package/dist/css-rules/no-trailing-zero/rule-definition.js +20 -0
- package/dist/css-rules/no-trailing-zero/trailing-zero-processor.d.ts +31 -0
- package/dist/css-rules/no-trailing-zero/trailing-zero-processor.js +165 -0
- package/dist/css-rules/no-trailing-zero/trailing-zero-visitor-creator.d.ts +9 -0
- package/dist/css-rules/no-trailing-zero/trailing-zero-visitor-creator.js +85 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,16 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract)
|
|
6
6
|

|
|
7
7
|
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
**Maintaining this plugin takes time and care! If you find it valuable, please consider supporting its development:**
|
|
11
|
+
💖 GitHub Sponsors: <https://github.com/sponsors/antebudimir>
|
|
12
|
+
☕ Ko-fi: <https://ko-fi.com/antebudimir>
|
|
13
|
+
💸 Revolut: <https://revolut.me/antebudimir>
|
|
14
|
+
🟡 Liberapay: <https://liberapay.com/antebudimir>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
8
18
|
An ESLint plugin for enforcing best practices in
|
|
9
19
|
[vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract) CSS styles, including CSS property ordering
|
|
10
20
|
and additional linting rules. Available presets are for alphabetical and
|
|
@@ -67,7 +77,7 @@ There are two main ways to configure this plugin in your ESLint flat config:
|
|
|
67
77
|
|
|
68
78
|
### Option 1: Using extends (recommended, available from v1.10.0)
|
|
69
79
|
|
|
70
|
-
The simplest way to apply the recommended ruleset:
|
|
80
|
+
The simplest and most concise way to apply the recommended ruleset:
|
|
71
81
|
|
|
72
82
|
```typescript
|
|
73
83
|
import { defineConfig } from 'eslint/config';
|
|
@@ -78,13 +88,18 @@ export default defineConfig([
|
|
|
78
88
|
files: ['**/*.css.ts'],
|
|
79
89
|
ignores: ['src/**/theme-contract.css.ts'],
|
|
80
90
|
extends: [vanillaExtract.configs.recommended],
|
|
91
|
+
// You can still override rules when using extends
|
|
92
|
+
// rules: {
|
|
93
|
+
// 'vanilla-extract/concentric-order': 'off',
|
|
94
|
+
// 'vanilla-extract/alphabetical-order': 'error',
|
|
95
|
+
// },
|
|
81
96
|
},
|
|
82
97
|
];
|
|
83
98
|
```
|
|
84
99
|
|
|
85
|
-
### Option 2: Using plugins with explicit rule
|
|
100
|
+
### Option 2: Using plugins with explicit rule spreading
|
|
86
101
|
|
|
87
|
-
This approach
|
|
102
|
+
This approach is more explicit - you manually register the plugin and spread the recommended rules:
|
|
88
103
|
|
|
89
104
|
```typescript
|
|
90
105
|
import { defineConfig } from 'eslint/config';
|
|
@@ -106,13 +121,64 @@ export default defineConfig([
|
|
|
106
121
|
// 'vanilla-extract/no-empty-style-blocks': 'off', // Disable a recommended rule
|
|
107
122
|
// 'vanilla-extract/no-zero-unit': 'warn', // Change severity from error to warn
|
|
108
123
|
|
|
109
|
-
//
|
|
110
|
-
// 'vanilla-extract/
|
|
124
|
+
// Switch to a different ordering rule (see "Important" section below)
|
|
125
|
+
// 'vanilla-extract/concentric-order': 'off',
|
|
126
|
+
// 'vanilla-extract/alphabetical-order': 'error',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### Important: Only Enable One Ordering Rule at a Time
|
|
133
|
+
|
|
134
|
+
The plugin includes three CSS property ordering rules: `alphabetical-order`, `concentric-order`, and `custom-order`. **Only one ordering rule should be enabled at a time** to avoid conflicting auto-fixes.
|
|
135
|
+
|
|
136
|
+
If you want to use a different ordering rule than the one in the recommended config, you must explicitly disable the default rule.
|
|
137
|
+
|
|
138
|
+
##### Example: Switching from concentric to alphabetical ordering
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
export default [
|
|
142
|
+
{
|
|
143
|
+
files: ['**/*.css.ts'],
|
|
144
|
+
plugins: {
|
|
145
|
+
'vanilla-extract': vanillaExtract,
|
|
146
|
+
},
|
|
147
|
+
rules: {
|
|
148
|
+
...vanillaExtract.configs.recommended.rules,
|
|
149
|
+
'vanilla-extract/concentric-order': 'off', // Disable the default
|
|
150
|
+
'vanilla-extract/alphabetical-order': 'error', // Enable alphabetical
|
|
111
151
|
},
|
|
112
152
|
},
|
|
113
153
|
];
|
|
114
154
|
```
|
|
115
155
|
|
|
156
|
+
##### Example: Using custom-order instead of the recommended concentric-order
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
export default [
|
|
160
|
+
{
|
|
161
|
+
files: ['**/*.css.ts'],
|
|
162
|
+
plugins: {
|
|
163
|
+
'vanilla-extract': vanillaExtract,
|
|
164
|
+
},
|
|
165
|
+
rules: {
|
|
166
|
+
...vanillaExtract.configs.recommended.rules,
|
|
167
|
+
'vanilla-extract/concentric-order': 'off', // Disable the default
|
|
168
|
+
'vanilla-extract/custom-order': [
|
|
169
|
+
'error',
|
|
170
|
+
{
|
|
171
|
+
groupOrder: ['font', 'dimensions', 'margin', 'padding', 'position', 'border'],
|
|
172
|
+
sortRemainingProperties: 'alphabetical',
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
];
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
> **⚠️ Warning:** If multiple ordering rules are enabled simultaneously, they will produce conflicting auto-fixes that ESLint cannot apply, causing auto-fix on save to fail. Always ensure only one ordering rule is active.
|
|
181
|
+
|
|
116
182
|
### Using with FlatCompat (for ESLint 8.57.0 & 8.57.1)
|
|
117
183
|
|
|
118
184
|
If you're migrating from legacy ESLint configurations, you can use the `FlatCompat` utility to convert them while adding
|
|
@@ -193,10 +259,16 @@ The recommended configuration enables the following rules with error severity:
|
|
|
193
259
|
|
|
194
260
|
- `vanilla-extract/concentric-order`: Enforces concentric CSS property ordering
|
|
195
261
|
- `vanilla-extract/no-empty-style-blocks`: Prevents empty style blocks
|
|
196
|
-
- `vanilla-extract/no-
|
|
197
|
-
- `vanilla-extract/no-
|
|
262
|
+
- `vanilla-extract/no-trailing-zero`: Disallows trailing zeros in numeric CSS values
|
|
263
|
+
- `vanilla-extract/no-unknown-unit`: Prohibits usage of unrecognized CSS units
|
|
264
|
+
- `vanilla-extract/no-zero-unit`: Removes unnecessary units for zero values
|
|
265
|
+
|
|
266
|
+
**Additional rules available** (not enabled by default):
|
|
198
267
|
|
|
199
|
-
|
|
268
|
+
- `vanilla-extract/alphabetical-order`: Alternative ordering rule (alphabetical sorting)
|
|
269
|
+
- `vanilla-extract/custom-order`: Alternative ordering rule (custom group-based sorting)
|
|
270
|
+
|
|
271
|
+
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.
|
|
200
272
|
|
|
201
273
|
### Custom Configuration
|
|
202
274
|
|
|
@@ -230,6 +302,8 @@ export default [
|
|
|
230
302
|
];
|
|
231
303
|
```
|
|
232
304
|
|
|
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
|
+
|
|
233
307
|
## Rules
|
|
234
308
|
|
|
235
309
|
### vanilla-extract/alphabetical-order
|
|
@@ -391,6 +465,37 @@ export const recipeWithEmptyVariants = recipe({
|
|
|
391
465
|
});
|
|
392
466
|
```
|
|
393
467
|
|
|
468
|
+
## vanilla-extract/no-trailing-zero
|
|
469
|
+
|
|
470
|
+
This rule disallows trailing zeros in numeric CSS values within vanilla-extract style objects. It helps maintain cleaner
|
|
471
|
+
and more consistent CSS by removing unnecessary trailing zeros from decimal numbers.
|
|
472
|
+
|
|
473
|
+
```typescript
|
|
474
|
+
// ❌ Incorrect
|
|
475
|
+
import { style } from '@vanilla-extract/css';
|
|
476
|
+
|
|
477
|
+
export const myStyle = style({
|
|
478
|
+
margin: '1.0px',
|
|
479
|
+
padding: '2.50rem',
|
|
480
|
+
opacity: 1.0,
|
|
481
|
+
lineHeight: 2.50,
|
|
482
|
+
width: '0.0em',
|
|
483
|
+
transition: 'all 0.30s ease',
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
// ✅ Correct
|
|
487
|
+
import { style } from '@vanilla-extract/css';
|
|
488
|
+
|
|
489
|
+
export const myStyle = style({
|
|
490
|
+
margin: '1px',
|
|
491
|
+
padding: '2.5rem',
|
|
492
|
+
opacity: 1,
|
|
493
|
+
lineHeight: 2.5,
|
|
494
|
+
width: '0',
|
|
495
|
+
transition: 'all 0.3s ease',
|
|
496
|
+
});
|
|
497
|
+
```
|
|
498
|
+
|
|
394
499
|
## vanilla-extract/no-unknown-unit
|
|
395
500
|
|
|
396
501
|
This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units
|
|
@@ -539,17 +644,17 @@ The roadmap outlines the project's current status and future plans:
|
|
|
539
644
|
- Recommended ESLint configuration for the plugin.
|
|
540
645
|
- `no-zero-unit` rule to disallow units when the value is zero.
|
|
541
646
|
- `no-unknown-unit` rule to disallow unknown units.
|
|
542
|
-
-
|
|
647
|
+
- `no-trailing-zero` rule to disallow trailing zeros in numbers.
|
|
648
|
+
- Support for using the plugin's recommended config via the extends field (as discussed in
|
|
543
649
|
[issue #3](https://github.com/antebudimir/eslint-plugin-vanilla-extract/issues/3))
|
|
544
650
|
- Comprehensive rule testing.
|
|
545
651
|
|
|
546
652
|
### Current Work
|
|
547
653
|
|
|
548
|
-
- `no-
|
|
654
|
+
- `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
|
|
549
655
|
|
|
550
656
|
### Upcoming Features
|
|
551
657
|
|
|
552
|
-
- `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
|
|
553
658
|
- `prefer-logical-properties` rule to enforce use of logical properties.
|
|
554
659
|
- `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
|
|
555
660
|
- `no-global-style` rule to disallow use of `globalStyle` function.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import tsParser from '@typescript-eslint/parser';
|
|
2
|
+
import { run } from 'eslint-vitest-rule-tester';
|
|
3
|
+
import noTrailingZeroRule from '../rule-definition.js';
|
|
4
|
+
run({
|
|
5
|
+
name: 'vanilla-extract/no-trailing-zero',
|
|
6
|
+
rule: noTrailingZeroRule,
|
|
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: '0',
|
|
20
|
+
padding: '1px',
|
|
21
|
+
width: '1.5rem',
|
|
22
|
+
height: '0.5em',
|
|
23
|
+
fontSize: '2rem',
|
|
24
|
+
});
|
|
25
|
+
`,
|
|
26
|
+
name: 'should allow values without trailing zeros',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
code: `
|
|
30
|
+
import { style } from '@vanilla-extract/css';
|
|
31
|
+
const myStyle = style({
|
|
32
|
+
margin: 0,
|
|
33
|
+
padding: 1,
|
|
34
|
+
opacity: 0.5,
|
|
35
|
+
lineHeight: 1.5,
|
|
36
|
+
});
|
|
37
|
+
`,
|
|
38
|
+
name: 'should allow numeric literals without trailing zeros',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
code: `
|
|
42
|
+
import { recipe } from '@vanilla-extract/css';
|
|
43
|
+
const myRecipe = recipe({
|
|
44
|
+
base: {
|
|
45
|
+
margin: '1px',
|
|
46
|
+
padding: '0.5rem',
|
|
47
|
+
},
|
|
48
|
+
variants: {
|
|
49
|
+
size: {
|
|
50
|
+
small: {
|
|
51
|
+
height: '10px',
|
|
52
|
+
width: '0.75em',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
`,
|
|
58
|
+
name: 'should allow recipe values without trailing zeros',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
code: `
|
|
62
|
+
import { style } from '@vanilla-extract/css';
|
|
63
|
+
const myStyle = style({
|
|
64
|
+
...spreadProps,
|
|
65
|
+
margin: '1px',
|
|
66
|
+
'@media': {
|
|
67
|
+
'1.0rem': '0.5rem' // Key shouldn't be checked
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
`,
|
|
71
|
+
name: 'should ignore spread elements and object keys',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
code: `
|
|
75
|
+
import { style } from '@vanilla-extract/css';
|
|
76
|
+
const myStyle = style({
|
|
77
|
+
margin: \`1.0\${someUnit}\`, // Template literal
|
|
78
|
+
padding: someVariable,
|
|
79
|
+
width: calculateWidth(),
|
|
80
|
+
});
|
|
81
|
+
`,
|
|
82
|
+
name: 'should ignore non-literal values',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
code: `
|
|
86
|
+
import { globalStyle } from '@vanilla-extract/css';
|
|
87
|
+
const callExpression = someObject.fontFace({ src: '...' }); // Non-Identifier callee
|
|
88
|
+
`,
|
|
89
|
+
name: 'should ignore member expression callees',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
code: `
|
|
93
|
+
import { fontFace } from '@vanilla-extract/css';
|
|
94
|
+
fontFace(); // Missing arguments
|
|
95
|
+
`,
|
|
96
|
+
name: 'should handle missing fontFace arguments',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
code: `
|
|
100
|
+
import { globalFontFace } from '@vanilla-extract/css';
|
|
101
|
+
globalFontFace('my-font'); // Missing style argument
|
|
102
|
+
`,
|
|
103
|
+
name: 'should handle missing globalFontFace style argument',
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
code: `
|
|
107
|
+
import { style } from '@vanilla-extract/css';
|
|
108
|
+
const myStyle = style({
|
|
109
|
+
margin: '10px',
|
|
110
|
+
padding: '100rem',
|
|
111
|
+
width: '1000%',
|
|
112
|
+
});
|
|
113
|
+
`,
|
|
114
|
+
name: 'should allow integers without decimal points',
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
code: `
|
|
118
|
+
import { style } from '@vanilla-extract/css';
|
|
119
|
+
const myStyle = style({
|
|
120
|
+
margin: '11.01rem',
|
|
121
|
+
padding: '2.05em',
|
|
122
|
+
width: '0.101%',
|
|
123
|
+
height: '10.001px',
|
|
124
|
+
});
|
|
125
|
+
`,
|
|
126
|
+
name: 'should not flag zeros in the middle of decimal numbers',
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
invalid: [
|
|
130
|
+
{
|
|
131
|
+
code: `
|
|
132
|
+
import { style } from '@vanilla-extract/css';
|
|
133
|
+
const myStyle = style({
|
|
134
|
+
margin: '1.0px',
|
|
135
|
+
padding: '2.50rem',
|
|
136
|
+
});
|
|
137
|
+
`,
|
|
138
|
+
errors: [{ messageId: 'trailingZero' }, { messageId: 'trailingZero' }],
|
|
139
|
+
output: `
|
|
140
|
+
import { style } from '@vanilla-extract/css';
|
|
141
|
+
const myStyle = style({
|
|
142
|
+
margin: '1px',
|
|
143
|
+
padding: '2.5rem',
|
|
144
|
+
});
|
|
145
|
+
`,
|
|
146
|
+
name: 'should fix trailing zeros in string values with units',
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
code: `
|
|
150
|
+
import { style } from '@vanilla-extract/css';
|
|
151
|
+
const myStyle = style({
|
|
152
|
+
opacity: 1.0,
|
|
153
|
+
lineHeight: 2.50,
|
|
154
|
+
});
|
|
155
|
+
`,
|
|
156
|
+
errors: [{ messageId: 'trailingZero' }, { messageId: 'trailingZero' }],
|
|
157
|
+
output: `
|
|
158
|
+
import { style } from '@vanilla-extract/css';
|
|
159
|
+
const myStyle = style({
|
|
160
|
+
opacity: 1,
|
|
161
|
+
lineHeight: 2.5,
|
|
162
|
+
});
|
|
163
|
+
`,
|
|
164
|
+
name: 'should fix trailing zeros in numeric literals',
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
code: `
|
|
168
|
+
import { style } from '@vanilla-extract/css';
|
|
169
|
+
const myStyle = style({
|
|
170
|
+
margin: '0.0',
|
|
171
|
+
padding: '0.00px',
|
|
172
|
+
width: '0.0rem',
|
|
173
|
+
});
|
|
174
|
+
`,
|
|
175
|
+
errors: [{ messageId: 'trailingZero' }, { messageId: 'trailingZero' }, { messageId: 'trailingZero' }],
|
|
176
|
+
output: `
|
|
177
|
+
import { style } from '@vanilla-extract/css';
|
|
178
|
+
const myStyle = style({
|
|
179
|
+
margin: '0',
|
|
180
|
+
padding: '0',
|
|
181
|
+
width: '0',
|
|
182
|
+
});
|
|
183
|
+
`,
|
|
184
|
+
name: 'should convert 0.0 to 0',
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
code: `
|
|
188
|
+
import { recipe } from '@vanilla-extract/css';
|
|
189
|
+
const myRecipe = recipe({
|
|
190
|
+
base: {
|
|
191
|
+
margin: '1.0px',
|
|
192
|
+
},
|
|
193
|
+
variants: {
|
|
194
|
+
size: {
|
|
195
|
+
small: {
|
|
196
|
+
height: '2.50vh',
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
`,
|
|
202
|
+
errors: [{ messageId: 'trailingZero' }, { messageId: 'trailingZero' }],
|
|
203
|
+
output: `
|
|
204
|
+
import { recipe } from '@vanilla-extract/css';
|
|
205
|
+
const myRecipe = recipe({
|
|
206
|
+
base: {
|
|
207
|
+
margin: '1px',
|
|
208
|
+
},
|
|
209
|
+
variants: {
|
|
210
|
+
size: {
|
|
211
|
+
small: {
|
|
212
|
+
height: '2.5vh',
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
`,
|
|
218
|
+
name: 'should handle recipe trailing zeros',
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
code: `
|
|
222
|
+
import { style } from '@vanilla-extract/css';
|
|
223
|
+
const myStyle = style({
|
|
224
|
+
margin: '1.0px',
|
|
225
|
+
'@media': {
|
|
226
|
+
'(min-width: 768px)': {
|
|
227
|
+
padding: '2.50rem'
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
`,
|
|
232
|
+
errors: 2,
|
|
233
|
+
output: `
|
|
234
|
+
import { style } from '@vanilla-extract/css';
|
|
235
|
+
const myStyle = style({
|
|
236
|
+
margin: '1px',
|
|
237
|
+
'@media': {
|
|
238
|
+
'(min-width: 768px)': {
|
|
239
|
+
padding: '2.5rem'
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
`,
|
|
244
|
+
name: 'should handle nested media queries',
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
code: `
|
|
248
|
+
import { style } from '@vanilla-extract/css';
|
|
249
|
+
const myStyle = style({
|
|
250
|
+
'::before': {
|
|
251
|
+
content: '""',
|
|
252
|
+
margin: '1.0px'
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
`,
|
|
256
|
+
errors: 1,
|
|
257
|
+
output: `
|
|
258
|
+
import { style } from '@vanilla-extract/css';
|
|
259
|
+
const myStyle = style({
|
|
260
|
+
'::before': {
|
|
261
|
+
content: '""',
|
|
262
|
+
margin: '1px'
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
`,
|
|
266
|
+
name: 'should handle pseudo-elements',
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
code: `
|
|
270
|
+
import { style } from '@vanilla-extract/css';
|
|
271
|
+
const myStyle = style({
|
|
272
|
+
margin: '1.0px',
|
|
273
|
+
nested: {
|
|
274
|
+
object: {
|
|
275
|
+
padding: '2.50rem',
|
|
276
|
+
deeper: {
|
|
277
|
+
width: '3.00%'
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
`,
|
|
283
|
+
errors: 3,
|
|
284
|
+
output: `
|
|
285
|
+
import { style } from '@vanilla-extract/css';
|
|
286
|
+
const myStyle = style({
|
|
287
|
+
margin: '1px',
|
|
288
|
+
nested: {
|
|
289
|
+
object: {
|
|
290
|
+
padding: '2.5rem',
|
|
291
|
+
deeper: {
|
|
292
|
+
width: '3%'
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
`,
|
|
298
|
+
name: 'should handle multiple levels of nesting',
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
code: `
|
|
302
|
+
import { fontFace, globalFontFace } from '@vanilla-extract/css';
|
|
303
|
+
|
|
304
|
+
fontFace({
|
|
305
|
+
src: '...',
|
|
306
|
+
lineGap: '1.0rem'
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
globalFontFace('my-font', {
|
|
310
|
+
src: '...',
|
|
311
|
+
sizeAdjust: '100.0%'
|
|
312
|
+
});
|
|
313
|
+
`,
|
|
314
|
+
errors: 2,
|
|
315
|
+
output: `
|
|
316
|
+
import { fontFace, globalFontFace } from '@vanilla-extract/css';
|
|
317
|
+
|
|
318
|
+
fontFace({
|
|
319
|
+
src: '...',
|
|
320
|
+
lineGap: '1rem'
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
globalFontFace('my-font', {
|
|
324
|
+
src: '...',
|
|
325
|
+
sizeAdjust: '100%'
|
|
326
|
+
});
|
|
327
|
+
`,
|
|
328
|
+
name: 'should handle fontFace and globalFontFace arguments',
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
code: `
|
|
332
|
+
import { globalKeyframes, globalStyle } from '@vanilla-extract/css';
|
|
333
|
+
|
|
334
|
+
globalKeyframes('spin', {
|
|
335
|
+
'0%': { transform: 'rotate(0deg)' },
|
|
336
|
+
'100%': { transform: 'rotate(360.0deg)' }
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
globalStyle('html', {
|
|
340
|
+
margin: '1.0px',
|
|
341
|
+
padding: '2.50rem'
|
|
342
|
+
});
|
|
343
|
+
`,
|
|
344
|
+
errors: 3,
|
|
345
|
+
output: `
|
|
346
|
+
import { globalKeyframes, globalStyle } from '@vanilla-extract/css';
|
|
347
|
+
|
|
348
|
+
globalKeyframes('spin', {
|
|
349
|
+
'0%': { transform: 'rotate(0deg)' },
|
|
350
|
+
'100%': { transform: 'rotate(360deg)' }
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
globalStyle('html', {
|
|
354
|
+
margin: '1px',
|
|
355
|
+
padding: '2.5rem'
|
|
356
|
+
});
|
|
357
|
+
`,
|
|
358
|
+
name: 'should handle globalKeyframes and globalStyle arguments',
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
code: `
|
|
362
|
+
import { globalStyle } from '@vanilla-extract/css';
|
|
363
|
+
globalStyle('html', {
|
|
364
|
+
'@media': {
|
|
365
|
+
'(min-width: 768px)': {
|
|
366
|
+
margin: '1.0px'
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
`,
|
|
371
|
+
errors: 1,
|
|
372
|
+
output: `
|
|
373
|
+
import { globalStyle } from '@vanilla-extract/css';
|
|
374
|
+
globalStyle('html', {
|
|
375
|
+
'@media': {
|
|
376
|
+
'(min-width: 768px)': {
|
|
377
|
+
margin: '1px'
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
`,
|
|
382
|
+
name: 'should handle nested globalStyle arguments',
|
|
383
|
+
},
|
|
384
|
+
{
|
|
385
|
+
code: `
|
|
386
|
+
import { style } from '@vanilla-extract/css';
|
|
387
|
+
const myStyle = style({
|
|
388
|
+
margin: '-1.0px',
|
|
389
|
+
padding: '-2.50rem',
|
|
390
|
+
top: '-0.0vh',
|
|
391
|
+
});
|
|
392
|
+
`,
|
|
393
|
+
errors: [{ messageId: 'trailingZero' }, { messageId: 'trailingZero' }, { messageId: 'trailingZero' }],
|
|
394
|
+
output: `
|
|
395
|
+
import { style } from '@vanilla-extract/css';
|
|
396
|
+
const myStyle = style({
|
|
397
|
+
margin: '-1px',
|
|
398
|
+
padding: '-2.5rem',
|
|
399
|
+
top: '0',
|
|
400
|
+
});
|
|
401
|
+
`,
|
|
402
|
+
name: 'should handle negative values with trailing zeros',
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
code: `
|
|
406
|
+
import { style } from '@vanilla-extract/css';
|
|
407
|
+
const myStyle = style({
|
|
408
|
+
margin: '1.50em',
|
|
409
|
+
padding: '0.50rem',
|
|
410
|
+
width: '10.00%',
|
|
411
|
+
});
|
|
412
|
+
`,
|
|
413
|
+
errors: [{ messageId: 'trailingZero' }, { messageId: 'trailingZero' }, { messageId: 'trailingZero' }],
|
|
414
|
+
output: `
|
|
415
|
+
import { style } from '@vanilla-extract/css';
|
|
416
|
+
const myStyle = style({
|
|
417
|
+
margin: '1.5em',
|
|
418
|
+
padding: '0.5rem',
|
|
419
|
+
width: '10%',
|
|
420
|
+
});
|
|
421
|
+
`,
|
|
422
|
+
name: 'should remove trailing zeros from decimal values',
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
code: `
|
|
426
|
+
import { style } from '@vanilla-extract/css';
|
|
427
|
+
const myStyle = style({
|
|
428
|
+
padding: '1.0px 2.50rem 3.00em 0.50vh',
|
|
429
|
+
});
|
|
430
|
+
`,
|
|
431
|
+
errors: 1,
|
|
432
|
+
output: `
|
|
433
|
+
import { style } from '@vanilla-extract/css';
|
|
434
|
+
const myStyle = style({
|
|
435
|
+
padding: '1px 2.5rem 3em 0.5vh',
|
|
436
|
+
});
|
|
437
|
+
`,
|
|
438
|
+
name: 'should handle multiple values in a single string',
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
code: `
|
|
442
|
+
import { style } from '@vanilla-extract/css';
|
|
443
|
+
const myStyle = style({
|
|
444
|
+
transition: 'all 0.30s ease',
|
|
445
|
+
animation: 'spin 2.0s linear',
|
|
446
|
+
});
|
|
447
|
+
`,
|
|
448
|
+
errors: 2,
|
|
449
|
+
output: `
|
|
450
|
+
import { style } from '@vanilla-extract/css';
|
|
451
|
+
const myStyle = style({
|
|
452
|
+
transition: 'all 0.3s ease',
|
|
453
|
+
animation: 'spin 2s linear',
|
|
454
|
+
});
|
|
455
|
+
`,
|
|
456
|
+
name: 'should handle time units',
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
code: `
|
|
460
|
+
import { style } from '@vanilla-extract/css';
|
|
461
|
+
const myStyle = style({
|
|
462
|
+
transform: 'rotate(45.0deg)',
|
|
463
|
+
filter: 'hue-rotate(180.00deg)',
|
|
464
|
+
});
|
|
465
|
+
`,
|
|
466
|
+
errors: 2,
|
|
467
|
+
output: `
|
|
468
|
+
import { style } from '@vanilla-extract/css';
|
|
469
|
+
const myStyle = style({
|
|
470
|
+
transform: 'rotate(45deg)',
|
|
471
|
+
filter: 'hue-rotate(180deg)',
|
|
472
|
+
});
|
|
473
|
+
`,
|
|
474
|
+
name: 'should handle angle units',
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
code: `
|
|
478
|
+
import { styleVariants } from '@vanilla-extract/css';
|
|
479
|
+
const variants = styleVariants({
|
|
480
|
+
small: { padding: '1.0px' },
|
|
481
|
+
medium: { padding: '2.50px' },
|
|
482
|
+
large: { padding: '3.00px' },
|
|
483
|
+
});
|
|
484
|
+
`,
|
|
485
|
+
errors: 3,
|
|
486
|
+
output: `
|
|
487
|
+
import { styleVariants } from '@vanilla-extract/css';
|
|
488
|
+
const variants = styleVariants({
|
|
489
|
+
small: { padding: '1px' },
|
|
490
|
+
medium: { padding: '2.5px' },
|
|
491
|
+
large: { padding: '3px' },
|
|
492
|
+
});
|
|
493
|
+
`,
|
|
494
|
+
name: 'should handle styleVariants',
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
code: `
|
|
498
|
+
import { keyframes } from '@vanilla-extract/css';
|
|
499
|
+
const spin = keyframes({
|
|
500
|
+
'0%': { transform: 'rotate(0.0deg)' },
|
|
501
|
+
'50%': { transform: 'rotate(180.0deg)' },
|
|
502
|
+
'100%': { transform: 'rotate(360.0deg)' },
|
|
503
|
+
});
|
|
504
|
+
`,
|
|
505
|
+
errors: 3,
|
|
506
|
+
output: `
|
|
507
|
+
import { keyframes } from '@vanilla-extract/css';
|
|
508
|
+
const spin = keyframes({
|
|
509
|
+
'0%': { transform: 'rotate(0deg)' },
|
|
510
|
+
'50%': { transform: 'rotate(180deg)' },
|
|
511
|
+
'100%': { transform: 'rotate(360deg)' },
|
|
512
|
+
});
|
|
513
|
+
`,
|
|
514
|
+
name: 'should handle keyframes',
|
|
515
|
+
},
|
|
516
|
+
{
|
|
517
|
+
code: `
|
|
518
|
+
import { style } from '@vanilla-extract/css';
|
|
519
|
+
const myStyle = style({
|
|
520
|
+
margin: '1.000px',
|
|
521
|
+
padding: '2.5000rem',
|
|
522
|
+
width: '0.00000em',
|
|
523
|
+
});
|
|
524
|
+
`,
|
|
525
|
+
errors: 3,
|
|
526
|
+
output: `
|
|
527
|
+
import { style } from '@vanilla-extract/css';
|
|
528
|
+
const myStyle = style({
|
|
529
|
+
margin: '1px',
|
|
530
|
+
padding: '2.5rem',
|
|
531
|
+
width: '0',
|
|
532
|
+
});
|
|
533
|
+
`,
|
|
534
|
+
name: 'should handle multiple trailing zeros',
|
|
535
|
+
},
|
|
536
|
+
],
|
|
537
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './rule-definition.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './rule-definition.js';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createTrailingZeroVisitors } from './trailing-zero-visitor-creator.js';
|
|
2
|
+
const noTrailingZeroRule = {
|
|
3
|
+
meta: {
|
|
4
|
+
type: 'suggestion',
|
|
5
|
+
docs: {
|
|
6
|
+
description: 'disallow trailing zeros in numeric CSS values',
|
|
7
|
+
category: 'Stylistic Issues',
|
|
8
|
+
recommended: true,
|
|
9
|
+
},
|
|
10
|
+
fixable: 'code',
|
|
11
|
+
schema: [],
|
|
12
|
+
messages: {
|
|
13
|
+
trailingZero: 'Numeric value "{{ value }}" has unnecessary trailing zeros. Use "{{ fixed }}" instead.',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return createTrailingZeroVisitors(context);
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
export default noTrailingZeroRule;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a value has trailing zeros and returns the fixed value if needed.
|
|
5
|
+
*
|
|
6
|
+
* @param value The string value to check
|
|
7
|
+
* @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
|
|
8
|
+
*/
|
|
9
|
+
export declare const checkTrailingZero: (value: string) => {
|
|
10
|
+
hasTrailingZero: boolean;
|
|
11
|
+
fixed: string;
|
|
12
|
+
} | null;
|
|
13
|
+
/**
|
|
14
|
+
* Processes a single string value and checks for trailing zeros in all numeric values.
|
|
15
|
+
* Handles strings with multiple numeric values (e.g., "1.0px 2.50em").
|
|
16
|
+
* Also handles values within function calls (e.g., "rotate(45.0deg)").
|
|
17
|
+
*
|
|
18
|
+
* @param value The string value to process
|
|
19
|
+
* @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
|
|
20
|
+
*/
|
|
21
|
+
export declare const processStringValue: (value: string) => {
|
|
22
|
+
hasTrailingZero: boolean;
|
|
23
|
+
fixed: string;
|
|
24
|
+
} | null;
|
|
25
|
+
/**
|
|
26
|
+
* Recursively processes a style object, reporting and fixing instances of trailing zeros in numeric values.
|
|
27
|
+
*
|
|
28
|
+
* @param ruleContext The ESLint rule context.
|
|
29
|
+
* @param node The ObjectExpression node representing the style object to be processed.
|
|
30
|
+
*/
|
|
31
|
+
export declare const processTrailingZeroInStyleObject: (ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression) => void;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Regex to match numbers with trailing zeros.
|
|
4
|
+
* Matches patterns like:
|
|
5
|
+
* - 1.0, 2.50, 0.0, 0.50
|
|
6
|
+
* - 1.0px, 2.50rem, 0.0em
|
|
7
|
+
* - -1.0, -2.50px
|
|
8
|
+
*
|
|
9
|
+
* Groups:
|
|
10
|
+
* 1: Optional minus sign
|
|
11
|
+
* 2: Integer part
|
|
12
|
+
* 3: Significant fractional digits (optional)
|
|
13
|
+
* 4: Trailing zeros
|
|
14
|
+
* 5: Optional unit
|
|
15
|
+
*/
|
|
16
|
+
const TRAILING_ZERO_REGEX = /^(-?)(\d+)\.(\d*[1-9])?(0+)([a-z%]+)?$/i;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a value has trailing zeros and returns the fixed value if needed.
|
|
19
|
+
*
|
|
20
|
+
* @param value The string value to check
|
|
21
|
+
* @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
|
|
22
|
+
*/
|
|
23
|
+
export const checkTrailingZero = (value) => {
|
|
24
|
+
const trimmedValue = value.trim();
|
|
25
|
+
const match = trimmedValue.match(TRAILING_ZERO_REGEX);
|
|
26
|
+
if (!match) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const [, minus = '', integerPart, significantFractional = '', , unit = ''] = match;
|
|
30
|
+
// Handle special case: 0.0 or 0.00 etc. should become just "0"
|
|
31
|
+
if (integerPart === '0' && !significantFractional) {
|
|
32
|
+
return {
|
|
33
|
+
hasTrailingZero: true,
|
|
34
|
+
fixed: '0',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// If there's no significant fractional part (e.g., "1.0" -> "1")
|
|
38
|
+
if (!significantFractional) {
|
|
39
|
+
return {
|
|
40
|
+
hasTrailingZero: true,
|
|
41
|
+
fixed: `${minus}${integerPart}${unit}`,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// If there's a significant fractional part (e.g., "1.50" -> "1.5")
|
|
45
|
+
return {
|
|
46
|
+
hasTrailingZero: true,
|
|
47
|
+
fixed: `${minus}${integerPart}.${significantFractional}${unit}`,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Processes a single string value and checks for trailing zeros in all numeric values.
|
|
52
|
+
* Handles strings with multiple numeric values (e.g., "1.0px 2.50em").
|
|
53
|
+
* Also handles values within function calls (e.g., "rotate(45.0deg)").
|
|
54
|
+
*
|
|
55
|
+
* @param value The string value to process
|
|
56
|
+
* @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
|
|
57
|
+
*/
|
|
58
|
+
export const processStringValue = (value) => {
|
|
59
|
+
// First, try to match the entire value
|
|
60
|
+
const directMatch = checkTrailingZero(value);
|
|
61
|
+
if (directMatch?.hasTrailingZero) {
|
|
62
|
+
return directMatch;
|
|
63
|
+
}
|
|
64
|
+
// Split by whitespace to handle multiple values
|
|
65
|
+
const parts = value.split(/(\s+)/);
|
|
66
|
+
let hasAnyTrailingZero = false;
|
|
67
|
+
const fixedParts = parts.map((part) => {
|
|
68
|
+
// Preserve whitespace
|
|
69
|
+
if (/^\s+$/.test(part)) {
|
|
70
|
+
return part;
|
|
71
|
+
}
|
|
72
|
+
// Try to match the whole part first
|
|
73
|
+
const result = checkTrailingZero(part);
|
|
74
|
+
if (result?.hasTrailingZero) {
|
|
75
|
+
hasAnyTrailingZero = true;
|
|
76
|
+
return result.fixed;
|
|
77
|
+
}
|
|
78
|
+
// If no match, try to find and replace numbers within the part (e.g., inside function calls)
|
|
79
|
+
const regex = /(-?\d+)\.(\d*[1-9])?(0+)(?![0-9])([a-z%]+)?/gi;
|
|
80
|
+
const fixedPart = part.replace(regex, (_, integerWithSign, significantFractional, __, unit) => {
|
|
81
|
+
// Reconstruct the number without trailing zeros
|
|
82
|
+
const integerPart = integerWithSign;
|
|
83
|
+
const sig = significantFractional || '';
|
|
84
|
+
const u = unit || '';
|
|
85
|
+
// Handle 0.0 case - if it's zero and no unit, return just '0', otherwise keep the unit
|
|
86
|
+
if (integerPart === '0' && !sig) {
|
|
87
|
+
hasAnyTrailingZero = true;
|
|
88
|
+
return u ? `0${u}` : '0';
|
|
89
|
+
}
|
|
90
|
+
// Handle X.0 case
|
|
91
|
+
if (!sig) {
|
|
92
|
+
hasAnyTrailingZero = true;
|
|
93
|
+
return `${integerPart}${u}`;
|
|
94
|
+
}
|
|
95
|
+
// Handle X.Y0 case
|
|
96
|
+
hasAnyTrailingZero = true;
|
|
97
|
+
return `${integerPart}.${sig}${u}`;
|
|
98
|
+
});
|
|
99
|
+
return fixedPart;
|
|
100
|
+
});
|
|
101
|
+
if (!hasAnyTrailingZero) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
hasTrailingZero: true,
|
|
106
|
+
fixed: fixedParts.join(''),
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Recursively processes a style object, reporting and fixing instances of trailing zeros in numeric values.
|
|
111
|
+
*
|
|
112
|
+
* @param ruleContext The ESLint rule context.
|
|
113
|
+
* @param node The ObjectExpression node representing the style object to be processed.
|
|
114
|
+
*/
|
|
115
|
+
export const processTrailingZeroInStyleObject = (ruleContext, node) => {
|
|
116
|
+
node.properties.forEach((property) => {
|
|
117
|
+
if (property.type !== 'Property') {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// Process direct string literal values
|
|
121
|
+
if (property.value.type === 'Literal' && typeof property.value.value === 'string') {
|
|
122
|
+
const result = processStringValue(property.value.value);
|
|
123
|
+
if (result?.hasTrailingZero) {
|
|
124
|
+
ruleContext.report({
|
|
125
|
+
node: property.value,
|
|
126
|
+
messageId: 'trailingZero',
|
|
127
|
+
data: {
|
|
128
|
+
value: property.value.value,
|
|
129
|
+
fixed: result.fixed,
|
|
130
|
+
},
|
|
131
|
+
fix: (fixer) => fixer.replaceText(property.value, `'${result.fixed}'`),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Process numeric literal values (e.g., margin: 1.0)
|
|
136
|
+
if (property.value.type === 'Literal' && typeof property.value.value === 'number') {
|
|
137
|
+
// Use the raw property to get the original source text (which preserves trailing zeros)
|
|
138
|
+
const rawValue = property.value.raw || property.value.value.toString();
|
|
139
|
+
const result = checkTrailingZero(rawValue);
|
|
140
|
+
if (result?.hasTrailingZero) {
|
|
141
|
+
ruleContext.report({
|
|
142
|
+
node: property.value,
|
|
143
|
+
messageId: 'trailingZero',
|
|
144
|
+
data: {
|
|
145
|
+
value: rawValue,
|
|
146
|
+
fixed: result.fixed,
|
|
147
|
+
},
|
|
148
|
+
fix: (fixer) => fixer.replaceText(property.value, result.fixed),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Process nested objects (selectors, media queries, etc.)
|
|
153
|
+
if (property.value.type === 'ObjectExpression') {
|
|
154
|
+
processTrailingZeroInStyleObject(ruleContext, property.value);
|
|
155
|
+
}
|
|
156
|
+
// Process arrays (for styleVariants with array values)
|
|
157
|
+
if (property.value.type === 'ArrayExpression') {
|
|
158
|
+
property.value.elements.forEach((element) => {
|
|
159
|
+
if (element && element.type === 'ObjectExpression') {
|
|
160
|
+
processTrailingZeroInStyleObject(ruleContext, element);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* Creates ESLint rule visitors for detecting and processing trailing zeros in numeric CSS values.
|
|
4
|
+
* Uses reference tracking to automatically detect vanilla-extract functions based on their import statements.
|
|
5
|
+
*
|
|
6
|
+
* @param context The ESLint rule context.
|
|
7
|
+
* @returns An object with visitor functions for the ESLint rule.
|
|
8
|
+
*/
|
|
9
|
+
export declare const createTrailingZeroVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
|
|
@@ -0,0 +1,85 @@
|
|
|
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 { processTrailingZeroInStyleObject } from './trailing-zero-processor.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates ESLint rule visitors for detecting and processing trailing zeros in numeric CSS values.
|
|
8
|
+
* Uses reference tracking to automatically detect vanilla-extract functions based on their import statements.
|
|
9
|
+
*
|
|
10
|
+
* @param context The ESLint rule context.
|
|
11
|
+
* @returns An object with visitor functions for the ESLint rule.
|
|
12
|
+
*/
|
|
13
|
+
export const createTrailingZeroVisitors = (context) => {
|
|
14
|
+
const tracker = new ReferenceTracker();
|
|
15
|
+
const trackingVisitor = createReferenceTrackingVisitor(tracker);
|
|
16
|
+
return {
|
|
17
|
+
// Include the reference tracking visitors
|
|
18
|
+
...trackingVisitor,
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
if (node.callee.type !== AST_NODE_TYPES.Identifier) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const functionName = node.callee.name;
|
|
24
|
+
// Check if this function is tracked as a vanilla-extract function
|
|
25
|
+
if (!tracker.isTrackedFunction(functionName)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const originalName = tracker.getOriginalName(functionName);
|
|
29
|
+
if (!originalName) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// Handle different function types based on their original imported name
|
|
33
|
+
switch (originalName) {
|
|
34
|
+
case 'fontFace':
|
|
35
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
36
|
+
processTrailingZeroInStyleObject(context, node.arguments[0]);
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
case 'globalFontFace':
|
|
40
|
+
if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
41
|
+
processTrailingZeroInStyleObject(context, node.arguments[1]);
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
case 'style':
|
|
45
|
+
if (node.arguments.length > 0) {
|
|
46
|
+
processStyleNode(context, node.arguments[0], processTrailingZeroInStyleObject);
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
case 'styleVariants':
|
|
50
|
+
case 'keyframes':
|
|
51
|
+
// For styleVariants and keyframes, the argument is an object where each property value is a style object
|
|
52
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
53
|
+
const variantsObject = node.arguments[0];
|
|
54
|
+
variantsObject.properties.forEach((property) => {
|
|
55
|
+
if (property.type === 'Property' && property.value.type === 'ObjectExpression') {
|
|
56
|
+
processTrailingZeroInStyleObject(context, property.value);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case 'globalStyle':
|
|
62
|
+
if (node.arguments.length >= 2) {
|
|
63
|
+
processStyleNode(context, node.arguments[1], processTrailingZeroInStyleObject);
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
case 'globalKeyframes':
|
|
67
|
+
// For globalKeyframes, the second argument is an object where each property value is a style object
|
|
68
|
+
if (node.arguments.length >= 2 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
69
|
+
const keyframesObject = node.arguments[1];
|
|
70
|
+
keyframesObject.properties.forEach((property) => {
|
|
71
|
+
if (property.type === 'Property' && property.value.type === 'ObjectExpression') {
|
|
72
|
+
processTrailingZeroInStyleObject(context, property.value);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case 'recipe':
|
|
78
|
+
if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
79
|
+
processRecipeProperties(context, node.arguments[0], processTrailingZeroInStyleObject);
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ declare const vanillaExtract: {
|
|
|
8
8
|
'concentric-order': import("eslint").Rule.RuleModule;
|
|
9
9
|
'custom-order': import("eslint").Rule.RuleModule;
|
|
10
10
|
'no-empty-style-blocks': import("eslint").Rule.RuleModule;
|
|
11
|
+
'no-trailing-zero': import("eslint").Rule.RuleModule;
|
|
11
12
|
'no-unknown-unit': import("eslint").Rule.RuleModule;
|
|
12
13
|
'no-zero-unit': import("eslint").Rule.RuleModule;
|
|
13
14
|
};
|
package/dist/index.js
CHANGED
|
@@ -2,18 +2,20 @@ import alphabeticalOrderRule from './css-rules/alphabetical-order/index.js';
|
|
|
2
2
|
import concentricOrderRule from './css-rules/concentric-order/index.js';
|
|
3
3
|
import customOrderRule from './css-rules/custom-order/rule-definition.js';
|
|
4
4
|
import noEmptyStyleBlocksRule from './css-rules/no-empty-blocks/rule-definition.js';
|
|
5
|
+
import noTrailingZeroRule from './css-rules/no-trailing-zero/rule-definition.js';
|
|
5
6
|
import noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
|
|
6
7
|
import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
|
|
7
8
|
const vanillaExtract = {
|
|
8
9
|
meta: {
|
|
9
10
|
name: '@antebudimir/eslint-plugin-vanilla-extract',
|
|
10
|
-
version: '1.
|
|
11
|
+
version: '1.12.0',
|
|
11
12
|
},
|
|
12
13
|
rules: {
|
|
13
14
|
'alphabetical-order': alphabeticalOrderRule,
|
|
14
15
|
'concentric-order': concentricOrderRule,
|
|
15
16
|
'custom-order': customOrderRule,
|
|
16
17
|
'no-empty-style-blocks': noEmptyStyleBlocksRule,
|
|
18
|
+
'no-trailing-zero': noTrailingZeroRule,
|
|
17
19
|
'no-unknown-unit': noUnknownUnitRule,
|
|
18
20
|
'no-zero-unit': noZeroUnitRule,
|
|
19
21
|
},
|
|
@@ -27,6 +29,7 @@ Object.assign(vanillaExtract.configs, {
|
|
|
27
29
|
rules: {
|
|
28
30
|
'vanilla-extract/concentric-order': 'error',
|
|
29
31
|
'vanilla-extract/no-empty-style-blocks': 'error',
|
|
32
|
+
'vanilla-extract/no-trailing-zero': 'error',
|
|
30
33
|
'vanilla-extract/no-unknown-unit': 'error',
|
|
31
34
|
'vanilla-extract/no-zero-unit': 'error',
|
|
32
35
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antebudimir/eslint-plugin-vanilla-extract",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "ESLint plugin for enforcing best practices in vanilla-extract CSS styles, including CSS property ordering and additional linting rules.",
|
|
5
5
|
"author": "Ante Budimir",
|
|
6
6
|
"license": "MIT",
|