@antebudimir/eslint-plugin-vanilla-extract 1.8.0 → 1.10.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 +190 -22
- package/dist/css-rules/no-unknown-unit/index.d.ts +2 -0
- package/dist/css-rules/no-unknown-unit/index.js +2 -0
- package/dist/css-rules/no-unknown-unit/rule-definition.d.ts +3 -0
- package/dist/css-rules/no-unknown-unit/rule-definition.js +19 -0
- package/dist/css-rules/no-unknown-unit/unknown-unit-processor.d.ts +11 -0
- package/dist/css-rules/no-unknown-unit/unknown-unit-processor.js +175 -0
- package/dist/css-rules/no-unknown-unit/unknown-unit-visitor-creator.d.ts +6 -0
- package/dist/css-rules/no-unknown-unit/unknown-unit-visitor-creator.js +38 -0
- package/dist/css-rules/no-zero-unit/rule-definition.js +1 -1
- package/dist/index.d.ts +3 -11
- package/dist/index.js +17 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
# @antebudimir/eslint-plugin-vanilla-extract
|
|
2
2
|
|
|
3
|
-
[](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml)
|
|
3
|
+
[](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml)
|
|
4
|
+
[](https://coveralls.io/github/antebudimir/eslint-plugin-vanilla-extract?branch=main)
|
|
5
|
+
[](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract)
|
|
6
|
+

|
|
4
7
|
|
|
5
|
-
An ESLint plugin for enforcing best practices in
|
|
8
|
+
An ESLint plugin for enforcing best practices in
|
|
9
|
+
[vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract) CSS styles, including CSS property ordering
|
|
10
|
+
and additional linting rules. Available presets are for alphabetical and
|
|
11
|
+
[concentric](https://rhodesmill.org/brandon/2011/concentric-css/) CSS ordering. The plugin also supports a custom group
|
|
12
|
+
ordering option based on groups available in [concentric CSS](src/css-rules/concentric-order/concentric-groups.ts).
|
|
6
13
|
|
|
7
14
|
## Demo
|
|
8
15
|
|
|
@@ -14,7 +21,7 @@ An ESLint plugin for enforcing best practices in [vanilla-extract](https://githu
|
|
|
14
21
|
- Alphabetical ordering for clean, predictable style organization
|
|
15
22
|
- Concentric ordering for logical, outside-in property arrangement
|
|
16
23
|
- Custom group ordering option for more fine-grained control
|
|
17
|
-
-
|
|
24
|
+
- Compatible with ESLint 8.57.0+ and fully optimized for ESLint 9's flat config system
|
|
18
25
|
- Provides auto-fix capability to automatically sort properties
|
|
19
26
|
- Handles multiple vanilla-extract APIs (style, styleVariants, recipe, globalStyle, etc.)
|
|
20
27
|
- Handles complex cases like nested objects, arrays of styles, and pseudo selectors
|
|
@@ -23,9 +30,12 @@ An ESLint plugin for enforcing best practices in [vanilla-extract](https://githu
|
|
|
23
30
|
|
|
24
31
|
## Requirements
|
|
25
32
|
|
|
26
|
-
- ESLint
|
|
33
|
+
- ESLint 8.57.0 or higher
|
|
27
34
|
- Node.js 18.18.0 or higher
|
|
28
35
|
- ESM (ECMAScript Modules) only
|
|
36
|
+
- Flat config system using either:
|
|
37
|
+
- `eslint.config.mjs` (recommended, always works with ESM plugins)
|
|
38
|
+
- `eslint.config.js` (only if your package.json has `"type": "module"`)
|
|
29
39
|
|
|
30
40
|
## Installation
|
|
31
41
|
|
|
@@ -38,21 +48,47 @@ yarn add --dev @antebudimir/eslint-plugin-vanilla-extract
|
|
|
38
48
|
|
|
39
49
|
# Using pnpm
|
|
40
50
|
pnpm add -D @antebudimir/eslint-plugin-vanilla-extract
|
|
51
|
+
|
|
52
|
+
# For ESLint 8.57.0 with flat config, you'll also need:
|
|
53
|
+
npm install --save-dev @eslint/eslintrc @eslint/js
|
|
54
|
+
yarn add --dev @eslint/eslintrc @eslint/js
|
|
55
|
+
pnpm add -D @eslint/eslintrc @eslint/js
|
|
41
56
|
```
|
|
42
57
|
|
|
43
58
|
## Usage
|
|
44
59
|
|
|
45
60
|
**Note: This plugin is ESM-only.** It must be used with ESM configurations and can't be used with CommonJS `require()`.
|
|
46
61
|
|
|
47
|
-
###
|
|
62
|
+
### Configuration Options
|
|
63
|
+
|
|
64
|
+
There are two main ways to configure this plugin in your ESLint flat config:
|
|
65
|
+
|
|
66
|
+
### Option 1: Using extends (recommended, available from v1.10.0)
|
|
48
67
|
|
|
49
|
-
|
|
68
|
+
The simplest way to apply the recommended ruleset:
|
|
50
69
|
|
|
51
70
|
```typescript
|
|
71
|
+
import { defineConfig } from 'eslint/config';
|
|
52
72
|
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
53
73
|
|
|
54
|
-
|
|
55
|
-
|
|
74
|
+
export default defineConfig([
|
|
75
|
+
{
|
|
76
|
+
files: ['**/*.css.ts'],
|
|
77
|
+
ignores: ['src/**/theme-contract.css.ts'],
|
|
78
|
+
extends: [vanillaExtract.configs.recommended],
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Option 2: Using plugins with explicit rule configuration
|
|
84
|
+
|
|
85
|
+
This approach gives you more control over individual rules:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { defineConfig } from 'eslint/config';
|
|
89
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
90
|
+
|
|
91
|
+
export default defineConfig([
|
|
56
92
|
{
|
|
57
93
|
files: ['**/*.css.ts'],
|
|
58
94
|
ignores: ['src/**/theme-contract.css.ts'],
|
|
@@ -62,12 +98,12 @@ export default [
|
|
|
62
98
|
rules: {
|
|
63
99
|
// Apply all recommended rules
|
|
64
100
|
...vanillaExtract.configs.recommended.rules,
|
|
65
|
-
|
|
101
|
+
|
|
66
102
|
// Optionally override specific rules
|
|
67
103
|
// 'vanilla-extract/concentric-order': 'warn', // Change severity from error to warn
|
|
68
104
|
// 'vanilla-extract/no-empty-style-blocks': 'off', // Disable a recommended rule
|
|
69
105
|
// 'vanilla-extract/no-zero-unit': 'warn', // Change severity from error to warn
|
|
70
|
-
|
|
106
|
+
|
|
71
107
|
// Add additional rules not in recommended config
|
|
72
108
|
// 'vanilla-extract/alphabetical-order': 'error', // Override concentric-order rule
|
|
73
109
|
},
|
|
@@ -75,12 +111,87 @@ export default [
|
|
|
75
111
|
];
|
|
76
112
|
```
|
|
77
113
|
|
|
114
|
+
### Using with FlatCompat (for ESLint 8.57.0 & 8.57.1)
|
|
115
|
+
|
|
116
|
+
If you're migrating from legacy ESLint configurations, you can use the `FlatCompat` utility to convert them while adding
|
|
117
|
+
vanilla-extract support:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import path from 'path';
|
|
121
|
+
import { fileURLToPath } from 'url';
|
|
122
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
123
|
+
import js from '@eslint/js';
|
|
124
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
125
|
+
|
|
126
|
+
// Mimic CommonJS variables
|
|
127
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
128
|
+
const __dirname = path.dirname(__filename);
|
|
129
|
+
|
|
130
|
+
// Create a compatibility layer instance
|
|
131
|
+
const compat = new FlatCompat({
|
|
132
|
+
baseDirectory: __dirname,
|
|
133
|
+
recommendedConfig: js.configs.recommended,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
export default [
|
|
137
|
+
// Convert your existing ESLint configs
|
|
138
|
+
...compat.config({
|
|
139
|
+
extends: [
|
|
140
|
+
'eslint:recommended',
|
|
141
|
+
'plugin:@typescript-eslint/recommended',
|
|
142
|
+
// etc
|
|
143
|
+
],
|
|
144
|
+
}),
|
|
145
|
+
|
|
146
|
+
// Add vanilla-extract by using explicit rule config
|
|
147
|
+
{
|
|
148
|
+
files: ['**/*.css.ts'],
|
|
149
|
+
ignores: ['src/**/theme-contract.css.ts'],
|
|
150
|
+
plugins: {
|
|
151
|
+
'vanilla-extract': vanillaExtract,
|
|
152
|
+
},
|
|
153
|
+
rules: {
|
|
154
|
+
// Apply all recommended rules
|
|
155
|
+
...vanillaExtract.configs.recommended.rules,
|
|
156
|
+
// or specify rule by rule as described above
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
];
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
#### Common Issues with FlatCompat
|
|
163
|
+
|
|
164
|
+
1. **Error: "Unexpected top-level property 'files'"**
|
|
165
|
+
|
|
166
|
+
- Solution: When using `compat.config()`, use `overrides` instead of `files` at the top level.
|
|
167
|
+
|
|
168
|
+
2. **Error: "Missing parameter 'recommendedConfig' in FlatCompat constructor"**
|
|
169
|
+
|
|
170
|
+
- Solution: Import `js` from `@eslint/js` and add `recommendedConfig: js.configs.recommended` to the FlatCompat
|
|
171
|
+
constructor.
|
|
172
|
+
|
|
173
|
+
3. **Error: "Unexpected undefined config at user-defined index 0"**
|
|
174
|
+
- Solution: Make sure you're using a default export for your configuration array.
|
|
175
|
+
|
|
176
|
+
### VS Code Integration
|
|
177
|
+
|
|
178
|
+
For VS Code users, add these settings to your `.vscode/settings.json`:
|
|
179
|
+
|
|
180
|
+
```json
|
|
181
|
+
{
|
|
182
|
+
"eslint.useFlatConfig": true,
|
|
183
|
+
"eslint.experimental.useFlatConfig": true,
|
|
184
|
+
"eslint.validate": ["javascript", "typescript", "typescriptreact"]
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
78
188
|
### Recommended Configuration
|
|
79
189
|
|
|
80
190
|
The recommended configuration enables the following rules with error severity:
|
|
81
191
|
|
|
82
192
|
- `vanilla-extract/concentric-order`: Enforces concentric CSS property ordering
|
|
83
193
|
- `vanilla-extract/no-empty-style-blocks`: Prevents empty style blocks
|
|
194
|
+
- `vanilla-extract/no-unknown-unit`: prohibits usage of unrecognized CSS units.
|
|
84
195
|
- `vanilla-extract/no-zero-unit`: removes unnecessary units for zero values
|
|
85
196
|
|
|
86
197
|
You can use the recommended configuration as a starting point and override rules as needed for your project.
|
|
@@ -110,6 +221,7 @@ export default [
|
|
|
110
221
|
sortRemainingProperties: 'concentric', // 'alphabetical' is default
|
|
111
222
|
},
|
|
112
223
|
],
|
|
224
|
+
'vanilla-extract/no-unknown-unit': 'error',
|
|
113
225
|
'vanilla-extract/no-zero-unit': 'warn',
|
|
114
226
|
},
|
|
115
227
|
},
|
|
@@ -179,20 +291,27 @@ export const myStyle = style({
|
|
|
179
291
|
|
|
180
292
|
### vanilla-extract/custom-order
|
|
181
293
|
|
|
182
|
-
The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your
|
|
294
|
+
The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your
|
|
295
|
+
vanilla-extract styles. You can specify an array of property groups in your preferred order, and the rule will ensure
|
|
296
|
+
that properties within these groups are sorted according to their position in the concentric CSS model.
|
|
183
297
|
|
|
184
298
|
Key features of this rule include:
|
|
185
299
|
|
|
186
300
|
1. Custom group ordering: Define your preferred order of CSS property groups.
|
|
187
|
-
2. Handling of unspecified groups: All groups not included in the custom array will have their properties sorted after
|
|
188
|
-
|
|
301
|
+
2. Handling of unspecified groups: All groups not included in the custom array will have their properties sorted after
|
|
302
|
+
the last specified group.
|
|
303
|
+
3. Flexible sorting options: You can choose to sort remaining properties either alphabetically or following the
|
|
304
|
+
concentric CSS order by setting the `sortRemainingProperties` option to 'alphabetical' or 'concentric' respectively.
|
|
189
305
|
|
|
190
306
|
Default behavior:
|
|
191
307
|
|
|
192
308
|
- If not set, `sortRemainingProperties` defaults to 'alphabetical'.
|
|
193
|
-
- If no `groupOrder` is specified or an empty array is provided, the rule will default to sorting all properties
|
|
309
|
+
- If no `groupOrder` is specified or an empty array is provided, the rule will default to sorting all properties
|
|
310
|
+
alphabetically, and `sortRemainingProperties` will be ignored even if set.
|
|
194
311
|
|
|
195
|
-
To configure the rule, add it to your ESLint configuration file with your desired options. You can customize the
|
|
312
|
+
To configure the rule, add it to your ESLint configuration file with your desired options. You can customize the
|
|
313
|
+
`groups` array to include any number of available CSS property groups you want to enforce, with a minimum of one group
|
|
314
|
+
required.
|
|
196
315
|
|
|
197
316
|
```typescript
|
|
198
317
|
// ❌ Incorrect (Unordered)
|
|
@@ -235,7 +354,8 @@ export const myStyle = style({
|
|
|
235
354
|
|
|
236
355
|
### vanilla-extract/no-empty-style-blocks
|
|
237
356
|
|
|
238
|
-
This rule detects and prevents empty style blocks in vanilla-extract stylesheets. It helps maintain cleaner codebases by
|
|
357
|
+
This rule detects and prevents empty style blocks in vanilla-extract stylesheets. It helps maintain cleaner codebases by
|
|
358
|
+
eliminating empty style definitions that often result from incomplete refactoring or forgotten implementations.
|
|
239
359
|
|
|
240
360
|
```typescript
|
|
241
361
|
// ❌ Incorrect
|
|
@@ -269,9 +389,49 @@ export const recipeWithEmptyVariants = recipe({
|
|
|
269
389
|
});
|
|
270
390
|
```
|
|
271
391
|
|
|
392
|
+
## vanilla-extract/no-unknown-unit
|
|
393
|
+
|
|
394
|
+
This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units
|
|
395
|
+
that could cause styling issues or browser compatibility problems.
|
|
396
|
+
|
|
397
|
+
```typescript
|
|
398
|
+
// ❌ Incorrect
|
|
399
|
+
import { style, globalStyle, recipe } from '@vanilla-extract/css';
|
|
400
|
+
|
|
401
|
+
export const invalidStyle = style({
|
|
402
|
+
margin: '5abc', // Non-existent unit
|
|
403
|
+
fontSize: '1.5rems', // Typo in unit
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
export const myRecipe = recipe({
|
|
407
|
+
variants: {
|
|
408
|
+
size: {
|
|
409
|
+
large: { padding: '4xm' }, // Invalid unit
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// ✅ Correct
|
|
415
|
+
import { style, globalStyle, recipe } from '@vanilla-extract/css';
|
|
416
|
+
|
|
417
|
+
export const validStyle = style({
|
|
418
|
+
margin: '5rem',
|
|
419
|
+
fontSize: '1.5rem',
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
export const myRecipe = recipe({
|
|
423
|
+
variants: {
|
|
424
|
+
size: {
|
|
425
|
+
large: { padding: '4em' },
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
```
|
|
430
|
+
|
|
272
431
|
## vanilla-extract/no-zero-unit
|
|
273
432
|
|
|
274
|
-
This rule enforces the removal of unnecessary units for zero values in vanilla-extract style objects. It helps maintain
|
|
433
|
+
This rule enforces the removal of unnecessary units for zero values in vanilla-extract style objects. It helps maintain
|
|
434
|
+
cleaner and more consistent CSS by eliminating redundant units when the value is zero.
|
|
275
435
|
|
|
276
436
|
```typescript
|
|
277
437
|
// ❌ Incorrect
|
|
@@ -299,7 +459,8 @@ export const myStyle = style({
|
|
|
299
459
|
|
|
300
460
|
## Font Face Declarations
|
|
301
461
|
|
|
302
|
-
For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce
|
|
462
|
+
For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce
|
|
463
|
+
the same special ordering:
|
|
303
464
|
|
|
304
465
|
1. The `src` property always appears first
|
|
305
466
|
2. All remaining properties are sorted alphabetically
|
|
@@ -327,7 +488,8 @@ Opinionated, but it is what it is. If someone has a suggestion for a better orde
|
|
|
327
488
|
|
|
328
489
|
## Concentric CSS Model
|
|
329
490
|
|
|
330
|
-
Here's a list of all available groups from the provided
|
|
491
|
+
Here's a list of all available groups from the provided
|
|
492
|
+
[concentricGroups](src/css-rules/concentric-order/concentric-groups.ts) array:
|
|
331
493
|
|
|
332
494
|
1. boxSizing
|
|
333
495
|
2. position
|
|
@@ -359,7 +521,8 @@ Here's a list of all available groups from the provided [concentricGroups](src/c
|
|
|
359
521
|
28. counters
|
|
360
522
|
29. breaks
|
|
361
523
|
|
|
362
|
-
These groups represent different categories of CSS properties, organized in a concentric order from outside to inside.
|
|
524
|
+
These groups represent different categories of CSS properties, organized in a concentric order from outside to inside.
|
|
525
|
+
Each group contains related CSS properties that affect specific aspects of an element's styling and layout.
|
|
363
526
|
|
|
364
527
|
## Roadmap
|
|
365
528
|
|
|
@@ -373,11 +536,13 @@ The roadmap outlines the project's current status and future plans:
|
|
|
373
536
|
- `no-empty-style-blocks` rule to disallow empty blocks.
|
|
374
537
|
- Recommended ESLint configuration for the plugin.
|
|
375
538
|
- `no-zero-unit` rule to disallow units when the value is zero.
|
|
539
|
+
- `no-unknown-unit` rule to disallow unknown units.
|
|
376
540
|
- Comprehensive rule testing.
|
|
377
541
|
|
|
378
542
|
### Current Work
|
|
379
543
|
|
|
380
|
-
-
|
|
544
|
+
- Support for using the plugin’s recommended config via the extends field (as discussed in
|
|
545
|
+
[issue #3](https://github.com/antebudimir/eslint-plugin-vanilla-extract/issues/3))
|
|
381
546
|
|
|
382
547
|
### Upcoming Features
|
|
383
548
|
|
|
@@ -386,7 +551,10 @@ The roadmap outlines the project's current status and future plans:
|
|
|
386
551
|
- `prefer-logical-properties` rule to enforce use of logical properties.
|
|
387
552
|
- `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
|
|
388
553
|
- `no-global-style` rule to disallow use of `globalStyle` function.
|
|
389
|
-
-
|
|
554
|
+
- `property-unit-match` rule to enforce valid units per CSS property specs. **Note**: This feature will only be
|
|
555
|
+
implemented if there's sufficient interest from the community.
|
|
556
|
+
- Option to sort properties within user-defined concentric groups alphabetically instead of following the concentric
|
|
557
|
+
order. **Note**: This feature will only be implemented if there's sufficient interest from the community.
|
|
390
558
|
|
|
391
559
|
## Contributing
|
|
392
560
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createUnknownUnitVisitors } from './unknown-unit-visitor-creator.js';
|
|
2
|
+
const noUnknownUnitRule = {
|
|
3
|
+
meta: {
|
|
4
|
+
type: 'problem',
|
|
5
|
+
docs: {
|
|
6
|
+
description: 'disallow invalid or unknown CSS units in vanilla-extract style objects',
|
|
7
|
+
category: 'Possible Errors',
|
|
8
|
+
recommended: true,
|
|
9
|
+
},
|
|
10
|
+
schema: [],
|
|
11
|
+
messages: {
|
|
12
|
+
unknownUnit: 'The unit "{{ unit }}" in value "{{ value }}" is not recognized as a valid CSS unit.',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
create(context) {
|
|
16
|
+
return createUnknownUnitVisitors(context);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
export default noUnknownUnitRule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Recursively processes a style object, reporting instances of
|
|
5
|
+
* unknown CSS units.
|
|
6
|
+
*
|
|
7
|
+
* @param context The ESLint rule context.
|
|
8
|
+
* @param node The ObjectExpression node representing the style object to be
|
|
9
|
+
* processed.
|
|
10
|
+
*/
|
|
11
|
+
export declare const processUnknownUnitInStyleObject: (context: Rule.RuleContext, node: TSESTree.ObjectExpression) => void;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* List of valid CSS units according to CSS specifications.
|
|
4
|
+
*/
|
|
5
|
+
const VALID_CSS_UNITS = [
|
|
6
|
+
// Absolute length units
|
|
7
|
+
'px',
|
|
8
|
+
'cm',
|
|
9
|
+
'mm',
|
|
10
|
+
'Q',
|
|
11
|
+
'in',
|
|
12
|
+
'pc',
|
|
13
|
+
'pt',
|
|
14
|
+
// Relative length units
|
|
15
|
+
'em',
|
|
16
|
+
'ex',
|
|
17
|
+
'ch',
|
|
18
|
+
'rem',
|
|
19
|
+
'lh',
|
|
20
|
+
'rlh',
|
|
21
|
+
'vw',
|
|
22
|
+
'vh',
|
|
23
|
+
'vmin',
|
|
24
|
+
'vmax',
|
|
25
|
+
'vb',
|
|
26
|
+
'vi',
|
|
27
|
+
'svw',
|
|
28
|
+
'svh',
|
|
29
|
+
'lvw',
|
|
30
|
+
'lvh',
|
|
31
|
+
'dvw',
|
|
32
|
+
'dvh',
|
|
33
|
+
// Percentage
|
|
34
|
+
'%',
|
|
35
|
+
// Angle units
|
|
36
|
+
'deg',
|
|
37
|
+
'grad',
|
|
38
|
+
'rad',
|
|
39
|
+
'turn',
|
|
40
|
+
// Time units
|
|
41
|
+
'ms',
|
|
42
|
+
's',
|
|
43
|
+
// Frequency units
|
|
44
|
+
'Hz',
|
|
45
|
+
'kHz',
|
|
46
|
+
// Resolution units
|
|
47
|
+
'dpi',
|
|
48
|
+
'dpcm',
|
|
49
|
+
'dppx',
|
|
50
|
+
'x',
|
|
51
|
+
// Flexible length units
|
|
52
|
+
'fr',
|
|
53
|
+
// Other valid units
|
|
54
|
+
'cap',
|
|
55
|
+
'ic',
|
|
56
|
+
'rex',
|
|
57
|
+
'cqw',
|
|
58
|
+
'cqh',
|
|
59
|
+
'cqi',
|
|
60
|
+
'cqb',
|
|
61
|
+
'cqmin',
|
|
62
|
+
'cqmax',
|
|
63
|
+
];
|
|
64
|
+
/**
|
|
65
|
+
* Regular expression to extract units from CSS values.
|
|
66
|
+
* Matches numeric values followed by a unit.
|
|
67
|
+
*/
|
|
68
|
+
const CSS_VALUE_WITH_UNIT_REGEX = /^(-?\d*\.?\d+)([a-zA-Z%]+)$/i;
|
|
69
|
+
/**
|
|
70
|
+
* Splits a CSS value string into individual parts, handling spaces not inside functions.
|
|
71
|
+
*/
|
|
72
|
+
const splitCssValues = (value) => {
|
|
73
|
+
return value
|
|
74
|
+
.split(/(?<!\([^)]*)\s+/) // Split on spaces not inside functions
|
|
75
|
+
.map((part) => part.trim())
|
|
76
|
+
.filter((part) => part.length > 0);
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Check if a CSS value contains a valid CSS unit.
|
|
80
|
+
*/
|
|
81
|
+
const checkCssUnit = (value) => {
|
|
82
|
+
const values = splitCssValues(value);
|
|
83
|
+
for (const value of values) {
|
|
84
|
+
// Skip values containing CSS functions
|
|
85
|
+
if (value.includes('(')) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const match = value.match(CSS_VALUE_WITH_UNIT_REGEX);
|
|
89
|
+
if (!match) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const unit = match[2].toLowerCase(); // match[2] is guaranteed by regex pattern
|
|
93
|
+
if (!VALID_CSS_UNITS.includes(unit)) {
|
|
94
|
+
return {
|
|
95
|
+
hasUnit: true,
|
|
96
|
+
unit: match[2], // Preserve original casing
|
|
97
|
+
isValid: false,
|
|
98
|
+
invalidValue: value,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { hasUnit: false, unit: null, isValid: true };
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Extracts string value from a node if it's a string literal or template literal.
|
|
106
|
+
*/
|
|
107
|
+
const getStringValue = (node) => {
|
|
108
|
+
if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string') {
|
|
109
|
+
return node.value;
|
|
110
|
+
}
|
|
111
|
+
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.quasis.length === 1) {
|
|
112
|
+
const firstQuasi = node.quasis[0];
|
|
113
|
+
return firstQuasi?.value.raw ? firstQuasi.value.raw : null;
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Recursively processes a style object, reporting instances of
|
|
119
|
+
* unknown CSS units.
|
|
120
|
+
*
|
|
121
|
+
* @param context The ESLint rule context.
|
|
122
|
+
* @param node The ObjectExpression node representing the style object to be
|
|
123
|
+
* processed.
|
|
124
|
+
*/
|
|
125
|
+
export const processUnknownUnitInStyleObject = (context, node) => {
|
|
126
|
+
// Defensive: This function is only called with ObjectExpression nodes by the rule visitor.
|
|
127
|
+
// This check's for type safety and future-proofing. It's not covered by rule tests
|
|
128
|
+
// because the rule architecture prevents non-ObjectExpression nodes from reaching here.
|
|
129
|
+
if (!node || node.type !== AST_NODE_TYPES.ObjectExpression) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
for (const property of node.properties) {
|
|
133
|
+
if (property.type !== AST_NODE_TYPES.Property) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
// Get property key name if possible
|
|
137
|
+
let propertyName = null;
|
|
138
|
+
if (property.key.type === AST_NODE_TYPES.Identifier) {
|
|
139
|
+
propertyName = property.key.name;
|
|
140
|
+
}
|
|
141
|
+
else if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
|
|
142
|
+
propertyName = property.key.value;
|
|
143
|
+
}
|
|
144
|
+
if (propertyName === '@media' || propertyName === 'selectors') {
|
|
145
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
146
|
+
for (const nestedProperty of property.value.properties) {
|
|
147
|
+
if (nestedProperty.type === AST_NODE_TYPES.Property &&
|
|
148
|
+
nestedProperty.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
149
|
+
processUnknownUnitInStyleObject(context, nestedProperty.value);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
// Process direct string values
|
|
156
|
+
const value = getStringValue(property.value);
|
|
157
|
+
if (value) {
|
|
158
|
+
const result = checkCssUnit(value);
|
|
159
|
+
if (result.hasUnit && !result.isValid && result.invalidValue) {
|
|
160
|
+
context.report({
|
|
161
|
+
node: property.value,
|
|
162
|
+
messageId: 'unknownUnit',
|
|
163
|
+
data: {
|
|
164
|
+
unit: result.unit || '',
|
|
165
|
+
value: result.invalidValue,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// Process nested objects (including those not handled by special cases)
|
|
171
|
+
if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
172
|
+
processUnknownUnitInStyleObject(context, property.value);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
|
|
3
|
+
import { processStyleNode } from '../shared-utils/style-node-processor.js';
|
|
4
|
+
import { processUnknownUnitInStyleObject } from './unknown-unit-processor.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates ESLint rule visitors for detecting and processing unknown CSS units
|
|
7
|
+
* in style-related function calls.
|
|
8
|
+
*/
|
|
9
|
+
export const createUnknownUnitVisitors = (context) => {
|
|
10
|
+
return {
|
|
11
|
+
CallExpression(node) {
|
|
12
|
+
if (node.callee.type !== AST_NODE_TYPES.Identifier) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (['fontFace', 'globalFontFace'].includes(node.callee.name)) {
|
|
16
|
+
const argumentIndex = node.callee.name === 'fontFace' ? 0 : 1;
|
|
17
|
+
if (node.arguments.length > argumentIndex &&
|
|
18
|
+
node.arguments[argumentIndex]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
19
|
+
processUnknownUnitInStyleObject(context, node.arguments[argumentIndex]);
|
|
20
|
+
}
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (['keyframes', 'style', 'styleVariants'].includes(node.callee.name)) {
|
|
24
|
+
if (node.arguments.length > 0) {
|
|
25
|
+
processStyleNode(context, node.arguments[0], processUnknownUnitInStyleObject);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (['globalKeyframes', 'globalStyle'].includes(node.callee.name) && node.arguments.length >= 2) {
|
|
29
|
+
processStyleNode(context, node.arguments[1], processUnknownUnitInStyleObject);
|
|
30
|
+
}
|
|
31
|
+
if (node.callee.name === 'recipe' &&
|
|
32
|
+
node.arguments.length > 0 &&
|
|
33
|
+
node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
34
|
+
processRecipeProperties(context, node.arguments[0], processUnknownUnitInStyleObject);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
declare const vanillaExtract: {
|
|
2
2
|
meta: {
|
|
3
3
|
name: string;
|
|
4
4
|
version: string;
|
|
@@ -8,17 +8,9 @@ export 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-unknown-unit': import("eslint").Rule.RuleModule;
|
|
11
12
|
'no-zero-unit': import("eslint").Rule.RuleModule;
|
|
12
13
|
};
|
|
13
|
-
configs: {
|
|
14
|
-
recommended: {
|
|
15
|
-
plugins: string[];
|
|
16
|
-
rules: {
|
|
17
|
-
'vanilla-extract/concentric-order': string;
|
|
18
|
-
'vanilla-extract/no-empty-style-blocks': string;
|
|
19
|
-
'vanilla-extract/no-zero-unit': string;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
};
|
|
14
|
+
configs: {};
|
|
23
15
|
};
|
|
24
16
|
export default vanillaExtract;
|
package/dist/index.js
CHANGED
|
@@ -2,28 +2,34 @@ 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 noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
|
|
5
6
|
import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
|
|
6
|
-
|
|
7
|
+
const vanillaExtract = {
|
|
7
8
|
meta: {
|
|
8
9
|
name: '@antebudimir/eslint-plugin-vanilla-extract',
|
|
9
|
-
version: '1.
|
|
10
|
+
version: '1.10.0',
|
|
10
11
|
},
|
|
11
12
|
rules: {
|
|
12
13
|
'alphabetical-order': alphabeticalOrderRule,
|
|
13
14
|
'concentric-order': concentricOrderRule,
|
|
14
15
|
'custom-order': customOrderRule,
|
|
15
16
|
'no-empty-style-blocks': noEmptyStyleBlocksRule,
|
|
17
|
+
'no-unknown-unit': noUnknownUnitRule,
|
|
16
18
|
'no-zero-unit': noZeroUnitRule,
|
|
17
19
|
},
|
|
18
|
-
configs: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
configs: {},
|
|
21
|
+
};
|
|
22
|
+
Object.assign(vanillaExtract.configs, {
|
|
23
|
+
recommended: {
|
|
24
|
+
plugins: {
|
|
25
|
+
'vanilla-extract': vanillaExtract,
|
|
26
|
+
},
|
|
27
|
+
rules: {
|
|
28
|
+
'vanilla-extract/concentric-order': 'error',
|
|
29
|
+
'vanilla-extract/no-empty-style-blocks': 'error',
|
|
30
|
+
'vanilla-extract/no-unknown-unit': 'error',
|
|
31
|
+
'vanilla-extract/no-zero-unit': 'error',
|
|
26
32
|
},
|
|
27
33
|
},
|
|
28
|
-
};
|
|
34
|
+
});
|
|
29
35
|
export default vanillaExtract;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antebudimir/eslint-plugin-vanilla-extract",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.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",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"node": ">=18.18.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"eslint": ">=
|
|
48
|
+
"eslint": ">=8.57.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@eslint/eslintrc": "^3.3.0",
|