@antebudimir/eslint-plugin-vanilla-extract 1.15.0 → 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.
Files changed (39) hide show
  1. package/README.md +73 -4
  2. package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +8 -2
  3. package/dist/css-rules/no-empty-blocks/recipe-processor.js +23 -3
  4. package/dist/css-rules/no-trailing-zero/index.d.ts +2 -1
  5. package/dist/css-rules/no-trailing-zero/index.js +2 -1
  6. package/dist/css-rules/no-unitless-values/_tests_/no-unitless-values.test.d.ts +1 -0
  7. package/dist/css-rules/no-unitless-values/_tests_/no-unitless-values.test.js +444 -0
  8. package/dist/css-rules/no-unitless-values/index.d.ts +2 -0
  9. package/dist/css-rules/no-unitless-values/index.js +2 -0
  10. package/dist/css-rules/no-unitless-values/rule-definition.d.ts +3 -0
  11. package/dist/css-rules/no-unitless-values/rule-definition.js +35 -0
  12. package/dist/css-rules/no-unitless-values/unitless-value-processor.d.ts +13 -0
  13. package/dist/css-rules/no-unitless-values/unitless-value-processor.js +330 -0
  14. package/dist/css-rules/no-unitless-values/unitless-value-visitor-creator.d.ts +11 -0
  15. package/dist/css-rules/{no-px-unit/visitor-creator.js → no-unitless-values/unitless-value-visitor-creator.js} +26 -13
  16. package/dist/css-rules/shared-utils/empty-object-processor.d.ts +5 -0
  17. package/dist/css-rules/shared-utils/empty-object-processor.js +11 -0
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.js +3 -1
  20. package/package.json +2 -1
  21. package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.d.ts +0 -2
  22. package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.js +0 -55
  23. package/dist/css-rules/no-px-unit/processor.d.ts +0 -3
  24. package/dist/css-rules/no-px-unit/processor.js +0 -93
  25. package/dist/css-rules/no-px-unit/visitor-creator.d.ts +0 -2
  26. package/dist/demo-bad-example.css.d.ts +0 -4
  27. package/dist/demo-bad-example.css.js +0 -45
  28. package/dist/demo-good-example.css.d.ts +0 -5
  29. package/dist/demo-good-example.css.js +0 -48
  30. package/dist/demo-theme.css.d.ts +0 -37
  31. package/dist/demo-theme.css.js +0 -39
  32. package/dist/sample-bad.css.d.ts +0 -26
  33. package/dist/sample-bad.css.js +0 -85
  34. package/dist/sample-good.css.d.ts +0 -27
  35. package/dist/sample-good.css.js +0 -94
  36. package/dist/sample-theme.css.d.ts +0 -37
  37. package/dist/sample-theme.css.js +0 -38
  38. package/dist/sample.css.d.ts +0 -75
  39. package/dist/sample.css.js +0 -530
@@ -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;
@@ -2,51 +2,64 @@ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
2
  import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
3
3
  import { ReferenceTracker, createReferenceTrackingVisitor } from '../shared-utils/reference-tracker.js';
4
4
  import { processStyleNode } from '../shared-utils/style-node-processor.js';
5
- import { processNoPxUnitInStyleObject } from './processor.js';
6
- export const createNoPxUnitVisitors = (context) => {
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 = {}) => {
7
15
  const tracker = new ReferenceTracker();
8
16
  const trackingVisitor = createReferenceTrackingVisitor(tracker);
9
- const options = context.options?.[0] || {};
10
- const allowSet = new Set((options.allow ?? []).map((s) => s));
11
- const process = (ctx, obj) => processNoPxUnitInStyleObject(ctx, obj, allowSet);
17
+ const processWithOptions = (ruleContext, node) => {
18
+ processUnitlessValueInStyleObject(ruleContext, node, options);
19
+ };
12
20
  return {
13
21
  ...trackingVisitor,
14
22
  CallExpression(node) {
15
- if (node.callee.type !== AST_NODE_TYPES.Identifier)
23
+ if (node.callee.type !== AST_NODE_TYPES.Identifier) {
16
24
  return;
25
+ }
17
26
  const functionName = node.callee.name;
18
- if (!tracker.isTrackedFunction(functionName))
27
+ // Check if this function is tracked as a vanilla-extract function
28
+ if (!tracker.isTrackedFunction(functionName)) {
19
29
  return;
30
+ }
20
31
  const originalName = tracker.getOriginalName(functionName);
21
- if (!originalName)
32
+ if (!originalName) {
22
33
  return;
34
+ }
35
+ // Handle different function types based on their original imported name
23
36
  switch (originalName) {
24
37
  case 'fontFace':
25
38
  if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
26
- process(context, node.arguments[0]);
39
+ processWithOptions(context, node.arguments[0]);
27
40
  }
28
41
  break;
29
42
  case 'globalFontFace':
30
43
  if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
31
- process(context, node.arguments[1]);
44
+ processWithOptions(context, node.arguments[1]);
32
45
  }
33
46
  break;
34
47
  case 'style':
35
48
  case 'styleVariants':
36
49
  case 'keyframes':
37
50
  if (node.arguments.length > 0) {
38
- processStyleNode(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
51
+ processStyleNode(context, node.arguments[0], processWithOptions);
39
52
  }
40
53
  break;
41
54
  case 'globalStyle':
42
55
  case 'globalKeyframes':
43
56
  if (node.arguments.length >= 2) {
44
- processStyleNode(context, node.arguments[1], (ctx, obj) => process(ctx, obj));
57
+ processStyleNode(context, node.arguments[1], processWithOptions);
45
58
  }
46
59
  break;
47
60
  case 'recipe':
48
61
  if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
49
- processRecipeProperties(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
62
+ processRecipeProperties(context, node.arguments[0], processWithOptions);
50
63
  }
51
64
  break;
52
65
  }
@@ -9,3 +9,8 @@ export declare const isObjectExpression: (node: TSESTree.Node) => node is TSESTr
9
9
  * @returns True if the node is an ObjectExpression with no properties.
10
10
  */
11
11
  export declare const isEmptyObject: (node: TSESTree.Node) => boolean;
12
+ /**
13
+ * Checks if a CallExpression has an empty object as its first argument.
14
+ * Examples: sprinkles({}), style({}), recipe({})
15
+ */
16
+ export declare const isCallExpressionWithEmptyObject: (node: TSESTree.CallExpression) => boolean;
@@ -13,3 +13,14 @@ export const isObjectExpression = (node) => {
13
13
  export const isEmptyObject = (node) => {
14
14
  return isObjectExpression(node) && node.properties.length === 0;
15
15
  };
16
+ /**
17
+ * Checks if a CallExpression has an empty object as its first argument.
18
+ * Examples: sprinkles({}), style({}), recipe({})
19
+ */
20
+ export const isCallExpressionWithEmptyObject = (node) => {
21
+ if (node.arguments.length === 0) {
22
+ return false;
23
+ }
24
+ const firstArgument = node.arguments[0];
25
+ return firstArgument?.type === 'ObjectExpression' && isEmptyObject(firstArgument);
26
+ };
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.0',
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.0",
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",
@@ -60,6 +60,7 @@
60
60
  "@typescript-eslint/utils": "^8.26.1",
61
61
  "@vanilla-extract/css": "^1.17.1",
62
62
  "@vanilla-extract/recipes": "^0.5.5",
63
+ "@vanilla-extract/sprinkles": "^1.6.0",
63
64
  "@vitest/coverage-v8": "3.0.8",
64
65
  "eslint": "^9.22.0",
65
66
  "eslint-config-prettier": "^10.1.1",
@@ -1,2 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- export declare const createNoPxUnitVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
@@ -1,55 +0,0 @@
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 { processNoPxUnitInStyleObject } from './processor.js';
6
- export const createNoPxUnitVisitors = (context) => {
7
- const tracker = new ReferenceTracker();
8
- const trackingVisitor = createReferenceTrackingVisitor(tracker);
9
- const options = context.options?.[0] || {};
10
- const allowSet = new Set((options.allow ?? []).map((s) => s));
11
- const process = (ctx, obj) => processNoPxUnitInStyleObject(ctx, obj, allowSet);
12
- return {
13
- ...trackingVisitor,
14
- CallExpression(node) {
15
- if (node.callee.type !== AST_NODE_TYPES.Identifier)
16
- return;
17
- const functionName = node.callee.name;
18
- if (!tracker.isTrackedFunction(functionName))
19
- return;
20
- const originalName = tracker.getOriginalName(functionName);
21
- if (!originalName)
22
- return;
23
- switch (originalName) {
24
- case 'fontFace':
25
- if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
26
- process(context, node.arguments[0]);
27
- }
28
- break;
29
- case 'globalFontFace':
30
- if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
31
- process(context, node.arguments[1]);
32
- }
33
- break;
34
- case 'style':
35
- case 'styleVariants':
36
- case 'keyframes':
37
- if (node.arguments.length > 0) {
38
- processStyleNode(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
39
- }
40
- break;
41
- case 'globalStyle':
42
- case 'globalKeyframes':
43
- if (node.arguments.length >= 2) {
44
- processStyleNode(context, node.arguments[1], (ctx, obj) => process(ctx, obj));
45
- }
46
- break;
47
- case 'recipe':
48
- if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
49
- processRecipeProperties(context, node.arguments[0], (ctx, obj) => process(ctx, obj));
50
- }
51
- break;
52
- }
53
- },
54
- };
55
- };
@@ -1,3 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- import { TSESTree } from '@typescript-eslint/utils';
3
- export declare const processNoPxUnitInStyleObject: (context: Rule.RuleContext, node: TSESTree.ObjectExpression, allowSet: Set<string>) => void;
@@ -1,93 +0,0 @@
1
- import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
- const containsPx = (text) => /(^|\W)-?\d*\.?\d*px(?![a-zA-Z])/i.test(text);
3
- const replacePxWith = (text, replacement) => text.replace(/px(?![a-zA-Z])/g, replacement);
4
- const toKebab = (name) => name.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
5
- const getValueText = (node) => {
6
- if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string') {
7
- return node.value;
8
- }
9
- if (node.type === AST_NODE_TYPES.TemplateLiteral) {
10
- // Join all quasis (ignore expressions content)
11
- const raw = node.quasis.map((q) => q.value.raw ?? '').join('');
12
- return raw;
13
- }
14
- return null;
15
- };
16
- const canSuggestFix = (node) => {
17
- if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string')
18
- return 'literal';
19
- if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0)
20
- return 'simple-template';
21
- return null;
22
- };
23
- export const processNoPxUnitInStyleObject = (context, node, allowSet) => {
24
- for (const property of node.properties) {
25
- if (property.type !== AST_NODE_TYPES.Property)
26
- continue;
27
- // Determine property name when possible
28
- let propertyName = null;
29
- if (property.key.type === AST_NODE_TYPES.Identifier) {
30
- propertyName = property.key.name;
31
- }
32
- else if (property.key.type === AST_NODE_TYPES.Literal && typeof property.key.value === 'string') {
33
- propertyName = property.key.value;
34
- }
35
- // Recurse into known nested containers
36
- if (propertyName === '@media' || propertyName === 'selectors') {
37
- if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
38
- for (const nested of property.value.properties) {
39
- if (nested.type === AST_NODE_TYPES.Property && nested.value.type === AST_NODE_TYPES.ObjectExpression) {
40
- processNoPxUnitInStyleObject(context, nested.value, allowSet);
41
- }
42
- }
43
- }
44
- continue;
45
- }
46
- // Traverse any nested object
47
- if (property.value.type === AST_NODE_TYPES.ObjectExpression) {
48
- processNoPxUnitInStyleObject(context, property.value, allowSet);
49
- continue;
50
- }
51
- // Skip if property is whitelisted (supports both camelCase and kebab-case)
52
- if (propertyName) {
53
- const kebab = toKebab(propertyName);
54
- if (allowSet.has(propertyName) || allowSet.has(kebab)) {
55
- continue;
56
- }
57
- }
58
- // Check string or template literal values
59
- const text = getValueText(property.value);
60
- if (text && containsPx(text)) {
61
- const fixability = canSuggestFix(property.value);
62
- context.report({
63
- node: property.value,
64
- messageId: 'noPxUnit',
65
- suggest: fixability
66
- ? [
67
- {
68
- messageId: 'removePx',
69
- fix: (fixer) => {
70
- const newText = replacePxWith(text, '');
71
- if (fixability === 'literal') {
72
- return fixer.replaceText(property.value, `'${newText}'`);
73
- }
74
- // simple template with no expressions
75
- return fixer.replaceText(property.value, `\`${newText}\``);
76
- },
77
- },
78
- {
79
- messageId: 'replaceWithRem',
80
- fix: (fixer) => {
81
- const newText = replacePxWith(text, 'rem');
82
- if (fixability === 'literal') {
83
- return fixer.replaceText(property.value, `'${newText}'`);
84
- }
85
- return fixer.replaceText(property.value, `\`${newText}\``);
86
- },
87
- },
88
- ]
89
- : undefined,
90
- });
91
- }
92
- }
93
- };
@@ -1,2 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- export declare const createNoPxUnitVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
@@ -1,4 +0,0 @@
1
- export declare const button: string;
2
- export declare const card: string;
3
- export declare const text: string;
4
- export declare const container: string;