@antebudimir/eslint-plugin-vanilla-extract 1.7.0 → 1.9.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 (36) hide show
  1. package/README.md +76 -3
  2. package/dist/css-rules/no-empty-blocks/conditional-processor.d.ts +1 -1
  3. package/dist/css-rules/no-empty-blocks/conditional-processor.js +2 -2
  4. package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.d.ts +1 -1
  5. package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.js +2 -2
  6. package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.d.ts +4 -4
  7. package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +79 -79
  8. package/dist/css-rules/no-empty-blocks/node-remover.d.ts +1 -1
  9. package/dist/css-rules/no-empty-blocks/node-remover.js +2 -2
  10. package/dist/css-rules/no-empty-blocks/property-utils.d.ts +1 -1
  11. package/dist/css-rules/no-empty-blocks/property-utils.js +2 -2
  12. package/dist/css-rules/no-empty-blocks/recipe-processor.d.ts +1 -1
  13. package/dist/css-rules/no-empty-blocks/recipe-processor.js +2 -2
  14. package/dist/css-rules/no-empty-blocks/style-variants-processor.d.ts +1 -1
  15. package/dist/css-rules/no-empty-blocks/style-variants-processor.js +2 -2
  16. package/dist/css-rules/no-unknown-unit/index.d.ts +2 -0
  17. package/dist/css-rules/no-unknown-unit/index.js +2 -0
  18. package/dist/css-rules/no-unknown-unit/rule-definition.d.ts +3 -0
  19. package/dist/css-rules/no-unknown-unit/rule-definition.js +19 -0
  20. package/dist/css-rules/no-unknown-unit/unknown-unit-processor.d.ts +11 -0
  21. package/dist/css-rules/no-unknown-unit/unknown-unit-processor.js +175 -0
  22. package/dist/css-rules/no-unknown-unit/unknown-unit-visitor-creator.d.ts +6 -0
  23. package/dist/css-rules/no-unknown-unit/unknown-unit-visitor-creator.js +38 -0
  24. package/dist/css-rules/no-zero-unit/_tests_/no-zero-unit.test.d.ts +1 -0
  25. package/dist/css-rules/no-zero-unit/_tests_/no-zero-unit.test.js +333 -0
  26. package/dist/css-rules/no-zero-unit/index.d.ts +2 -0
  27. package/dist/css-rules/no-zero-unit/index.js +2 -0
  28. package/dist/css-rules/no-zero-unit/rule-definition.d.ts +3 -0
  29. package/dist/css-rules/no-zero-unit/rule-definition.js +20 -0
  30. package/dist/css-rules/no-zero-unit/zero-unit-processor.d.ts +9 -0
  31. package/dist/css-rules/no-zero-unit/zero-unit-processor.js +29 -0
  32. package/dist/css-rules/no-zero-unit/zero-unit-visitor-creator.d.ts +14 -0
  33. package/dist/css-rules/no-zero-unit/zero-unit-visitor-creator.js +46 -0
  34. package/dist/index.d.ts +4 -0
  35. package/dist/index.js +7 -1
  36. package/package.json +1 -1
package/README.md CHANGED
@@ -66,6 +66,7 @@ export default [
66
66
  // Optionally override specific rules
67
67
  // 'vanilla-extract/concentric-order': 'warn', // Change severity from error to warn
68
68
  // 'vanilla-extract/no-empty-style-blocks': 'off', // Disable a recommended rule
69
+ // 'vanilla-extract/no-zero-unit': 'warn', // Change severity from error to warn
69
70
 
70
71
  // Add additional rules not in recommended config
71
72
  // 'vanilla-extract/alphabetical-order': 'error', // Override concentric-order rule
@@ -80,6 +81,8 @@ The recommended configuration enables the following rules with error severity:
80
81
 
81
82
  - `vanilla-extract/concentric-order`: Enforces concentric CSS property ordering
82
83
  - `vanilla-extract/no-empty-style-blocks`: Prevents empty style blocks
84
+ - `vanilla-extract/no-unknown-unit`: prohibits usage of unrecognized CSS units.
85
+ - `vanilla-extract/no-zero-unit`: removes unnecessary units for zero values
83
86
 
84
87
  You can use the recommended configuration as a starting point and override rules as needed for your project.
85
88
 
@@ -108,6 +111,8 @@ export default [
108
111
  sortRemainingProperties: 'concentric', // 'alphabetical' is default
109
112
  },
110
113
  ],
114
+ 'vanilla-extract/no-unknown-unit': 'error',
115
+ 'vanilla-extract/no-zero-unit': 'warn',
111
116
  },
112
117
  },
113
118
  ];
@@ -266,6 +271,72 @@ export const recipeWithEmptyVariants = recipe({
266
271
  });
267
272
  ```
268
273
 
274
+ ## vanilla-extract/no-unknown-unit
275
+
276
+ This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units that could cause styling issues or browser compatibility problems.
277
+
278
+ ```typescript
279
+ // ❌ Incorrect
280
+ import { style, globalStyle, recipe } from '@vanilla-extract/css';
281
+
282
+ export const invalidStyle = style({
283
+ margin: '5abc', // Non-existent unit
284
+ fontSize: '1.5rems', // Typo in unit
285
+ });
286
+
287
+ export const myRecipe = recipe({
288
+ variants: {
289
+ size: {
290
+ large: { padding: '4xm' } // Invalid unit
291
+ }
292
+ }
293
+ });
294
+
295
+ // ✅ Correct
296
+ import { style, globalStyle, recipe } from '@vanilla-extract/css';
297
+
298
+ export const validStyle = style({
299
+ margin: '5rem',
300
+ fontSize: '1.5rem',
301
+ });
302
+
303
+ export const myRecipe = recipe({
304
+ variants: {
305
+ size: {
306
+ large: { padding: '4em' }
307
+ }
308
+ }
309
+ });
310
+ ```
311
+
312
+ ## vanilla-extract/no-zero-unit
313
+
314
+ This rule enforces the removal of unnecessary units for zero values in vanilla-extract style objects. It helps maintain cleaner and more consistent CSS by eliminating redundant units when the value is zero.
315
+
316
+ ```typescript
317
+ // ❌ Incorrect
318
+ import { style } from '@vanilla-extract/css';
319
+
320
+ export const myStyle = style({
321
+ margin: '0px',
322
+ padding: '0rem',
323
+ width: '0%',
324
+ height: '0vh',
325
+ top: '-0em',
326
+ });
327
+
328
+ // ✅ Correct
329
+ import { style } from '@vanilla-extract/css';
330
+
331
+ export const myStyle = style({
332
+ margin: '0',
333
+ padding: '0',
334
+ width: '0',
335
+ height: '0',
336
+ top: '0',
337
+ });
338
+ ```
339
+
269
340
  ## Font Face Declarations
270
341
 
271
342
  For `fontFace` and `globalFontFace` API calls, all three ordering rules (alphabetical, concentric, and custom) enforce the same special ordering:
@@ -341,20 +412,22 @@ The roadmap outlines the project's current status and future plans:
341
412
  - Support for multiple vanilla-extract APIs (e.g., `style`, `styleVariants`, `recipe`, `globalStyle`, `fontFace`, etc.).
342
413
  - `no-empty-style-blocks` rule to disallow empty blocks.
343
414
  - Recommended ESLint configuration for the plugin.
415
+ - `no-zero-unit` rule to disallow units when the value is zero.
416
+ - `no-unknown-unit` rule to disallow unknown units.
344
417
  - Comprehensive rule testing.
345
418
 
346
419
  ### Current Work
347
420
 
348
- - `no-zero-unit` rule to disallow units when the value is zero.
421
+ - Support for using the plugin’s recommended config via the extends field (as discussed in [issue #3](https://github.com/antebudimir/eslint-plugin-vanilla-extract/issues/3))
349
422
 
350
423
  ### Upcoming Features
351
424
 
352
- - `no-unknown-units` rule to disallow unknown units.
353
- - `no-number-trailing-zeros` rule to disallow trailing zeros in numbers.
425
+ - `no-number-trailing-zero` rule to disallow trailing zeros in numbers.
354
426
  - `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
355
427
  - `prefer-logical-properties` rule to enforce use of logical properties.
356
428
  - `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
357
429
  - `no-global-style` rule to disallow use of `globalStyle` function.
430
+ - `property-unit-match` rule to enforce valid units per CSS property specs. **Note**: This feature will only be implemented if there's sufficient interest from the community.
358
431
  - Option to sort properties within user-defined concentric groups alphabetically instead of following the concentric order. **Note**: This feature will only be implemented if there's sufficient interest from the community.
359
432
 
360
433
  ## Contributing
@@ -3,4 +3,4 @@ import { TSESTree } from '@typescript-eslint/utils';
3
3
  /**
4
4
  * Handles conditional expressions with empty objects.
5
5
  */
6
- export declare function processConditionalExpression(context: Rule.RuleContext, node: TSESTree.ConditionalExpression, reportedNodes: Set<TSESTree.Node>, callNode: TSESTree.CallExpression): void;
6
+ export declare const processConditionalExpression: (context: Rule.RuleContext, node: TSESTree.ConditionalExpression, reportedNodes: Set<TSESTree.Node>, callNode: TSESTree.CallExpression) => void;
@@ -4,7 +4,7 @@ import { reportEmptyDeclaration } from './fix-utils.js';
4
4
  /**
5
5
  * Handles conditional expressions with empty objects.
6
6
  */
7
- export function processConditionalExpression(context, node, reportedNodes, callNode) {
7
+ export const processConditionalExpression = (context, node, reportedNodes, callNode) => {
8
8
  const isConsequentEmpty = node.consequent.type === 'ObjectExpression' && isEmptyObject(node.consequent);
9
9
  const isAlternateEmpty = node.alternate.type === 'ObjectExpression' && isEmptyObject(node.alternate);
10
10
  // If both branches are empty, report the entire declaration for removal
@@ -23,4 +23,4 @@ export function processConditionalExpression(context, node, reportedNodes, callN
23
23
  messageId: 'emptyConditionalStyle',
24
24
  });
25
25
  }
26
- }
26
+ };
@@ -3,4 +3,4 @@ import { TSESTree } from '@typescript-eslint/utils';
3
3
  /**
4
4
  * Processes nested style objects like selectors and media queries.
5
5
  */
6
- export declare function processEmptyNestedStyles(ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
6
+ export declare const processEmptyNestedStyles: (ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>) => void;
@@ -5,7 +5,7 @@ import { areAllChildrenEmpty, getStyleKeyName } from './property-utils.js';
5
5
  /**
6
6
  * Processes nested style objects like selectors and media queries.
7
7
  */
8
- export function processEmptyNestedStyles(ruleContext, node, reportedNodes) {
8
+ export const processEmptyNestedStyles = (ruleContext, node, reportedNodes) => {
9
9
  node.properties.forEach((property) => {
10
10
  if (property.type !== 'Property') {
11
11
  return;
@@ -62,4 +62,4 @@ export function processEmptyNestedStyles(ruleContext, node, reportedNodes) {
62
62
  });
63
63
  }
64
64
  });
65
- }
65
+ };
@@ -1,12 +1,12 @@
1
1
  import type { Rule } from 'eslint';
2
2
  import { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Checks if a style object is effectively empty (contains only empty objects).
5
+ */
6
+ export declare const isEffectivelyEmptyStylesObject: (stylesObject: TSESTree.ObjectExpression) => boolean;
3
7
  /**
4
8
  * Creates ESLint rule visitors for detecting empty style blocks in vanilla-extract.
5
9
  * @param ruleContext The ESLint rule rule context.
6
10
  * @returns An object with visitor functions for the ESLint rule.
7
11
  */
8
12
  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;
@@ -7,6 +7,85 @@ import { removeNodeWithComma } from './node-remover.js';
7
7
  import { getStyleKeyName } from './property-utils.js';
8
8
  import { processRecipeProperties } from './recipe-processor.js';
9
9
  import { processStyleVariants } from './style-variants-processor.js';
10
+ /**
11
+ * Checks if a style object is effectively empty (contains only empty objects).
12
+ */
13
+ export const isEffectivelyEmptyStylesObject = (stylesObject) => {
14
+ // Empty object itself
15
+ if (stylesObject.properties.length === 0) {
16
+ return true;
17
+ }
18
+ // For recipe objects, we need special handling
19
+ let hasBaseProperty = false;
20
+ let isBaseEmpty = true;
21
+ let hasVariantsProperty = false;
22
+ let areAllVariantsEmpty = true;
23
+ // First pass: identify recipe properties
24
+ for (const property of stylesObject.properties) {
25
+ if (property.type !== 'Property') {
26
+ continue;
27
+ }
28
+ const propertyName = getStyleKeyName(property.key);
29
+ if (!propertyName) {
30
+ continue;
31
+ }
32
+ if (propertyName === 'base') {
33
+ hasBaseProperty = true;
34
+ if (property.value.type === 'ObjectExpression' && !isEmptyObject(property.value)) {
35
+ isBaseEmpty = false;
36
+ }
37
+ }
38
+ else if (propertyName === 'variants') {
39
+ hasVariantsProperty = true;
40
+ if (property.value.type === 'ObjectExpression' && !isEmptyObject(property.value)) {
41
+ areAllVariantsEmpty = false;
42
+ }
43
+ }
44
+ }
45
+ // If this looks like a recipe object (has base or variants)
46
+ if (hasBaseProperty || hasVariantsProperty) {
47
+ // A recipe is effectively empty if both base and variants are empty
48
+ return isBaseEmpty && areAllVariantsEmpty;
49
+ }
50
+ // / For non-recipe objects, check if all special properties (selectors, media queries, variants) are effectively empty
51
+ function isSpecialProperty(propertyName) {
52
+ return (propertyName === 'selectors' || (propertyName && propertyName.startsWith('@')) || propertyName === 'variants');
53
+ }
54
+ const specialProperties = stylesObject.properties.filter((prop) => prop.type === 'Property' && isSpecialProperty(getStyleKeyName(prop.key)));
55
+ const allSpecialPropertiesEmpty = specialProperties.every((property) => {
56
+ if (property.value.type === 'ObjectExpression' && isEmptyObject(property.value)) {
57
+ return true;
58
+ }
59
+ const propertyName = getStyleKeyName(property.key);
60
+ // This defensive check handles malformed AST nodes that lack valid property names.
61
+ // This is difficult to test because it's challenging to construct a valid AST
62
+ // where getStyleKeyName would return a falsy value.
63
+ if (!propertyName) {
64
+ return false;
65
+ }
66
+ // For selectors, media queries and supports, check if all nested objects are empty
67
+ if ((propertyName === 'selectors' || (propertyName && propertyName.startsWith('@'))) &&
68
+ property.value.type === 'ObjectExpression') {
69
+ // This handles the edge case of an empty properties array.
70
+ // This code path is difficult to test in isolation because it requires
71
+ // constructing a specific AST structure that bypasses earlier conditions.
72
+ if (property.value.properties.length === 0) {
73
+ return true;
74
+ }
75
+ return property.value.properties.every((nestedProperty) => {
76
+ return (nestedProperty.type === 'Property' &&
77
+ nestedProperty.value.type === 'ObjectExpression' &&
78
+ isEmptyObject(nestedProperty.value));
79
+ });
80
+ }
81
+ // Default fallback for cases not handled by the conditions above.
82
+ // This is difficult to test because it requires creating an AST structure
83
+ // that doesn't trigger any of the preceding return statements.
84
+ return false;
85
+ });
86
+ // If we have special properties and they're all empty, the style is effectively empty
87
+ return specialProperties.length > 0 && allSpecialPropertiesEmpty;
88
+ };
10
89
  /**
11
90
  * Creates ESLint rule visitors for detecting empty style blocks in vanilla-extract.
12
91
  * @param ruleContext The ESLint rule rule context.
@@ -102,82 +181,3 @@ export const createEmptyStyleVisitors = (ruleContext) => {
102
181
  },
103
182
  };
104
183
  };
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
- }
@@ -7,4 +7,4 @@ import type { TSESTree } from '@typescript-eslint/utils';
7
7
  * @param fixer The ESLint fixer.
8
8
  * @returns The fix object.
9
9
  */
10
- export declare function removeNodeWithComma(ruleContext: Rule.RuleContext, node: TSESTree.Node, fixer: Rule.RuleFixer): Rule.Fix;
10
+ export declare const removeNodeWithComma: (ruleContext: Rule.RuleContext, node: TSESTree.Node, fixer: Rule.RuleFixer) => Rule.Fix;
@@ -5,11 +5,11 @@
5
5
  * @param fixer The ESLint fixer.
6
6
  * @returns The fix object.
7
7
  */
8
- export function removeNodeWithComma(ruleContext, node, fixer) {
8
+ export const removeNodeWithComma = (ruleContext, node, fixer) => {
9
9
  const sourceCode = ruleContext.sourceCode;
10
10
  const tokenAfter = sourceCode.getTokenAfter(node);
11
11
  if (tokenAfter && tokenAfter.value === ',' && node.range && tokenAfter.range) {
12
12
  return fixer.removeRange([node.range[0], tokenAfter.range[1]]);
13
13
  }
14
14
  return fixer.remove(node);
15
- }
15
+ };
@@ -2,7 +2,7 @@ import { TSESTree } from '@typescript-eslint/utils';
2
2
  /**
3
3
  * Gets the property name regardless of whether it's an identifier or a literal.
4
4
  */
5
- export declare function getStyleKeyName(key: TSESTree.Expression | TSESTree.PrivateIdentifier): string | null;
5
+ export declare const getStyleKeyName: (key: TSESTree.Expression | TSESTree.PrivateIdentifier) => string | null;
6
6
  /**
7
7
  * Checks if all properties in a style object are empty objects.
8
8
  */
@@ -3,7 +3,7 @@ import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
3
3
  /**
4
4
  * Gets the property name regardless of whether it's an identifier or a literal.
5
5
  */
6
- export function getStyleKeyName(key) {
6
+ export const getStyleKeyName = (key) => {
7
7
  if (key.type === 'Identifier') {
8
8
  return key.name;
9
9
  }
@@ -11,7 +11,7 @@ export function getStyleKeyName(key) {
11
11
  return key.value;
12
12
  }
13
13
  return null;
14
- }
14
+ };
15
15
  /**
16
16
  * Checks if all properties in a style object are empty objects.
17
17
  */
@@ -8,4 +8,4 @@ import { TSESTree } from '@typescript-eslint/utils';
8
8
  * @param recipeNode The recipe object node to process.
9
9
  * @param reportedNodes A set of nodes that have already been reported by other processors.
10
10
  */
11
- export declare function processRecipeProperties(ruleContext: Rule.RuleContext, recipeNode: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
11
+ export declare const processRecipeProperties: (ruleContext: Rule.RuleContext, recipeNode: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>) => void;
@@ -11,7 +11,7 @@ import { areAllChildrenEmpty, getStyleKeyName } from './property-utils.js';
11
11
  * @param recipeNode The recipe object node to process.
12
12
  * @param reportedNodes A set of nodes that have already been reported by other processors.
13
13
  */
14
- export function processRecipeProperties(ruleContext, recipeNode, reportedNodes) {
14
+ export const processRecipeProperties = (ruleContext, recipeNode, reportedNodes) => {
15
15
  recipeNode.properties.forEach((property) => {
16
16
  if (property.type !== 'Property') {
17
17
  return;
@@ -119,4 +119,4 @@ export function processRecipeProperties(ruleContext, recipeNode, reportedNodes)
119
119
  }
120
120
  }
121
121
  });
122
- }
122
+ };
@@ -7,4 +7,4 @@ import { TSESTree } from '@typescript-eslint/utils';
7
7
  * @param node The styleVariants call argument (object expression).
8
8
  * @param reportedNodes A set of nodes that have already been reported.
9
9
  */
10
- export declare function processStyleVariants(ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
10
+ export declare const processStyleVariants: (ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>) => void;
@@ -8,7 +8,7 @@ import { removeNodeWithComma } from './node-remover.js';
8
8
  * @param node The styleVariants call argument (object expression).
9
9
  * @param reportedNodes A set of nodes that have already been reported.
10
10
  */
11
- export function processStyleVariants(ruleContext, node, reportedNodes) {
11
+ export const processStyleVariants = (ruleContext, node, reportedNodes) => {
12
12
  node.properties.forEach((property) => {
13
13
  if (property.type !== 'Property') {
14
14
  return;
@@ -42,4 +42,4 @@ export function processStyleVariants(ruleContext, node, reportedNodes) {
42
42
  return;
43
43
  }
44
44
  });
45
- }
45
+ };
@@ -0,0 +1,2 @@
1
+ import noUnknownUnitRule from './rule-definition.js';
2
+ export default noUnknownUnitRule;
@@ -0,0 +1,2 @@
1
+ import noUnknownUnitRule from './rule-definition.js';
2
+ export default noUnknownUnitRule;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ declare const noUnknownUnitRule: Rule.RuleModule;
3
+ export default noUnknownUnitRule;
@@ -0,0 +1,19 @@
1
+ import { createUnknownUnitVisitors } from './unknown-unit-visitor-creator.js';
2
+ const noUnknownUnitRule = {
3
+ meta: {
4
+ type: 'problem',
5
+ docs: {
6
+ description: 'disallow invalid or unknown CSS units in vanilla-extract style objects',
7
+ category: 'Possible Errors',
8
+ recommended: true,
9
+ },
10
+ schema: [],
11
+ messages: {
12
+ unknownUnit: 'The unit "{{ unit }}" in value "{{ value }}" is not recognized as a valid CSS unit.',
13
+ },
14
+ },
15
+ create(context) {
16
+ return createUnknownUnitVisitors(context);
17
+ },
18
+ };
19
+ export default noUnknownUnitRule;
@@ -0,0 +1,11 @@
1
+ import type { Rule } from 'eslint';
2
+ import { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Recursively processes a style object, reporting instances of
5
+ * unknown CSS units.
6
+ *
7
+ * @param context The ESLint rule context.
8
+ * @param node The ObjectExpression node representing the style object to be
9
+ * processed.
10
+ */
11
+ export declare const processUnknownUnitInStyleObject: (context: Rule.RuleContext, node: TSESTree.ObjectExpression) => void;
@@ -0,0 +1,175 @@
1
+ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * List of valid CSS units according to CSS specifications.
4
+ */
5
+ const VALID_CSS_UNITS = [
6
+ // Absolute length units
7
+ 'px',
8
+ 'cm',
9
+ 'mm',
10
+ 'Q',
11
+ 'in',
12
+ 'pc',
13
+ 'pt',
14
+ // Relative length units
15
+ 'em',
16
+ 'ex',
17
+ 'ch',
18
+ 'rem',
19
+ 'lh',
20
+ 'rlh',
21
+ 'vw',
22
+ 'vh',
23
+ 'vmin',
24
+ 'vmax',
25
+ 'vb',
26
+ 'vi',
27
+ 'svw',
28
+ 'svh',
29
+ 'lvw',
30
+ 'lvh',
31
+ 'dvw',
32
+ 'dvh',
33
+ // Percentage
34
+ '%',
35
+ // Angle units
36
+ 'deg',
37
+ 'grad',
38
+ 'rad',
39
+ 'turn',
40
+ // Time units
41
+ 'ms',
42
+ 's',
43
+ // Frequency units
44
+ 'Hz',
45
+ 'kHz',
46
+ // Resolution units
47
+ 'dpi',
48
+ 'dpcm',
49
+ 'dppx',
50
+ 'x',
51
+ // Flexible length units
52
+ 'fr',
53
+ // Other valid units
54
+ 'cap',
55
+ 'ic',
56
+ 'rex',
57
+ 'cqw',
58
+ 'cqh',
59
+ 'cqi',
60
+ 'cqb',
61
+ 'cqmin',
62
+ 'cqmax',
63
+ ];
64
+ /**
65
+ * Regular expression to extract units from CSS values.
66
+ * Matches numeric values followed by a unit.
67
+ */
68
+ const CSS_VALUE_WITH_UNIT_REGEX = /^(-?\d*\.?\d+)([a-zA-Z%]+)$/i;
69
+ /**
70
+ * Splits a CSS value string into individual parts, handling spaces not inside functions.
71
+ */
72
+ const splitCssValues = (value) => {
73
+ return value
74
+ .split(/(?<!\([^)]*)\s+/) // Split on spaces not inside functions
75
+ .map((part) => part.trim())
76
+ .filter((part) => part.length > 0);
77
+ };
78
+ /**
79
+ * Check if a CSS value contains a valid CSS unit.
80
+ */
81
+ const checkCssUnit = (value) => {
82
+ const values = splitCssValues(value);
83
+ for (const value of values) {
84
+ // Skip values containing CSS functions
85
+ if (value.includes('(')) {
86
+ continue;
87
+ }
88
+ const match = value.match(CSS_VALUE_WITH_UNIT_REGEX);
89
+ if (!match) {
90
+ continue;
91
+ }
92
+ const unit = match[2].toLowerCase(); // match[2] is guaranteed by regex pattern
93
+ if (!VALID_CSS_UNITS.includes(unit)) {
94
+ return {
95
+ hasUnit: true,
96
+ unit: match[2], // Preserve original casing
97
+ isValid: false,
98
+ invalidValue: value,
99
+ };
100
+ }
101
+ }
102
+ return { hasUnit: false, unit: null, isValid: true };
103
+ };
104
+ /**
105
+ * Extracts string value from a node if it's a string literal or template literal.
106
+ */
107
+ const getStringValue = (node) => {
108
+ if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string') {
109
+ return node.value;
110
+ }
111
+ if (node.type === AST_NODE_TYPES.TemplateLiteral && node.quasis.length === 1) {
112
+ const firstQuasi = node.quasis[0];
113
+ return firstQuasi?.value.raw ? firstQuasi.value.raw : null;
114
+ }
115
+ return null;
116
+ };
117
+ /**
118
+ * Recursively processes a style object, reporting instances of
119
+ * unknown CSS units.
120
+ *
121
+ * @param context The ESLint rule context.
122
+ * @param node The ObjectExpression node representing the style object to be
123
+ * processed.
124
+ */
125
+ export const processUnknownUnitInStyleObject = (context, node) => {
126
+ // Defensive: This function is only called with ObjectExpression nodes by the rule visitor.
127
+ // This check's for type safety and future-proofing. It's not covered by rule tests
128
+ // because the rule architecture prevents non-ObjectExpression nodes from reaching here.
129
+ if (!node || node.type !== AST_NODE_TYPES.ObjectExpression) {
130
+ return;
131
+ }
132
+ for (const property of node.properties) {
133
+ if (property.type !== AST_NODE_TYPES.Property) {
134
+ continue;
135
+ }
136
+ // Get property key name if possible
137
+ let propertyName = null;
138
+ if (property.key.type === AST_NODE_TYPES.Identifier) {
139
+ propertyName = property.key.name;
140
+ }
141
+ else if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
142
+ propertyName = property.key.value;
143
+ }
144
+ if (propertyName === '@media' || propertyName === 'selectors') {
145
+ if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
146
+ for (const nestedProperty of property.value.properties) {
147
+ if (nestedProperty.type === AST_NODE_TYPES.Property &&
148
+ nestedProperty.value.type === AST_NODE_TYPES.ObjectExpression) {
149
+ processUnknownUnitInStyleObject(context, nestedProperty.value);
150
+ }
151
+ }
152
+ }
153
+ continue;
154
+ }
155
+ // Process direct string values
156
+ const value = getStringValue(property.value);
157
+ if (value) {
158
+ const result = checkCssUnit(value);
159
+ if (result.hasUnit && !result.isValid && result.invalidValue) {
160
+ context.report({
161
+ node: property.value,
162
+ messageId: 'unknownUnit',
163
+ data: {
164
+ unit: result.unit || '',
165
+ value: result.invalidValue,
166
+ },
167
+ });
168
+ }
169
+ }
170
+ // Process nested objects (including those not handled by special cases)
171
+ if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
172
+ processUnknownUnitInStyleObject(context, property.value);
173
+ }
174
+ }
175
+ };
@@ -0,0 +1,6 @@
1
+ import type { Rule } from 'eslint';
2
+ /**
3
+ * Creates ESLint rule visitors for detecting and processing unknown CSS units
4
+ * in style-related function calls.
5
+ */
6
+ export declare const createUnknownUnitVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
@@ -0,0 +1,38 @@
1
+ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
+ import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
3
+ import { processStyleNode } from '../shared-utils/style-node-processor.js';
4
+ import { processUnknownUnitInStyleObject } from './unknown-unit-processor.js';
5
+ /**
6
+ * Creates ESLint rule visitors for detecting and processing unknown CSS units
7
+ * in style-related function calls.
8
+ */
9
+ export const createUnknownUnitVisitors = (context) => {
10
+ return {
11
+ CallExpression(node) {
12
+ if (node.callee.type !== AST_NODE_TYPES.Identifier) {
13
+ return;
14
+ }
15
+ if (['fontFace', 'globalFontFace'].includes(node.callee.name)) {
16
+ const argumentIndex = node.callee.name === 'fontFace' ? 0 : 1;
17
+ if (node.arguments.length > argumentIndex &&
18
+ node.arguments[argumentIndex]?.type === AST_NODE_TYPES.ObjectExpression) {
19
+ processUnknownUnitInStyleObject(context, node.arguments[argumentIndex]);
20
+ }
21
+ return;
22
+ }
23
+ if (['keyframes', 'style', 'styleVariants'].includes(node.callee.name)) {
24
+ if (node.arguments.length > 0) {
25
+ processStyleNode(context, node.arguments[0], processUnknownUnitInStyleObject);
26
+ }
27
+ }
28
+ if (['globalKeyframes', 'globalStyle'].includes(node.callee.name) && node.arguments.length >= 2) {
29
+ processStyleNode(context, node.arguments[1], processUnknownUnitInStyleObject);
30
+ }
31
+ if (node.callee.name === 'recipe' &&
32
+ node.arguments.length > 0 &&
33
+ node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
34
+ processRecipeProperties(context, node.arguments[0], processUnknownUnitInStyleObject);
35
+ }
36
+ },
37
+ };
38
+ };
@@ -0,0 +1,333 @@
1
+ import tsParser from '@typescript-eslint/parser';
2
+ import { run } from 'eslint-vitest-rule-tester';
3
+ import noZeroUnitRule from '../rule-definition.js';
4
+ run({
5
+ name: 'vanilla-extract/no-zero-unit',
6
+ rule: noZeroUnitRule,
7
+ languageOptions: {
8
+ parser: tsParser,
9
+ parserOptions: {
10
+ ecmaVersion: 2022,
11
+ sourceType: 'module',
12
+ },
13
+ },
14
+ valid: [
15
+ {
16
+ code: `
17
+ import { style } from '@vanilla-extract/css';
18
+ const myStyle = style({
19
+ margin: '0',
20
+ padding: 0,
21
+ width: '100%',
22
+ });
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ import { recipe } from '@vanilla-extract/css';
28
+ const myRecipe = recipe({
29
+ base: {
30
+ margin: '0',
31
+ padding: 0,
32
+ },
33
+ variants: {
34
+ size: {
35
+ small: {
36
+ height: '0',
37
+ width: '10px',
38
+ },
39
+ },
40
+ },
41
+ });
42
+ `,
43
+ },
44
+ {
45
+ code: `
46
+ import { style } from '@vanilla-extract/css';
47
+ const myStyle = style({
48
+ ...spreadProps,
49
+ margin: 0,
50
+ '@media': {
51
+ '0rem': '0' // Key shouldn't be checked
52
+ }
53
+ });
54
+ `,
55
+ name: 'should ignore spread elements and object keys',
56
+ },
57
+ {
58
+ code: `
59
+ import { style } from '@vanilla-extract/css';
60
+ const myStyle = style({
61
+ margin: \`0\${someUnit}\`, // Template literal
62
+ padding: someVariable
63
+ });
64
+ `,
65
+ name: 'should ignore non-literal values',
66
+ },
67
+ {
68
+ code: `
69
+ import { globalStyle } from '@vanilla-extract/css';
70
+ const callExpression = someObject.fontFace({ src: '...' }); // Non-Identifier callee
71
+ `,
72
+ name: 'should ignore member expression callees',
73
+ },
74
+ {
75
+ code: `
76
+ import { fontFace } from '@vanilla-extract/css';
77
+ fontFace(); // Missing arguments
78
+ `,
79
+ name: 'should handle missing fontFace arguments',
80
+ },
81
+ {
82
+ code: `
83
+ import { globalFontFace } from '@vanilla-extract/css';
84
+ globalFontFace('my-font'); // Missing style argument
85
+ `,
86
+ name: 'should handle missing globalFontFace style argument',
87
+ },
88
+ ],
89
+ invalid: [
90
+ {
91
+ code: `
92
+ import { style } from '@vanilla-extract/css';
93
+ const myStyle = style({
94
+ margin: '0px',
95
+ padding: '0rem',
96
+ });
97
+ `,
98
+ errors: [{ messageId: 'noZeroUnit' }, { messageId: 'noZeroUnit' }],
99
+ output: `
100
+ import { style } from '@vanilla-extract/css';
101
+ const myStyle = style({
102
+ margin: '0',
103
+ padding: '0',
104
+ });
105
+ `,
106
+ },
107
+ {
108
+ code: `
109
+ import { recipe } from '@vanilla-extract/css';
110
+ const myRecipe = recipe({
111
+ base: {
112
+ margin: '0px',
113
+ },
114
+ variants: {
115
+ size: {
116
+ small: {
117
+ height: '0vh',
118
+ },
119
+ },
120
+ },
121
+ });
122
+ `,
123
+ errors: [{ messageId: 'noZeroUnit' }, { messageId: 'noZeroUnit' }],
124
+ output: `
125
+ import { recipe } from '@vanilla-extract/css';
126
+ const myRecipe = recipe({
127
+ base: {
128
+ margin: '0',
129
+ },
130
+ variants: {
131
+ size: {
132
+ small: {
133
+ height: '0',
134
+ },
135
+ },
136
+ },
137
+ });
138
+ `,
139
+ },
140
+ {
141
+ code: `
142
+ import { style } from '@vanilla-extract/css';
143
+ const myStyle = style({
144
+ margin: '0px',
145
+ '@media': {
146
+ '(min-width: 768px)': {
147
+ padding: '0rem'
148
+ }
149
+ }
150
+ });
151
+ `,
152
+ errors: 2,
153
+ output: `
154
+ import { style } from '@vanilla-extract/css';
155
+ const myStyle = style({
156
+ margin: '0',
157
+ '@media': {
158
+ '(min-width: 768px)': {
159
+ padding: '0'
160
+ }
161
+ }
162
+ });
163
+ `,
164
+ name: 'should handle nested media queries',
165
+ },
166
+ {
167
+ code: `
168
+ import { style } from '@vanilla-extract/css';
169
+ const myStyle = style({
170
+ '::before': {
171
+ content: '""',
172
+ margin: '0px'
173
+ }
174
+ });
175
+ `,
176
+ errors: 1,
177
+ output: `
178
+ import { style } from '@vanilla-extract/css';
179
+ const myStyle = style({
180
+ '::before': {
181
+ content: '""',
182
+ margin: '0'
183
+ }
184
+ });
185
+ `,
186
+ name: 'should handle pseudo-elements',
187
+ },
188
+ {
189
+ code: `
190
+ import { style } from '@vanilla-extract/css';
191
+ const myStyle = style({
192
+ margin: '0px',
193
+ nested: {
194
+ object: {
195
+ padding: '0rem',
196
+ deeper: {
197
+ width: '0%'
198
+ }
199
+ }
200
+ }
201
+ });
202
+ `,
203
+ errors: 3,
204
+ output: `
205
+ import { style } from '@vanilla-extract/css';
206
+ const myStyle = style({
207
+ margin: '0',
208
+ nested: {
209
+ object: {
210
+ padding: '0',
211
+ deeper: {
212
+ width: '0'
213
+ }
214
+ }
215
+ }
216
+ });
217
+ `,
218
+ name: 'should handle multiple levels of nesting',
219
+ },
220
+ {
221
+ code: `
222
+ import { fontFace, globalFontFace } from '@vanilla-extract/css';
223
+
224
+ fontFace({
225
+ src: '...',
226
+ lineGap: '0rem'
227
+ });
228
+
229
+ globalFontFace('my-font', {
230
+ src: '...',
231
+ sizeAdjust: '0%'
232
+ });
233
+ `,
234
+ errors: 2,
235
+ output: `
236
+ import { fontFace, globalFontFace } from '@vanilla-extract/css';
237
+
238
+ fontFace({
239
+ src: '...',
240
+ lineGap: '0'
241
+ });
242
+
243
+ globalFontFace('my-font', {
244
+ src: '...',
245
+ sizeAdjust: '0'
246
+ });
247
+ `,
248
+ name: 'should handle fontFace and globalFontFace arguments',
249
+ },
250
+ // 0deg is valid (deg isn't in our unit check)
251
+ {
252
+ code: `
253
+ import { globalKeyframes, globalStyle } from '@vanilla-extract/css';
254
+
255
+ globalKeyframes('spin', {
256
+ '0%': { transform: 'rotate(0deg)' },
257
+ '100%': { transform: 'rotate(0deg)' }
258
+ });
259
+
260
+ globalStyle('html', {
261
+ margin: '0px',
262
+ padding: '0rem'
263
+ });
264
+ `,
265
+ errors: 2,
266
+ output: `
267
+ import { globalKeyframes, globalStyle } from '@vanilla-extract/css';
268
+
269
+ globalKeyframes('spin', {
270
+ '0%': { transform: 'rotate(0deg)' },
271
+ '100%': { transform: 'rotate(0deg)' }
272
+ });
273
+
274
+ globalStyle('html', {
275
+ margin: '0',
276
+ padding: '0'
277
+ });
278
+ `,
279
+ name: 'should handle globalKeyframes and globalStyle arguments',
280
+ },
281
+ {
282
+ code: `
283
+ import { globalStyle } from '@vanilla-extract/css';
284
+ globalStyle('html', {
285
+ '@media': {
286
+ '(min-width: 768px)': {
287
+ margin: '0px'
288
+ }
289
+ }
290
+ });
291
+ `,
292
+ errors: 1,
293
+ output: `
294
+ import { globalStyle } from '@vanilla-extract/css';
295
+ globalStyle('html', {
296
+ '@media': {
297
+ '(min-width: 768px)': {
298
+ margin: '0'
299
+ }
300
+ }
301
+ });
302
+ `,
303
+ name: 'should handle nested globalStyle arguments',
304
+ },
305
+ {
306
+ code: `
307
+ import { style } from '@vanilla-extract/css';
308
+ const myStyle = style({
309
+ margin: '-0px',
310
+ padding: '-0rem',
311
+ top: '-0vh',
312
+ left: '-0%',
313
+ });
314
+ `,
315
+ errors: [
316
+ { messageId: 'noZeroUnit' },
317
+ { messageId: 'noZeroUnit' },
318
+ { messageId: 'noZeroUnit' },
319
+ { messageId: 'noZeroUnit' },
320
+ ],
321
+ output: `
322
+ import { style } from '@vanilla-extract/css';
323
+ const myStyle = style({
324
+ margin: '0',
325
+ padding: '0',
326
+ top: '0',
327
+ left: '0',
328
+ });
329
+ `,
330
+ name: 'should convert negative zero with units to simple zero',
331
+ },
332
+ ],
333
+ });
@@ -0,0 +1,2 @@
1
+ import noZeroUnitRule from './rule-definition.js';
2
+ export default noZeroUnitRule;
@@ -0,0 +1,2 @@
1
+ import noZeroUnitRule from './rule-definition.js';
2
+ export default noZeroUnitRule;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ declare const noZeroUnitRule: Rule.RuleModule;
3
+ export default noZeroUnitRule;
@@ -0,0 +1,20 @@
1
+ import { createZeroUnitVisitors } from './zero-unit-visitor-creator.js';
2
+ const noZeroUnitRule = {
3
+ meta: {
4
+ type: 'suggestion',
5
+ docs: {
6
+ description: 'enforce unitless zero in numeric values',
7
+ category: 'Stylistic Issues',
8
+ recommended: true,
9
+ },
10
+ fixable: 'code',
11
+ schema: [],
12
+ messages: {
13
+ noZeroUnit: 'Zero values don’t need a unit. Replace with "0".',
14
+ },
15
+ },
16
+ create(context) {
17
+ return createZeroUnitVisitors(context);
18
+ },
19
+ };
20
+ export default noZeroUnitRule;
@@ -0,0 +1,9 @@
1
+ import type { Rule } from 'eslint';
2
+ import { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Recursively processes a style object, reporting and fixing instances of zero values with units.
5
+ *
6
+ * @param ruleContext The ESLint rule context.
7
+ * @param node The ObjectExpression node representing the style object to be processed.
8
+ */
9
+ export declare const processZeroUnitInStyleObject: (ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression) => void;
@@ -0,0 +1,29 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ const ZERO_VALUE_WITH_UNIT_REGEX = /^-?0(px|em|rem|%|vh|vw|vmin|vmax|ex|ch|cm|mm|in|pt|pc|Q|fr)$/;
3
+ /**
4
+ * Recursively processes a style object, reporting and fixing instances of zero values with units.
5
+ *
6
+ * @param ruleContext The ESLint rule context.
7
+ * @param node The ObjectExpression node representing the style object to be processed.
8
+ */
9
+ export const processZeroUnitInStyleObject = (ruleContext, node) => {
10
+ node.properties.forEach((property) => {
11
+ if (property.type !== 'Property') {
12
+ return;
13
+ }
14
+ // Process direct string literal values
15
+ if (property.value.type === 'Literal' &&
16
+ typeof property.value.value === 'string' &&
17
+ ZERO_VALUE_WITH_UNIT_REGEX.test(property.value.value)) {
18
+ ruleContext.report({
19
+ node: property.value,
20
+ messageId: 'noZeroUnit',
21
+ fix: (fixer) => fixer.replaceText(property.value, "'0'"),
22
+ });
23
+ }
24
+ // Process nested objects (selectors, media queries, etc.)
25
+ if (property.value.type === 'ObjectExpression') {
26
+ processZeroUnitInStyleObject(ruleContext, property.value);
27
+ }
28
+ });
29
+ };
@@ -0,0 +1,14 @@
1
+ import type { Rule } from 'eslint';
2
+ /**
3
+ * Creates ESLint rule visitors for detecting and processing zero values with units in style-related function calls.
4
+ *
5
+ * @param context The ESLint rule context.
6
+ * @returns An object with visitor functions for the ESLint rule.
7
+ *
8
+ * This function sets up visitors for the following cases:
9
+ * 1. The `fontFace` and `globalFontFace` functions, processing their object arguments.
10
+ * 2. Style-related functions: `keyframes`, `style`, `styleVariants`, processing their style objects.
11
+ * 3. The `globalKeyframes` and `globalStyle` functions, processing the second argument as style objects.
12
+ * 4. The `recipe` function, processing the first argument as the recipe object.
13
+ */
14
+ export declare const createZeroUnitVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
@@ -0,0 +1,46 @@
1
+ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
+ import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
3
+ import { processStyleNode } from '../shared-utils/style-node-processor.js';
4
+ import { processZeroUnitInStyleObject } from './zero-unit-processor.js';
5
+ /**
6
+ * Creates ESLint rule visitors for detecting and processing zero values with units in style-related function calls.
7
+ *
8
+ * @param context The ESLint rule context.
9
+ * @returns An object with visitor functions for the ESLint rule.
10
+ *
11
+ * This function sets up visitors for the following cases:
12
+ * 1. The `fontFace` and `globalFontFace` functions, processing their object arguments.
13
+ * 2. Style-related functions: `keyframes`, `style`, `styleVariants`, processing their style objects.
14
+ * 3. The `globalKeyframes` and `globalStyle` functions, processing the second argument as style objects.
15
+ * 4. The `recipe` function, processing the first argument as the recipe object.
16
+ */
17
+ export const createZeroUnitVisitors = (context) => {
18
+ return {
19
+ CallExpression(node) {
20
+ if (node.callee.type !== AST_NODE_TYPES.Identifier) {
21
+ return;
22
+ }
23
+ if (['fontFace', 'globalFontFace'].includes(node.callee.name)) {
24
+ const argumentIndex = node.callee.name === 'fontFace' ? 0 : 1;
25
+ if (node.arguments.length > argumentIndex &&
26
+ node.arguments[argumentIndex]?.type === AST_NODE_TYPES.ObjectExpression) {
27
+ processZeroUnitInStyleObject(context, node.arguments[argumentIndex]);
28
+ }
29
+ return;
30
+ }
31
+ if (['keyframes', 'style', 'styleVariants'].includes(node.callee.name)) {
32
+ if (node.arguments.length > 0) {
33
+ processStyleNode(context, node.arguments[0], processZeroUnitInStyleObject);
34
+ }
35
+ }
36
+ if (['globalKeyframes', 'globalStyle'].includes(node.callee.name) && node.arguments.length >= 2) {
37
+ processStyleNode(context, node.arguments[1], processZeroUnitInStyleObject);
38
+ }
39
+ if (node.callee.name === 'recipe' &&
40
+ node.arguments.length > 0 &&
41
+ node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
42
+ processRecipeProperties(context, node.arguments[0], processZeroUnitInStyleObject);
43
+ }
44
+ },
45
+ };
46
+ };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,8 @@ export declare const vanillaExtract: {
8
8
  'concentric-order': import("eslint").Rule.RuleModule;
9
9
  'custom-order': import("eslint").Rule.RuleModule;
10
10
  'no-empty-style-blocks': import("eslint").Rule.RuleModule;
11
+ 'no-unknown-unit': import("eslint").Rule.RuleModule;
12
+ 'no-zero-unit': import("eslint").Rule.RuleModule;
11
13
  };
12
14
  configs: {
13
15
  recommended: {
@@ -15,6 +17,8 @@ export declare const vanillaExtract: {
15
17
  rules: {
16
18
  'vanilla-extract/concentric-order': string;
17
19
  'vanilla-extract/no-empty-style-blocks': string;
20
+ 'vanilla-extract/no-unknown-unit': string;
21
+ 'vanilla-extract/no-zero-unit': string;
18
22
  };
19
23
  };
20
24
  };
package/dist/index.js CHANGED
@@ -2,16 +2,20 @@ import alphabeticalOrderRule from './css-rules/alphabetical-order/index.js';
2
2
  import concentricOrderRule from './css-rules/concentric-order/index.js';
3
3
  import customOrderRule from './css-rules/custom-order/rule-definition.js';
4
4
  import noEmptyStyleBlocksRule from './css-rules/no-empty-blocks/rule-definition.js';
5
+ import noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
6
+ import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
5
7
  export const vanillaExtract = {
6
8
  meta: {
7
9
  name: '@antebudimir/eslint-plugin-vanilla-extract',
8
- version: '1.7.0',
10
+ version: '1.9.0',
9
11
  },
10
12
  rules: {
11
13
  'alphabetical-order': alphabeticalOrderRule,
12
14
  'concentric-order': concentricOrderRule,
13
15
  'custom-order': customOrderRule,
14
16
  'no-empty-style-blocks': noEmptyStyleBlocksRule,
17
+ 'no-unknown-unit': noUnknownUnitRule,
18
+ 'no-zero-unit': noZeroUnitRule,
15
19
  },
16
20
  configs: {
17
21
  recommended: {
@@ -19,6 +23,8 @@ export const vanillaExtract = {
19
23
  rules: {
20
24
  'vanilla-extract/concentric-order': 'error',
21
25
  'vanilla-extract/no-empty-style-blocks': 'error',
26
+ 'vanilla-extract/no-unknown-unit': 'error',
27
+ 'vanilla-extract/no-zero-unit': 'error',
22
28
  },
23
29
  },
24
30
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.7.0",
3
+ "version": "1.9.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",