@antebudimir/eslint-plugin-vanilla-extract 1.4.7 → 1.6.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 +52 -6
- package/dist/css-rules/alphabetical-order/property-order-enforcer.js +3 -3
- package/dist/css-rules/alphabetical-order/recipe-order-enforcer.js +3 -4
- package/dist/css-rules/alphabetical-order/style-object-processor.js +7 -8
- package/dist/css-rules/concentric-order/property-order-enforcer.js +10 -11
- package/dist/css-rules/concentric-order/recipe-order-enforcer.js +3 -4
- package/dist/css-rules/concentric-order/style-object-processor.js +15 -16
- package/dist/css-rules/concentric-order/types.d.ts +1 -0
- package/dist/css-rules/custom-order/property-order-enforcer.d.ts +2 -2
- package/dist/css-rules/custom-order/property-order-enforcer.js +50 -46
- package/dist/css-rules/custom-order/recipe-order-enforcer.d.ts +2 -1
- package/dist/css-rules/custom-order/recipe-order-enforcer.js +4 -5
- package/dist/css-rules/custom-order/rule-definition.js +3 -1
- package/dist/css-rules/custom-order/style-object-processor.d.ts +3 -2
- package/dist/css-rules/custom-order/style-object-processor.js +28 -29
- package/dist/css-rules/no-empty-blocks/conditional-processor.d.ts +6 -0
- package/dist/css-rules/no-empty-blocks/conditional-processor.js +26 -0
- package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.d.ts +6 -0
- package/dist/css-rules/no-empty-blocks/empty-nested-style-processor.js +65 -0
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.d.ts +12 -0
- package/dist/css-rules/no-empty-blocks/empty-style-visitor-creator.js +183 -0
- package/dist/css-rules/no-empty-blocks/fix-utils.d.ts +22 -0
- package/dist/css-rules/no-empty-blocks/fix-utils.js +75 -0
- package/dist/css-rules/no-empty-blocks/index.d.ts +2 -0
- package/dist/css-rules/no-empty-blocks/index.js +2 -0
- package/dist/css-rules/no-empty-blocks/node-remover.d.ts +10 -0
- package/dist/css-rules/no-empty-blocks/node-remover.js +15 -0
- package/dist/css-rules/no-empty-blocks/property-utils.d.ts +9 -0
- package/dist/css-rules/no-empty-blocks/property-utils.js +28 -0
- package/dist/css-rules/no-empty-blocks/recipe-processor.d.ts +11 -0
- package/dist/css-rules/no-empty-blocks/recipe-processor.js +122 -0
- package/dist/css-rules/no-empty-blocks/rule-definition.d.ts +3 -0
- package/dist/css-rules/no-empty-blocks/rule-definition.js +30 -0
- package/dist/css-rules/no-empty-blocks/style-variants-processor.d.ts +10 -0
- package/dist/css-rules/no-empty-blocks/style-variants-processor.js +45 -0
- package/dist/css-rules/shared-utils/alphabetical-property-comparator.js +3 -3
- package/dist/css-rules/shared-utils/empty-object-processor.d.ts +11 -0
- package/dist/css-rules/shared-utils/empty-object-processor.js +15 -0
- package/dist/css-rules/shared-utils/font-face-property-order-enforcer.js +3 -3
- package/dist/css-rules/shared-utils/order-strategy-visitor-creator.d.ts +3 -1
- package/dist/css-rules/shared-utils/order-strategy-visitor-creator.js +1 -1
- package/dist/css-rules/shared-utils/property-separator.d.ts +1 -1
- package/dist/css-rules/shared-utils/property-separator.js +2 -2
- package/dist/css-rules/types.d.ts +1 -0
- package/dist/css-rules/types.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -3
- package/package.json +13 -13
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Creates ESLint rule visitors for detecting empty style blocks in vanilla-extract.
|
|
5
|
+
* @param ruleContext The ESLint rule rule context.
|
|
6
|
+
* @returns An object with visitor functions for the ESLint rule.
|
|
7
|
+
*/
|
|
8
|
+
export declare const createEmptyStyleVisitors: (ruleContext: Rule.RuleContext) => Rule.RuleListener;
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a style object is effectively empty (contains only empty objects).
|
|
11
|
+
*/
|
|
12
|
+
export declare function isEffectivelyEmptyStylesObject(stylesObject: TSESTree.ObjectExpression): boolean;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { processConditionalExpression } from './conditional-processor.js';
|
|
4
|
+
import { processEmptyNestedStyles } from './empty-nested-style-processor.js';
|
|
5
|
+
import { reportEmptyDeclaration } from './fix-utils.js';
|
|
6
|
+
import { removeNodeWithComma } from './node-remover.js';
|
|
7
|
+
import { getStyleKeyName } from './property-utils.js';
|
|
8
|
+
import { processRecipeProperties } from './recipe-processor.js';
|
|
9
|
+
import { processStyleVariants } from './style-variants-processor.js';
|
|
10
|
+
/**
|
|
11
|
+
* Creates ESLint rule visitors for detecting empty style blocks in vanilla-extract.
|
|
12
|
+
* @param ruleContext The ESLint rule rule context.
|
|
13
|
+
* @returns An object with visitor functions for the ESLint rule.
|
|
14
|
+
*/
|
|
15
|
+
export const createEmptyStyleVisitors = (ruleContext) => {
|
|
16
|
+
// Track reported nodes to prevent duplicate reports
|
|
17
|
+
const reportedNodes = new Set();
|
|
18
|
+
return {
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
if (node.callee.type !== 'Identifier') {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Target vanilla-extract style functions
|
|
24
|
+
const styleApiFunctions = [
|
|
25
|
+
'style',
|
|
26
|
+
'styleVariants',
|
|
27
|
+
'recipe',
|
|
28
|
+
'globalStyle',
|
|
29
|
+
'fontFace',
|
|
30
|
+
'globalFontFace',
|
|
31
|
+
'keyframes',
|
|
32
|
+
'globalKeyframes',
|
|
33
|
+
];
|
|
34
|
+
if (!styleApiFunctions.includes(node.callee.name) || node.arguments.length === 0) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// Handle styleVariants specifically
|
|
38
|
+
if (node.callee.name === 'styleVariants' && node.arguments[0]?.type === 'ObjectExpression') {
|
|
39
|
+
processStyleVariants(ruleContext, node.arguments[0], reportedNodes);
|
|
40
|
+
// If the entire styleVariants object is empty after processing, remove the declaration
|
|
41
|
+
if (isEmptyObject(node.arguments[0])) {
|
|
42
|
+
reportEmptyDeclaration(ruleContext, node.arguments[0], node);
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const defaultStyleArgumentIndex = 0;
|
|
47
|
+
const globalFunctionNames = ['globalStyle', 'globalFontFace', 'globalKeyframes'];
|
|
48
|
+
// Determine the style argument index based on the function name
|
|
49
|
+
const styleArgumentIndex = globalFunctionNames.includes(node.callee.name) ? 1 : defaultStyleArgumentIndex;
|
|
50
|
+
// For global functions, check if we have enough arguments
|
|
51
|
+
if (styleArgumentIndex === 1 && node.arguments.length <= styleArgumentIndex) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const styleArgument = node.arguments[styleArgumentIndex];
|
|
55
|
+
// This defensive check prevents duplicate processing of nodes.
|
|
56
|
+
// This code path's difficult to test because the ESLint visitor pattern
|
|
57
|
+
// typically ensures each node is only visited once per rule execution.
|
|
58
|
+
if (reportedNodes.has(styleArgument)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
// Handle conditional expressions
|
|
62
|
+
if (styleArgument?.type === 'ConditionalExpression') {
|
|
63
|
+
processConditionalExpression(ruleContext, styleArgument, reportedNodes, node);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// Direct empty object case - remove the entire declaration
|
|
67
|
+
if (styleArgument?.type === 'ObjectExpression' && isEmptyObject(styleArgument)) {
|
|
68
|
+
reportedNodes.add(styleArgument);
|
|
69
|
+
reportEmptyDeclaration(ruleContext, styleArgument, node);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
// For recipe - check if entire recipe is effectively empty
|
|
73
|
+
if (node.callee.name === 'recipe' && styleArgument?.type === 'ObjectExpression') {
|
|
74
|
+
if (isEffectivelyEmptyStylesObject(styleArgument)) {
|
|
75
|
+
reportedNodes.add(styleArgument);
|
|
76
|
+
reportEmptyDeclaration(ruleContext, styleArgument, node);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// Process individual properties in recipe
|
|
80
|
+
processRecipeProperties(ruleContext, styleArgument, reportedNodes);
|
|
81
|
+
}
|
|
82
|
+
// For style objects with nested empty objects
|
|
83
|
+
if (styleArgument?.type === 'ObjectExpression') {
|
|
84
|
+
// Check for spread elements
|
|
85
|
+
styleArgument.properties.forEach((property) => {
|
|
86
|
+
if (property.type === 'SpreadElement' &&
|
|
87
|
+
property.argument.type === 'ObjectExpression' &&
|
|
88
|
+
isEmptyObject(property.argument)) {
|
|
89
|
+
reportedNodes.add(property.argument);
|
|
90
|
+
ruleContext.report({
|
|
91
|
+
node: property.argument,
|
|
92
|
+
messageId: 'emptySpreadObject',
|
|
93
|
+
fix(fixer) {
|
|
94
|
+
return removeNodeWithComma(ruleContext, property, fixer);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
// Process nested selectors and media queries
|
|
100
|
+
processEmptyNestedStyles(ruleContext, styleArgument, reportedNodes);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Checks if a style object is effectively empty (contains only empty objects).
|
|
107
|
+
*/
|
|
108
|
+
export function isEffectivelyEmptyStylesObject(stylesObject) {
|
|
109
|
+
// Empty object itself
|
|
110
|
+
if (stylesObject.properties.length === 0) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// For recipe objects, we need special handling
|
|
114
|
+
let hasBaseProperty = false;
|
|
115
|
+
let isBaseEmpty = true;
|
|
116
|
+
let hasVariantsProperty = false;
|
|
117
|
+
let areAllVariantsEmpty = true;
|
|
118
|
+
// First pass: identify recipe properties
|
|
119
|
+
for (const property of stylesObject.properties) {
|
|
120
|
+
if (property.type !== 'Property') {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const propertyName = getStyleKeyName(property.key);
|
|
124
|
+
if (!propertyName) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (propertyName === 'base') {
|
|
128
|
+
hasBaseProperty = true;
|
|
129
|
+
if (property.value.type === 'ObjectExpression' && !isEmptyObject(property.value)) {
|
|
130
|
+
isBaseEmpty = false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (propertyName === 'variants') {
|
|
134
|
+
hasVariantsProperty = true;
|
|
135
|
+
if (property.value.type === 'ObjectExpression' && !isEmptyObject(property.value)) {
|
|
136
|
+
areAllVariantsEmpty = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// If this looks like a recipe object (has base or variants)
|
|
141
|
+
if (hasBaseProperty || hasVariantsProperty) {
|
|
142
|
+
// A recipe is effectively empty if both base and variants are empty
|
|
143
|
+
return isBaseEmpty && areAllVariantsEmpty;
|
|
144
|
+
}
|
|
145
|
+
// / For non-recipe objects, check if all special properties (selectors, media queries, variants) are effectively empty
|
|
146
|
+
function isSpecialProperty(propertyName) {
|
|
147
|
+
return (propertyName === 'selectors' || (propertyName && propertyName.startsWith('@')) || propertyName === 'variants');
|
|
148
|
+
}
|
|
149
|
+
const specialProperties = stylesObject.properties.filter((prop) => prop.type === 'Property' && isSpecialProperty(getStyleKeyName(prop.key)));
|
|
150
|
+
const allSpecialPropertiesEmpty = specialProperties.every((property) => {
|
|
151
|
+
if (property.value.type === 'ObjectExpression' && isEmptyObject(property.value)) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
const propertyName = getStyleKeyName(property.key);
|
|
155
|
+
// This defensive check handles malformed AST nodes that lack valid property names.
|
|
156
|
+
// This is difficult to test because it's challenging to construct a valid AST
|
|
157
|
+
// where getStyleKeyName would return a falsy value.
|
|
158
|
+
if (!propertyName) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
// For selectors, media queries and supports, check if all nested objects are empty
|
|
162
|
+
if ((propertyName === 'selectors' || (propertyName && propertyName.startsWith('@'))) &&
|
|
163
|
+
property.value.type === 'ObjectExpression') {
|
|
164
|
+
// This handles the edge case of an empty properties array.
|
|
165
|
+
// This code path is difficult to test in isolation because it requires
|
|
166
|
+
// constructing a specific AST structure that bypasses earlier conditions.
|
|
167
|
+
if (property.value.properties.length === 0) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
return property.value.properties.every((nestedProperty) => {
|
|
171
|
+
return (nestedProperty.type === 'Property' &&
|
|
172
|
+
nestedProperty.value.type === 'ObjectExpression' &&
|
|
173
|
+
isEmptyObject(nestedProperty.value));
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// Default fallback for cases not handled by the conditions above.
|
|
177
|
+
// This is difficult to test because it requires creating an AST structure
|
|
178
|
+
// that doesn't trigger any of the preceding return statements.
|
|
179
|
+
return false;
|
|
180
|
+
});
|
|
181
|
+
// If we have special properties and they're all empty, the style is effectively empty
|
|
182
|
+
return specialProperties.length > 0 && allSpecialPropertiesEmpty;
|
|
183
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Finds the target declaration node for a given call expression.
|
|
5
|
+
*
|
|
6
|
+
* Traverses the AST upwards from the `callNode` to find the appropriate
|
|
7
|
+
* declaration node. For global APIs, it targets the expression statement.
|
|
8
|
+
* For non-global APIs, it looks for variable declarations or export
|
|
9
|
+
* named declarations.
|
|
10
|
+
*
|
|
11
|
+
* @param callNode - The call expression node to start from.
|
|
12
|
+
* @param isGlobalApi - A flag indicating whether the API is global.
|
|
13
|
+
* @returns The target declaration node or null if not found.
|
|
14
|
+
*/
|
|
15
|
+
export declare const findTargetDeclarationNode: (callNode: TSESTree.CallExpression, isGlobalApi: boolean) => TSESTree.Node | null;
|
|
16
|
+
/**
|
|
17
|
+
* Reports an issue for an empty style declaration, and provides a fix to remove the declaration.
|
|
18
|
+
* @param ruleContext The ESLint rule context.
|
|
19
|
+
* @param node The node to report the issue on.
|
|
20
|
+
* @param callNode The CallExpression node of the style() function.
|
|
21
|
+
*/
|
|
22
|
+
export declare const reportEmptyDeclaration: (ruleContext: Rule.RuleContext, node: TSESTree.Node, callNode: TSESTree.CallExpression) => void;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Finds the target declaration node for a given call expression.
|
|
4
|
+
*
|
|
5
|
+
* Traverses the AST upwards from the `callNode` to find the appropriate
|
|
6
|
+
* declaration node. For global APIs, it targets the expression statement.
|
|
7
|
+
* For non-global APIs, it looks for variable declarations or export
|
|
8
|
+
* named declarations.
|
|
9
|
+
*
|
|
10
|
+
* @param callNode - The call expression node to start from.
|
|
11
|
+
* @param isGlobalApi - A flag indicating whether the API is global.
|
|
12
|
+
* @returns The target declaration node or null if not found.
|
|
13
|
+
*/
|
|
14
|
+
export const findTargetDeclarationNode = (callNode, isGlobalApi) => {
|
|
15
|
+
let current = callNode;
|
|
16
|
+
while (current.parent) {
|
|
17
|
+
current = current.parent;
|
|
18
|
+
// For global APIs, we only need to check for ExpressionStatement
|
|
19
|
+
if (isGlobalApi && current.type === 'ExpressionStatement') {
|
|
20
|
+
return current;
|
|
21
|
+
}
|
|
22
|
+
// For non-global APIs, check for variable declarations and exports
|
|
23
|
+
if (!isGlobalApi) {
|
|
24
|
+
if (current.type === 'VariableDeclarator' && current.parent && current.parent.type === 'VariableDeclaration') {
|
|
25
|
+
// If this is part of an export, get the export declaration
|
|
26
|
+
if (current.parent.parent && current.parent.parent.type === 'ExportNamedDeclaration') {
|
|
27
|
+
return current.parent.parent;
|
|
28
|
+
}
|
|
29
|
+
return current.parent;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Reports an issue for an empty style declaration, and provides a fix to remove the declaration.
|
|
37
|
+
* @param ruleContext The ESLint rule context.
|
|
38
|
+
* @param node The node to report the issue on.
|
|
39
|
+
* @param callNode The CallExpression node of the style() function.
|
|
40
|
+
*/
|
|
41
|
+
export const reportEmptyDeclaration = (ruleContext, node, callNode) => {
|
|
42
|
+
// Check if this is a global API function
|
|
43
|
+
const isGlobalApi = callNode.callee.type === 'Identifier' &&
|
|
44
|
+
['globalStyle', 'globalFontFace', 'globalKeyframes'].includes(callNode.callee.name);
|
|
45
|
+
// Find the parent declaration node
|
|
46
|
+
const targetDeclarationNode = findTargetDeclarationNode(callNode, isGlobalApi);
|
|
47
|
+
if (targetDeclarationNode) {
|
|
48
|
+
ruleContext.report({
|
|
49
|
+
node: node,
|
|
50
|
+
messageId: 'emptyStyleDeclaration',
|
|
51
|
+
fix(fixer) {
|
|
52
|
+
const sourceCode = ruleContext.sourceCode;
|
|
53
|
+
const startLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[0]).line;
|
|
54
|
+
const lineStart = sourceCode.getIndexFromLoc({
|
|
55
|
+
line: startLine,
|
|
56
|
+
column: 0,
|
|
57
|
+
});
|
|
58
|
+
// Find next line after the declaration
|
|
59
|
+
const endLine = sourceCode.getLocFromIndex(targetDeclarationNode.range[1]).line;
|
|
60
|
+
const nextLineStart = sourceCode.getIndexFromLoc({
|
|
61
|
+
line: endLine + 1,
|
|
62
|
+
column: 0,
|
|
63
|
+
});
|
|
64
|
+
return fixer.removeRange([lineStart, nextLineStart]);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Report the issue without providing a fix
|
|
70
|
+
ruleContext.report({
|
|
71
|
+
node: node,
|
|
72
|
+
messageId: 'emptyStyleDeclaration',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import type { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Removes the given node and also removes a trailing comma if it exists.
|
|
5
|
+
* @param ruleContext The ESLint rule context.
|
|
6
|
+
* @param node The node to remove.
|
|
7
|
+
* @param fixer The ESLint fixer.
|
|
8
|
+
* @returns The fix object.
|
|
9
|
+
*/
|
|
10
|
+
export declare function removeNodeWithComma(ruleContext: Rule.RuleContext, node: TSESTree.Node, fixer: Rule.RuleFixer): Rule.Fix;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes the given node and also removes a trailing comma if it exists.
|
|
3
|
+
* @param ruleContext The ESLint rule context.
|
|
4
|
+
* @param node The node to remove.
|
|
5
|
+
* @param fixer The ESLint fixer.
|
|
6
|
+
* @returns The fix object.
|
|
7
|
+
*/
|
|
8
|
+
export function removeNodeWithComma(ruleContext, node, fixer) {
|
|
9
|
+
const sourceCode = ruleContext.sourceCode;
|
|
10
|
+
const tokenAfter = sourceCode.getTokenAfter(node);
|
|
11
|
+
if (tokenAfter && tokenAfter.value === ',' && node.range && tokenAfter.range) {
|
|
12
|
+
return fixer.removeRange([node.range[0], tokenAfter.range[1]]);
|
|
13
|
+
}
|
|
14
|
+
return fixer.remove(node);
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Gets the property name regardless of whether it's an identifier or a literal.
|
|
4
|
+
*/
|
|
5
|
+
export declare function getStyleKeyName(key: TSESTree.Expression | TSESTree.PrivateIdentifier): string | null;
|
|
6
|
+
/**
|
|
7
|
+
* Checks if all properties in a style object are empty objects.
|
|
8
|
+
*/
|
|
9
|
+
export declare const areAllChildrenEmpty: (stylesObject: TSESTree.ObjectExpression) => boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
/**
|
|
4
|
+
* Gets the property name regardless of whether it's an identifier or a literal.
|
|
5
|
+
*/
|
|
6
|
+
export function getStyleKeyName(key) {
|
|
7
|
+
if (key.type === 'Identifier') {
|
|
8
|
+
return key.name;
|
|
9
|
+
}
|
|
10
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
11
|
+
return key.value;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Checks if all properties in a style object are empty objects.
|
|
17
|
+
*/
|
|
18
|
+
export const areAllChildrenEmpty = (stylesObject) => {
|
|
19
|
+
if (stylesObject.properties.length === 0) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
return stylesObject.properties.every((property) => {
|
|
23
|
+
if (property.type !== 'Property' || property.value.type !== 'ObjectExpression') {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return isEmptyObject(property.value);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Processes a recipe object, removing empty `base` and `variants` properties, as well as empty
|
|
5
|
+
* variant categories and values.
|
|
6
|
+
*
|
|
7
|
+
* @param ruleContext The ESLint rule context.
|
|
8
|
+
* @param recipeNode The recipe object node to process.
|
|
9
|
+
* @param reportedNodes A set of nodes that have already been reported by other processors.
|
|
10
|
+
*/
|
|
11
|
+
export declare function processRecipeProperties(ruleContext: Rule.RuleContext, recipeNode: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { processEmptyNestedStyles } from './empty-nested-style-processor.js';
|
|
4
|
+
import { removeNodeWithComma } from './node-remover.js';
|
|
5
|
+
import { areAllChildrenEmpty, getStyleKeyName } from './property-utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Processes a recipe object, removing empty `base` and `variants` properties, as well as empty
|
|
8
|
+
* variant categories and values.
|
|
9
|
+
*
|
|
10
|
+
* @param ruleContext The ESLint rule context.
|
|
11
|
+
* @param recipeNode The recipe object node to process.
|
|
12
|
+
* @param reportedNodes A set of nodes that have already been reported by other processors.
|
|
13
|
+
*/
|
|
14
|
+
export function processRecipeProperties(ruleContext, recipeNode, reportedNodes) {
|
|
15
|
+
recipeNode.properties.forEach((property) => {
|
|
16
|
+
if (property.type !== 'Property') {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const propertyName = getStyleKeyName(property.key);
|
|
20
|
+
if (!propertyName) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Handle empty base or variants properties
|
|
24
|
+
if ((propertyName === 'base' || propertyName === 'variants') &&
|
|
25
|
+
property.value.type === 'ObjectExpression' &&
|
|
26
|
+
isEmptyObject(property.value)) {
|
|
27
|
+
if (!reportedNodes.has(property)) {
|
|
28
|
+
reportedNodes.add(property);
|
|
29
|
+
ruleContext.report({
|
|
30
|
+
node: property,
|
|
31
|
+
messageId: 'emptyRecipeProperty',
|
|
32
|
+
data: {
|
|
33
|
+
propertyName,
|
|
34
|
+
},
|
|
35
|
+
fix(fixer) {
|
|
36
|
+
return removeNodeWithComma(ruleContext, property, fixer);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Process base property nested objects
|
|
42
|
+
if (propertyName === 'base' && property.value.type === 'ObjectExpression') {
|
|
43
|
+
processEmptyNestedStyles(ruleContext, property.value, reportedNodes);
|
|
44
|
+
}
|
|
45
|
+
// Process variant values
|
|
46
|
+
if (propertyName === 'variants' && property.value.type === 'ObjectExpression') {
|
|
47
|
+
// If variants is empty, it will be handled by the check above
|
|
48
|
+
if (!isEmptyObject(property.value)) {
|
|
49
|
+
// Process variant categories
|
|
50
|
+
property.value.properties.forEach((variantCategoryProperty) => {
|
|
51
|
+
if (variantCategoryProperty.type !== 'Property' ||
|
|
52
|
+
variantCategoryProperty.value.type !== 'ObjectExpression') {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
// Check if all values in this category are empty
|
|
56
|
+
if (isEmptyObject(variantCategoryProperty.value) || areAllChildrenEmpty(variantCategoryProperty.value)) {
|
|
57
|
+
if (!reportedNodes.has(variantCategoryProperty)) {
|
|
58
|
+
reportedNodes.add(variantCategoryProperty);
|
|
59
|
+
ruleContext.report({
|
|
60
|
+
node: variantCategoryProperty,
|
|
61
|
+
messageId: 'emptyVariantCategory',
|
|
62
|
+
fix(fixer) {
|
|
63
|
+
return removeNodeWithComma(ruleContext, variantCategoryProperty, fixer);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Process individual variant values
|
|
70
|
+
variantCategoryProperty.value.properties.forEach((variantValueProperty) => {
|
|
71
|
+
if (variantValueProperty.type !== 'Property') {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
// Check for non-object variant values
|
|
75
|
+
if (variantValueProperty.value.type !== 'ObjectExpression') {
|
|
76
|
+
if (!reportedNodes.has(variantValueProperty)) {
|
|
77
|
+
reportedNodes.add(variantValueProperty);
|
|
78
|
+
// Get a user-friendly type description as a string
|
|
79
|
+
const friendlyType = (() => {
|
|
80
|
+
const nodeType = variantValueProperty.value.type;
|
|
81
|
+
if (nodeType === 'Literal') {
|
|
82
|
+
const literalValue = variantValueProperty.value;
|
|
83
|
+
return literalValue.value === null ? 'null' : typeof literalValue.value;
|
|
84
|
+
}
|
|
85
|
+
else if (nodeType === 'Identifier') {
|
|
86
|
+
return 'variable';
|
|
87
|
+
}
|
|
88
|
+
return nodeType;
|
|
89
|
+
})();
|
|
90
|
+
ruleContext.report({
|
|
91
|
+
node: variantValueProperty,
|
|
92
|
+
messageId: 'invalidPropertyType',
|
|
93
|
+
data: {
|
|
94
|
+
type: friendlyType,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// Check for empty objects in variant properties
|
|
101
|
+
if (isEmptyObject(variantValueProperty.value)) {
|
|
102
|
+
if (!reportedNodes.has(variantValueProperty)) {
|
|
103
|
+
reportedNodes.add(variantValueProperty);
|
|
104
|
+
ruleContext.report({
|
|
105
|
+
node: variantValueProperty,
|
|
106
|
+
messageId: 'emptyVariantValue',
|
|
107
|
+
fix(fixer) {
|
|
108
|
+
return removeNodeWithComma(ruleContext, variantValueProperty, fixer);
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
// Process nested styles within variant values
|
|
115
|
+
processEmptyNestedStyles(ruleContext, variantValueProperty.value, reportedNodes);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createEmptyStyleVisitors } from './empty-style-visitor-creator.js';
|
|
2
|
+
const noEmptyStyleBlocksRule = {
|
|
3
|
+
meta: {
|
|
4
|
+
type: 'suggestion',
|
|
5
|
+
docs: {
|
|
6
|
+
description: 'disallow empty style blocks in vanilla-extract stylesheets',
|
|
7
|
+
category: 'Best Practices',
|
|
8
|
+
recommended: true,
|
|
9
|
+
},
|
|
10
|
+
fixable: 'code',
|
|
11
|
+
schema: [],
|
|
12
|
+
messages: {
|
|
13
|
+
emptyConditionalStyle: 'Empty conditional style object should be removed.',
|
|
14
|
+
emptyMedia: 'Empty @media object should be removed.',
|
|
15
|
+
emptyNestedStyle: 'Empty nested style object should be removed.',
|
|
16
|
+
emptyRecipeProperty: 'Empty {{propertyName}} object in recipe should be removed.',
|
|
17
|
+
emptySelectors: 'Empty selectors object should be removed.',
|
|
18
|
+
emptySpreadObject: 'Empty spread object should be removed.',
|
|
19
|
+
emptyStyleDeclaration: 'Declarations with only empty style blocks should be removed.',
|
|
20
|
+
emptySupports: 'Empty @supports object should be removed.',
|
|
21
|
+
emptyVariantCategory: 'Empty variant category should be removed.',
|
|
22
|
+
emptyVariantValue: 'Empty variant value should be removed.',
|
|
23
|
+
invalidPropertyType: 'Variant values must be objects, found {{type}} instead.',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
create(ruleContext) {
|
|
27
|
+
return createEmptyStyleVisitors(ruleContext);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
export default noEmptyStyleBlocksRule;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Processes styleVariants function calls to detect and remove empty style variants.
|
|
5
|
+
*
|
|
6
|
+
* @param ruleContext The ESLint rule context.
|
|
7
|
+
* @param node The styleVariants call argument (object expression).
|
|
8
|
+
* @param reportedNodes A set of nodes that have already been reported.
|
|
9
|
+
*/
|
|
10
|
+
export declare function processStyleVariants(ruleContext: Rule.RuleContext, node: TSESTree.ObjectExpression, reportedNodes: Set<TSESTree.Node>): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
3
|
+
import { removeNodeWithComma } from './node-remover.js';
|
|
4
|
+
/**
|
|
5
|
+
* Processes styleVariants function calls to detect and remove empty style variants.
|
|
6
|
+
*
|
|
7
|
+
* @param ruleContext The ESLint rule context.
|
|
8
|
+
* @param node The styleVariants call argument (object expression).
|
|
9
|
+
* @param reportedNodes A set of nodes that have already been reported.
|
|
10
|
+
*/
|
|
11
|
+
export function processStyleVariants(ruleContext, node, reportedNodes) {
|
|
12
|
+
node.properties.forEach((property) => {
|
|
13
|
+
if (property.type !== 'Property') {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
// Check for empty arrays
|
|
17
|
+
if (property.value.type === 'ArrayExpression' && property.value.elements.length === 0) {
|
|
18
|
+
if (!reportedNodes.has(property)) {
|
|
19
|
+
reportedNodes.add(property);
|
|
20
|
+
ruleContext.report({
|
|
21
|
+
node: property,
|
|
22
|
+
messageId: 'emptyVariantValue',
|
|
23
|
+
fix(fixer) {
|
|
24
|
+
return removeNodeWithComma(ruleContext, property, fixer);
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
// Check for empty objects
|
|
31
|
+
if (property.value.type === 'ObjectExpression' && isEmptyObject(property.value)) {
|
|
32
|
+
if (!reportedNodes.has(property)) {
|
|
33
|
+
reportedNodes.add(property);
|
|
34
|
+
ruleContext.report({
|
|
35
|
+
node: property,
|
|
36
|
+
messageId: 'emptyVariantValue',
|
|
37
|
+
fix(fixer) {
|
|
38
|
+
return removeNodeWithComma(ruleContext, property, fixer);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getPropertyNameForSorting } from './property-separator.js';
|
|
2
2
|
/**
|
|
3
3
|
* Compares two CSS properties alphabetically.
|
|
4
4
|
* @param firstProperty The first property to compare.
|
|
@@ -6,8 +6,8 @@ import { getPropertyName } from './property-separator.js';
|
|
|
6
6
|
* @returns A number indicating the relative order of the properties (-1, 0, or 1).
|
|
7
7
|
*/
|
|
8
8
|
export const comparePropertiesAlphabetically = (firstProperty, secondProperty) => {
|
|
9
|
-
const firstName =
|
|
10
|
-
const secondName =
|
|
9
|
+
const firstName = getPropertyNameForSorting(firstProperty);
|
|
10
|
+
const secondName = getPropertyNameForSorting(secondProperty);
|
|
11
11
|
// Special handling for 'src' property - it should always come first (relates to font face APIs only)
|
|
12
12
|
if (firstName === 'src') {
|
|
13
13
|
return -1;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Type guard to check if a node is an ObjectExpression.
|
|
4
|
+
*/
|
|
5
|
+
export declare const isObjectExpression: (node: TSESTree.Node) => node is TSESTree.ObjectExpression;
|
|
6
|
+
/**
|
|
7
|
+
* Checks if an object expression is empty (has no properties).
|
|
8
|
+
* @param node The node to check.
|
|
9
|
+
* @returns True if the node is an ObjectExpression with no properties.
|
|
10
|
+
*/
|
|
11
|
+
export declare const isEmptyObject: (node: TSESTree.Node) => boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Type guard to check if a node is an ObjectExpression.
|
|
4
|
+
*/
|
|
5
|
+
export const isObjectExpression = (node) => {
|
|
6
|
+
return node.type === 'ObjectExpression';
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Checks if an object expression is empty (has no properties).
|
|
10
|
+
* @param node The node to check.
|
|
11
|
+
* @returns True if the node is an ObjectExpression with no properties.
|
|
12
|
+
*/
|
|
13
|
+
export const isEmptyObject = (node) => {
|
|
14
|
+
return isObjectExpression(node) && node.properties.length === 0;
|
|
15
|
+
};
|