@antebudimir/eslint-plugin-vanilla-extract 1.5.0 → 1.7.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 +78 -4
- package/dist/css-rules/alphabetical-order/property-order-enforcer.js +3 -3
- package/dist/css-rules/concentric-order/style-object-processor.js +2 -2
- package/dist/css-rules/concentric-order/types.d.ts +1 -0
- package/dist/css-rules/custom-order/property-order-enforcer.d.ts +2 -2
- package/dist/css-rules/custom-order/property-order-enforcer.js +2 -3
- package/dist/css-rules/custom-order/recipe-order-enforcer.d.ts +2 -1
- package/dist/css-rules/custom-order/recipe-order-enforcer.js +2 -2
- package/dist/css-rules/custom-order/style-object-processor.d.ts +3 -2
- package/dist/css-rules/custom-order/style-object-processor.js +7 -7
- package/dist/css-rules/no-empty-blocks/conditional-processor.d.ts +6 -0
- package/dist/css-rules/no-empty-blocks/conditional-processor.js +26 -0
- package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.d.ts +6 -0
- package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.js +65 -0
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.d.ts +12 -0
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +183 -0
- package/dist/css-rules/no-empty-blocks/fix-utils.d.ts +22 -0
- package/dist/css-rules/no-empty-blocks/fix-utils.js +75 -0
- package/dist/css-rules/no-empty-blocks/index.d.ts +2 -0
- package/dist/css-rules/no-empty-blocks/index.js +2 -0
- package/dist/css-rules/no-empty-blocks/node-remover.d.ts +10 -0
- package/dist/css-rules/no-empty-blocks/node-remover.js +15 -0
- package/dist/css-rules/no-empty-blocks/property-utils.d.ts +9 -0
- package/dist/css-rules/no-empty-blocks/property-utils.js +28 -0
- package/dist/css-rules/no-empty-blocks/recipe-processor.d.ts +11 -0
- package/dist/css-rules/no-empty-blocks/recipe-processor.js +122 -0
- package/dist/css-rules/no-empty-blocks/rule-definition.d.ts +3 -0
- package/dist/css-rules/no-empty-blocks/rule-definition.js +30 -0
- package/dist/css-rules/no-empty-blocks/style-variants-processor.d.ts +10 -0
- package/dist/css-rules/no-empty-blocks/style-variants-processor.js +45 -0
- package/dist/css-rules/shared-utils/alphabetical-property-comparator.js +3 -3
- package/dist/css-rules/shared-utils/empty-object-processor.d.ts +11 -0
- package/dist/css-rules/shared-utils/empty-object-processor.js +15 -0
- package/dist/css-rules/shared-utils/font-face-property-order-enforcer.js +3 -3
- package/dist/css-rules/shared-utils/order-strategy-visitor-creator.d.ts +3 -1
- package/dist/css-rules/shared-utils/property-separator.d.ts +1 -1
- package/dist/css-rules/shared-utils/property-separator.js +2 -2
- package/dist/css-rules/types.d.ts +1 -0
- package/dist/css-rules/types.js +1 -0
- package/dist/index.d.ts +4 -20
- package/dist/index.js +9 -20
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @antebudimir/eslint-plugin-vanilla-extract
|
|
2
2
|
|
|
3
|
-
[](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml) [](https://coveralls.io/github/antebudimir/eslint-plugin-vanilla-extract?branch=main) [](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract) 
|
|
4
4
|
|
|
5
5
|
An ESLint plugin for enforcing best practices in [vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract) CSS styles, including CSS property ordering and additional linting rules. Available presets are for alphabetical and [concentric](https://rhodesmill.org/brandon/2011/concentric-css/) CSS ordering. The plugin also supports a custom group ordering option based on groups available in [concentric CSS](src/css-rules/concentric-order/concentric-groups.ts).
|
|
6
6
|
|
|
@@ -51,6 +51,43 @@ Create or update your `eslint.config.js` or `eslint.config.mjs` file:
|
|
|
51
51
|
```typescript
|
|
52
52
|
import vanillaExtract from '@antebudimir/eslint-plugin-vanilla-extract';
|
|
53
53
|
|
|
54
|
+
// Using the recommended configuration
|
|
55
|
+
export default [
|
|
56
|
+
{
|
|
57
|
+
files: ['**/*.css.ts'],
|
|
58
|
+
ignores: ['src/**/theme-contract.css.ts'],
|
|
59
|
+
plugins: {
|
|
60
|
+
'vanilla-extract': vanillaExtract,
|
|
61
|
+
},
|
|
62
|
+
rules: {
|
|
63
|
+
// Apply all recommended rules
|
|
64
|
+
...vanillaExtract.configs.recommended.rules,
|
|
65
|
+
|
|
66
|
+
// Optionally override specific rules
|
|
67
|
+
// 'vanilla-extract/concentric-order': 'warn', // Change severity from error to warn
|
|
68
|
+
// 'vanilla-extract/no-empty-style-blocks': 'off', // Disable a recommended rule
|
|
69
|
+
|
|
70
|
+
// Add additional rules not in recommended config
|
|
71
|
+
// 'vanilla-extract/alphabetical-order': 'error', // Override concentric-order rule
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Recommended Configuration
|
|
78
|
+
|
|
79
|
+
The recommended configuration enables the following rules with error severity:
|
|
80
|
+
|
|
81
|
+
- `vanilla-extract/concentric-order`: Enforces concentric CSS property ordering
|
|
82
|
+
- `vanilla-extract/no-empty-style-blocks`: Prevents empty style blocks
|
|
83
|
+
|
|
84
|
+
You can use the recommended configuration as a starting point and override rules as needed for your project.
|
|
85
|
+
|
|
86
|
+
### Custom Configuration
|
|
87
|
+
|
|
88
|
+
If you prefer not to use the recommended configuration, you can still configure rules manually:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
54
91
|
export default [
|
|
55
92
|
{
|
|
56
93
|
files: ['**/*.css.ts'],
|
|
@@ -193,6 +230,42 @@ export const myStyle = style({
|
|
|
193
230
|
});
|
|
194
231
|
```
|
|
195
232
|
|
|
233
|
+
### vanilla-extract/no-empty-style-blocks
|
|
234
|
+
|
|
235
|
+
This rule detects and prevents empty style blocks in vanilla-extract stylesheets. It helps maintain cleaner codebases by eliminating empty style definitions that often result from incomplete refactoring or forgotten implementations.
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
// ❌ Incorrect
|
|
239
|
+
import { style } from '@vanilla-extract/css';
|
|
240
|
+
|
|
241
|
+
export const emptyStyle = style({});
|
|
242
|
+
|
|
243
|
+
export const nestedEmpty = style({
|
|
244
|
+
color: 'blue',
|
|
245
|
+
|
|
246
|
+
':hover': {},
|
|
247
|
+
'@media': {
|
|
248
|
+
'(min-width: 768px)': {},
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
export const recipeWithEmptyVariants = recipe({
|
|
253
|
+
base: { color: 'black' },
|
|
254
|
+
variants: {},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// ✅ Correct
|
|
258
|
+
import { style } from '@vanilla-extract/css';
|
|
259
|
+
|
|
260
|
+
export const nestedEmpty = style({
|
|
261
|
+
color: 'blue',
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
export const recipeWithEmptyVariants = recipe({
|
|
265
|
+
base: { color: 'black' },
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
196
269
|
## Font Face Declarations
|
|
197
270
|
|
|
198
271
|
For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce the same special ordering:
|
|
@@ -266,17 +339,18 @@ The roadmap outlines the project's current status and future plans:
|
|
|
266
339
|
- Initial release with support for alphabetical, concentric, and custom group CSS ordering.
|
|
267
340
|
- Auto-fix capability integrated into ESLint.
|
|
268
341
|
- Support for multiple vanilla-extract APIs (e.g., `style`, `styleVariants`, `recipe`, `globalStyle`, `fontFace`, etc.).
|
|
269
|
-
-
|
|
342
|
+
- `no-empty-style-blocks` rule to disallow empty blocks.
|
|
343
|
+
- Recommended ESLint configuration for the plugin.
|
|
344
|
+
- Comprehensive rule testing.
|
|
270
345
|
|
|
271
346
|
### Current Work
|
|
272
347
|
|
|
273
|
-
- `no-
|
|
348
|
+
- `no-zero-unit` rule to disallow units when the value is zero.
|
|
274
349
|
|
|
275
350
|
### Upcoming Features
|
|
276
351
|
|
|
277
352
|
- `no-unknown-units` rule to disallow unknown units.
|
|
278
353
|
- `no-number-trailing-zeros` rule to disallow trailing zeros in numbers.
|
|
279
|
-
- `no-zero-unit` rule to disallow units when the value is zero.
|
|
280
354
|
- `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
|
|
281
355
|
- `prefer-logical-properties` rule to enforce use of logical properties.
|
|
282
356
|
- `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
2
|
import { comparePropertiesAlphabetically } from '../shared-utils/alphabetical-property-comparator.js';
|
|
3
3
|
import { generateFixesForCSSOrder } from '../shared-utils/css-order-fixer.js';
|
|
4
|
-
import {
|
|
4
|
+
import { getPropertyNameForSorting } from '../shared-utils/property-separator.js';
|
|
5
5
|
/**
|
|
6
6
|
* Reports an ordering issue to ESLint and generates fixes.
|
|
7
7
|
* @param ruleContext The ESLint rule context.
|
|
@@ -14,8 +14,8 @@ const reportOrderingIssue = (ruleContext, currentProperty, nextProperty, regular
|
|
|
14
14
|
node: nextProperty,
|
|
15
15
|
messageId: 'alphabeticalOrder',
|
|
16
16
|
data: {
|
|
17
|
-
nextProperty:
|
|
18
|
-
currentProperty:
|
|
17
|
+
nextProperty: getPropertyNameForSorting(nextProperty),
|
|
18
|
+
currentProperty: getPropertyNameForSorting(currentProperty),
|
|
19
19
|
},
|
|
20
20
|
fix: (fixer) => generateFixesForCSSOrder(fixer, ruleContext, regularProperties, comparePropertiesAlphabetically, (property) => property),
|
|
21
21
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
2
2
|
import { createCSSPropertyPriorityMap } from '../shared-utils/css-property-priority-map.js';
|
|
3
3
|
import { isSelectorsObject, processNestedSelectors } from '../shared-utils/nested-selectors-processor.js';
|
|
4
|
-
import {
|
|
4
|
+
import { getPropertyNameForSorting, separateProperties } from '../shared-utils/property-separator.js';
|
|
5
5
|
import { enforceConcentricCSSOrder } from './property-order-enforcer.js';
|
|
6
6
|
const cssPropertyPriorityMap = createCSSPropertyPriorityMap();
|
|
7
7
|
/**
|
|
@@ -12,7 +12,7 @@ const cssPropertyPriorityMap = createCSSPropertyPriorityMap();
|
|
|
12
12
|
*/
|
|
13
13
|
const buildCSSPropertyInfoList = (regularStyleProperties) => {
|
|
14
14
|
return regularStyleProperties.map((styleProperty) => {
|
|
15
|
-
const propertyName =
|
|
15
|
+
const propertyName = getPropertyNameForSorting(styleProperty);
|
|
16
16
|
const propertyInfo = cssPropertyPriorityMap.get(propertyName);
|
|
17
17
|
return {
|
|
18
18
|
name: propertyName,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
|
-
import type { CSSPropertyInfo } from '../concentric-order/types.js';
|
|
2
|
+
import type { CSSPropertyInfo, SortRemainingProperties } from '../concentric-order/types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Enforces a custom ordering of CSS properties based on user-defined groups.
|
|
5
5
|
*
|
|
@@ -14,4 +14,4 @@ import type { CSSPropertyInfo } from '../concentric-order/types.js';
|
|
|
14
14
|
* specified strategy. If an ordering violation is detected, an ESLint report is generated
|
|
15
15
|
* with a suggested fix.
|
|
16
16
|
*/
|
|
17
|
-
export declare const enforceCustomGroupOrder: (ruleContext: Rule.RuleContext, cssPropertyInfoList: CSSPropertyInfo[], userDefinedGroups?: string[], sortRemainingProperties?:
|
|
17
|
+
export declare const enforceCustomGroupOrder: (ruleContext: Rule.RuleContext, cssPropertyInfoList: CSSPropertyInfo[], userDefinedGroups?: string[], sortRemainingProperties?: SortRemainingProperties) => void;
|
|
@@ -54,9 +54,8 @@ export const enforceCustomGroupOrder = (ruleContext, cssPropertyInfoList, userDe
|
|
|
54
54
|
if (violatingProperty) {
|
|
55
55
|
const indexInSorted = cssPropertyInfoList.indexOf(violatingProperty);
|
|
56
56
|
const sortedProperty = sortedPropertyList[indexInSorted];
|
|
57
|
-
// Defensive programming - sortedProperty will always exist and have a name because sortedPropertyList is derived from cssPropertyInfoList and cssPropertyInfoList exists and is non-empty
|
|
58
|
-
//
|
|
59
|
-
/* v8 ignore next */
|
|
57
|
+
// Defensive programming - sortedProperty will always exist and have a name because sortedPropertyList is derived from cssPropertyInfoList and cssPropertyInfoList exists and is non-empty.
|
|
58
|
+
// This fallback is theoretically unreachable in practice but included for type safety.
|
|
60
59
|
const nextPropertyName = sortedProperty?.name ?? '';
|
|
61
60
|
ruleContext.report({
|
|
62
61
|
node: violatingProperty.node,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
2
|
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
import type { SortRemainingProperties } from '../concentric-order/types.js';
|
|
3
4
|
/**
|
|
4
5
|
* Enforces custom group ordering of CSS properties within a recipe function call.
|
|
5
6
|
*
|
|
@@ -13,4 +14,4 @@ import { TSESTree } from '@typescript-eslint/utils';
|
|
|
13
14
|
* 3. Applies custom group ordering to CSS properties in relevant properties (e.g., 'base', 'variants').
|
|
14
15
|
* 4. Processes nested selectors and style objects recursively.
|
|
15
16
|
*/
|
|
16
|
-
export declare const enforceUserDefinedGroupOrderInRecipe: (ruleContext: Rule.RuleContext, callExpression: TSESTree.CallExpression, userDefinedGroups: string[],
|
|
17
|
+
export declare const enforceUserDefinedGroupOrderInRecipe: (ruleContext: Rule.RuleContext, callExpression: TSESTree.CallExpression, userDefinedGroups: string[], sortRemainingProperties?: SortRemainingProperties) => void;
|
|
@@ -15,9 +15,9 @@ import { enforceUserDefinedGroupOrderInStyleObject } from './style-object-proces
|
|
|
15
15
|
* 3. Applies custom group ordering to CSS properties in relevant properties (e.g., 'base', 'variants').
|
|
16
16
|
* 4. Processes nested selectors and style objects recursively.
|
|
17
17
|
*/
|
|
18
|
-
export const enforceUserDefinedGroupOrderInRecipe = (ruleContext, callExpression, userDefinedGroups,
|
|
18
|
+
export const enforceUserDefinedGroupOrderInRecipe = (ruleContext, callExpression, userDefinedGroups, sortRemainingProperties) => {
|
|
19
19
|
if (callExpression.arguments[0]?.type === 'ObjectExpression') {
|
|
20
20
|
const recipeObjectExpression = callExpression.arguments[0];
|
|
21
|
-
processRecipeProperties(ruleContext, recipeObjectExpression, (currentContext, styleObject) => processStyleNode(currentContext, styleObject, (styleContext, styleObjectNode) => enforceUserDefinedGroupOrderInStyleObject(styleContext, styleObjectNode, userDefinedGroups,
|
|
21
|
+
processRecipeProperties(ruleContext, recipeObjectExpression, (currentContext, styleObject) => processStyleNode(currentContext, styleObject, (styleContext, styleObjectNode) => enforceUserDefinedGroupOrderInStyleObject(styleContext, styleObjectNode, userDefinedGroups, sortRemainingProperties)));
|
|
22
22
|
}
|
|
23
23
|
};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
2
|
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
import type { SortRemainingProperties } from '../concentric-order/types.js';
|
|
3
4
|
/**
|
|
4
5
|
* Enforces a custom ordering of CSS properties based on user-defined groups in a given style object.
|
|
5
6
|
*
|
|
6
7
|
* @param context The ESLint rule context for reporting and fixing issues.
|
|
7
8
|
* @param styleObject The ObjectExpression node representing the style object to be processed.
|
|
8
9
|
* @param userDefinedGroups An array of property groups in the desired order.
|
|
9
|
-
* @param
|
|
10
|
+
* @param sortRemainingProperties Strategy for sorting properties not in user-defined groups ('alphabetical' or 'concentric'). Defaults to 'concentric'.
|
|
10
11
|
*
|
|
11
12
|
* This function:
|
|
12
13
|
* 1. Validates the input styleObject.
|
|
@@ -16,4 +17,4 @@ import { TSESTree } from '@typescript-eslint/utils';
|
|
|
16
17
|
* 5. Enforces custom group ordering on the properties.
|
|
17
18
|
* 6. Recursively processes nested selectors and style objects.
|
|
18
19
|
*/
|
|
19
|
-
export declare const enforceUserDefinedGroupOrderInStyleObject: (ruleContext: Rule.RuleContext, styleObject: TSESTree.ObjectExpression, userDefinedGroups: string[],
|
|
20
|
+
export declare const enforceUserDefinedGroupOrderInStyleObject: (ruleContext: Rule.RuleContext, styleObject: TSESTree.ObjectExpression, userDefinedGroups: string[], sortRemainingProperties?: SortRemainingProperties) => void;
|
|
@@ -2,7 +2,7 @@ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
|
2
2
|
import { concentricGroups } from '../concentric-order/concentric-groups.js';
|
|
3
3
|
import { createCSSPropertyPriorityMap } from '../shared-utils/css-property-priority-map.js';
|
|
4
4
|
import { isSelectorsObject, processNestedSelectors } from '../shared-utils/nested-selectors-processor.js';
|
|
5
|
-
import {
|
|
5
|
+
import { getPropertyNameForSorting, separateProperties } from '../shared-utils/property-separator.js';
|
|
6
6
|
import { enforceCustomGroupOrder } from './property-order-enforcer.js';
|
|
7
7
|
/**
|
|
8
8
|
* Enforces a custom ordering of CSS properties based on user-defined groups in a given style object.
|
|
@@ -10,7 +10,7 @@ import { enforceCustomGroupOrder } from './property-order-enforcer.js';
|
|
|
10
10
|
* @param context The ESLint rule context for reporting and fixing issues.
|
|
11
11
|
* @param styleObject The ObjectExpression node representing the style object to be processed.
|
|
12
12
|
* @param userDefinedGroups An array of property groups in the desired order.
|
|
13
|
-
* @param
|
|
13
|
+
* @param sortRemainingProperties Strategy for sorting properties not in user-defined groups ('alphabetical' or 'concentric'). Defaults to 'concentric'.
|
|
14
14
|
*
|
|
15
15
|
* This function:
|
|
16
16
|
* 1. Validates the input styleObject.
|
|
@@ -20,12 +20,12 @@ import { enforceCustomGroupOrder } from './property-order-enforcer.js';
|
|
|
20
20
|
* 5. Enforces custom group ordering on the properties.
|
|
21
21
|
* 6. Recursively processes nested selectors and style objects.
|
|
22
22
|
*/
|
|
23
|
-
export const enforceUserDefinedGroupOrderInStyleObject = (ruleContext, styleObject, userDefinedGroups,
|
|
23
|
+
export const enforceUserDefinedGroupOrderInStyleObject = (ruleContext, styleObject, userDefinedGroups, sortRemainingProperties = 'concentric') => {
|
|
24
24
|
if (styleObject?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
25
25
|
if (isSelectorsObject(styleObject)) {
|
|
26
26
|
styleObject.properties.forEach((property) => {
|
|
27
27
|
if (property.type === AST_NODE_TYPES.Property && property.value.type === AST_NODE_TYPES.ObjectExpression) {
|
|
28
|
-
enforceUserDefinedGroupOrderInStyleObject(ruleContext, property.value, userDefinedGroups,
|
|
28
|
+
enforceUserDefinedGroupOrderInStyleObject(ruleContext, property.value, userDefinedGroups, sortRemainingProperties);
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
31
|
return;
|
|
@@ -33,7 +33,7 @@ export const enforceUserDefinedGroupOrderInStyleObject = (ruleContext, styleObje
|
|
|
33
33
|
const cssPropertyPriorityMap = createCSSPropertyPriorityMap(userDefinedGroups);
|
|
34
34
|
const { regularProperties } = separateProperties(styleObject.properties);
|
|
35
35
|
const cssPropertyInfoList = regularProperties.map((property) => {
|
|
36
|
-
const propertyName =
|
|
36
|
+
const propertyName = getPropertyNameForSorting(property);
|
|
37
37
|
const propertyInfo = cssPropertyPriorityMap.get(propertyName);
|
|
38
38
|
const group = userDefinedGroups.find((groupName) => concentricGroups[groupName]?.includes(propertyName)) || 'remaining';
|
|
39
39
|
return {
|
|
@@ -45,7 +45,7 @@ export const enforceUserDefinedGroupOrderInStyleObject = (ruleContext, styleObje
|
|
|
45
45
|
inUserGroup: propertyInfo?.inUserGroup ?? false,
|
|
46
46
|
};
|
|
47
47
|
});
|
|
48
|
-
enforceCustomGroupOrder(ruleContext, cssPropertyInfoList, userDefinedGroups,
|
|
49
|
-
processNestedSelectors(ruleContext, styleObject, (nestedContext, nestedNode) => enforceUserDefinedGroupOrderInStyleObject(nestedContext, nestedNode, userDefinedGroups,
|
|
48
|
+
enforceCustomGroupOrder(ruleContext, cssPropertyInfoList, userDefinedGroups, sortRemainingProperties);
|
|
49
|
+
processNestedSelectors(ruleContext, styleObject, (nestedContext, nestedNode) => enforceUserDefinedGroupOrderInStyleObject(nestedContext, nestedNode, userDefinedGroups, sortRemainingProperties));
|
|
50
50
|
}
|
|
51
51
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Handles conditional expressions with empty objects.
|
|
5
|
+
*/
|
|
6
|
+
export declare function processConditionalExpression(context: Rule.RuleContext, node: TSESTree.ConditionalExpression, reportedNodes: Set<TSESTree.Node>, callNode: TSESTree.CallExpression): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { reportEmptyDeclaration } from './fix-utils.js';
|
|
4
|
+
/**
|
|
5
|
+
* Handles conditional expressions with empty objects.
|
|
6
|
+
*/
|
|
7
|
+
export function processConditionalExpression(context, node, reportedNodes, callNode) {
|
|
8
|
+
const isConsequentEmpty = node.consequent.type === 'ObjectExpression' && isEmptyObject(node.consequent);
|
|
9
|
+
const isAlternateEmpty = node.alternate.type === 'ObjectExpression' && isEmptyObject(node.alternate);
|
|
10
|
+
// If both branches are empty, report the entire declaration for removal
|
|
11
|
+
if (isConsequentEmpty && isAlternateEmpty) {
|
|
12
|
+
reportedNodes.add(node);
|
|
13
|
+
reportEmptyDeclaration(context, node, callNode);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
// Otherwise, handle individual empty branches
|
|
17
|
+
if (isConsequentEmpty || isAlternateEmpty) {
|
|
18
|
+
const emptyNode = isConsequentEmpty ? node.consequent : node.alternate;
|
|
19
|
+
reportedNodes.add(emptyNode);
|
|
20
|
+
// No fix provided, just flagging the issue
|
|
21
|
+
context.report({
|
|
22
|
+
node: emptyNode,
|
|
23
|
+
messageId: 'emptyConditionalStyle',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Processes nested style objects like selectors and media queries.
|
|
5
|
+
*/
|
|
6
|
+
export declare function processEmptyNestedStyles(ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { removeNodeWithComma } from './node-remover.js';
|
|
4
|
+
import { areAllChildrenEmpty, getStyleKeyName } from './property-utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* Processes nested style objects like selectors and media queries.
|
|
7
|
+
*/
|
|
8
|
+
export function processEmptyNestedStyles(ruleContext, node, reportedNodes) {
|
|
9
|
+
node.properties.forEach((property) => {
|
|
10
|
+
if (property.type !== 'Property') {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const propertyName = getStyleKeyName(property.key);
|
|
14
|
+
if (!propertyName) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
// Handle selectors, media queries, and supports
|
|
18
|
+
if ((propertyName === 'selectors' || propertyName.startsWith('@')) && property.value.type === 'ObjectExpression') {
|
|
19
|
+
// If the container is empty or all its children are empty, remove the entire property
|
|
20
|
+
if (isEmptyObject(property.value) || areAllChildrenEmpty(property.value)) {
|
|
21
|
+
if (!reportedNodes.has(property)) {
|
|
22
|
+
reportedNodes.add(property);
|
|
23
|
+
const messageId = propertyName === 'selectors'
|
|
24
|
+
? 'emptySelectors'
|
|
25
|
+
: propertyName === '@media'
|
|
26
|
+
? 'emptyMedia'
|
|
27
|
+
: propertyName === '@supports'
|
|
28
|
+
? 'emptySupports'
|
|
29
|
+
: 'emptyConditionalStyle';
|
|
30
|
+
ruleContext.report({
|
|
31
|
+
node: property,
|
|
32
|
+
messageId,
|
|
33
|
+
fix(fixer) {
|
|
34
|
+
return removeNodeWithComma(ruleContext, property, fixer);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Process individual selectors or media queries if we're not removing the entire container
|
|
41
|
+
property.value.properties.forEach((nestedProperty) => {
|
|
42
|
+
if (nestedProperty.type === 'Property') {
|
|
43
|
+
if (nestedProperty.value.type === 'ObjectExpression') {
|
|
44
|
+
if (isEmptyObject(nestedProperty.value)) {
|
|
45
|
+
if (!reportedNodes.has(nestedProperty)) {
|
|
46
|
+
reportedNodes.add(nestedProperty);
|
|
47
|
+
ruleContext.report({
|
|
48
|
+
node: nestedProperty,
|
|
49
|
+
messageId: 'emptyNestedStyle',
|
|
50
|
+
fix(fixer) {
|
|
51
|
+
return removeNodeWithComma(ruleContext, nestedProperty, fixer);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// Recursively process nested styles (for deeply nested selectors/media)
|
|
58
|
+
processEmptyNestedStyles(ruleContext, nestedProperty.value, reportedNodes);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Creates ESLint rule visitors for detecting empty style blocks in vanilla-extract.
|
|
5
|
+
* @param ruleContext The ESLint rule rule context.
|
|
6
|
+
* @returns An object with visitor functions for the ESLint rule.
|
|
7
|
+
*/
|
|
8
|
+
export declare const createEmptyStyleVisitors: (ruleContext: Rule.RuleContext) => Rule.RuleListener;
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a style object is effectively empty (contains only empty objects).
|
|
11
|
+
*/
|
|
12
|
+
export declare function isEffectivelyEmptyStylesObject(stylesObject: TSESTree.ObjectExpression): boolean;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { processConditionalExpression } from './conditional-processor.js';
|
|
4
|
+
import { processEmptyNestedStyles } from './empty-nested-style-processor.js';
|
|
5
|
+
import { reportEmptyDeclaration } from './fix-utils.js';
|
|
6
|
+
import { removeNodeWithComma } from './node-remover.js';
|
|
7
|
+
import { getStyleKeyName } from './property-utils.js';
|
|
8
|
+
import { processRecipeProperties } from './recipe-processor.js';
|
|
9
|
+
import { processStyleVariants } from './style-variants-processor.js';
|
|
10
|
+
/**
|
|
11
|
+
* Creates ESLint rule visitors for detecting empty style blocks in vanilla-extract.
|
|
12
|
+
* @param ruleContext The ESLint rule rule context.
|
|
13
|
+
* @returns An object with visitor functions for the ESLint rule.
|
|
14
|
+
*/
|
|
15
|
+
export const createEmptyStyleVisitors = (ruleContext) => {
|
|
16
|
+
// Track reported nodes to prevent duplicate reports
|
|
17
|
+
const reportedNodes = new Set();
|
|
18
|
+
return {
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
if (node.callee.type !== 'Identifier') {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Target vanilla-extract style functions
|
|
24
|
+
const styleApiFunctions = [
|
|
25
|
+
'style',
|
|
26
|
+
'styleVariants',
|
|
27
|
+
'recipe',
|
|
28
|
+
'globalStyle',
|
|
29
|
+
'fontFace',
|
|
30
|
+
'globalFontFace',
|
|
31
|
+
'keyframes',
|
|
32
|
+
'globalKeyframes',
|
|
33
|
+
];
|
|
34
|
+
if (!styleApiFunctions.includes(node.callee.name) || node.arguments.length === 0) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// Handle styleVariants specifically
|
|
38
|
+
if (node.callee.name === 'styleVariants' && node.arguments[0]?.type === 'ObjectExpression') {
|
|
39
|
+
processStyleVariants(ruleContext, node.arguments[0], reportedNodes);
|
|
40
|
+
// If the entire styleVariants object is empty after processing, remove the declaration
|
|
41
|
+
if (isEmptyObject(node.arguments[0])) {
|
|
42
|
+
reportEmptyDeclaration(ruleContext, node.arguments[0], node);
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const defaultStyleArgumentIndex = 0;
|
|
47
|
+
const globalFunctionNames = ['globalStyle', 'globalFontFace', 'globalKeyframes'];
|
|
48
|
+
// Determine the style argument index based on the function name
|
|
49
|
+
const styleArgumentIndex = globalFunctionNames.includes(node.callee.name) ? 1 : defaultStyleArgumentIndex;
|
|
50
|
+
// For global functions, check if we have enough arguments
|
|
51
|
+
if (styleArgumentIndex === 1 && node.arguments.length <= styleArgumentIndex) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const styleArgument = node.arguments[styleArgumentIndex];
|
|
55
|
+
// This defensive check prevents duplicate processing of nodes.
|
|
56
|
+
// This code path's difficult to test because the ESLint visitor pattern
|
|
57
|
+
// typically ensures each node is only visited once per rule execution.
|
|
58
|
+
if (reportedNodes.has(styleArgument)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
// Handle conditional expressions
|
|
62
|
+
if (styleArgument?.type === 'ConditionalExpression') {
|
|
63
|
+
processConditionalExpression(ruleContext, styleArgument, reportedNodes, node);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// Direct empty object case - remove the entire declaration
|
|
67
|
+
if (styleArgument?.type === 'ObjectExpression' && isEmptyObject(styleArgument)) {
|
|
68
|
+
reportedNodes.add(styleArgument);
|
|
69
|
+
reportEmptyDeclaration(ruleContext, styleArgument, node);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
// For recipe - check if entire recipe is effectively empty
|
|
73
|
+
if (node.callee.name === 'recipe' && styleArgument?.type === 'ObjectExpression') {
|
|
74
|
+
if (isEffectivelyEmptyStylesObject(styleArgument)) {
|
|
75
|
+
reportedNodes.add(styleArgument);
|
|
76
|
+
reportEmptyDeclaration(ruleContext, styleArgument, node);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// Process individual properties in recipe
|
|
80
|
+
processRecipeProperties(ruleContext, styleArgument, reportedNodes);
|
|
81
|
+
}
|
|
82
|
+
// For style objects with nested empty objects
|
|
83
|
+
if (styleArgument?.type === 'ObjectExpression') {
|
|
84
|
+
// Check for spread elements
|
|
85
|
+
styleArgument.properties.forEach((property) => {
|
|
86
|
+
if (property.type === 'SpreadElement' &&
|
|
87
|
+
property.argument.type === 'ObjectExpression' &&
|
|
88
|
+
isEmptyObject(property.argument)) {
|
|
89
|
+
reportedNodes.add(property.argument);
|
|
90
|
+
ruleContext.report({
|
|
91
|
+
node: property.argument,
|
|
92
|
+
messageId: 'emptySpreadObject',
|
|
93
|
+
fix(fixer) {
|
|
94
|
+
return removeNodeWithComma(ruleContext, property, fixer);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
// Process nested selectors and media queries
|
|
100
|
+
processEmptyNestedStyles(ruleContext, styleArgument, reportedNodes);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Checks if a style object is effectively empty (contains only empty objects).
|
|
107
|
+
*/
|
|
108
|
+
export function isEffectivelyEmptyStylesObject(stylesObject) {
|
|
109
|
+
// Empty object itself
|
|
110
|
+
if (stylesObject.properties.length === 0) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// For recipe objects, we need special handling
|
|
114
|
+
let hasBaseProperty = false;
|
|
115
|
+
let isBaseEmpty = true;
|
|
116
|
+
let hasVariantsProperty = false;
|
|
117
|
+
let areAllVariantsEmpty = true;
|
|
118
|
+
// First pass: identify recipe properties
|
|
119
|
+
for (const property of stylesObject.properties) {
|
|
120
|
+
if (property.type !== 'Property') {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const propertyName = getStyleKeyName(property.key);
|
|
124
|
+
if (!propertyName) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (propertyName === 'base') {
|
|
128
|
+
hasBaseProperty = true;
|
|
129
|
+
if (property.value.type === 'ObjectExpression' && !isEmptyObject(property.value)) {
|
|
130
|
+
isBaseEmpty = false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (propertyName === 'variants') {
|
|
134
|
+
hasVariantsProperty = true;
|
|
135
|
+
if (property.value.type === 'ObjectExpression' && !isEmptyObject(property.value)) {
|
|
136
|
+
areAllVariantsEmpty = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// If this looks like a recipe object (has base or variants)
|
|
141
|
+
if (hasBaseProperty || hasVariantsProperty) {
|
|
142
|
+
// A recipe is effectively empty if both base and variants are empty
|
|
143
|
+
return isBaseEmpty && areAllVariantsEmpty;
|
|
144
|
+
}
|
|
145
|
+
// / For non-recipe objects, check if all special properties (selectors, media queries, variants) are effectively empty
|
|
146
|
+
function isSpecialProperty(propertyName) {
|
|
147
|
+
return (propertyName === 'selectors' || (propertyName && propertyName.startsWith('@')) || propertyName === 'variants');
|
|
148
|
+
}
|
|
149
|
+
const specialProperties = stylesObject.properties.filter((prop) => prop.type === 'Property' && isSpecialProperty(getStyleKeyName(prop.key)));
|
|
150
|
+
const allSpecialPropertiesEmpty = specialProperties.every((property) => {
|
|
151
|
+
if (property.value.type === 'ObjectExpression' && isEmptyObject(property.value)) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
const propertyName = getStyleKeyName(property.key);
|
|
155
|
+
// This defensive check handles malformed AST nodes that lack valid property names.
|
|
156
|
+
// This is difficult to test because it's challenging to construct a valid AST
|
|
157
|
+
// where getStyleKeyName would return a falsy value.
|
|
158
|
+
if (!propertyName) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
// For selectors, media queries and supports, check if all nested objects are empty
|
|
162
|
+
if ((propertyName === 'selectors' || (propertyName && propertyName.startsWith('@'))) &&
|
|
163
|
+
property.value.type === 'ObjectExpression') {
|
|
164
|
+
// This handles the edge case of an empty properties array.
|
|
165
|
+
// This code path is difficult to test in isolation because it requires
|
|
166
|
+
// constructing a specific AST structure that bypasses earlier conditions.
|
|
167
|
+
if (property.value.properties.length === 0) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
return property.value.properties.every((nestedProperty) => {
|
|
171
|
+
return (nestedProperty.type === 'Property' &&
|
|
172
|
+
nestedProperty.value.type === 'ObjectExpression' &&
|
|
173
|
+
isEmptyObject(nestedProperty.value));
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// Default fallback for cases not handled by the conditions above.
|
|
177
|
+
// This is difficult to test because it requires creating an AST structure
|
|
178
|
+
// that doesn't trigger any of the preceding return statements.
|
|
179
|
+
return false;
|
|
180
|
+
});
|
|
181
|
+
// If we have special properties and they're all empty, the style is effectively empty
|
|
182
|
+
return specialProperties.length > 0 && allSpecialPropertiesEmpty;
|
|
183
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Finds the target declaration node for a given call expression.
|
|
5
|
+
*
|
|
6
|
+
* Traverses the AST upwards from the `callNode` to find the appropriate
|
|
7
|
+
* declaration node. For global APIs, it targets the expression statement.
|
|
8
|
+
* For non-global APIs, it looks for variable declarations or export
|
|
9
|
+
* named declarations.
|
|
10
|
+
*
|
|
11
|
+
* @param callNode - The call expression node to start from.
|
|
12
|
+
* @param isGlobalApi - A flag indicating whether the API is global.
|
|
13
|
+
* @returns The target declaration node or null if not found.
|
|
14
|
+
*/
|
|
15
|
+
export declare const findTargetDeclarationNode: (callNode: TSESTree.CallExpression, isGlobalApi: boolean) => TSESTree.Node | null;
|
|
16
|
+
/**
|
|
17
|
+
* Reports an issue for an empty style declaration, and provides a fix to remove the declaration.
|
|
18
|
+
* @param ruleContext The ESLint rule context.
|
|
19
|
+
* @param node The node to report the issue on.
|
|
20
|
+
* @param callNode The CallExpression node of the style() function.
|
|
21
|
+
*/
|
|
22
|
+
export declare const reportEmptyDeclaration: (ruleContext: Rule.RuleContext, node: TSESTree.Node, callNode: TSESTree.CallExpression) => void;
|