@antebudimir/eslint-plugin-vanilla-extract 1.9.0 → 1.11.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.
@@ -0,0 +1,260 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Tracks vanilla-extract function imports and their local bindings
4
+ */
5
+ export class ReferenceTracker {
6
+ constructor() {
7
+ this.imports = new Map();
8
+ this.wrapperFunctions = new Map(); // wrapper function name -> detailed info
9
+ this.trackedFunctions = {
10
+ styleFunctions: new Set(),
11
+ recipeFunctions: new Set(),
12
+ fontFaceFunctions: new Set(),
13
+ globalFunctions: new Set(),
14
+ keyframeFunctions: new Set(),
15
+ };
16
+ }
17
+ /**
18
+ * Processes import declarations to track vanilla-extract functions
19
+ */
20
+ processImportDeclaration(node) {
21
+ const source = node.source.value;
22
+ // Check if this is a vanilla-extract import
23
+ if (typeof source !== 'string' || !this.isVanillaExtractSource(source)) {
24
+ return;
25
+ }
26
+ node.specifiers.forEach((specifier) => {
27
+ if (specifier.type === 'ImportSpecifier') {
28
+ const importedName = specifier.imported.type === 'Identifier' ? specifier.imported.name : specifier.imported.value;
29
+ const localName = specifier.local.name;
30
+ const reference = {
31
+ source,
32
+ importedName,
33
+ localName,
34
+ };
35
+ this.imports.set(localName, reference);
36
+ this.categorizeFunction(localName, importedName);
37
+ }
38
+ });
39
+ }
40
+ /**
41
+ * Processes variable declarations to track re-assignments and destructuring
42
+ */
43
+ processVariableDeclarator(node) {
44
+ // Handle destructuring assignments like: const { style, recipe } = vanillaExtract;
45
+ if (node.id.type === 'ObjectPattern' && node.init?.type === 'Identifier') {
46
+ const sourceIdentifier = node.init.name;
47
+ const sourceReference = this.imports.get(sourceIdentifier);
48
+ if (sourceReference && this.isVanillaExtractSource(sourceReference.source)) {
49
+ node.id.properties.forEach((property) => {
50
+ if (property.type === 'Property' &&
51
+ property.key.type === 'Identifier' &&
52
+ property.value.type === 'Identifier') {
53
+ const importedName = property.key.name;
54
+ const localName = property.value.name;
55
+ const reference = {
56
+ source: sourceReference.source,
57
+ importedName,
58
+ localName,
59
+ };
60
+ this.imports.set(localName, reference);
61
+ this.categorizeFunction(localName, importedName);
62
+ }
63
+ });
64
+ }
65
+ }
66
+ // Handle simple assignments like: const myStyle = style;
67
+ if (node.id.type === 'Identifier' && node.init?.type === 'Identifier') {
68
+ const sourceReference = this.imports.get(node.init.name);
69
+ if (sourceReference) {
70
+ this.imports.set(node.id.name, sourceReference);
71
+ this.categorizeFunction(node.id.name, sourceReference.importedName);
72
+ }
73
+ }
74
+ // Handle arrow function assignments that wrap vanilla-extract functions
75
+ if (node.id.type === 'Identifier' && node.init?.type === 'ArrowFunctionExpression') {
76
+ this.analyzeWrapperFunction(node.id.name, node.init);
77
+ }
78
+ }
79
+ /**
80
+ * Processes function declarations to detect wrapper functions
81
+ */
82
+ processFunctionDeclaration(node) {
83
+ if (node.id?.name) {
84
+ this.analyzeWrapperFunction(node.id.name, node);
85
+ }
86
+ }
87
+ /**
88
+ * Analyzes a function to see if it wraps a vanilla-extract function
89
+ */
90
+ analyzeWrapperFunction(functionName, functionNode) {
91
+ const body = functionNode.body;
92
+ // Handle arrow functions with expression body
93
+ if (functionNode.type === 'ArrowFunctionExpression' && body.type !== 'BlockStatement') {
94
+ this.analyzeWrapperExpression(functionName, body);
95
+ return;
96
+ }
97
+ // Handle functions with block statement body
98
+ if (body.type === 'BlockStatement') {
99
+ this.traverseBlockForVanillaExtractCalls(functionName, body);
100
+ }
101
+ }
102
+ /**
103
+ * Analyzes a wrapper function expression to detect vanilla-extract calls and parameter mapping
104
+ */
105
+ analyzeWrapperExpression(wrapperName, expression) {
106
+ if (expression.type === 'CallExpression' && expression.callee.type === 'Identifier') {
107
+ const calledFunction = expression.callee.name;
108
+ if (this.isTrackedFunction(calledFunction)) {
109
+ const originalName = this.getOriginalName(calledFunction);
110
+ if (originalName) {
111
+ // For now, create a simple wrapper info
112
+ const wrapperInfo = {
113
+ originalFunction: originalName,
114
+ parameterMapping: 1, // layerStyle uses second parameter as the style object
115
+ };
116
+ this.wrapperFunctions.set(wrapperName, wrapperInfo);
117
+ this.categorizeFunction(wrapperName, originalName);
118
+ }
119
+ }
120
+ }
121
+ }
122
+ /**
123
+ * Checks if a node is a vanilla-extract function call
124
+ */
125
+ checkForVanillaExtractCall(wrapperName, node) {
126
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
127
+ const calledFunction = node.callee.name;
128
+ if (this.isTrackedFunction(calledFunction)) {
129
+ const originalName = this.getOriginalName(calledFunction);
130
+ if (originalName) {
131
+ const wrapperInfo = {
132
+ originalFunction: originalName,
133
+ parameterMapping: 0, // Default to first parameter
134
+ };
135
+ this.wrapperFunctions.set(wrapperName, wrapperInfo);
136
+ this.categorizeFunction(wrapperName, originalName);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ /**
142
+ * Traverses a block statement to find vanilla-extract calls
143
+ */
144
+ traverseBlockForVanillaExtractCalls(wrapperName, block) {
145
+ for (const statement of block.body) {
146
+ if (statement.type === 'ReturnStatement' && statement.argument) {
147
+ this.checkForVanillaExtractCall(wrapperName, statement.argument);
148
+ }
149
+ else if (statement.type === 'ExpressionStatement') {
150
+ this.checkForVanillaExtractCall(wrapperName, statement.expression);
151
+ }
152
+ }
153
+ }
154
+ /**
155
+ * Checks if a function name corresponds to a tracked vanilla-extract function
156
+ */
157
+ isTrackedFunction(functionName) {
158
+ return this.imports.has(functionName) || this.wrapperFunctions.has(functionName);
159
+ }
160
+ /**
161
+ * Gets the category of a tracked function
162
+ */
163
+ getFunctionCategory(functionName) {
164
+ if (this.trackedFunctions.styleFunctions.has(functionName)) {
165
+ return 'styleFunctions';
166
+ }
167
+ if (this.trackedFunctions.recipeFunctions.has(functionName)) {
168
+ return 'recipeFunctions';
169
+ }
170
+ if (this.trackedFunctions.fontFaceFunctions.has(functionName)) {
171
+ return 'fontFaceFunctions';
172
+ }
173
+ if (this.trackedFunctions.globalFunctions.has(functionName)) {
174
+ return 'globalFunctions';
175
+ }
176
+ if (this.trackedFunctions.keyframeFunctions.has(functionName)) {
177
+ return 'keyframeFunctions';
178
+ }
179
+ return null;
180
+ }
181
+ /**
182
+ * Gets the original imported name for a local function name
183
+ */
184
+ getOriginalName(localName) {
185
+ const reference = this.imports.get(localName);
186
+ if (reference) {
187
+ return reference.importedName;
188
+ }
189
+ // Check if it's a wrapper function
190
+ const wrapperInfo = this.wrapperFunctions.get(localName);
191
+ return wrapperInfo?.originalFunction ?? null;
192
+ }
193
+ /**
194
+ * Gets wrapper function information
195
+ */
196
+ getWrapperInfo(functionName) {
197
+ return this.wrapperFunctions.get(functionName) ?? null;
198
+ }
199
+ /**
200
+ * Gets all tracked functions by category
201
+ */
202
+ getTrackedFunctions() {
203
+ return this.trackedFunctions;
204
+ }
205
+ /**
206
+ * Resets the tracker state (useful for processing multiple files)
207
+ */
208
+ reset() {
209
+ this.imports.clear();
210
+ this.wrapperFunctions.clear();
211
+ this.trackedFunctions.styleFunctions.clear();
212
+ this.trackedFunctions.recipeFunctions.clear();
213
+ this.trackedFunctions.fontFaceFunctions.clear();
214
+ this.trackedFunctions.globalFunctions.clear();
215
+ this.trackedFunctions.keyframeFunctions.clear();
216
+ }
217
+ isVanillaExtractSource(source) {
218
+ return (source === '@vanilla-extract/css' ||
219
+ source === '@vanilla-extract/recipes' ||
220
+ source.startsWith('@vanilla-extract/'));
221
+ }
222
+ categorizeFunction(localName, importedName) {
223
+ switch (importedName) {
224
+ case 'style':
225
+ case 'styleVariants':
226
+ this.trackedFunctions.styleFunctions.add(localName);
227
+ break;
228
+ case 'recipe':
229
+ this.trackedFunctions.recipeFunctions.add(localName);
230
+ break;
231
+ case 'fontFace':
232
+ case 'globalFontFace':
233
+ this.trackedFunctions.fontFaceFunctions.add(localName);
234
+ break;
235
+ case 'globalStyle':
236
+ case 'globalKeyframes':
237
+ this.trackedFunctions.globalFunctions.add(localName);
238
+ break;
239
+ case 'keyframes':
240
+ this.trackedFunctions.keyframeFunctions.add(localName);
241
+ break;
242
+ }
243
+ }
244
+ }
245
+ /**
246
+ * Creates a visitor that tracks vanilla-extract imports and bindings
247
+ */
248
+ export function createReferenceTrackingVisitor(tracker) {
249
+ return {
250
+ ImportDeclaration(node) {
251
+ tracker.processImportDeclaration(node);
252
+ },
253
+ VariableDeclarator(node) {
254
+ tracker.processVariableDeclarator(node);
255
+ },
256
+ FunctionDeclaration(node) {
257
+ tracker.processFunctionDeclaration(node);
258
+ },
259
+ };
260
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const vanillaExtract: {
1
+ declare const vanillaExtract: {
2
2
  meta: {
3
3
  name: string;
4
4
  version: string;
@@ -11,16 +11,6 @@ export declare const vanillaExtract: {
11
11
  'no-unknown-unit': import("eslint").Rule.RuleModule;
12
12
  'no-zero-unit': import("eslint").Rule.RuleModule;
13
13
  };
14
- configs: {
15
- recommended: {
16
- plugins: string[];
17
- rules: {
18
- 'vanilla-extract/concentric-order': string;
19
- 'vanilla-extract/no-empty-style-blocks': string;
20
- 'vanilla-extract/no-unknown-unit': string;
21
- 'vanilla-extract/no-zero-unit': string;
22
- };
23
- };
24
- };
14
+ configs: {};
25
15
  };
26
16
  export default vanillaExtract;
package/dist/index.js CHANGED
@@ -4,10 +4,10 @@ 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 noUnknownUnitRule from './css-rules/no-unknown-unit/rule-definition.js';
6
6
  import noZeroUnitRule from './css-rules/no-zero-unit/rule-definition.js';
7
- export const vanillaExtract = {
7
+ const vanillaExtract = {
8
8
  meta: {
9
9
  name: '@antebudimir/eslint-plugin-vanilla-extract',
10
- version: '1.9.0',
10
+ version: '1.11.0',
11
11
  },
12
12
  rules: {
13
13
  'alphabetical-order': alphabeticalOrderRule,
@@ -17,16 +17,19 @@ export const vanillaExtract = {
17
17
  'no-unknown-unit': noUnknownUnitRule,
18
18
  'no-zero-unit': noZeroUnitRule,
19
19
  },
20
- configs: {
21
- recommended: {
22
- plugins: ['vanilla-extract'],
23
- rules: {
24
- 'vanilla-extract/concentric-order': 'error',
25
- 'vanilla-extract/no-empty-style-blocks': 'error',
26
- 'vanilla-extract/no-unknown-unit': 'error',
27
- 'vanilla-extract/no-zero-unit': 'error',
28
- },
20
+ configs: {},
21
+ };
22
+ Object.assign(vanillaExtract.configs, {
23
+ recommended: {
24
+ plugins: {
25
+ 'vanilla-extract': vanillaExtract,
26
+ },
27
+ rules: {
28
+ 'vanilla-extract/concentric-order': 'error',
29
+ 'vanilla-extract/no-empty-style-blocks': 'error',
30
+ 'vanilla-extract/no-unknown-unit': 'error',
31
+ 'vanilla-extract/no-zero-unit': 'error',
29
32
  },
30
33
  },
31
- };
34
+ });
32
35
  export default vanillaExtract;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "description": "ESLint plugin for enforcing best practices in vanilla-extract CSS styles, including CSS property ordering and additional linting rules.",
5
5
  "author": "Ante Budimir",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "node": ">=18.18.0"
46
46
  },
47
47
  "peerDependencies": {
48
- "eslint": ">=9.0.0"
48
+ "eslint": ">=8.57.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@eslint/eslintrc": "^3.3.0",