@antebudimir/eslint-plugin-vanilla-extract 1.11.1 → 1.13.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 (30) hide show
  1. package/README.md +84 -14
  2. package/dist/css-rules/no-px-unit/_tests_/no-px-unit.test.d.ts +1 -0
  3. package/dist/css-rules/no-px-unit/_tests_/no-px-unit.test.js +163 -0
  4. package/dist/css-rules/no-px-unit/index.d.ts +2 -0
  5. package/dist/css-rules/no-px-unit/index.js +2 -0
  6. package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.d.ts +2 -0
  7. package/dist/css-rules/no-px-unit/no-px-unit-visitor-creator.js +55 -0
  8. package/dist/css-rules/no-px-unit/processor.d.ts +3 -0
  9. package/dist/css-rules/no-px-unit/processor.js +93 -0
  10. package/dist/css-rules/no-px-unit/px-unit-processor.d.ts +14 -0
  11. package/dist/css-rules/no-px-unit/px-unit-processor.js +104 -0
  12. package/dist/css-rules/no-px-unit/px-unit-visitor-creator.d.ts +11 -0
  13. package/dist/css-rules/no-px-unit/px-unit-visitor-creator.js +64 -0
  14. package/dist/css-rules/no-px-unit/rule-definition.d.ts +3 -0
  15. package/dist/css-rules/no-px-unit/rule-definition.js +37 -0
  16. package/dist/css-rules/no-px-unit/visitor-creator.d.ts +2 -0
  17. package/dist/css-rules/no-px-unit/visitor-creator.js +55 -0
  18. package/dist/css-rules/no-trailing-zero/_tests_/no-trailing-zero.test.d.ts +1 -0
  19. package/dist/css-rules/no-trailing-zero/_tests_/no-trailing-zero.test.js +537 -0
  20. package/dist/css-rules/no-trailing-zero/index.d.ts +1 -0
  21. package/dist/css-rules/no-trailing-zero/index.js +1 -0
  22. package/dist/css-rules/no-trailing-zero/rule-definition.d.ts +3 -0
  23. package/dist/css-rules/no-trailing-zero/rule-definition.js +20 -0
  24. package/dist/css-rules/no-trailing-zero/trailing-zero-processor.d.ts +31 -0
  25. package/dist/css-rules/no-trailing-zero/trailing-zero-processor.js +165 -0
  26. package/dist/css-rules/no-trailing-zero/trailing-zero-visitor-creator.d.ts +9 -0
  27. package/dist/css-rules/no-trailing-zero/trailing-zero-visitor-creator.js +85 -0
  28. package/dist/index.d.ts +2 -0
  29. package/dist/index.js +6 -1
  30. package/package.json +2 -2
@@ -0,0 +1 @@
1
+ export { default } from './rule-definition.js';
@@ -0,0 +1 @@
1
+ export { default } from './rule-definition.js';
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ declare const noTrailingZeroRule: Rule.RuleModule;
3
+ export default noTrailingZeroRule;
@@ -0,0 +1,20 @@
1
+ import { createTrailingZeroVisitors } from './trailing-zero-visitor-creator.js';
2
+ const noTrailingZeroRule = {
3
+ meta: {
4
+ type: 'suggestion',
5
+ docs: {
6
+ description: 'disallow trailing zeros in numeric CSS values',
7
+ category: 'Stylistic Issues',
8
+ recommended: true,
9
+ },
10
+ fixable: 'code',
11
+ schema: [],
12
+ messages: {
13
+ trailingZero: 'Numeric value "{{ value }}" has unnecessary trailing zeros. Use "{{ fixed }}" instead.',
14
+ },
15
+ },
16
+ create(context) {
17
+ return createTrailingZeroVisitors(context);
18
+ },
19
+ };
20
+ export default noTrailingZeroRule;
@@ -0,0 +1,31 @@
1
+ import type { Rule } from 'eslint';
2
+ import { TSESTree } from '@typescript-eslint/utils';
3
+ /**
4
+ * Checks if a value has trailing zeros and returns the fixed value if needed.
5
+ *
6
+ * @param value The string value to check
7
+ * @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
8
+ */
9
+ export declare const checkTrailingZero: (value: string) => {
10
+ hasTrailingZero: boolean;
11
+ fixed: string;
12
+ } | null;
13
+ /**
14
+ * Processes a single string value and checks for trailing zeros in all numeric values.
15
+ * Handles strings with multiple numeric values (e.g., "1.0px 2.50em").
16
+ * Also handles values within function calls (e.g., "rotate(45.0deg)").
17
+ *
18
+ * @param value The string value to process
19
+ * @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
20
+ */
21
+ export declare const processStringValue: (value: string) => {
22
+ hasTrailingZero: boolean;
23
+ fixed: string;
24
+ } | null;
25
+ /**
26
+ * Recursively processes a style object, reporting and fixing instances of trailing zeros in numeric values.
27
+ *
28
+ * @param ruleContext The ESLint rule context.
29
+ * @param node The ObjectExpression node representing the style object to be processed.
30
+ */
31
+ export declare const processTrailingZeroInStyleObject: (ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression) => void;
@@ -0,0 +1,165 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Regex to match numbers with trailing zeros.
4
+ * Matches patterns like:
5
+ * - 1.0, 2.50, 0.0, 0.50
6
+ * - 1.0px, 2.50rem, 0.0em
7
+ * - -1.0, -2.50px
8
+ *
9
+ * Groups:
10
+ * 1: Optional minus sign
11
+ * 2: Integer part
12
+ * 3: Significant fractional digits (optional)
13
+ * 4: Trailing zeros
14
+ * 5: Optional unit
15
+ */
16
+ const TRAILING_ZERO_REGEX = /^(-?)(\d+)\.(\d*[1-9])?(0+)([a-z%]+)?$/i;
17
+ /**
18
+ * Checks if a value has trailing zeros and returns the fixed value if needed.
19
+ *
20
+ * @param value The string value to check
21
+ * @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
22
+ */
23
+ export const checkTrailingZero = (value) => {
24
+ const trimmedValue = value.trim();
25
+ const match = trimmedValue.match(TRAILING_ZERO_REGEX);
26
+ if (!match) {
27
+ return null;
28
+ }
29
+ const [, minus = '', integerPart, significantFractional = '', , unit = ''] = match;
30
+ // Handle special case: 0.0 or 0.00 etc. should become just "0"
31
+ if (integerPart === '0' && !significantFractional) {
32
+ return {
33
+ hasTrailingZero: true,
34
+ fixed: '0',
35
+ };
36
+ }
37
+ // If there's no significant fractional part (e.g., "1.0" -> "1")
38
+ if (!significantFractional) {
39
+ return {
40
+ hasTrailingZero: true,
41
+ fixed: `${minus}${integerPart}${unit}`,
42
+ };
43
+ }
44
+ // If there's a significant fractional part (e.g., "1.50" -> "1.5")
45
+ return {
46
+ hasTrailingZero: true,
47
+ fixed: `${minus}${integerPart}.${significantFractional}${unit}`,
48
+ };
49
+ };
50
+ /**
51
+ * Processes a single string value and checks for trailing zeros in all numeric values.
52
+ * Handles strings with multiple numeric values (e.g., "1.0px 2.50em").
53
+ * Also handles values within function calls (e.g., "rotate(45.0deg)").
54
+ *
55
+ * @param value The string value to process
56
+ * @returns Object with hasTrailingZero flag and fixed value, or null if no trailing zeros
57
+ */
58
+ export const processStringValue = (value) => {
59
+ // First, try to match the entire value
60
+ const directMatch = checkTrailingZero(value);
61
+ if (directMatch?.hasTrailingZero) {
62
+ return directMatch;
63
+ }
64
+ // Split by whitespace to handle multiple values
65
+ const parts = value.split(/(\s+)/);
66
+ let hasAnyTrailingZero = false;
67
+ const fixedParts = parts.map((part) => {
68
+ // Preserve whitespace
69
+ if (/^\s+$/.test(part)) {
70
+ return part;
71
+ }
72
+ // Try to match the whole part first
73
+ const result = checkTrailingZero(part);
74
+ if (result?.hasTrailingZero) {
75
+ hasAnyTrailingZero = true;
76
+ return result.fixed;
77
+ }
78
+ // If no match, try to find and replace numbers within the part (e.g., inside function calls)
79
+ const regex = /(-?\d+)\.(\d*[1-9])?(0+)(?![0-9])([a-z%]+)?/gi;
80
+ const fixedPart = part.replace(regex, (_, integerWithSign, significantFractional, __, unit) => {
81
+ // Reconstruct the number without trailing zeros
82
+ const integerPart = integerWithSign;
83
+ const sig = significantFractional || '';
84
+ const u = unit || '';
85
+ // Handle 0.0 case - if it's zero and no unit, return just '0', otherwise keep the unit
86
+ if (integerPart === '0' && !sig) {
87
+ hasAnyTrailingZero = true;
88
+ return u ? `0${u}` : '0';
89
+ }
90
+ // Handle X.0 case
91
+ if (!sig) {
92
+ hasAnyTrailingZero = true;
93
+ return `${integerPart}${u}`;
94
+ }
95
+ // Handle X.Y0 case
96
+ hasAnyTrailingZero = true;
97
+ return `${integerPart}.${sig}${u}`;
98
+ });
99
+ return fixedPart;
100
+ });
101
+ if (!hasAnyTrailingZero) {
102
+ return null;
103
+ }
104
+ return {
105
+ hasTrailingZero: true,
106
+ fixed: fixedParts.join(''),
107
+ };
108
+ };
109
+ /**
110
+ * Recursively processes a style object, reporting and fixing instances of trailing zeros in numeric values.
111
+ *
112
+ * @param ruleContext The ESLint rule context.
113
+ * @param node The ObjectExpression node representing the style object to be processed.
114
+ */
115
+ export const processTrailingZeroInStyleObject = (ruleContext, node) => {
116
+ node.properties.forEach((property) => {
117
+ if (property.type !== 'Property') {
118
+ return;
119
+ }
120
+ // Process direct string literal values
121
+ if (property.value.type === 'Literal' && typeof property.value.value === 'string') {
122
+ const result = processStringValue(property.value.value);
123
+ if (result?.hasTrailingZero) {
124
+ ruleContext.report({
125
+ node: property.value,
126
+ messageId: 'trailingZero',
127
+ data: {
128
+ value: property.value.value,
129
+ fixed: result.fixed,
130
+ },
131
+ fix: (fixer) => fixer.replaceText(property.value, `'${result.fixed}'`),
132
+ });
133
+ }
134
+ }
135
+ // Process numeric literal values (e.g., margin: 1.0)
136
+ if (property.value.type === 'Literal' && typeof property.value.value === 'number') {
137
+ // Use the raw property to get the original source text (which preserves trailing zeros)
138
+ const rawValue = property.value.raw || property.value.value.toString();
139
+ const result = checkTrailingZero(rawValue);
140
+ if (result?.hasTrailingZero) {
141
+ ruleContext.report({
142
+ node: property.value,
143
+ messageId: 'trailingZero',
144
+ data: {
145
+ value: rawValue,
146
+ fixed: result.fixed,
147
+ },
148
+ fix: (fixer) => fixer.replaceText(property.value, result.fixed),
149
+ });
150
+ }
151
+ }
152
+ // Process nested objects (selectors, media queries, etc.)
153
+ if (property.value.type === 'ObjectExpression') {
154
+ processTrailingZeroInStyleObject(ruleContext, property.value);
155
+ }
156
+ // Process arrays (for styleVariants with array values)
157
+ if (property.value.type === 'ArrayExpression') {
158
+ property.value.elements.forEach((element) => {
159
+ if (element && element.type === 'ObjectExpression') {
160
+ processTrailingZeroInStyleObject(ruleContext, element);
161
+ }
162
+ });
163
+ }
164
+ });
165
+ };
@@ -0,0 +1,9 @@
1
+ import type { Rule } from 'eslint';
2
+ /**
3
+ * Creates ESLint rule visitors for detecting and processing trailing zeros in numeric CSS values.
4
+ * Uses reference tracking to automatically detect vanilla-extract functions based on their import statements.
5
+ *
6
+ * @param context The ESLint rule context.
7
+ * @returns An object with visitor functions for the ESLint rule.
8
+ */
9
+ export declare const createTrailingZeroVisitors: (context: Rule.RuleContext) => Rule.RuleListener;
@@ -0,0 +1,85 @@
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 { processTrailingZeroInStyleObject } from './trailing-zero-processor.js';
6
+ /**
7
+ * Creates ESLint rule visitors for detecting and processing trailing zeros in numeric CSS values.
8
+ * Uses reference tracking to automatically detect vanilla-extract functions based on their import statements.
9
+ *
10
+ * @param context The ESLint rule context.
11
+ * @returns An object with visitor functions for the ESLint rule.
12
+ */
13
+ export const createTrailingZeroVisitors = (context) => {
14
+ const tracker = new ReferenceTracker();
15
+ const trackingVisitor = createReferenceTrackingVisitor(tracker);
16
+ return {
17
+ // Include the reference tracking visitors
18
+ ...trackingVisitor,
19
+ CallExpression(node) {
20
+ if (node.callee.type !== AST_NODE_TYPES.Identifier) {
21
+ return;
22
+ }
23
+ const functionName = node.callee.name;
24
+ // Check if this function is tracked as a vanilla-extract function
25
+ if (!tracker.isTrackedFunction(functionName)) {
26
+ return;
27
+ }
28
+ const originalName = tracker.getOriginalName(functionName);
29
+ if (!originalName) {
30
+ return;
31
+ }
32
+ // Handle different function types based on their original imported name
33
+ switch (originalName) {
34
+ case 'fontFace':
35
+ if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
36
+ processTrailingZeroInStyleObject(context, node.arguments[0]);
37
+ }
38
+ break;
39
+ case 'globalFontFace':
40
+ if (node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
41
+ processTrailingZeroInStyleObject(context, node.arguments[1]);
42
+ }
43
+ break;
44
+ case 'style':
45
+ if (node.arguments.length > 0) {
46
+ processStyleNode(context, node.arguments[0], processTrailingZeroInStyleObject);
47
+ }
48
+ break;
49
+ case 'styleVariants':
50
+ case 'keyframes':
51
+ // For styleVariants and keyframes, the argument is an object where each property value is a style object
52
+ if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
53
+ const variantsObject = node.arguments[0];
54
+ variantsObject.properties.forEach((property) => {
55
+ if (property.type === 'Property' && property.value.type === 'ObjectExpression') {
56
+ processTrailingZeroInStyleObject(context, property.value);
57
+ }
58
+ });
59
+ }
60
+ break;
61
+ case 'globalStyle':
62
+ if (node.arguments.length >= 2) {
63
+ processStyleNode(context, node.arguments[1], processTrailingZeroInStyleObject);
64
+ }
65
+ break;
66
+ case 'globalKeyframes':
67
+ // For globalKeyframes, the second argument is an object where each property value is a style object
68
+ if (node.arguments.length >= 2 && node.arguments[1]?.type === AST_NODE_TYPES.ObjectExpression) {
69
+ const keyframesObject = node.arguments[1];
70
+ keyframesObject.properties.forEach((property) => {
71
+ if (property.type === 'Property' && property.value.type === 'ObjectExpression') {
72
+ processTrailingZeroInStyleObject(context, property.value);
73
+ }
74
+ });
75
+ }
76
+ break;
77
+ case 'recipe':
78
+ if (node.arguments.length > 0 && node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression) {
79
+ processRecipeProperties(context, node.arguments[0], processTrailingZeroInStyleObject);
80
+ }
81
+ break;
82
+ }
83
+ },
84
+ };
85
+ };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,8 @@ 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-px-unit': import("eslint").Rule.RuleModule;
12
+ 'no-trailing-zero': import("eslint").Rule.RuleModule;
11
13
  'no-unknown-unit': import("eslint").Rule.RuleModule;
12
14
  'no-zero-unit': import("eslint").Rule.RuleModule;
13
15
  };
package/dist/index.js CHANGED
@@ -2,18 +2,22 @@ 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 noPxUnitRule from './css-rules/no-px-unit/index.js';
6
+ import noTrailingZeroRule from './css-rules/no-trailing-zero/rule-definition.js';
5
7
  import noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
6
8
  import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
7
9
  const vanillaExtract = {
8
10
  meta: {
9
11
  name: '@antebudimir/eslint-plugin-vanilla-extract',
10
- version: '1.11.1',
12
+ version: '1.13.0',
11
13
  },
12
14
  rules: {
13
15
  'alphabetical-order': alphabeticalOrderRule,
14
16
  'concentric-order': concentricOrderRule,
15
17
  'custom-order': customOrderRule,
16
18
  'no-empty-style-blocks': noEmptyStyleBlocksRule,
19
+ 'no-px-unit': noPxUnitRule,
20
+ 'no-trailing-zero': noTrailingZeroRule,
17
21
  'no-unknown-unit': noUnknownUnitRule,
18
22
  'no-zero-unit': noZeroUnitRule,
19
23
  },
@@ -27,6 +31,7 @@ Object.assign(vanillaExtract.configs, {
27
31
  rules: {
28
32
  'vanilla-extract/concentric-order': 'error',
29
33
  'vanilla-extract/no-empty-style-blocks': 'error',
34
+ 'vanilla-extract/no-trailing-zero': 'error',
30
35
  'vanilla-extract/no-unknown-unit': 'error',
31
36
  'vanilla-extract/no-zero-unit': 'error',
32
37
  },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.11.1",
4
- "description": "ESLint plugin for enforcing best practices in vanilla-extract CSS styles, including CSS property ordering and additional linting rules.",
3
+ "version": "1.13.0",
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",
7
7
  "keywords": [