@antebudimir/eslint-plugin-vanilla-extract 1.8.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.
package/README.md CHANGED
@@ -81,6 +81,7 @@ The recommended configuration enables the following rules with error severity:
81
81
 
82
82
  - `vanilla-extract/concentric-order`: Enforces concentric CSS property ordering
83
83
  - `vanilla-extract/no-empty-style-blocks`: Prevents empty style blocks
84
+ - `vanilla-extract/no-unknown-unit`: prohibits usage of unrecognized CSS units.
84
85
  - `vanilla-extract/no-zero-unit`: removes unnecessary units for zero values
85
86
 
86
87
  You can use the recommended configuration as a starting point and override rules as needed for your project.
@@ -110,6 +111,7 @@ export default [
110
111
  sortRemainingProperties: 'concentric', // 'alphabetical' is default
111
112
  },
112
113
  ],
114
+ 'vanilla-extract/no-unknown-unit': 'error',
113
115
  'vanilla-extract/no-zero-unit': 'warn',
114
116
  },
115
117
  },
@@ -269,6 +271,44 @@ export const recipeWithEmptyVariants = recipe({
269
271
  });
270
272
  ```
271
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
+
272
312
  ## vanilla-extract/no-zero-unit
273
313
 
274
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.
@@ -373,11 +413,12 @@ The roadmap outlines the project's current status and future plans:
373
413
  - `no-empty-style-blocks` rule to disallow empty blocks.
374
414
  - Recommended ESLint configuration for the plugin.
375
415
  - `no-zero-unit` rule to disallow units when the value is zero.
416
+ - `no-unknown-unit` rule to disallow unknown units.
376
417
  - Comprehensive rule testing.
377
418
 
378
419
  ### Current Work
379
420
 
380
- - `no-unknown-unit` rule to disallow unknown units.
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))
381
422
 
382
423
  ### Upcoming Features
383
424
 
@@ -386,6 +427,7 @@ The roadmap outlines the project's current status and future plans:
386
427
  - `prefer-logical-properties` rule to enforce use of logical properties.
387
428
  - `prefer-theme-tokens` rule to enforce use of theme tokens instead of hard-coded values when available.
388
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.
389
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.
390
432
 
391
433
  ## Contributing
@@ -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
+ };
@@ -10,7 +10,7 @@ const noZeroUnitRule = {
10
10
  fixable: 'code',
11
11
  schema: [],
12
12
  messages: {
13
- noZeroUnit: 'Unit with zero value is unnecessary. Use 0 instead.',
13
+ noZeroUnit: 'Zero values don’t need a unit. Replace with "0".',
14
14
  },
15
15
  },
16
16
  create(context) {
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare const vanillaExtract: {
8
8
  'concentric-order': import("eslint").Rule.RuleModule;
9
9
  'custom-order': import("eslint").Rule.RuleModule;
10
10
  'no-empty-style-blocks': import("eslint").Rule.RuleModule;
11
+ 'no-unknown-unit': import("eslint").Rule.RuleModule;
11
12
  'no-zero-unit': import("eslint").Rule.RuleModule;
12
13
  };
13
14
  configs: {
@@ -16,6 +17,7 @@ export declare const vanillaExtract: {
16
17
  rules: {
17
18
  'vanilla-extract/concentric-order': string;
18
19
  'vanilla-extract/no-empty-style-blocks': string;
20
+ 'vanilla-extract/no-unknown-unit': string;
19
21
  'vanilla-extract/no-zero-unit': string;
20
22
  };
21
23
  };
package/dist/index.js CHANGED
@@ -2,17 +2,19 @@ import alphabeticalOrderRule from './css-rules/alphabetical-order/index.js';
2
2
  import concentricOrderRule from './css-rules/concentric-order/index.js';
3
3
  import customOrderRule from './css-rules/custom-order/rule-definition.js';
4
4
  import noEmptyStyleBlocksRule from './css-rules/no-empty-blocks/rule-definition.js';
5
+ import noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
5
6
  import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
6
7
  export const vanillaExtract = {
7
8
  meta: {
8
9
  name: '@antebudimir/eslint-plugin-vanilla-extract',
9
- version: '1.8.0',
10
+ version: '1.9.0',
10
11
  },
11
12
  rules: {
12
13
  'alphabetical-order': alphabeticalOrderRule,
13
14
  'concentric-order': concentricOrderRule,
14
15
  'custom-order': customOrderRule,
15
16
  'no-empty-style-blocks': noEmptyStyleBlocksRule,
17
+ 'no-unknown-unit': noUnknownUnitRule,
16
18
  'no-zero-unit': noZeroUnitRule,
17
19
  },
18
20
  configs: {
@@ -21,6 +23,7 @@ export const vanillaExtract = {
21
23
  rules: {
22
24
  'vanilla-extract/concentric-order': 'error',
23
25
  'vanilla-extract/no-empty-style-blocks': 'error',
26
+ 'vanilla-extract/no-unknown-unit': 'error',
24
27
  'vanilla-extract/no-zero-unit': 'error',
25
28
  },
26
29
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.8.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",