@antebudimir/eslint-plugin-vanilla-extract 1.9.0 → 1.11.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 +160 -32
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.d.ts +2 -3
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +101 -68
- package/dist/css-rules/no-unknown-unit/unknown-unit-visitor-creator.d.ts +2 -1
- package/dist/css-rules/no-unknown-unit/unknown-unit-visitor-creator.js +43 -18
- package/dist/css-rules/no-zero-unit/zero-unit-visitor-creator.d.ts +1 -6
- package/dist/css-rules/no-zero-unit/zero-unit-visitor-creator.js +42 -23
- package/dist/css-rules/shared-utils/order-strategy-visitor-creator.d.ts +3 -9
- package/dist/css-rules/shared-utils/order-strategy-visitor-creator.js +88 -63
- package/dist/css-rules/shared-utils/reference-based-visitor-creator.d.ts +19 -0
- package/dist/css-rules/shared-utils/reference-based-visitor-creator.js +108 -0
- package/dist/css-rules/shared-utils/reference-tracker.d.ts +85 -0
- package/dist/css-rules/shared-utils/reference-tracker.js +260 -0
- package/dist/index.d.ts +2 -12
- package/dist/index.js +15 -12
- 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,18 +21,23 @@ 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
|
|
21
28
|
- Works with camelCase properties as used in vanilla-extract
|
|
22
29
|
- Additional linting rules for enhanced code quality (see roadmap for upcoming features)
|
|
30
|
+
- Automatic wrapper function detection - works with custom wrapper functions that call vanilla-extract APIs, using
|
|
31
|
+
reference tracking to apply all rules regardless of how vanilla-extract functions are wrapped
|
|
23
32
|
|
|
24
33
|
## Requirements
|
|
25
34
|
|
|
26
|
-
- ESLint
|
|
35
|
+
- ESLint 8.57.0 or higher
|
|
27
36
|
- Node.js 18.18.0 or higher
|
|
28
37
|
- ESM (ECMAScript Modules) only
|
|
38
|
+
- Flat config system using either:
|
|
39
|
+
- `eslint.config.mjs` (recommended, always works with ESM plugins)
|
|
40
|
+
- `eslint.config.js` (only if your package.json has `"type": "module"`)
|
|
29
41
|
|
|
30
42
|
## Installation
|
|
31
43
|
|
|
@@ -38,21 +50,47 @@ yarn add --dev @antebudimir/eslint-plugin-vanilla-extract
|
|
|
38
50
|
|
|
39
51
|
# Using pnpm
|
|
40
52
|
pnpm add -D @antebudimir/eslint-plugin-vanilla-extract
|
|
53
|
+
|
|
54
|
+
# For ESLint 8.57.0 with flat config, you'll also need:
|
|
55
|
+
npm install --save-dev @eslint/eslintrc @eslint/js
|
|
56
|
+
yarn add --dev @eslint/eslintrc @eslint/js
|
|
57
|
+
pnpm add -D @eslint/eslintrc @eslint/js
|
|
41
58
|
```
|
|
42
59
|
|
|
43
60
|
## Usage
|
|
44
61
|
|
|
45
62
|
**Note: This plugin is ESM-only.** It must be used with ESM configurations and can't be used with CommonJS `require()`.
|
|
46
63
|
|
|
47
|
-
###
|
|
64
|
+
### Configuration Options
|
|
65
|
+
|
|
66
|
+
There are two main ways to configure this plugin in your ESLint flat config:
|
|
48
67
|
|
|
49
|
-
|
|
68
|
+
### Option 1: Using extends (recommended, available from v1.10.0)
|
|
69
|
+
|
|
70
|
+
The simplest way to apply the recommended ruleset:
|
|
50
71
|
|
|
51
72
|
```typescript
|
|
73
|
+
import { defineConfig } from 'eslint/config';
|
|
52
74
|
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
53
75
|
|
|
54
|
-
|
|
55
|
-
|
|
76
|
+
export default defineConfig([
|
|
77
|
+
{
|
|
78
|
+
files: ['**/*.css.ts'],
|
|
79
|
+
ignores: ['src/**/theme-contract.css.ts'],
|
|
80
|
+
extends: [vanillaExtract.configs.recommended],
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Option 2: Using plugins with explicit rule configuration
|
|
86
|
+
|
|
87
|
+
This approach gives you more control over individual rules:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { defineConfig } from 'eslint/config';
|
|
91
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
92
|
+
|
|
93
|
+
export default defineConfig([
|
|
56
94
|
{
|
|
57
95
|
files: ['**/*.css.ts'],
|
|
58
96
|
ignores: ['src/**/theme-contract.css.ts'],
|
|
@@ -62,12 +100,12 @@ export default [
|
|
|
62
100
|
rules: {
|
|
63
101
|
// Apply all recommended rules
|
|
64
102
|
...vanillaExtract.configs.recommended.rules,
|
|
65
|
-
|
|
103
|
+
|
|
66
104
|
// Optionally override specific rules
|
|
67
105
|
// 'vanilla-extract/concentric-order': 'warn', // Change severity from error to warn
|
|
68
106
|
// 'vanilla-extract/no-empty-style-blocks': 'off', // Disable a recommended rule
|
|
69
107
|
// 'vanilla-extract/no-zero-unit': 'warn', // Change severity from error to warn
|
|
70
|
-
|
|
108
|
+
|
|
71
109
|
// Add additional rules not in recommended config
|
|
72
110
|
// 'vanilla-extract/alphabetical-order': 'error', // Override concentric-order rule
|
|
73
111
|
},
|
|
@@ -75,6 +113,80 @@ export default [
|
|
|
75
113
|
];
|
|
76
114
|
```
|
|
77
115
|
|
|
116
|
+
### Using with FlatCompat (for ESLint 8.57.0 & 8.57.1)
|
|
117
|
+
|
|
118
|
+
If you're migrating from legacy ESLint configurations, you can use the `FlatCompat` utility to convert them while adding
|
|
119
|
+
vanilla-extract support:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import path from 'path';
|
|
123
|
+
import { fileURLToPath } from 'url';
|
|
124
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
125
|
+
import js from '@eslint/js';
|
|
126
|
+
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
127
|
+
|
|
128
|
+
// Mimic CommonJS variables
|
|
129
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
130
|
+
const __dirname = path.dirname(__filename);
|
|
131
|
+
|
|
132
|
+
// Create a compatibility layer instance
|
|
133
|
+
const compat = new FlatCompat({
|
|
134
|
+
baseDirectory: __dirname,
|
|
135
|
+
recommendedConfig: js.configs.recommended,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
export default [
|
|
139
|
+
// Convert your existing ESLint configs
|
|
140
|
+
...compat.config({
|
|
141
|
+
extends: [
|
|
142
|
+
'eslint:recommended',
|
|
143
|
+
'plugin:@typescript-eslint/recommended',
|
|
144
|
+
// etc
|
|
145
|
+
],
|
|
146
|
+
}),
|
|
147
|
+
|
|
148
|
+
// Add vanilla-extract by using explicit rule config
|
|
149
|
+
{
|
|
150
|
+
files: ['**/*.css.ts'],
|
|
151
|
+
ignores: ['src/**/theme-contract.css.ts'],
|
|
152
|
+
plugins: {
|
|
153
|
+
'vanilla-extract': vanillaExtract,
|
|
154
|
+
},
|
|
155
|
+
rules: {
|
|
156
|
+
// Apply all recommended rules
|
|
157
|
+
...vanillaExtract.configs.recommended.rules,
|
|
158
|
+
// or specify rule by rule as described above
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
];
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### Common Issues with FlatCompat
|
|
165
|
+
|
|
166
|
+
1. **Error: "Unexpected top-level property 'files'"**
|
|
167
|
+
|
|
168
|
+
- Solution: When using `compat.config()`, use `overrides` instead of `files` at the top level.
|
|
169
|
+
|
|
170
|
+
2. **Error: "Missing parameter 'recommendedConfig' in FlatCompat constructor"**
|
|
171
|
+
|
|
172
|
+
- Solution: Import `js` from `@eslint/js` and add `recommendedConfig: js.configs.recommended` to the FlatCompat
|
|
173
|
+
constructor.
|
|
174
|
+
|
|
175
|
+
3. **Error: "Unexpected undefined config at user-defined index 0"**
|
|
176
|
+
- Solution: Make sure you're using a default export for your configuration array.
|
|
177
|
+
|
|
178
|
+
### VS Code Integration
|
|
179
|
+
|
|
180
|
+
For VS Code users, add these settings to your `.vscode/settings.json`:
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{
|
|
184
|
+
"eslint.useFlatConfig": true,
|
|
185
|
+
"eslint.experimental.useFlatConfig": true,
|
|
186
|
+
"eslint.validate": ["javascript", "typescript", "typescriptreact"]
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
78
190
|
### Recommended Configuration
|
|
79
191
|
|
|
80
192
|
The recommended configuration enables the following rules with error severity:
|
|
@@ -181,20 +293,27 @@ export const myStyle = style({
|
|
|
181
293
|
|
|
182
294
|
### vanilla-extract/custom-order
|
|
183
295
|
|
|
184
|
-
The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your
|
|
296
|
+
The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your
|
|
297
|
+
vanilla-extract styles. You can specify an array of property groups in your preferred order, and the rule will ensure
|
|
298
|
+
that properties within these groups are sorted according to their position in the concentric CSS model.
|
|
185
299
|
|
|
186
300
|
Key features of this rule include:
|
|
187
301
|
|
|
188
302
|
1. Custom group ordering: Define your preferred order of CSS property groups.
|
|
189
|
-
2. Handling of unspecified groups: All groups not included in the custom array will have their properties sorted after
|
|
190
|
-
|
|
303
|
+
2. Handling of unspecified groups: All groups not included in the custom array will have their properties sorted after
|
|
304
|
+
the last specified group.
|
|
305
|
+
3. Flexible sorting options: You can choose to sort remaining properties either alphabetically or following the
|
|
306
|
+
concentric CSS order by setting the `sortRemainingProperties` option to 'alphabetical' or 'concentric' respectively.
|
|
191
307
|
|
|
192
308
|
Default behavior:
|
|
193
309
|
|
|
194
310
|
- If not set, `sortRemainingProperties` defaults to 'alphabetical'.
|
|
195
|
-
- If no `groupOrder` is specified or an empty array is provided, the rule will default to sorting all properties
|
|
311
|
+
- If no `groupOrder` is specified or an empty array is provided, the rule will default to sorting all properties
|
|
312
|
+
alphabetically, and `sortRemainingProperties` will be ignored even if set.
|
|
196
313
|
|
|
197
|
-
To configure the rule, add it to your ESLint configuration file with your desired options. You can customize the
|
|
314
|
+
To configure the rule, add it to your ESLint configuration file with your desired options. You can customize the
|
|
315
|
+
`groups` array to include any number of available CSS property groups you want to enforce, with a minimum of one group
|
|
316
|
+
required.
|
|
198
317
|
|
|
199
318
|
```typescript
|
|
200
319
|
// ❌ Incorrect (Unordered)
|
|
@@ -237,7 +356,8 @@ export const myStyle = style({
|
|
|
237
356
|
|
|
238
357
|
### vanilla-extract/no-empty-style-blocks
|
|
239
358
|
|
|
240
|
-
This rule detects and prevents empty style blocks in vanilla-extract stylesheets. It helps maintain cleaner codebases by
|
|
359
|
+
This rule detects and prevents empty style blocks in vanilla-extract stylesheets. It helps maintain cleaner codebases by
|
|
360
|
+
eliminating empty style definitions that often result from incomplete refactoring or forgotten implementations.
|
|
241
361
|
|
|
242
362
|
```typescript
|
|
243
363
|
// ❌ Incorrect
|
|
@@ -273,23 +393,24 @@ export const recipeWithEmptyVariants = recipe({
|
|
|
273
393
|
|
|
274
394
|
## vanilla-extract/no-unknown-unit
|
|
275
395
|
|
|
276
|
-
This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units
|
|
396
|
+
This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units
|
|
397
|
+
that could cause styling issues or browser compatibility problems.
|
|
277
398
|
|
|
278
399
|
```typescript
|
|
279
400
|
// ❌ Incorrect
|
|
280
401
|
import { style, globalStyle, recipe } from '@vanilla-extract/css';
|
|
281
402
|
|
|
282
403
|
export const invalidStyle = style({
|
|
283
|
-
margin: '5abc',
|
|
404
|
+
margin: '5abc', // Non-existent unit
|
|
284
405
|
fontSize: '1.5rems', // Typo in unit
|
|
285
406
|
});
|
|
286
407
|
|
|
287
408
|
export const myRecipe = recipe({
|
|
288
409
|
variants: {
|
|
289
410
|
size: {
|
|
290
|
-
large: { padding: '4xm' } // Invalid unit
|
|
291
|
-
}
|
|
292
|
-
}
|
|
411
|
+
large: { padding: '4xm' }, // Invalid unit
|
|
412
|
+
},
|
|
413
|
+
},
|
|
293
414
|
});
|
|
294
415
|
|
|
295
416
|
// ✅ Correct
|
|
@@ -303,15 +424,16 @@ export const validStyle = style({
|
|
|
303
424
|
export const myRecipe = recipe({
|
|
304
425
|
variants: {
|
|
305
426
|
size: {
|
|
306
|
-
large: { padding: '4em' }
|
|
307
|
-
}
|
|
308
|
-
}
|
|
427
|
+
large: { padding: '4em' },
|
|
428
|
+
},
|
|
429
|
+
},
|
|
309
430
|
});
|
|
310
431
|
```
|
|
311
432
|
|
|
312
433
|
## vanilla-extract/no-zero-unit
|
|
313
434
|
|
|
314
|
-
This rule enforces the removal of unnecessary units for zero values in vanilla-extract style objects. It helps maintain
|
|
435
|
+
This rule enforces the removal of unnecessary units for zero values in vanilla-extract style objects. It helps maintain
|
|
436
|
+
cleaner and more consistent CSS by eliminating redundant units when the value is zero.
|
|
315
437
|
|
|
316
438
|
```typescript
|
|
317
439
|
// ❌ Incorrect
|
|
@@ -339,7 +461,8 @@ export const myStyle = style({
|
|
|
339
461
|
|
|
340
462
|
## Font Face Declarations
|
|
341
463
|
|
|
342
|
-
For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce
|
|
464
|
+
For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce
|
|
465
|
+
the same special ordering:
|
|
343
466
|
|
|
344
467
|
1. The `src` property always appears first
|
|
345
468
|
2. All remaining properties are sorted alphabetically
|
|
@@ -367,7 +490,8 @@ Opinionated, but it is what it is. If someone has a suggestion for a better orde
|
|
|
367
490
|
|
|
368
491
|
## Concentric CSS Model
|
|
369
492
|
|
|
370
|
-
Here's a list of all available groups from the provided
|
|
493
|
+
Here's a list of all available groups from the provided
|
|
494
|
+
[concentricGroups](src/css-rules/concentric-order/concentric-groups.ts) array:
|
|
371
495
|
|
|
372
496
|
1. boxSizing
|
|
373
497
|
2. position
|
|
@@ -399,7 +523,8 @@ Here's a list of all available groups from the provided [concentricGroups](src/c
|
|
|
399
523
|
28. counters
|
|
400
524
|
29. breaks
|
|
401
525
|
|
|
402
|
-
These groups represent different categories of CSS properties, organized in a concentric order from outside to inside.
|
|
526
|
+
These groups represent different categories of CSS properties, organized in a concentric order from outside to inside.
|
|
527
|
+
Each group contains related CSS properties that affect specific aspects of an element's styling and layout.
|
|
403
528
|
|
|
404
529
|
## Roadmap
|
|
405
530
|
|
|
@@ -414,21 +539,24 @@ The roadmap outlines the project's current status and future plans:
|
|
|
414
539
|
- Recommended ESLint configuration for the plugin.
|
|
415
540
|
- `no-zero-unit` rule to disallow units when the value is zero.
|
|
416
541
|
- `no-unknown-unit` rule to disallow unknown units.
|
|
542
|
+
- Support for using the plugin’s recommended config via the extends field (as discussed in
|
|
543
|
+
[issue #3](https://github.com/antebudimir/eslint-plugin-vanilla-extract/issues/3))
|
|
417
544
|
- Comprehensive rule testing.
|
|
418
545
|
|
|
419
546
|
### Current Work
|
|
420
547
|
|
|
421
|
-
-
|
|
548
|
+
- `no-number-trailing-zero` rule to disallow trailing zeros in numbers.
|
|
422
549
|
|
|
423
550
|
### Upcoming Features
|
|
424
551
|
|
|
425
|
-
- `no-number-trailing-zero` rule to disallow trailing zeros in numbers.
|
|
426
552
|
- `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
|
|
427
553
|
- `prefer-logical-properties` rule to enforce use of logical properties.
|
|
428
554
|
- `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
|
|
429
555
|
- `no-global-style` rule to disallow use of `globalStyle` function.
|
|
430
|
-
- `property-unit-match` rule to enforce valid units per CSS property specs. **Note**: This feature will only be
|
|
431
|
-
|
|
556
|
+
- `property-unit-match` rule to enforce valid units per CSS property specs. **Note**: This feature will only be
|
|
557
|
+
implemented if there's sufficient interest from the community.
|
|
558
|
+
- Option to sort properties within user-defined concentric groups alphabetically instead of following the concentric
|
|
559
|
+
order. **Note**: This feature will only be implemented if there's sufficient interest from the community.
|
|
432
560
|
|
|
433
561
|
## Contributing
|
|
434
562
|
|
|
@@ -5,8 +5,7 @@ import { TSESTree } from '@typescript-eslint/utils';
|
|
|
5
5
|
*/
|
|
6
6
|
export declare const isEffectivelyEmptyStylesObject: (stylesObject: TSESTree.ObjectExpression) => boolean;
|
|
7
7
|
/**
|
|
8
|
-
* Creates ESLint rule visitors for detecting empty style blocks
|
|
9
|
-
*
|
|
10
|
-
* @returns An object with visitor functions for the ESLint rule.
|
|
8
|
+
* Creates ESLint rule visitors for detecting empty style blocks using reference tracking.
|
|
9
|
+
* This automatically detects vanilla-extract functions based on their import statements.
|
|
11
10
|
*/
|
|
12
11
|
export declare const createEmptyStyleVisitors: (ruleContext: Rule.RuleContext) => Rule.RuleListener;
|
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
2
|
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { ReferenceTracker, createReferenceTrackingVisitor } from '../shared-utils/reference-tracker.js';
|
|
3
4
|
import { processConditionalExpression } from './conditional-processor.js';
|
|
4
5
|
import { processEmptyNestedStyles } from './empty-nested-style-processor.js';
|
|
5
6
|
import { reportEmptyDeclaration } from './fix-utils.js';
|
|
6
|
-
import { removeNodeWithComma } from './node-remover.js';
|
|
7
7
|
import { getStyleKeyName } from './property-utils.js';
|
|
8
8
|
import { processRecipeProperties } from './recipe-processor.js';
|
|
9
9
|
import { processStyleVariants } from './style-variants-processor.js';
|
|
10
|
+
/**
|
|
11
|
+
* Checks if a nested object (selectors, media, supports) contains only empty objects.
|
|
12
|
+
*/
|
|
13
|
+
const isNestedObjectEmpty = (obj) => {
|
|
14
|
+
if (obj.properties.length === 0) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return obj.properties.every((property) => {
|
|
18
|
+
if (property.type !== 'Property') {
|
|
19
|
+
return true; // Skip non-property elements
|
|
20
|
+
}
|
|
21
|
+
if (property.value.type === 'ObjectExpression') {
|
|
22
|
+
return isEmptyObject(property.value);
|
|
23
|
+
}
|
|
24
|
+
return false; // Non-object values mean it's not empty
|
|
25
|
+
});
|
|
26
|
+
};
|
|
10
27
|
/**
|
|
11
28
|
* Checks if a style object is effectively empty (contains only empty objects).
|
|
12
29
|
*/
|
|
@@ -42,98 +59,98 @@ export const isEffectivelyEmptyStylesObject = (stylesObject) => {
|
|
|
42
59
|
}
|
|
43
60
|
}
|
|
44
61
|
}
|
|
45
|
-
// If this looks like a recipe
|
|
62
|
+
// If this looks like a recipe (has base or variants), check recipe-specific emptiness
|
|
46
63
|
if (hasBaseProperty || hasVariantsProperty) {
|
|
47
|
-
// A recipe is effectively empty if both base and variants are empty
|
|
48
64
|
return isBaseEmpty && areAllVariantsEmpty;
|
|
49
65
|
}
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const specialProperties = stylesObject.properties.filter((prop) => prop.type === 'Property' && isSpecialProperty(getStyleKeyName(prop.key)));
|
|
55
|
-
const allSpecialPropertiesEmpty = specialProperties.every((property) => {
|
|
56
|
-
if (property.value.type === 'ObjectExpression' && isEmptyObject(property.value)) {
|
|
57
|
-
return true;
|
|
66
|
+
// For regular style objects, check if all properties are effectively empty
|
|
67
|
+
return stylesObject.properties.every((property) => {
|
|
68
|
+
if (property.type !== 'Property') {
|
|
69
|
+
return true; // Skip spread elements for emptiness check
|
|
58
70
|
}
|
|
59
71
|
const propertyName = getStyleKeyName(property.key);
|
|
60
|
-
// This defensive check handles malformed AST nodes that lack valid property names.
|
|
61
|
-
// This is difficult to test because it's challenging to construct a valid AST
|
|
62
|
-
// where getStyleKeyName would return a falsy value.
|
|
63
72
|
if (!propertyName) {
|
|
64
|
-
return
|
|
73
|
+
return true; // Skip properties we can't identify
|
|
65
74
|
}
|
|
66
|
-
//
|
|
67
|
-
if (
|
|
68
|
-
property.value.type === 'ObjectExpression') {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return property.value
|
|
76
|
-
return (nestedProperty.type === 'Property' &&
|
|
77
|
-
nestedProperty.value.type === 'ObjectExpression' &&
|
|
78
|
-
isEmptyObject(nestedProperty.value));
|
|
79
|
-
});
|
|
75
|
+
// Handle special nested objects like selectors, media queries, supports
|
|
76
|
+
if (propertyName === 'selectors' || propertyName.startsWith('@')) {
|
|
77
|
+
if (property.value.type === 'ObjectExpression') {
|
|
78
|
+
return isNestedObjectEmpty(property.value);
|
|
79
|
+
}
|
|
80
|
+
return false; // Non-object values in these properties
|
|
81
|
+
}
|
|
82
|
+
// Handle regular CSS properties
|
|
83
|
+
if (property.value.type === 'ObjectExpression') {
|
|
84
|
+
return isEmptyObject(property.value);
|
|
80
85
|
}
|
|
81
|
-
|
|
82
|
-
// This is difficult to test because it requires creating an AST structure
|
|
83
|
-
// that doesn't trigger any of the preceding return statements.
|
|
84
|
-
return false;
|
|
86
|
+
return false; // Non-empty property (literal values, etc.)
|
|
85
87
|
});
|
|
86
|
-
// If we have special properties and they're all empty, the style is effectively empty
|
|
87
|
-
return specialProperties.length > 0 && allSpecialPropertiesEmpty;
|
|
88
88
|
};
|
|
89
89
|
/**
|
|
90
|
-
* Creates ESLint rule visitors for detecting empty style blocks
|
|
91
|
-
*
|
|
92
|
-
* @returns An object with visitor functions for the ESLint rule.
|
|
90
|
+
* Creates ESLint rule visitors for detecting empty style blocks using reference tracking.
|
|
91
|
+
* This automatically detects vanilla-extract functions based on their import statements.
|
|
93
92
|
*/
|
|
94
93
|
export const createEmptyStyleVisitors = (ruleContext) => {
|
|
95
|
-
|
|
94
|
+
const tracker = new ReferenceTracker();
|
|
95
|
+
const trackingVisitor = createReferenceTrackingVisitor(tracker);
|
|
96
96
|
const reportedNodes = new Set();
|
|
97
97
|
return {
|
|
98
|
+
// Include the reference tracking visitors
|
|
99
|
+
...trackingVisitor,
|
|
98
100
|
CallExpression(node) {
|
|
99
101
|
if (node.callee.type !== 'Identifier') {
|
|
100
102
|
return;
|
|
101
103
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
'keyframes',
|
|
111
|
-
'globalKeyframes',
|
|
112
|
-
];
|
|
113
|
-
if (!styleApiFunctions.includes(node.callee.name) || node.arguments.length === 0) {
|
|
104
|
+
const functionName = node.callee.name;
|
|
105
|
+
// Check if this function is tracked as a vanilla-extract function
|
|
106
|
+
if (!tracker.isTrackedFunction(functionName)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const originalName = tracker.getOriginalName(functionName);
|
|
110
|
+
const wrapperInfo = tracker.getWrapperInfo(functionName);
|
|
111
|
+
if (!originalName || node.arguments.length === 0) {
|
|
114
112
|
return;
|
|
115
113
|
}
|
|
116
114
|
// Handle styleVariants specifically
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
|
|
115
|
+
if (originalName === 'styleVariants') {
|
|
116
|
+
// For wrapper functions, use the correct parameter index
|
|
117
|
+
const styleArgumentIndex = wrapperInfo?.parameterMapping ?? 0;
|
|
118
|
+
if (node.arguments.length <= styleArgumentIndex) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (node.arguments[styleArgumentIndex]?.type === 'ObjectExpression') {
|
|
122
|
+
processStyleVariants(ruleContext, node.arguments[styleArgumentIndex], reportedNodes);
|
|
123
|
+
// If the entire styleVariants object is empty after processing, remove the declaration
|
|
124
|
+
if (isEmptyObject(node.arguments[styleArgumentIndex])) {
|
|
125
|
+
reportEmptyDeclaration(ruleContext, node.arguments[styleArgumentIndex], node);
|
|
126
|
+
}
|
|
122
127
|
}
|
|
123
128
|
return;
|
|
124
129
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
// Determine the style argument index based on the original function name and wrapper info
|
|
131
|
+
let styleArgumentIndex;
|
|
132
|
+
if (wrapperInfo) {
|
|
133
|
+
// Use wrapper function parameter mapping
|
|
134
|
+
styleArgumentIndex = wrapperInfo.parameterMapping;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Use original logic for direct vanilla-extract calls
|
|
138
|
+
styleArgumentIndex =
|
|
139
|
+
originalName === 'globalStyle' || originalName === 'globalKeyframes' || originalName === 'globalFontFace'
|
|
140
|
+
? 1
|
|
141
|
+
: 0;
|
|
142
|
+
}
|
|
129
143
|
// For global functions, check if we have enough arguments
|
|
130
|
-
if (
|
|
144
|
+
if ((originalName === 'globalStyle' || originalName === 'globalKeyframes' || originalName === 'globalFontFace') &&
|
|
145
|
+
node.arguments.length <= styleArgumentIndex) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// For wrapper functions, ensure we have enough arguments
|
|
149
|
+
if (wrapperInfo && node.arguments.length <= styleArgumentIndex) {
|
|
131
150
|
return;
|
|
132
151
|
}
|
|
133
152
|
const styleArgument = node.arguments[styleArgumentIndex];
|
|
134
153
|
// This defensive check prevents duplicate processing of nodes.
|
|
135
|
-
// This code path's difficult to test because the ESLint visitor pattern
|
|
136
|
-
// typically ensures each node is only visited once per rule execution.
|
|
137
154
|
if (reportedNodes.has(styleArgument)) {
|
|
138
155
|
return;
|
|
139
156
|
}
|
|
@@ -149,14 +166,27 @@ export const createEmptyStyleVisitors = (ruleContext) => {
|
|
|
149
166
|
return;
|
|
150
167
|
}
|
|
151
168
|
// For recipe - check if entire recipe is effectively empty
|
|
152
|
-
if (
|
|
153
|
-
if (
|
|
169
|
+
if (originalName === 'recipe') {
|
|
170
|
+
if (styleArgument?.type === 'ObjectExpression') {
|
|
171
|
+
if (isEffectivelyEmptyStylesObject(styleArgument)) {
|
|
172
|
+
reportedNodes.add(styleArgument);
|
|
173
|
+
reportEmptyDeclaration(ruleContext, styleArgument, node);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
// Process individual properties in recipe
|
|
177
|
+
processRecipeProperties(ruleContext, styleArgument, reportedNodes);
|
|
178
|
+
}
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
// Handle fontFace functions - both fontFace and globalFontFace need empty object checks
|
|
182
|
+
if (originalName === 'fontFace' || originalName === 'globalFontFace') {
|
|
183
|
+
// Direct empty object case - remove the entire declaration
|
|
184
|
+
if (styleArgument?.type === 'ObjectExpression' && isEmptyObject(styleArgument)) {
|
|
154
185
|
reportedNodes.add(styleArgument);
|
|
155
186
|
reportEmptyDeclaration(ruleContext, styleArgument, node);
|
|
156
187
|
return;
|
|
157
188
|
}
|
|
158
|
-
|
|
159
|
-
processRecipeProperties(ruleContext, styleArgument, reportedNodes);
|
|
189
|
+
return;
|
|
160
190
|
}
|
|
161
191
|
// For style objects with nested empty objects
|
|
162
192
|
if (styleArgument?.type === 'ObjectExpression') {
|
|
@@ -170,7 +200,10 @@ export const createEmptyStyleVisitors = (ruleContext) => {
|
|
|
170
200
|
node: property.argument,
|
|
171
201
|
messageId: 'emptySpreadObject',
|
|
172
202
|
fix(fixer) {
|
|
173
|
-
|
|
203
|
+
if (property.range) {
|
|
204
|
+
return fixer.removeRange([property.range[0], property.range[1]]);
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
174
207
|
},
|
|
175
208
|
});
|
|
176
209
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
2
|
/**
|
|
3
3
|
* Creates ESLint rule visitors for detecting and processing unknown CSS units
|
|
4
|
-
* in style-related function calls.
|
|
4
|
+
* in style-related function calls using reference tracking.
|
|
5
|
+
* This automatically detects vanilla-extract functions based on their import statements.
|
|
5
6
|
*/
|
|
6
7
|
export declare const createUnknownUnitVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
|