@antebudimir/eslint-plugin-vanilla-extract 1.5.0 → 1.6.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.
Files changed (42) hide show
  1. package/README.md +40 -3
  2. package/dist/css-rules/alphabetical-order/property-order-enforcer.js +3 -3
  3. package/dist/css-rules/concentric-order/style-object-processor.js +2 -2
  4. package/dist/css-rules/concentric-order/types.d.ts +1 -0
  5. package/dist/css-rules/custom-order/property-order-enforcer.d.ts +2 -2
  6. package/dist/css-rules/custom-order/property-order-enforcer.js +2 -3
  7. package/dist/css-rules/custom-order/recipe-order-enforcer.d.ts +2 -1
  8. package/dist/css-rules/custom-order/recipe-order-enforcer.js +2 -2
  9. package/dist/css-rules/custom-order/style-object-processor.d.ts +3 -2
  10. package/dist/css-rules/custom-order/style-object-processor.js +7 -7
  11. package/dist/css-rules/no-empty-blocks/conditional-processor.d.ts +6 -0
  12. package/dist/css-rules/no-empty-blocks/conditional-processor.js +26 -0
  13. package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.d.ts +6 -0
  14. package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.js +65 -0
  15. package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.d.ts +12 -0
  16. package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +183 -0
  17. package/dist/css-rules/no-empty-blocks/fix-utils.d.ts +22 -0
  18. package/dist/css-rules/no-empty-blocks/fix-utils.js +75 -0
  19. package/dist/css-rules/no-empty-blocks/index.d.ts +2 -0
  20. package/dist/css-rules/no-empty-blocks/index.js +2 -0
  21. package/dist/css-rules/no-empty-blocks/node-remover.d.ts +10 -0
  22. package/dist/css-rules/no-empty-blocks/node-remover.js +15 -0
  23. package/dist/css-rules/no-empty-blocks/property-utils.d.ts +9 -0
  24. package/dist/css-rules/no-empty-blocks/property-utils.js +28 -0
  25. package/dist/css-rules/no-empty-blocks/recipe-processor.d.ts +11 -0
  26. package/dist/css-rules/no-empty-blocks/recipe-processor.js +122 -0
  27. package/dist/css-rules/no-empty-blocks/rule-definition.d.ts +3 -0
  28. package/dist/css-rules/no-empty-blocks/rule-definition.js +30 -0
  29. package/dist/css-rules/no-empty-blocks/style-variants-processor.d.ts +10 -0
  30. package/dist/css-rules/no-empty-blocks/style-variants-processor.js +45 -0
  31. package/dist/css-rules/shared-utils/alphabetical-property-comparator.js +3 -3
  32. package/dist/css-rules/shared-utils/empty-object-processor.d.ts +11 -0
  33. package/dist/css-rules/shared-utils/empty-object-processor.js +15 -0
  34. package/dist/css-rules/shared-utils/font-face-property-order-enforcer.js +3 -3
  35. package/dist/css-rules/shared-utils/order-strategy-visitor-creator.d.ts +3 -1
  36. package/dist/css-rules/shared-utils/property-separator.d.ts +1 -1
  37. package/dist/css-rules/shared-utils/property-separator.js +2 -2
  38. package/dist/css-rules/types.d.ts +1 -0
  39. package/dist/css-rules/types.js +1 -0
  40. package/dist/index.d.ts +3 -0
  41. package/dist/index.js +11 -3
  42. package/package.json +13 -13
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @antebudimir/eslint-plugin-vanilla-extract
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@antebudimir/eslint-plugin-vanilla-extract.svg)](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract) [![CI](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml/badge.svg)](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/antebudimir/eslint-plugin-vanilla-extract/badge.svg?branch=main)](https://coveralls.io/github/antebudimir/eslint-plugin-vanilla-extract?branch=main)
3
+ [![CI](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml/badge.svg)](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/antebudimir/eslint-plugin-vanilla-extract/badge.svg?branch=main)](https://coveralls.io/github/antebudimir/eslint-plugin-vanilla-extract?branch=main) [![npm version](https://img.shields.io/npm/v/@antebudimir/eslint-plugin-vanilla-extract.svg)](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract) ![NPM Downloads](https://img.shields.io/npm/d18m/%40antebudimir%2Feslint-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
 
@@ -193,6 +193,42 @@ export const myStyle = style({
193
193
  });
194
194
  ```
195
195
 
196
+ ### vanilla-extract/no-empty-style-blocks
197
+
198
+ 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.
199
+
200
+ ```typescript
201
+ // ❌ Incorrect
202
+ import { style } from '@vanilla-extract/css';
203
+
204
+ export const emptyStyle = style({});
205
+
206
+ export const nestedEmpty = style({
207
+ color: 'blue',
208
+
209
+ ':hover': {},
210
+ '@media': {
211
+ '(min-width: 768px)': {},
212
+ },
213
+ });
214
+
215
+ export const recipeWithEmptyVariants = recipe({
216
+ base: { color: 'black' },
217
+ variants: {},
218
+ });
219
+
220
+ // ✅ Correct
221
+ import { style } from '@vanilla-extract/css';
222
+
223
+ export const nestedEmpty = style({
224
+ color: 'blue',
225
+ });
226
+
227
+ export const recipeWithEmptyVariants = recipe({
228
+ base: { color: 'black' },
229
+ });
230
+ ```
231
+
196
232
  ## Font Face Declarations
197
233
 
198
234
  For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce the same special ordering:
@@ -266,11 +302,12 @@ The roadmap outlines the project's current status and future plans:
266
302
  - Initial release with support for alphabetical, concentric, and custom group CSS ordering.
267
303
  - Auto-fix capability integrated into ESLint.
268
304
  - Support for multiple vanilla-extract APIs (e.g., `style`, `styleVariants`, `recipe`, `globalStyle`, `fontFace`, etc.).
269
- - Rules tested.
305
+ - `no-empty-style-blocks` rule to disallow empty blocks.
306
+ - Comprehensive rule testing.
270
307
 
271
308
  ### Current Work
272
309
 
273
- - `no-empty-blocks` rule to disallow empty blocks.
310
+ - Setting up recommended ESLint configuration for the plugin.
274
311
 
275
312
  ### Upcoming Features
276
313
 
@@ -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 { getPropertyName } from '../shared-utils/property-separator.js';
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: getPropertyName(nextProperty),
18
- currentProperty: getPropertyName(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 { getPropertyName, separateProperties } from '../shared-utils/property-separator.js';
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 = getPropertyName(styleProperty);
15
+ const propertyName = getPropertyNameForSorting(styleProperty);
16
16
  const propertyInfo = cssPropertyPriorityMap.get(propertyName);
17
17
  return {
18
18
  name: propertyName,
@@ -6,3 +6,4 @@ export interface CSSPropertyInfo {
6
6
  positionInGroup: number;
7
7
  group?: string;
8
8
  }
9
+ export type SortRemainingProperties = 'alphabetical' | 'concentric';
@@ -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?: "alphabetical" | "concentric") => void;
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
- // Therefore, we can exclude the next line from coverage because it's unreachable in practice
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[], sortRemainingPropertiesMethod?: "alphabetical" | "concentric") => void;
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, sortRemainingPropertiesMethod) => {
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, sortRemainingPropertiesMethod)));
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 sortRemainingPropertiesMethod Strategy for sorting properties not in user-defined groups ('alphabetical' or 'concentric'). Defaults to 'concentric'.
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[], sortRemainingPropertiesMethod?: "alphabetical" | "concentric") => void;
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 { getPropertyName, separateProperties } from '../shared-utils/property-separator.js';
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 sortRemainingPropertiesMethod Strategy for sorting properties not in user-defined groups ('alphabetical' or 'concentric'). Defaults to 'concentric'.
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, sortRemainingPropertiesMethod = 'concentric') => {
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, sortRemainingPropertiesMethod);
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 = getPropertyName(property);
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, sortRemainingPropertiesMethod);
49
- processNestedSelectors(ruleContext, styleObject, (nestedContext, nestedNode) => enforceUserDefinedGroupOrderInStyleObject(nestedContext, nestedNode, userDefinedGroups, sortRemainingPropertiesMethod));
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;
@@ -0,0 +1,75 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Finds the target declaration node for a given call expression.
4
+ *
5
+ * Traverses the AST upwards from the `callNode` to find the appropriate
6
+ * declaration node. For global APIs, it targets the expression statement.
7
+ * For non-global APIs, it looks for variable declarations or export
8
+ * named declarations.
9
+ *
10
+ * @param callNode - The call expression node to start from.
11
+ * @param isGlobalApi - A flag indicating whether the API is global.
12
+ * @returns The target declaration node or null if not found.
13
+ */
14
+ export const findTargetDeclarationNode = (callNode, isGlobalApi) => {
15
+ let current = callNode;
16
+ while (current.parent) {
17
+ current = current.parent;
18
+ // For global APIs, we only need to check for ExpressionStatement
19
+ if (isGlobalApi && current.type === 'ExpressionStatement') {
20
+ return current;
21
+ }
22
+ // For non-global APIs, check for variable declarations and exports
23
+ if (!isGlobalApi) {
24
+ if (current.type === 'VariableDeclarator' && current.parent && current.parent.type === 'VariableDeclaration') {
25
+ // If this is part of an export, get the export declaration
26
+ if (current.parent.parent && current.parent.parent.type === 'ExportNamedDeclaration') {
27
+ return current.parent.parent;
28
+ }
29
+ return current.parent;
30
+ }
31
+ }
32
+ }
33
+ return null;
34
+ };
35
+ /**
36
+ * Reports an issue for an empty style declaration, and provides a fix to remove the declaration.
37
+ * @param ruleContext The ESLint rule context.
38
+ * @param node The node to report the issue on.
39
+ * @param callNode The CallExpression node of the style() function.
40
+ */
41
+ export const reportEmptyDeclaration = (ruleContext, node, callNode) => {
42
+ // Check if this is a global API function
43
+ const isGlobalApi = callNode.callee.type === 'Identifier' &&
44
+ ['globalStyle', 'globalFontFace', 'globalKeyframes'].includes(callNode.callee.name);
45
+ // Find the parent declaration node
46
+ const targetDeclarationNode = findTargetDeclarationNode(callNode, isGlobalApi);
47
+ if (targetDeclarationNode) {
48
+ ruleContext.report({
49
+ node: node,
50
+ messageId: 'emptyStyleDeclaration',
51
+ fix(fixer) {
52
+ const sourceCode = ruleContext.sourceCode;
53
+ const startLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[0]).line;
54
+ const lineStart = sourceCode.getIndexFromLoc({
55
+ line: startLine,
56
+ column: 0,
57
+ });
58
+ // Find next line after the declaration
59
+ const endLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[1]).line;
60
+ const nextLineStart = sourceCode.getIndexFromLoc({
61
+ line: endLine + 1,
62
+ column: 0,
63
+ });
64
+ return fixer.removeRange([lineStart, nextLineStart]);
65
+ },
66
+ });
67
+ }
68
+ else {
69
+ // Report the issue without providing a fix
70
+ ruleContext.report({
71
+ node: node,
72
+ messageId: 'emptyStyleDeclaration',
73
+ });
74
+ }
75
+ };
@@ -0,0 +1,2 @@
1
+ import noEmptyStyleBlocksRule from './rule-definition.js';
2
+ export default noEmptyStyleBlocksRule;
@@ -0,0 +1,2 @@
1
+ import noEmptyStyleBlocksRule from './rule-definition.js';
2
+ export default noEmptyStyleBlocksRule;
@@ -0,0 +1,10 @@
1
+ import type { Rule } from 'eslint';
2
+ import type { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Removes the given node and also removes a trailing comma if it exists.
5
+ * @param ruleContext The ESLint rule context.
6
+ * @param node The node to remove.
7
+ * @param fixer The ESLint fixer.
8
+ * @returns The fix object.
9
+ */
10
+ export declare function removeNodeWithComma(ruleContext: Rule.RuleContext, node: TSESTree.Node, fixer: Rule.RuleFixer): Rule.Fix;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Removes the given node and also removes a trailing comma if it exists.
3
+ * @param ruleContext The ESLint rule context.
4
+ * @param node The node to remove.
5
+ * @param fixer The ESLint fixer.
6
+ * @returns The fix object.
7
+ */
8
+ export function removeNodeWithComma(ruleContext, node, fixer) {
9
+ const sourceCode = ruleContext.sourceCode;
10
+ const tokenAfter = sourceCode.getTokenAfter(node);
11
+ if (tokenAfter && tokenAfter.value === ',' && node.range && tokenAfter.range) {
12
+ return fixer.removeRange([node.range[0], tokenAfter.range[1]]);
13
+ }
14
+ return fixer.remove(node);
15
+ }
@@ -0,0 +1,9 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Gets the property name regardless of whether it's an identifier or a literal.
4
+ */
5
+ export declare function getStyleKeyName(key: TSESTree.Expression | TSESTree.PrivateIdentifier): string | null;
6
+ /**
7
+ * Checks if all properties in a style object are empty objects.
8
+ */
9
+ export declare const areAllChildrenEmpty: (stylesObject: TSESTree.ObjectExpression) => boolean;
@@ -0,0 +1,28 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
3
+ /**
4
+ * Gets the property name regardless of whether it's an identifier or a literal.
5
+ */
6
+ export function getStyleKeyName(key) {
7
+ if (key.type === 'Identifier') {
8
+ return key.name;
9
+ }
10
+ if (key.type === 'Literal' && typeof key.value === 'string') {
11
+ return key.value;
12
+ }
13
+ return null;
14
+ }
15
+ /**
16
+ * Checks if all properties in a style object are empty objects.
17
+ */
18
+ export const areAllChildrenEmpty = (stylesObject) => {
19
+ if (stylesObject.properties.length === 0) {
20
+ return true;
21
+ }
22
+ return stylesObject.properties.every((property) => {
23
+ if (property.type !== 'Property' || property.value.type !== 'ObjectExpression') {
24
+ return false;
25
+ }
26
+ return isEmptyObject(property.value);
27
+ });
28
+ };
@@ -0,0 +1,11 @@
1
+ import type { Rule } from 'eslint';
2
+ import { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Processes a recipe object, removing empty `base` and `variants` properties, as well as empty
5
+ * variant categories and values.
6
+ *
7
+ * @param ruleContext The ESLint rule context.
8
+ * @param recipeNode The recipe object node to process.
9
+ * @param reportedNodes A set of nodes that have already been reported by other processors.
10
+ */
11
+ export declare function processRecipeProperties(ruleContext: Rule.RuleContext, recipeNode: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
@@ -0,0 +1,122 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
3
+ import { processEmptyNestedStyles } from './empty-nested-style-processor.js';
4
+ import { removeNodeWithComma } from './node-remover.js';
5
+ import { areAllChildrenEmpty, getStyleKeyName } from './property-utils.js';
6
+ /**
7
+ * Processes a recipe object, removing empty `base` and `variants` properties, as well as empty
8
+ * variant categories and values.
9
+ *
10
+ * @param ruleContext The ESLint rule context.
11
+ * @param recipeNode The recipe object node to process.
12
+ * @param reportedNodes A set of nodes that have already been reported by other processors.
13
+ */
14
+ export function processRecipeProperties(ruleContext, recipeNode, reportedNodes) {
15
+ recipeNode.properties.forEach((property) => {
16
+ if (property.type !== 'Property') {
17
+ return;
18
+ }
19
+ const propertyName = getStyleKeyName(property.key);
20
+ if (!propertyName) {
21
+ return;
22
+ }
23
+ // Handle empty base or variants properties
24
+ if ((propertyName === 'base' || propertyName === 'variants') &&
25
+ property.value.type === 'ObjectExpression' &&
26
+ isEmptyObject(property.value)) {
27
+ if (!reportedNodes.has(property)) {
28
+ reportedNodes.add(property);
29
+ ruleContext.report({
30
+ node: property,
31
+ messageId: 'emptyRecipeProperty',
32
+ data: {
33
+ propertyName,
34
+ },
35
+ fix(fixer) {
36
+ return removeNodeWithComma(ruleContext, property, fixer);
37
+ },
38
+ });
39
+ }
40
+ }
41
+ // Process base property nested objects
42
+ if (propertyName === 'base' && property.value.type === 'ObjectExpression') {
43
+ processEmptyNestedStyles(ruleContext, property.value, reportedNodes);
44
+ }
45
+ // Process variant values
46
+ if (propertyName === 'variants' && property.value.type === 'ObjectExpression') {
47
+ // If variants is empty, it will be handled by the check above
48
+ if (!isEmptyObject(property.value)) {
49
+ // Process variant categories
50
+ property.value.properties.forEach((variantCategoryProperty) => {
51
+ if (variantCategoryProperty.type !== 'Property' ||
52
+ variantCategoryProperty.value.type !== 'ObjectExpression') {
53
+ return;
54
+ }
55
+ // Check if all values in this category are empty
56
+ if (isEmptyObject(variantCategoryProperty.value) || areAllChildrenEmpty(variantCategoryProperty.value)) {
57
+ if (!reportedNodes.has(variantCategoryProperty)) {
58
+ reportedNodes.add(variantCategoryProperty);
59
+ ruleContext.report({
60
+ node: variantCategoryProperty,
61
+ messageId: 'emptyVariantCategory',
62
+ fix(fixer) {
63
+ return removeNodeWithComma(ruleContext, variantCategoryProperty, fixer);
64
+ },
65
+ });
66
+ }
67
+ return;
68
+ }
69
+ // Process individual variant values
70
+ variantCategoryProperty.value.properties.forEach((variantValueProperty) => {
71
+ if (variantValueProperty.type !== 'Property') {
72
+ return;
73
+ }
74
+ // Check for non-object variant values
75
+ if (variantValueProperty.value.type !== 'ObjectExpression') {
76
+ if (!reportedNodes.has(variantValueProperty)) {
77
+ reportedNodes.add(variantValueProperty);
78
+ // Get a user-friendly type description as a string
79
+ const friendlyType = (() => {
80
+ const nodeType = variantValueProperty.value.type;
81
+ if (nodeType === 'Literal') {
82
+ const literalValue = variantValueProperty.value;
83
+ return literalValue.value === null ? 'null' : typeof literalValue.value;
84
+ }
85
+ else if (nodeType === 'Identifier') {
86
+ return 'variable';
87
+ }
88
+ return nodeType;
89
+ })();
90
+ ruleContext.report({
91
+ node: variantValueProperty,
92
+ messageId: 'invalidPropertyType',
93
+ data: {
94
+ type: friendlyType,
95
+ },
96
+ });
97
+ }
98
+ return;
99
+ }
100
+ // Check for empty objects in variant properties
101
+ if (isEmptyObject(variantValueProperty.value)) {
102
+ if (!reportedNodes.has(variantValueProperty)) {
103
+ reportedNodes.add(variantValueProperty);
104
+ ruleContext.report({
105
+ node: variantValueProperty,
106
+ messageId: 'emptyVariantValue',
107
+ fix(fixer) {
108
+ return removeNodeWithComma(ruleContext, variantValueProperty, fixer);
109
+ },
110
+ });
111
+ }
112
+ }
113
+ else {
114
+ // Process nested styles within variant values
115
+ processEmptyNestedStyles(ruleContext, variantValueProperty.value, reportedNodes);
116
+ }
117
+ });
118
+ });
119
+ }
120
+ }
121
+ });
122
+ }
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ declare const noEmptyStyleBlocksRule: Rule.RuleModule;
3
+ export default noEmptyStyleBlocksRule;
@@ -0,0 +1,30 @@
1
+ import { createEmptyStyleVisitors } from './empty-style-visitor-creator.js';
2
+ const noEmptyStyleBlocksRule = {
3
+ meta: {
4
+ type: 'suggestion',
5
+ docs: {
6
+ description: 'disallow empty style blocks in vanilla-extract stylesheets',
7
+ category: 'Best Practices',
8
+ recommended: true,
9
+ },
10
+ fixable: 'code',
11
+ schema: [],
12
+ messages: {
13
+ emptyConditionalStyle: 'Empty conditional style object should be removed.',
14
+ emptyMedia: 'Empty @media object should be removed.',
15
+ emptyNestedStyle: 'Empty nested style object should be removed.',
16
+ emptyRecipeProperty: 'Empty {{propertyName}} object in recipe should be removed.',
17
+ emptySelectors: 'Empty selectors object should be removed.',
18
+ emptySpreadObject: 'Empty spread object should be removed.',
19
+ emptyStyleDeclaration: 'Declarations with only empty style blocks should be removed.',
20
+ emptySupports: 'Empty @supports object should be removed.',
21
+ emptyVariantCategory: 'Empty variant category should be removed.',
22
+ emptyVariantValue: 'Empty variant value should be removed.',
23
+ invalidPropertyType: 'Variant values must be objects, found {{type}} instead.',
24
+ },
25
+ },
26
+ create(ruleContext) {
27
+ return createEmptyStyleVisitors(ruleContext);
28
+ },
29
+ };
30
+ export default noEmptyStyleBlocksRule;
@@ -0,0 +1,10 @@
1
+ import type { Rule } from 'eslint';
2
+ import { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Processes styleVariants function calls to detect and remove empty style variants.
5
+ *
6
+ * @param ruleContext The ESLint rule context.
7
+ * @param node The styleVariants call argument (object expression).
8
+ * @param reportedNodes A set of nodes that have already been reported.
9
+ */
10
+ export declare function processStyleVariants(ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
@@ -0,0 +1,45 @@
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
+ /**
5
+ * Processes styleVariants function calls to detect and remove empty style variants.
6
+ *
7
+ * @param ruleContext The ESLint rule context.
8
+ * @param node The styleVariants call argument (object expression).
9
+ * @param reportedNodes A set of nodes that have already been reported.
10
+ */
11
+ export function processStyleVariants(ruleContext, node, reportedNodes) {
12
+ node.properties.forEach((property) => {
13
+ if (property.type !== 'Property') {
14
+ return;
15
+ }
16
+ // Check for empty arrays
17
+ if (property.value.type === 'ArrayExpression' && property.value.elements.length === 0) {
18
+ if (!reportedNodes.has(property)) {
19
+ reportedNodes.add(property);
20
+ ruleContext.report({
21
+ node: property,
22
+ messageId: 'emptyVariantValue',
23
+ fix(fixer) {
24
+ return removeNodeWithComma(ruleContext, property, fixer);
25
+ },
26
+ });
27
+ }
28
+ return;
29
+ }
30
+ // Check for empty objects
31
+ if (property.value.type === 'ObjectExpression' && isEmptyObject(property.value)) {
32
+ if (!reportedNodes.has(property)) {
33
+ reportedNodes.add(property);
34
+ ruleContext.report({
35
+ node: property,
36
+ messageId: 'emptyVariantValue',
37
+ fix(fixer) {
38
+ return removeNodeWithComma(ruleContext, property, fixer);
39
+ },
40
+ });
41
+ }
42
+ return;
43
+ }
44
+ });
45
+ }
@@ -1,4 +1,4 @@
1
- import { getPropertyName } from './property-separator.js';
1
+ import { getPropertyNameForSorting } from './property-separator.js';
2
2
  /**
3
3
  * Compares two CSS properties alphabetically.
4
4
  * @param firstProperty The first property to compare.
@@ -6,8 +6,8 @@ import { getPropertyName } from './property-separator.js';
6
6
  * @returns A number indicating the relative order of the properties (-1, 0, or 1).
7
7
  */
8
8
  export const comparePropertiesAlphabetically = (firstProperty, secondProperty) => {
9
- const firstName = getPropertyName(firstProperty);
10
- const secondName = getPropertyName(secondProperty);
9
+ const firstName = getPropertyNameForSorting(firstProperty);
10
+ const secondName = getPropertyNameForSorting(secondProperty);
11
11
  // Special handling for 'src' property - it should always come first (relates to font face APIs only)
12
12
  if (firstName === 'src') {
13
13
  return -1;
@@ -0,0 +1,11 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Type guard to check if a node is an ObjectExpression.
4
+ */
5
+ export declare const isObjectExpression: (node: TSESTree.Node) => node is TSESTree.ObjectExpression;
6
+ /**
7
+ * Checks if an object expression is empty (has no properties).
8
+ * @param node The node to check.
9
+ * @returns True if the node is an ObjectExpression with no properties.
10
+ */
11
+ export declare const isEmptyObject: (node: TSESTree.Node) => boolean;
@@ -0,0 +1,15 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Type guard to check if a node is an ObjectExpression.
4
+ */
5
+ export const isObjectExpression = (node) => {
6
+ return node.type === 'ObjectExpression';
7
+ };
8
+ /**
9
+ * Checks if an object expression is empty (has no properties).
10
+ * @param node The node to check.
11
+ * @returns True if the node is an ObjectExpression with no properties.
12
+ */
13
+ export const isEmptyObject = (node) => {
14
+ return isObjectExpression(node) && node.properties.length === 0;
15
+ };
@@ -1,6 +1,6 @@
1
1
  import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
2
  import { generateFixesForCSSOrder } from '../shared-utils/css-order-fixer.js';
3
- import { getPropertyName, separateProperties } from '../shared-utils/property-separator.js';
3
+ import { getPropertyNameForSorting, separateProperties } from '../shared-utils/property-separator.js';
4
4
  import { comparePropertiesAlphabetically } from './alphabetical-property-comparator.js';
5
5
  /**
6
6
  * Processes a font face declaration to enforce property ordering:
@@ -24,8 +24,8 @@ export const enforceFontFaceOrder = (ruleContext, fontFaceObject) => {
24
24
  }));
25
25
  const violatingPair = propertyPairs.find(({ currentProperty, nextProperty }) => comparePropertiesAlphabetically(currentProperty, nextProperty) > 0);
26
26
  if (violatingPair) {
27
- const nextPropertyName = getPropertyName(violatingPair.nextProperty);
28
- const currentPropertyName = getPropertyName(violatingPair.currentProperty);
27
+ const nextPropertyName = getPropertyNameForSorting(violatingPair.nextProperty);
28
+ const currentPropertyName = getPropertyNameForSorting(violatingPair.currentProperty);
29
29
  ruleContext.report({
30
30
  node: violatingPair.nextProperty,
31
31
  messageId: 'fontFaceOrder',
@@ -1,4 +1,6 @@
1
1
  import type { Rule } from 'eslint';
2
+ import type { SortRemainingProperties } from '../concentric-order/types.js';
3
+ import type { OrderingStrategy } from '../types.js';
2
4
  /**
3
5
  * Creates an ESLint rule listener with visitors for style-related function calls.
4
6
  * @param ruleContext The ESLint rule context.
@@ -15,4 +17,4 @@ import type { Rule } from 'eslint';
15
17
  *
16
18
  * Each visitor applies the appropriate ordering strategy to the style objects in these function calls.
17
19
  */
18
- export declare const createNodeVisitors: (ruleContext: Rule.RuleContext, orderingStrategy: "alphabetical" | "concentric" | "userDefinedGroupOrder", userDefinedGroupOrder?: string[], sortRemainingProperties?: "alphabetical" | "concentric") => Rule.RuleListener;
20
+ export declare const createNodeVisitors: (ruleContext: Rule.RuleContext, orderingStrategy: OrderingStrategy, userDefinedGroupOrder?: string[], sortRemainingProperties?: SortRemainingProperties) => Rule.RuleListener;
@@ -9,7 +9,7 @@ import { TSESTree } from '@typescript-eslint/utils';
9
9
  * - Literal (string): Returns the string value.
10
10
  * For any other type of key, it returns an empty string.
11
11
  */
12
- export declare const getPropertyName: (property: TSESTree.Property) => string;
12
+ export declare const getPropertyNameForSorting: (property: TSESTree.Property) => string;
13
13
  /**
14
14
  * Separates object properties into regular and special categories.
15
15
  * @param properties An array of object literal elements to be categorized.
@@ -9,7 +9,7 @@ import { TSESTree } from '@typescript-eslint/utils';
9
9
  * - Literal (string): Returns the string value.
10
10
  * For any other type of key, it returns an empty string.
11
11
  */
12
- export const getPropertyName = (property) => {
12
+ export const getPropertyNameForSorting = (property) => {
13
13
  if (property.key.type === 'Identifier') {
14
14
  return property.key.name;
15
15
  }
@@ -36,7 +36,7 @@ export const separateProperties = (properties) => {
36
36
  // Separate regular CSS properties from special ones (pseudo selectors, etc.)
37
37
  properties.forEach((property) => {
38
38
  if (property.type === 'Property') {
39
- const propName = getPropertyName(property);
39
+ const propName = getPropertyNameForSorting(property);
40
40
  if (propName.startsWith(':') || propName.startsWith('@') || propName === 'selectors') {
41
41
  specialProperties.push(property);
42
42
  }
@@ -0,0 +1 @@
1
+ export type OrderingStrategy = 'alphabetical' | 'concentric' | 'userDefinedGroupOrder';
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export declare const vanillaExtract: {
7
7
  'alphabetical-order': import("eslint").Rule.RuleModule;
8
8
  'concentric-order': import("eslint").Rule.RuleModule;
9
9
  'custom-order': import("eslint").Rule.RuleModule;
10
+ 'no-empty-style-blocks': import("eslint").Rule.RuleModule;
10
11
  };
11
12
  configs: {
12
13
  recommended: {
@@ -14,11 +15,13 @@ export declare const vanillaExtract: {
14
15
  'vanilla-extract': {
15
16
  rules: {
16
17
  'concentric-order': import("eslint").Rule.RuleModule;
18
+ 'no-empty-style-blocks': import("eslint").Rule.RuleModule;
17
19
  };
18
20
  };
19
21
  };
20
22
  rules: {
21
23
  'vanilla-extract/concentric-order': string;
24
+ 'vanilla-extract/no-empty-style-blocks': string;
22
25
  };
23
26
  }[];
24
27
  alphabetical: {
package/dist/index.js CHANGED
@@ -1,25 +1,33 @@
1
1
  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
+ import noEmptyStyleBlocksRule from './css-rules/no-empty-blocks/rule-definition.js';
4
5
  export const vanillaExtract = {
5
6
  meta: {
6
7
  name: '@antebudimir/eslint-plugin-vanilla-extract',
7
- version: '1.5.0',
8
+ version: '1.6.0',
8
9
  },
9
10
  rules: {
10
11
  'alphabetical-order': alphabeticalOrderRule,
11
12
  'concentric-order': concentricOrderRule,
12
13
  'custom-order': customOrderRule,
14
+ 'no-empty-style-blocks': noEmptyStyleBlocksRule,
13
15
  },
14
16
  configs: {
15
17
  recommended: [
16
18
  {
17
19
  plugins: {
18
20
  'vanilla-extract': {
19
- rules: { 'concentric-order': concentricOrderRule },
21
+ rules: {
22
+ 'concentric-order': concentricOrderRule,
23
+ 'no-empty-style-blocks': noEmptyStyleBlocksRule,
24
+ },
20
25
  },
21
26
  },
22
- rules: { 'vanilla-extract/concentric-order': 'warn' },
27
+ rules: {
28
+ 'vanilla-extract/concentric-order': 'warn',
29
+ 'vanilla-extract/no-empty-style-blocks': 'warn',
30
+ },
23
31
  },
24
32
  ],
25
33
  alphabetical: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.5.0",
3
+ "version": "1.6.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",
@@ -49,24 +49,24 @@
49
49
  },
50
50
  "devDependencies": {
51
51
  "@eslint/eslintrc": "^3.3.0",
52
- "@types/node": "^20.17.19",
53
- "@typescript-eslint/eslint-plugin": "^8.25.0",
54
- "@typescript-eslint/parser": "^8.25.0",
55
- "@typescript-eslint/rule-tester": "^8.26.0",
56
- "@typescript-eslint/utils": "^8.25.0",
52
+ "@types/node": "^20.17.24",
53
+ "@typescript-eslint/eslint-plugin": "^8.26.1",
54
+ "@typescript-eslint/parser": "^8.26.1",
55
+ "@typescript-eslint/rule-tester": "^8.26.1",
56
+ "@typescript-eslint/utils": "^8.26.1",
57
57
  "@vanilla-extract/css": "^1.17.1",
58
58
  "@vanilla-extract/recipes": "^0.5.5",
59
59
  "@vitest/coverage-v8": "3.0.8",
60
- "eslint": "^9.21.0",
61
- "eslint-config-prettier": "^10.0.2",
62
- "eslint-import-resolver-typescript": "^3.8.3",
60
+ "eslint": "^9.22.0",
61
+ "eslint-config-prettier": "^10.1.1",
62
+ "eslint-import-resolver-typescript": "^3.8.5",
63
63
  "eslint-plugin-eslint-plugin": "^6.4.0",
64
64
  "eslint-plugin-import": "^2.31.0",
65
65
  "eslint-vitest-rule-tester": "^1.1.0",
66
- "prettier": "^3.5.2",
67
- "typescript": "^5.7.3",
68
- "typescript-eslint": "^8.25.0",
69
- "vitest": "^3.0.8"
66
+ "prettier": "^3.5.3",
67
+ "typescript": "^5.8.2",
68
+ "typescript-eslint": "^8.26.1",
69
+ "vitest": "3.0.8"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "tsc",