@antebudimir/eslint-plugin-vanilla-extract 1.15.1 → 1.16.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
@@ -264,6 +264,7 @@ The recommended configuration enables the following rules with error severity:
264
264
  - `vanilla-extract/alphabetical-order`: Alternative ordering rule (alphabetical sorting)
265
265
  - `vanilla-extract/custom-order`: Alternative ordering rule (custom group-based sorting)
266
266
  - `vanilla-extract/no-px-unit`: Disallows px units with an optional allowlist
267
+ - `vanilla-extract/no-unitless-values`: Disallows unitless numeric values for CSS properties that require units
267
268
  - `vanilla-extract/prefer-logical-properties`: Enforces logical CSS properties over physical directional properties
268
269
  - `vanilla-extract/prefer-theme-tokens`: Enforces theme tokens instead of hard-coded values for colors, spacing, font sizes, border radius, border widths, shadows, z-index, opacity, font weights, and transitions/animations (optionally evaluates helper functions and template literals)
269
270
 
@@ -536,6 +537,77 @@ export const myStyle = style({
536
537
  });
537
538
  ```
538
539
 
540
+ ### vanilla-extract/no-unitless-values
541
+
542
+ This rule disallows unitless numeric values for CSS properties that require units in vanilla-extract style objects. It helps teams that prefer explicit units avoid confusion, as vanilla-extract automatically converts unitless numbers to `px` at runtime.
543
+
544
+ **Note:** This is an optional rule (not enabled in recommended config). Enable it only if your team prefers explicit units over vanilla-extract's automatic `px` conversion.
545
+
546
+ Configuration with allowed properties:
547
+
548
+ ```json
549
+ {
550
+ "rules": {
551
+ "vanilla-extract/no-unitless-values": ["warn", { "allow": ["width", "height"] }]
552
+ }
553
+ }
554
+ ```
555
+
556
+ ```typescript
557
+ // ❌ Incorrect
558
+ import { style } from '@vanilla-extract/css';
559
+
560
+ export const myStyle = style({
561
+ width: 100,
562
+ margin: 20,
563
+ padding: 10.5,
564
+ height: '50',
565
+ top: '-10',
566
+ });
567
+
568
+ // ✅ Correct
569
+ import { style } from '@vanilla-extract/css';
570
+
571
+ export const myStyle = style({
572
+ width: '100px',
573
+ margin: '20px',
574
+ padding: 0,
575
+ height: '50rem',
576
+ opacity: 0.5, // opacity accepts unitless values
577
+ lineHeight: 1.5, // line-height accepts unitless values
578
+ zIndex: 10, // z-index accepts unitless values
579
+ });
580
+ ```
581
+
582
+ **Properties that require units:**
583
+
584
+ - **Box model:** `width`, `height`, `minWidth`, `maxWidth`, `minHeight`, `maxHeight`, `min-width`, `max-width`, `min-height`, `max-height`
585
+ - **Spacing:** `margin`, `marginTop`, `marginRight`, `marginBottom`, `marginLeft`, `marginBlock`, `marginBlockStart`, `marginBlockEnd`, `marginInline`, `marginInlineStart`, `marginInlineEnd`, `margin-top`, `margin-right`, `margin-bottom`, `margin-left`, `margin-block`, `margin-block-start`, `margin-block-end`, `margin-inline`, `margin-inline-start`, `margin-inline-end`, `padding`, `paddingTop`, `paddingRight`, `paddingBottom`, `paddingLeft`, `paddingBlock`, `paddingBlockStart`, `paddingBlockEnd`, `paddingInline`, `paddingInlineStart`, `paddingInlineEnd`, `padding-top`, `padding-right`, `padding-bottom`, `padding-left`, `padding-block`, `padding-block-start`, `padding-block-end`, `padding-inline`, `padding-inline-start`, `padding-inline-end`
586
+ - **Positioning:** `top`, `right`, `bottom`, `left`, `inset`, `insetBlock`, `insetBlockStart`, `insetBlockEnd`, `insetInline`, `insetInlineStart`, `insetInlineEnd`, `inset-block`, `inset-block-start`, `inset-block-end`, `inset-inline`, `inset-inline-start`, `inset-inline-end`
587
+ - **Border:** `borderWidth`, `borderTopWidth`, `borderRightWidth`, `borderBottomWidth`, `borderLeftWidth`, `borderBlockWidth`, `borderBlockStartWidth`, `borderBlockEndWidth`, `borderInlineWidth`, `borderInlineStartWidth`, `borderInlineEndWidth`, `border-width`, `border-top-width`, `border-right-width`, `border-bottom-width`, `border-left-width`, `border-block-width`, `border-block-start-width`, `border-block-end-width`, `border-inline-width`, `border-inline-start-width`, `border-inline-end-width`, `borderRadius`, `borderTopLeftRadius`, `borderTopRightRadius`, `borderBottomLeftRadius`, `borderBottomRightRadius`, `borderStartStartRadius`, `borderStartEndRadius`, `borderEndStartRadius`, `borderEndEndRadius`, `border-radius`, `border-top-left-radius`, `border-top-right-radius`, `border-bottom-left-radius`, `border-bottom-right-radius`, `border-start-start-radius`, `border-start-end-radius`, `border-end-start-radius`, `border-end-end-radius`
588
+ - **Typography:** `fontSize`, `font-size`, `letterSpacing`, `letter-spacing`, `wordSpacing`, `word-spacing`, `textIndent`, `text-indent`
589
+ - **Layout:** `gap`, `rowGap`, `columnGap`, `row-gap`, `column-gap`, `flexBasis`, `flex-basis`
590
+ - **Outline:** `outlineWidth`, `outline-width`, `outlineOffset`, `outline-offset`
591
+ - **Other:** `blockSize`, `inlineSize`, `minBlockSize`, `maxBlockSize`, `minInlineSize`, `maxInlineSize`, `block-size`, `inline-size`, `min-block-size`, `max-block-size`, `min-inline-size`, `max-inline-size`
592
+
593
+ **Properties that accept unitless values:**
594
+
595
+ - **Common:** `opacity`, `zIndex`, `z-index`, `lineHeight`, `line-height`, `flexGrow`, `flex-grow`, `flexShrink`, `flex-shrink`, `order`, `fontWeight`, `font-weight`, `zoom`
596
+ - **Animation:** `animationIterationCount`, `animation-iteration-count`
597
+ - **Layout:** `columnCount`, `column-count`, `orphans`, `widows`
598
+ - **Grid:** `gridColumn`, `grid-column`, `gridColumnEnd`, `grid-column-end`, `gridColumnStart`, `grid-column-start`, `gridRow`, `grid-row`, `gridRowEnd`, `grid-row-end`, `gridRowStart`, `grid-row-start`
599
+ - **SVG:** `fillOpacity`, `fill-opacity`, `strokeOpacity`, `stroke-opacity`, `strokeMiterlimit`, `stroke-miterlimit`
600
+
601
+ **Why use this rule?**
602
+
603
+ While vanilla-extract safely converts unitless numbers to `px`, some teams prefer explicit units because:
604
+
605
+ 1. It makes the intended unit clear (px, rem, em, %, etc.)
606
+ 2. It prevents accidental use of px when rem or other units are preferred
607
+ 3. It aligns with CSS best practices of being explicit about units
608
+
609
+ **Auto-fix:** Not available. Since different teams prefer different units (px, rem, em, %), you must manually add your preferred unit.
610
+
539
611
  ### vanilla-extract/no-unknown-unit
540
612
 
541
613
  This rule enforces the use of valid CSS units in vanilla-extract style objects. It prevents typos and non-standard units
@@ -835,10 +907,7 @@ The roadmap outlines the project's current status and future plans:
835
907
  - `no-px-unit` rule to disallow use of `px` units with configurable whitelist.
836
908
  - `prefer-logical-properties` rule to enforce use of logical properties.
837
909
  - `prefer-theme-tokens` rule to enforce theme tokens instead of hard-coded values for colors, spacing, font sizes, border radius, border widths, shadows, z-index, opacity, font weights, and transitions/animations (optionally evaluates helper functions and template literals).
838
-
839
- ### Current Work
840
-
841
- - `no-unitless-values` rule that disallows numeric literals for CSS properties that are not unitless in CSS.
910
+ - `no-unitless-values` rule to disallow unitless numeric values for CSS properties that require units.
842
911
 
843
912
  ### Upcoming Features
844
913
 
@@ -1 +1,2 @@
1
- export { default } from './rule-definition.js';
1
+ import noTrailingZeroRule from './rule-definition.js';
2
+ export default noTrailingZeroRule;
@@ -1 +1,2 @@
1
- export { default } from './rule-definition.js';
1
+ import noTrailingZeroRule from './rule-definition.js';
2
+ export default noTrailingZeroRule;
@@ -0,0 +1,444 @@
1
+ import tsParser from '@typescript-eslint/parser';
2
+ import { run } from 'eslint-vitest-rule-tester';
3
+ import noUnitlessValuesRule from '../rule-definition.js';
4
+ run({
5
+ name: 'vanilla-extract/no-unitless-values',
6
+ rule: noUnitlessValuesRule,
7
+ languageOptions: {
8
+ parser: tsParser,
9
+ parserOptions: {
10
+ ecmaVersion: 2022,
11
+ sourceType: 'module',
12
+ },
13
+ },
14
+ valid: [
15
+ // Zero values are allowed
16
+ {
17
+ code: `
18
+ import { style } from '@vanilla-extract/css';
19
+ const myStyle = style({
20
+ margin: 0,
21
+ padding: 0,
22
+ width: 0,
23
+ });
24
+ `,
25
+ name: 'should allow zero values without units',
26
+ },
27
+ // String values with units
28
+ {
29
+ code: `
30
+ import { style } from '@vanilla-extract/css';
31
+ const myStyle = style({
32
+ width: '100px',
33
+ margin: '20px',
34
+ padding: '1rem',
35
+ fontSize: '16px',
36
+ });
37
+ `,
38
+ name: 'should allow string values with units',
39
+ },
40
+ // String zero values
41
+ {
42
+ code: `
43
+ import { style } from '@vanilla-extract/css';
44
+ const myStyle = style({
45
+ margin: '0',
46
+ padding: '0',
47
+ });
48
+ `,
49
+ name: 'should allow string zero values without units',
50
+ },
51
+ // Unitless-valid properties
52
+ {
53
+ code: `
54
+ import { style } from '@vanilla-extract/css';
55
+ const myStyle = style({
56
+ opacity: 0.5,
57
+ zIndex: 10,
58
+ lineHeight: 1.5,
59
+ flexGrow: 1,
60
+ flexShrink: 0,
61
+ order: 2,
62
+ fontWeight: 700,
63
+ zoom: 1.2,
64
+ });
65
+ `,
66
+ name: 'should allow unitless values for properties that accept them',
67
+ },
68
+ // Recipe with valid values
69
+ {
70
+ code: `
71
+ import { recipe } from '@vanilla-extract/css';
72
+ const myRecipe = recipe({
73
+ base: {
74
+ margin: '0',
75
+ padding: 0,
76
+ opacity: 0.8,
77
+ },
78
+ variants: {
79
+ size: {
80
+ small: {
81
+ height: '10px',
82
+ width: '10px',
83
+ zIndex: 1,
84
+ },
85
+ },
86
+ },
87
+ });
88
+ `,
89
+ name: 'should allow valid values in recipe',
90
+ },
91
+ // Template literals (not checked)
92
+ {
93
+ code: `
94
+ import { style } from '@vanilla-extract/css';
95
+ const myStyle = style({
96
+ margin: \`\${someValue}px\`,
97
+ padding: someVariable,
98
+ });
99
+ `,
100
+ name: 'should ignore template literals and variables',
101
+ },
102
+ // Nested selectors with valid values
103
+ {
104
+ code: `
105
+ import { style } from '@vanilla-extract/css';
106
+ const myStyle = style({
107
+ width: '100px',
108
+ ':hover': {
109
+ margin: '20px',
110
+ opacity: 0.8,
111
+ },
112
+ });
113
+ `,
114
+ name: 'should allow valid values in nested selectors',
115
+ },
116
+ // Media queries with valid values
117
+ {
118
+ code: `
119
+ import { style } from '@vanilla-extract/css';
120
+ const myStyle = style({
121
+ margin: 0,
122
+ '@media': {
123
+ '(min-width: 768px)': {
124
+ padding: '20px',
125
+ zIndex: 10,
126
+ }
127
+ }
128
+ });
129
+ `,
130
+ name: 'should allow valid values in media queries',
131
+ },
132
+ // globalStyle with valid values
133
+ {
134
+ code: `
135
+ import { globalStyle } from '@vanilla-extract/css';
136
+ globalStyle('html', {
137
+ margin: '0',
138
+ padding: 0,
139
+ lineHeight: 1.5,
140
+ });
141
+ `,
142
+ name: 'should allow valid values in globalStyle',
143
+ },
144
+ // fontFace with valid values
145
+ {
146
+ code: `
147
+ import { fontFace } from '@vanilla-extract/css';
148
+ fontFace({
149
+ src: 'url(...)',
150
+ fontWeight: 400,
151
+ });
152
+ `,
153
+ name: 'should allow valid values in fontFace',
154
+ },
155
+ // keyframes (animation values are strings)
156
+ {
157
+ code: `
158
+ import { keyframes } from '@vanilla-extract/css';
159
+ const spin = keyframes({
160
+ '0%': { transform: 'rotate(0deg)' },
161
+ '100%': { transform: 'rotate(360deg)' }
162
+ });
163
+ `,
164
+ name: 'should allow keyframes with string values',
165
+ },
166
+ // allow option
167
+ {
168
+ code: `
169
+ import { style } from '@vanilla-extract/css';
170
+ const myStyle = style({
171
+ width: 100,
172
+ margin: '20px',
173
+ });
174
+ `,
175
+ options: [{ allow: ['width'] }],
176
+ name: 'should allow properties specified in allow option',
177
+ },
178
+ // Kebab-case unitless-valid properties
179
+ {
180
+ code: `
181
+ import { style } from '@vanilla-extract/css';
182
+ const myStyle = style({
183
+ 'z-index': 10,
184
+ 'line-height': 1.5,
185
+ 'flex-grow': 1,
186
+ });
187
+ `,
188
+ name: 'should allow unitless values for kebab-case unitless-valid properties',
189
+ },
190
+ ],
191
+ invalid: [
192
+ // Basic unitless values
193
+ {
194
+ code: `
195
+ import { style } from '@vanilla-extract/css';
196
+ const myStyle = style({
197
+ width: 100,
198
+ margin: 20,
199
+ });
200
+ `,
201
+ errors: [
202
+ { messageId: 'noUnitlessValue', data: { property: 'width', value: '100' } },
203
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '20' } },
204
+ ],
205
+ name: 'should flag unitless numeric values for length properties',
206
+ },
207
+ // Decimal values
208
+ {
209
+ code: `
210
+ import { style } from '@vanilla-extract/css';
211
+ const myStyle = style({
212
+ padding: 10.5,
213
+ fontSize: 16.5,
214
+ });
215
+ `,
216
+ errors: [
217
+ { messageId: 'noUnitlessValue', data: { property: 'padding', value: '10.5' } },
218
+ { messageId: 'noUnitlessValue', data: { property: 'fontSize', value: '16.5' } },
219
+ ],
220
+ name: 'should flag decimal unitless values',
221
+ },
222
+ // Recipe with unitless values
223
+ {
224
+ code: `
225
+ import { recipe } from '@vanilla-extract/css';
226
+ const myRecipe = recipe({
227
+ base: {
228
+ margin: 10,
229
+ },
230
+ variants: {
231
+ size: {
232
+ small: {
233
+ height: 20,
234
+ },
235
+ },
236
+ },
237
+ });
238
+ `,
239
+ errors: [
240
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '10' } },
241
+ { messageId: 'noUnitlessValue', data: { property: 'height', value: '20' } },
242
+ ],
243
+ name: 'should flag unitless values in recipe',
244
+ },
245
+ // Nested selectors with unitless values
246
+ {
247
+ code: `
248
+ import { style } from '@vanilla-extract/css';
249
+ const myStyle = style({
250
+ width: 100,
251
+ ':hover': {
252
+ margin: 20,
253
+ },
254
+ });
255
+ `,
256
+ errors: [
257
+ { messageId: 'noUnitlessValue', data: { property: 'width', value: '100' } },
258
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '20' } },
259
+ ],
260
+ name: 'should flag unitless values in nested selectors',
261
+ },
262
+ // Media queries with unitless values
263
+ {
264
+ code: `
265
+ import { style } from '@vanilla-extract/css';
266
+ const myStyle = style({
267
+ '@media': {
268
+ '(min-width: 768px)': {
269
+ padding: 20,
270
+ }
271
+ }
272
+ });
273
+ `,
274
+ errors: [{ messageId: 'noUnitlessValue', data: { property: 'padding', value: '20' } }],
275
+ name: 'should flag unitless values in media queries',
276
+ },
277
+ // Multiple levels of nesting
278
+ {
279
+ code: `
280
+ import { style } from '@vanilla-extract/css';
281
+ const myStyle = style({
282
+ margin: 10,
283
+ nested: {
284
+ object: {
285
+ padding: 20,
286
+ deeper: {
287
+ width: 30
288
+ }
289
+ }
290
+ }
291
+ });
292
+ `,
293
+ errors: [
294
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '10' } },
295
+ { messageId: 'noUnitlessValue', data: { property: 'padding', value: '20' } },
296
+ { messageId: 'noUnitlessValue', data: { property: 'width', value: '30' } },
297
+ ],
298
+ name: 'should flag unitless values in deeply nested objects',
299
+ },
300
+ // globalStyle with unitless values
301
+ {
302
+ code: `
303
+ import { globalStyle } from '@vanilla-extract/css';
304
+ globalStyle('html', {
305
+ margin: 10,
306
+ padding: 20,
307
+ });
308
+ `,
309
+ errors: [
310
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '10' } },
311
+ { messageId: 'noUnitlessValue', data: { property: 'padding', value: '20' } },
312
+ ],
313
+ name: 'should flag unitless values in globalStyle',
314
+ },
315
+ // Various length properties
316
+ {
317
+ code: `
318
+ import { style } from '@vanilla-extract/css';
319
+ const myStyle = style({
320
+ width: 100,
321
+ height: 200,
322
+ minWidth: 50,
323
+ maxWidth: 500,
324
+ top: 10,
325
+ left: 20,
326
+ borderWidth: 2,
327
+ borderRadius: 5,
328
+ gap: 15,
329
+ fontSize: 16,
330
+ });
331
+ `,
332
+ errors: 10,
333
+ name: 'should flag all unitless values for various length properties',
334
+ },
335
+ // Kebab-case properties
336
+ {
337
+ code: `
338
+ import { style } from '@vanilla-extract/css';
339
+ const myStyle = style({
340
+ 'margin-top': 10,
341
+ 'padding-left': 20,
342
+ 'font-size': 16,
343
+ });
344
+ `,
345
+ errors: [
346
+ { messageId: 'noUnitlessValue', data: { property: 'margin-top', value: '10' } },
347
+ { messageId: 'noUnitlessValue', data: { property: 'padding-left', value: '20' } },
348
+ { messageId: 'noUnitlessValue', data: { property: 'font-size', value: '16' } },
349
+ ],
350
+ name: 'should flag unitless values for kebab-case properties',
351
+ },
352
+ // Logical properties
353
+ {
354
+ code: `
355
+ import { style } from '@vanilla-extract/css';
356
+ const myStyle = style({
357
+ marginBlock: 10,
358
+ paddingInline: 20,
359
+ insetBlockStart: 5,
360
+ });
361
+ `,
362
+ errors: [
363
+ { messageId: 'noUnitlessValue', data: { property: 'marginBlock', value: '10' } },
364
+ { messageId: 'noUnitlessValue', data: { property: 'paddingInline', value: '20' } },
365
+ { messageId: 'noUnitlessValue', data: { property: 'insetBlockStart', value: '5' } },
366
+ ],
367
+ name: 'should flag unitless values for logical properties',
368
+ },
369
+ // styleVariants
370
+ {
371
+ code: `
372
+ import { styleVariants } from '@vanilla-extract/css';
373
+ const variants = styleVariants({
374
+ small: { width: 10 },
375
+ large: { width: 100 },
376
+ });
377
+ `,
378
+ errors: [
379
+ { messageId: 'noUnitlessValue', data: { property: 'width', value: '10' } },
380
+ { messageId: 'noUnitlessValue', data: { property: 'width', value: '100' } },
381
+ ],
382
+ name: 'should flag unitless values in styleVariants',
383
+ },
384
+ // globalKeyframes
385
+ {
386
+ code: `
387
+ import { globalKeyframes } from '@vanilla-extract/css';
388
+ globalKeyframes('slide', {
389
+ '0%': { left: 0 },
390
+ '100%': { left: 100 }
391
+ });
392
+ `,
393
+ errors: [{ messageId: 'noUnitlessValue', data: { property: 'left', value: '100' } }],
394
+ name: 'should flag unitless values in globalKeyframes',
395
+ },
396
+ // Negative values
397
+ {
398
+ code: `
399
+ import { style } from '@vanilla-extract/css';
400
+ const myStyle = style({
401
+ margin: -10,
402
+ top: -5,
403
+ });
404
+ `,
405
+ errors: [
406
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '-10' } },
407
+ { messageId: 'noUnitlessValue', data: { property: 'top', value: '-5' } },
408
+ ],
409
+ name: 'should flag negative unitless values',
410
+ },
411
+ // String unitless values
412
+ {
413
+ code: `
414
+ import { style } from '@vanilla-extract/css';
415
+ const myStyle = style({
416
+ width: '100',
417
+ margin: '20',
418
+ padding: '10.5',
419
+ });
420
+ `,
421
+ errors: [
422
+ { messageId: 'noUnitlessValue', data: { property: 'width', value: '100' } },
423
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '20' } },
424
+ { messageId: 'noUnitlessValue', data: { property: 'padding', value: '10.5' } },
425
+ ],
426
+ name: 'should flag string unitless values',
427
+ },
428
+ // String negative unitless values
429
+ {
430
+ code: `
431
+ import { style } from '@vanilla-extract/css';
432
+ const myStyle = style({
433
+ margin: '-10',
434
+ top: '-5.5',
435
+ });
436
+ `,
437
+ errors: [
438
+ { messageId: 'noUnitlessValue', data: { property: 'margin', value: '-10' } },
439
+ { messageId: 'noUnitlessValue', data: { property: 'top', value: '-5.5' } },
440
+ ],
441
+ name: 'should flag string negative unitless values',
442
+ },
443
+ ],
444
+ });
@@ -0,0 +1,2 @@
1
+ import noUnitlessValuesRule from './rule-definition.js';
2
+ export default noUnitlessValuesRule;
@@ -0,0 +1,2 @@
1
+ import noUnitlessValuesRule from './rule-definition.js';
2
+ export default noUnitlessValuesRule;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ declare const noUnitlessValuesRule: Rule.RuleModule;
3
+ export default noUnitlessValuesRule;
@@ -0,0 +1,35 @@
1
+ import { createUnitlessValueVisitors } from './unitless-value-visitor-creator.js';
2
+ const noUnitlessValuesRule = {
3
+ meta: {
4
+ type: 'suggestion',
5
+ docs: {
6
+ description: 'disallow unitless numeric values for CSS properties that require units',
7
+ category: 'Stylistic Issues',
8
+ recommended: false,
9
+ },
10
+ schema: [
11
+ {
12
+ type: 'object',
13
+ properties: {
14
+ allow: {
15
+ type: 'array',
16
+ items: {
17
+ type: 'string',
18
+ },
19
+ uniqueItems: true,
20
+ default: [],
21
+ },
22
+ },
23
+ additionalProperties: false,
24
+ },
25
+ ],
26
+ messages: {
27
+ noUnitlessValue: 'Property "{{ property }}" has unitless value {{ value }}. Add an explicit unit (e.g., "{{ value }}px", "{{ value }}rem").',
28
+ },
29
+ },
30
+ create(context) {
31
+ const options = context.options[0] || {};
32
+ return createUnitlessValueVisitors(context, options);
33
+ },
34
+ };
35
+ export default noUnitlessValuesRule;
@@ -0,0 +1,13 @@
1
+ import type { Rule } from 'eslint';
2
+ import { TSESTree } from '@typescript-eslint/utils';
3
+ export interface NoUnitlessValuesOptions {
4
+ allow?: string[];
5
+ }
6
+ /**
7
+ * Recursively processes a style object, reporting instances of unitless numeric values for properties that require units.
8
+ *
9
+ * @param ruleContext The ESLint rule context.
10
+ * @param node The ObjectExpression node representing the style object to be processed.
11
+ * @param options Rule options including allow list.
12
+ */
13
+ export declare const processUnitlessValueInStyleObject: (ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, options?: NoUnitlessValuesOptions) => void;
@@ -0,0 +1,330 @@
1
+ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * CSS properties that require units for length/dimension values.
4
+ * These properties should not have unitless numeric values (except 0).
5
+ */
6
+ const PROPERTIES_REQUIRING_UNITS = new Set([
7
+ // Box model
8
+ 'width',
9
+ 'height',
10
+ 'minWidth',
11
+ 'maxWidth',
12
+ 'minHeight',
13
+ 'maxHeight',
14
+ 'min-width',
15
+ 'max-width',
16
+ 'min-height',
17
+ 'max-height',
18
+ // Spacing
19
+ 'margin',
20
+ 'marginTop',
21
+ 'marginRight',
22
+ 'marginBottom',
23
+ 'marginLeft',
24
+ 'marginBlock',
25
+ 'marginBlockStart',
26
+ 'marginBlockEnd',
27
+ 'marginInline',
28
+ 'marginInlineStart',
29
+ 'marginInlineEnd',
30
+ 'margin-top',
31
+ 'margin-right',
32
+ 'margin-bottom',
33
+ 'margin-left',
34
+ 'margin-block',
35
+ 'margin-block-start',
36
+ 'margin-block-end',
37
+ 'margin-inline',
38
+ 'margin-inline-start',
39
+ 'margin-inline-end',
40
+ 'padding',
41
+ 'paddingTop',
42
+ 'paddingRight',
43
+ 'paddingBottom',
44
+ 'paddingLeft',
45
+ 'paddingBlock',
46
+ 'paddingBlockStart',
47
+ 'paddingBlockEnd',
48
+ 'paddingInline',
49
+ 'paddingInlineStart',
50
+ 'paddingInlineEnd',
51
+ 'padding-top',
52
+ 'padding-right',
53
+ 'padding-bottom',
54
+ 'padding-left',
55
+ 'padding-block',
56
+ 'padding-block-start',
57
+ 'padding-block-end',
58
+ 'padding-inline',
59
+ 'padding-inline-start',
60
+ 'padding-inline-end',
61
+ // Positioning
62
+ 'top',
63
+ 'right',
64
+ 'bottom',
65
+ 'left',
66
+ 'inset',
67
+ 'insetBlock',
68
+ 'insetBlockStart',
69
+ 'insetBlockEnd',
70
+ 'insetInline',
71
+ 'insetInlineStart',
72
+ 'insetInlineEnd',
73
+ 'inset-block',
74
+ 'inset-block-start',
75
+ 'inset-block-end',
76
+ 'inset-inline',
77
+ 'inset-inline-start',
78
+ 'inset-inline-end',
79
+ // Border
80
+ 'borderWidth',
81
+ 'borderTopWidth',
82
+ 'borderRightWidth',
83
+ 'borderBottomWidth',
84
+ 'borderLeftWidth',
85
+ 'borderBlockWidth',
86
+ 'borderBlockStartWidth',
87
+ 'borderBlockEndWidth',
88
+ 'borderInlineWidth',
89
+ 'borderInlineStartWidth',
90
+ 'borderInlineEndWidth',
91
+ 'border-width',
92
+ 'border-top-width',
93
+ 'border-right-width',
94
+ 'border-bottom-width',
95
+ 'border-left-width',
96
+ 'border-block-width',
97
+ 'border-block-start-width',
98
+ 'border-block-end-width',
99
+ 'border-inline-width',
100
+ 'border-inline-start-width',
101
+ 'border-inline-end-width',
102
+ 'borderRadius',
103
+ 'borderTopLeftRadius',
104
+ 'borderTopRightRadius',
105
+ 'borderBottomLeftRadius',
106
+ 'borderBottomRightRadius',
107
+ 'borderStartStartRadius',
108
+ 'borderStartEndRadius',
109
+ 'borderEndStartRadius',
110
+ 'borderEndEndRadius',
111
+ 'border-radius',
112
+ 'border-top-left-radius',
113
+ 'border-top-right-radius',
114
+ 'border-bottom-left-radius',
115
+ 'border-bottom-right-radius',
116
+ 'border-start-start-radius',
117
+ 'border-start-end-radius',
118
+ 'border-end-start-radius',
119
+ 'border-end-end-radius',
120
+ // Typography
121
+ 'fontSize',
122
+ 'font-size',
123
+ 'letterSpacing',
124
+ 'letter-spacing',
125
+ 'wordSpacing',
126
+ 'word-spacing',
127
+ 'textIndent',
128
+ 'text-indent',
129
+ // Flexbox/Grid
130
+ 'gap',
131
+ 'rowGap',
132
+ 'columnGap',
133
+ 'row-gap',
134
+ 'column-gap',
135
+ 'flexBasis',
136
+ 'flex-basis',
137
+ // Outline
138
+ 'outlineWidth',
139
+ 'outline-width',
140
+ 'outlineOffset',
141
+ 'outline-offset',
142
+ // Other
143
+ 'blockSize',
144
+ 'inlineSize',
145
+ 'minBlockSize',
146
+ 'maxBlockSize',
147
+ 'minInlineSize',
148
+ 'maxInlineSize',
149
+ 'block-size',
150
+ 'inline-size',
151
+ 'min-block-size',
152
+ 'max-block-size',
153
+ 'min-inline-size',
154
+ 'max-inline-size',
155
+ ]);
156
+ /**
157
+ * CSS properties that accept unitless numeric values.
158
+ * These properties should NOT be flagged when they have numeric values.
159
+ */
160
+ const UNITLESS_VALID_PROPERTIES = new Set([
161
+ 'opacity',
162
+ 'zIndex',
163
+ 'z-index',
164
+ 'lineHeight',
165
+ 'line-height',
166
+ 'flexGrow',
167
+ 'flex-grow',
168
+ 'flexShrink',
169
+ 'flex-shrink',
170
+ 'order',
171
+ 'fontWeight',
172
+ 'font-weight',
173
+ 'zoom',
174
+ 'animationIterationCount',
175
+ 'animation-iteration-count',
176
+ 'columnCount',
177
+ 'column-count',
178
+ 'gridColumn',
179
+ 'grid-column',
180
+ 'gridColumnEnd',
181
+ 'grid-column-end',
182
+ 'gridColumnStart',
183
+ 'grid-column-start',
184
+ 'gridRow',
185
+ 'grid-row',
186
+ 'gridRowEnd',
187
+ 'grid-row-end',
188
+ 'gridRowStart',
189
+ 'grid-row-start',
190
+ 'orphans',
191
+ 'widows',
192
+ 'fillOpacity',
193
+ 'fill-opacity',
194
+ 'strokeOpacity',
195
+ 'stroke-opacity',
196
+ 'strokeMiterlimit',
197
+ 'stroke-miterlimit',
198
+ ]);
199
+ /**
200
+ * Checks if a property name requires units for numeric values.
201
+ */
202
+ const requiresUnits = (propertyName, allow = []) => {
203
+ if (allow.includes(propertyName)) {
204
+ return false;
205
+ }
206
+ if (UNITLESS_VALID_PROPERTIES.has(propertyName)) {
207
+ return false;
208
+ }
209
+ return PROPERTIES_REQUIRING_UNITS.has(propertyName);
210
+ };
211
+ /**
212
+ * Gets the property name from a Property node.
213
+ */
214
+ const getPropertyName = (property) => {
215
+ if (property.key.type === AST_NODE_TYPES.Identifier) {
216
+ return property.key.name;
217
+ }
218
+ if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
219
+ return property.key.value;
220
+ }
221
+ return null;
222
+ };
223
+ /**
224
+ * Recursively processes a style object, reporting instances of unitless numeric values for properties that require units.
225
+ *
226
+ * @param ruleContext The ESLint rule context.
227
+ * @param node The ObjectExpression node representing the style object to be processed.
228
+ * @param options Rule options including allow list.
229
+ */
230
+ export const processUnitlessValueInStyleObject = (ruleContext, node, options = {}) => {
231
+ const allow = options.allow || [];
232
+ node.properties.forEach((property) => {
233
+ if (property.type !== AST_NODE_TYPES.Property) {
234
+ return;
235
+ }
236
+ const propertyName = getPropertyName(property);
237
+ if (!propertyName) {
238
+ return;
239
+ }
240
+ // Skip special nested structures like @media, selectors, etc.
241
+ // These will be processed recursively
242
+ if (propertyName.startsWith('@') || propertyName.startsWith(':') || propertyName === 'selectors') {
243
+ if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
244
+ if (propertyName === '@media' || propertyName === 'selectors') {
245
+ property.value.properties.forEach((nestedProperty) => {
246
+ if (nestedProperty.type === AST_NODE_TYPES.Property &&
247
+ nestedProperty.value.type === AST_NODE_TYPES.ObjectExpression) {
248
+ processUnitlessValueInStyleObject(ruleContext, nestedProperty.value, options);
249
+ }
250
+ });
251
+ }
252
+ else {
253
+ // For pseudo-selectors and other nested objects, process directly
254
+ processUnitlessValueInStyleObject(ruleContext, property.value, options);
255
+ }
256
+ }
257
+ return;
258
+ }
259
+ // Check if this property requires units
260
+ if (!requiresUnits(propertyName, allow)) {
261
+ // Still need to process nested objects for non-CSS properties
262
+ if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
263
+ processUnitlessValueInStyleObject(ruleContext, property.value, options);
264
+ }
265
+ return;
266
+ }
267
+ // Check for unitless numeric literal values (e.g., width: 100)
268
+ if (property.value.type === AST_NODE_TYPES.Literal && typeof property.value.value === 'number') {
269
+ // Allow 0 without units (valid CSS), including -0
270
+ if (property.value.value === 0 || property.value.value === -0) {
271
+ return;
272
+ }
273
+ // Report unitless numeric value
274
+ ruleContext.report({
275
+ node: property.value,
276
+ messageId: 'noUnitlessValue',
277
+ data: {
278
+ property: propertyName,
279
+ value: String(property.value.value),
280
+ },
281
+ });
282
+ }
283
+ // Check for string literals that are unitless numbers (e.g., width: '100')
284
+ if (property.value.type === AST_NODE_TYPES.Literal && typeof property.value.value === 'string') {
285
+ const stringValue = property.value.value.trim();
286
+ // Check if the string is a pure number (with optional negative sign and decimals)
287
+ // This regex matches: -10, 10, 10.5, -10.5, but not 10px, 10rem, etc.
288
+ const unitlessNumberRegex = /^-?\d+(\.\d+)?$/;
289
+ if (unitlessNumberRegex.test(stringValue)) {
290
+ // Allow '0' and '-0' without units
291
+ const numValue = parseFloat(stringValue);
292
+ if (numValue === 0 || numValue === -0) {
293
+ return;
294
+ }
295
+ // Report unitless string numeric value
296
+ ruleContext.report({
297
+ node: property.value,
298
+ messageId: 'noUnitlessValue',
299
+ data: {
300
+ property: propertyName,
301
+ value: stringValue,
302
+ },
303
+ });
304
+ }
305
+ }
306
+ // Check for unary expressions (e.g., -10)
307
+ if (property.value.type === AST_NODE_TYPES.UnaryExpression && property.value.operator === '-') {
308
+ if (property.value.argument.type === AST_NODE_TYPES.Literal &&
309
+ typeof property.value.argument.value === 'number') {
310
+ // Allow -0 without units
311
+ if (property.value.argument.value === 0) {
312
+ return;
313
+ }
314
+ // Report unitless numeric value
315
+ ruleContext.report({
316
+ node: property.value,
317
+ messageId: 'noUnitlessValue',
318
+ data: {
319
+ property: propertyName,
320
+ value: `-${property.value.argument.value}`,
321
+ },
322
+ });
323
+ }
324
+ }
325
+ // Process nested objects (for complex selectors, etc.)
326
+ if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
327
+ processUnitlessValueInStyleObject(ruleContext, property.value, options);
328
+ }
329
+ });
330
+ };
@@ -0,0 +1,11 @@
1
+ import type { Rule } from 'eslint';
2
+ import type { NoUnitlessValuesOptions } from './unitless-value-processor.js';
3
+ /**
4
+ * Creates ESLint rule visitors for detecting unitless numeric values in style-related function calls.
5
+ * Uses reference tracking to automatically detect vanilla-extract functions based on their import statements.
6
+ *
7
+ * @param context The ESLint rule context.
8
+ * @param options Rule options including allowed property names.
9
+ * @returns An object with visitor functions for the ESLint rule.
10
+ */
11
+ export declare const createUnitlessValueVisitors: (context: Rule.RuleContext, options?: NoUnitlessValuesOptions) => Rule.RuleListener;
@@ -0,0 +1,68 @@
1
+ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
+ import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
3
+ import { ReferenceTracker, createReferenceTrackingVisitor } from '../shared-utils/reference-tracker.js';
4
+ import { processStyleNode } from '../shared-utils/style-node-processor.js';
5
+ import { processUnitlessValueInStyleObject } from './unitless-value-processor.js';
6
+ /**
7
+ * Creates ESLint rule visitors for detecting unitless numeric values in style-related function calls.
8
+ * Uses reference tracking to automatically detect vanilla-extract functions based on their import statements.
9
+ *
10
+ * @param context The ESLint rule context.
11
+ * @param options Rule options including allowed property names.
12
+ * @returns An object with visitor functions for the ESLint rule.
13
+ */
14
+ export const createUnitlessValueVisitors = (context, options = {}) => {
15
+ const tracker = new ReferenceTracker();
16
+ const trackingVisitor = createReferenceTrackingVisitor(tracker);
17
+ const processWithOptions = (ruleContext, node) => {
18
+ processUnitlessValueInStyleObject(ruleContext, node, options);
19
+ };
20
+ return {
21
+ ...trackingVisitor,
22
+ CallExpression(node) {
23
+ if (node.callee.type !== AST_NODE_TYPES.Identifier) {
24
+ return;
25
+ }
26
+ const functionName = node.callee.name;
27
+ // Check if this function is tracked as a vanilla-extract function
28
+ if (!tracker.isTrackedFunction(functionName)) {
29
+ return;
30
+ }
31
+ const originalName = tracker.getOriginalName(functionName);
32
+ if (!originalName) {
33
+ return;
34
+ }
35
+ // Handle different function types based on their original imported name
36
+ switch (originalName) {
37
+ case 'fontFace':
38
+ if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
39
+ processWithOptions(context, node.arguments[0]);
40
+ }
41
+ break;
42
+ case 'globalFontFace':
43
+ if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
44
+ processWithOptions(context, node.arguments[1]);
45
+ }
46
+ break;
47
+ case 'style':
48
+ case 'styleVariants':
49
+ case 'keyframes':
50
+ if (node.arguments.length > 0) {
51
+ processStyleNode(context, node.arguments[0], processWithOptions);
52
+ }
53
+ break;
54
+ case 'globalStyle':
55
+ case 'globalKeyframes':
56
+ if (node.arguments.length >= 2) {
57
+ processStyleNode(context, node.arguments[1], processWithOptions);
58
+ }
59
+ break;
60
+ case 'recipe':
61
+ if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
62
+ processRecipeProperties(context, node.arguments[0], processWithOptions);
63
+ }
64
+ break;
65
+ }
66
+ },
67
+ };
68
+ };
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ declare const vanillaExtract: {
10
10
  'no-empty-style-blocks': import("eslint").Rule.RuleModule;
11
11
  'no-px-unit': import("eslint").Rule.RuleModule;
12
12
  'no-trailing-zero': import("eslint").Rule.RuleModule;
13
+ 'no-unitless-values': import("eslint").Rule.RuleModule;
13
14
  'no-unknown-unit': import("eslint").Rule.RuleModule;
14
15
  'no-zero-unit': import("eslint").Rule.RuleModule;
15
16
  'prefer-logical-properties': import("eslint").Rule.RuleModule;
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import customOrderRule from './css-rules/custom-order/rule-definition.js';
4
4
  import noEmptyStyleBlocksRule from './css-rules/no-empty-blocks/rule-definition.js';
5
5
  import noPxUnitRule from './css-rules/no-px-unit/index.js';
6
6
  import noTrailingZeroRule from './css-rules/no-trailing-zero/rule-definition.js';
7
+ import noUnitlessValuesRule from './css-rules/no-unitless-values/rule-definition.js';
7
8
  import noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
8
9
  import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
9
10
  import preferLogicalPropertiesRule from './css-rules/prefer-logical-properties/index.js';
@@ -11,7 +12,7 @@ import preferThemeTokensRule from './css-rules/prefer-theme-tokens/index.js';
11
12
  const vanillaExtract = {
12
13
  meta: {
13
14
  name: '@antebudimir/eslint-plugin-vanilla-extract',
14
- version: '1.15.1',
15
+ version: '1.16.0',
15
16
  },
16
17
  rules: {
17
18
  'alphabetical-order': alphabeticalOrderRule,
@@ -20,6 +21,7 @@ const vanillaExtract = {
20
21
  'no-empty-style-blocks': noEmptyStyleBlocksRule,
21
22
  'no-px-unit': noPxUnitRule,
22
23
  'no-trailing-zero': noTrailingZeroRule,
24
+ 'no-unitless-values': noUnitlessValuesRule,
23
25
  'no-unknown-unit': noUnknownUnitRule,
24
26
  'no-zero-unit': noZeroUnitRule,
25
27
  'prefer-logical-properties': preferLogicalPropertiesRule,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.15.1",
3
+ "version": "1.16.0",
4
4
  "description": "Comprehensive ESLint plugin for vanilla-extract with CSS property ordering, style validation, and best practices enforcement. Supports alphabetical, concentric and custom CSS ordering, auto-fixing, and zero-runtime safety.",
5
5
  "author": "Ante Budimir",
6
6
  "license": "MIT",