@antebudimir/eslint-plugin-vanilla-extract 1.18.0 → 2.0.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 +7 -3
- package/dist/css-rules/alphabetical-order/rule-definition.js +0 -1
- package/dist/css-rules/concentric-order/rule-definition.js +0 -1
- package/dist/css-rules/custom-order/rule-definition.js +0 -1
- package/dist/css-rules/no-empty-blocks/rule-definition.js +0 -1
- package/dist/css-rules/no-px-unit/rule-definition.js +0 -1
- package/dist/css-rules/no-trailing-zero/rule-definition.js +0 -1
- package/dist/css-rules/no-unitless-values/rule-definition.js +0 -1
- package/dist/css-rules/no-unknown-unit/rule-definition.js +0 -1
- package/dist/css-rules/no-zero-unit/rule-definition.js +0 -1
- package/dist/css-rules/prefer-logical-properties/logical-properties-processor.js +6 -1
- package/dist/css-rules/prefer-logical-properties/rule-definition.js +0 -1
- package/dist/css-rules/prefer-theme-tokens/theme-token-processor.js +12 -2
- package/dist/css-rules/prefer-theme-tokens/theme-token-visitor-creator.js +5 -2
- package/dist/css-rules/shared-utils/css-order-fixer.d.ts +7 -3
- package/dist/css-rules/shared-utils/css-order-fixer.js +129 -19
- package/dist/index.js +1 -1
- package/package.json +10 -9
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ Comprehensive ESLint plugin for vanilla-extract that enforces best practices in
|
|
|
27
27
|
- Alphabetical ordering for clean, predictable style organization
|
|
28
28
|
- Concentric ordering for logical, outside-in property arrangement
|
|
29
29
|
- Custom group ordering option for more fine-grained control
|
|
30
|
-
- Compatible with ESLint 8.57.0
|
|
30
|
+
- Compatible with ESLint 8.57.0+, 9.x, and 10.x (flat config)
|
|
31
31
|
- Provides auto-fix capability to automatically sort properties
|
|
32
32
|
- Handles multiple vanilla-extract APIs (style, styleVariants, recipe, globalStyle, etc.)
|
|
33
33
|
- Handles complex cases like nested objects, arrays of styles, and pseudo selectors
|
|
@@ -38,13 +38,17 @@ Comprehensive ESLint plugin for vanilla-extract that enforces best practices in
|
|
|
38
38
|
|
|
39
39
|
## Requirements
|
|
40
40
|
|
|
41
|
-
- ESLint 8.57.0 or higher
|
|
42
|
-
- Node.js
|
|
41
|
+
- ESLint 8.57.0 or higher (8.57, 9.x, and 10.x are all supported and CI-tested)
|
|
42
|
+
- Node.js 20.19.0 or higher
|
|
43
43
|
- ESM (ECMAScript Modules) only
|
|
44
44
|
- Flat config system using either:
|
|
45
45
|
- `eslint.config.mjs` (recommended, always works with ESM plugins)
|
|
46
46
|
- `eslint.config.js` (only if your package.json has `"type": "module"`)
|
|
47
47
|
|
|
48
|
+
> **Note for ESLint 10 users:** if you also use `typescript-eslint`, make sure you're on
|
|
49
|
+
> `typescript-eslint` >= 8.46 (first release declaring ESLint 10 support) to avoid
|
|
50
|
+
> peer-dependency conflicts.
|
|
51
|
+
|
|
48
52
|
## Installation
|
|
49
53
|
|
|
50
54
|
```bash
|
|
@@ -4,7 +4,6 @@ const noPxUnitRule = {
|
|
|
4
4
|
type: 'suggestion',
|
|
5
5
|
docs: {
|
|
6
6
|
description: "disallow 'px' units in vanilla-extract style objects, with allowlist option",
|
|
7
|
-
category: 'Best Practices',
|
|
8
7
|
recommended: false,
|
|
9
8
|
},
|
|
10
9
|
// Suggestions are reported from helper modules, so static analysis in this file can’t detect them; disable the false positive.
|
|
@@ -97,7 +97,12 @@ const createPropertyKeyFix = (fixer, property, newPropertyName, context) => {
|
|
|
97
97
|
}
|
|
98
98
|
if (key.type === AST_NODE_TYPES.Literal && typeof key.value === 'string') {
|
|
99
99
|
// Preserve quote style
|
|
100
|
-
|
|
100
|
+
// getSourceCode() removed in ESLint v10
|
|
101
|
+
const sourceCode = context.sourceCode ||
|
|
102
|
+
context.getSourceCode?.();
|
|
103
|
+
if (!sourceCode) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
101
106
|
const originalText = sourceCode.getText(key);
|
|
102
107
|
const quote = originalText[0];
|
|
103
108
|
return fixer.replaceText(key, `${quote}${newPropertyName}${quote}`);
|
|
@@ -318,7 +318,12 @@ export const processThemeTokensInStyleObject = (context, node, options, analyzer
|
|
|
318
318
|
// Check if property value is a TemplateLiteral (e.g., `${rem(4)} ${rem(8)}`)
|
|
319
319
|
if (checkHelperFunctions && property.value.type === AST_NODE_TYPES.TemplateLiteral) {
|
|
320
320
|
// Get the source code of the template literal
|
|
321
|
-
|
|
321
|
+
// getSourceCode() removed in ESLint v10
|
|
322
|
+
const sourceCode = context.sourceCode ||
|
|
323
|
+
context.getSourceCode?.();
|
|
324
|
+
if (!sourceCode) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
322
327
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
323
328
|
const templateText = sourceCode.getText(property.value);
|
|
324
329
|
// Try to evaluate it
|
|
@@ -378,7 +383,12 @@ export const processThemeTokensInStyleObject = (context, node, options, analyzer
|
|
|
378
383
|
// Check if property value is a CallExpression (e.g., rem(48), clsx(...))
|
|
379
384
|
if (checkHelperFunctions && property.value.type === AST_NODE_TYPES.CallExpression) {
|
|
380
385
|
// Get the source code of the call expression
|
|
381
|
-
|
|
386
|
+
// getSourceCode() removed in ESLint v10
|
|
387
|
+
const sourceCode = context.sourceCode ||
|
|
388
|
+
context.getSourceCode?.();
|
|
389
|
+
if (!sourceCode) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
382
392
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
383
393
|
const callExpressionText = sourceCode.getText(property.value);
|
|
384
394
|
// Try to evaluate it
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cwd as nodeCwd } from 'node:process';
|
|
1
2
|
import * as path from 'path';
|
|
2
3
|
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
3
4
|
import { processRecipeProperties } from '../shared-utils/recipe-property-processor.js';
|
|
@@ -20,8 +21,10 @@ export const createThemeTokenVisitors = (context, options) => {
|
|
|
20
21
|
// Load theme contracts if provided
|
|
21
22
|
const themeContracts = options.themeContracts || [];
|
|
22
23
|
if (themeContracts.length > 0) {
|
|
23
|
-
// Use
|
|
24
|
-
|
|
24
|
+
// Use project root (cwd) to resolve theme contracts, or fallback to the linted file's directory.
|
|
25
|
+
// `cwd`/`filename` properties exist on ESLint v9+/v10+; `getCwd()`/`getFilename()` are the ESLint v8 fallbacks.
|
|
26
|
+
const legacyCtx = context;
|
|
27
|
+
const baseDir = legacyCtx.cwd || path.dirname(legacyCtx.filename || legacyCtx.getFilename?.() || nodeCwd());
|
|
25
28
|
themeContracts.forEach((contractPath) => {
|
|
26
29
|
analyzer.loadThemeContract(contractPath, baseDir);
|
|
27
30
|
});
|
|
@@ -10,8 +10,12 @@ import type { Rule } from 'eslint';
|
|
|
10
10
|
*
|
|
11
11
|
* This function performs the following steps:
|
|
12
12
|
* 1. Sorts the input properties using the provided comparison function.
|
|
13
|
-
* 2.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* 2. Splits every property into its attachable "chunk" (leading comments,
|
|
14
|
+
* property text, same-line trailing comment).
|
|
15
|
+
* 3. For every file slot whose property changed position, replaces the slot's
|
|
16
|
+
* whole chunk (from its first leading comment to its trailing comma /
|
|
17
|
+
* same-line comment) with the incoming property's chunk, re-adding the
|
|
18
|
+
* trailing comma when required so the result stays syntactically valid.
|
|
19
|
+
* 4. Comments always move together with the property they describe.
|
|
16
20
|
*/
|
|
17
21
|
export declare const generateFixesForCSSOrder: <T>(eslintFixer: Rule.RuleFixer, ruleContext: Rule.RuleContext, cssProperties: T[], compareProperties: (firstProperty: T, secondProperty: T) => number, extractNode: (property: T) => Rule.Node) => Rule.Fix[];
|
|
@@ -1,3 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the pieces of source text that belong to a property and should move
|
|
3
|
+
* with it when properties are reordered:
|
|
4
|
+
* 1. The leading comments directly above the property (contiguous block of
|
|
5
|
+
* comments with no blank line separating them from the property).
|
|
6
|
+
* 2. The property node itself.
|
|
7
|
+
* 3. A trailing comment on the same line as the property, if present.
|
|
8
|
+
*
|
|
9
|
+
* Delimiters that belong to the *slot* in the file rather than to the property
|
|
10
|
+
* (the trailing comma, the newlines, the indentation of the first line) are
|
|
11
|
+
* deliberately excluded so that swapping chunk texts between slots always
|
|
12
|
+
* produces valid syntax.
|
|
13
|
+
*/
|
|
14
|
+
const createPropertyChunkExtractor = (sourceCode) => {
|
|
15
|
+
// Comments already claimed as trailing comments of an earlier property must
|
|
16
|
+
// not also be claimed as leading comments of the next property.
|
|
17
|
+
const claimedTrailingComments = new Set();
|
|
18
|
+
const extractLeadingComments = (node) => {
|
|
19
|
+
const allCommentsBefore = sourceCode.getCommentsBefore(node);
|
|
20
|
+
if (allCommentsBefore.length === 0) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
// Walk upwards from the property, collecting the contiguous run of
|
|
24
|
+
// comments directly above it (no blank line in between).
|
|
25
|
+
const attachedComments = [];
|
|
26
|
+
let currentEndLine = node.loc.start.line;
|
|
27
|
+
for (let index = allCommentsBefore.length - 1; index >= 0; index--) {
|
|
28
|
+
const comment = allCommentsBefore[index];
|
|
29
|
+
if (!comment || claimedTrailingComments.has(comment)) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
// A blank line between the comment and what follows means the comment
|
|
33
|
+
// documents the block, not this specific property.
|
|
34
|
+
if (currentEndLine - comment.loc.end.line > 1) {
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
attachedComments.unshift(comment);
|
|
38
|
+
currentEndLine = comment.loc.start.line;
|
|
39
|
+
}
|
|
40
|
+
if (attachedComments.length === 0) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
// Slice from the first attached comment up to the property start. This
|
|
44
|
+
// includes the newlines and indentation between the comments and before
|
|
45
|
+
// the property, but not the indentation of the first comment's line (the
|
|
46
|
+
// destination slot already provides that indentation).
|
|
47
|
+
const firstComment = attachedComments[0];
|
|
48
|
+
const firstCommentStart = firstComment.range[0];
|
|
49
|
+
const commentsText = sourceCode.text.slice(firstCommentStart, node.range[0]);
|
|
50
|
+
return { start: firstCommentStart, text: commentsText };
|
|
51
|
+
};
|
|
52
|
+
// ESLint attaches a same-line trailing comment to the *following* node's
|
|
53
|
+
// leading comments rather than to this node, so trailing comments are found
|
|
54
|
+
// by scanning all comments for one starting on the property's last line.
|
|
55
|
+
const findTrailingComment = (node) => sourceCode.getAllComments().find((comment) => comment.loc.start.line === node.loc.end.line);
|
|
56
|
+
const extractTrailingCommentText = (node) => {
|
|
57
|
+
const trailingComment = findTrailingComment(node);
|
|
58
|
+
if (!trailingComment) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
claimedTrailingComments.add(trailingComment);
|
|
62
|
+
return sourceCode.text.slice(trailingComment.range[0], trailingComment.range[1]);
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
extractPropertyChunk: (node) => {
|
|
66
|
+
const leadingComments = extractLeadingComments(node);
|
|
67
|
+
return {
|
|
68
|
+
leadingCommentsStart: leadingComments?.start ?? null,
|
|
69
|
+
leadingCommentsText: leadingComments?.text ?? '',
|
|
70
|
+
nodeText: sourceCode.getText(node),
|
|
71
|
+
trailingCommentText: extractTrailingCommentText(node),
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
findTrailingComment,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
1
77
|
/**
|
|
2
78
|
* Generates ESLint fixes for CSS property ordering violations.
|
|
3
79
|
* @param eslintFixer The ESLint fixer instance used to create fix objects.
|
|
@@ -9,31 +85,65 @@
|
|
|
9
85
|
*
|
|
10
86
|
* This function performs the following steps:
|
|
11
87
|
* 1. Sorts the input properties using the provided comparison function.
|
|
12
|
-
* 2.
|
|
13
|
-
*
|
|
14
|
-
*
|
|
88
|
+
* 2. Splits every property into its attachable "chunk" (leading comments,
|
|
89
|
+
* property text, same-line trailing comment).
|
|
90
|
+
* 3. For every file slot whose property changed position, replaces the slot's
|
|
91
|
+
* whole chunk (from its first leading comment to its trailing comma /
|
|
92
|
+
* same-line comment) with the incoming property's chunk, re-adding the
|
|
93
|
+
* trailing comma when required so the result stays syntactically valid.
|
|
94
|
+
* 4. Comments always move together with the property they describe.
|
|
15
95
|
*/
|
|
16
96
|
export const generateFixesForCSSOrder = (eslintFixer, ruleContext, cssProperties, compareProperties, extractNode) => {
|
|
17
97
|
const sourceCode = ruleContext.sourceCode;
|
|
18
98
|
// Sort properties using the provided comparison function
|
|
19
99
|
const sortedProperties = [...cssProperties].sort(compareProperties);
|
|
20
|
-
|
|
21
|
-
const
|
|
100
|
+
const { extractPropertyChunk, findTrailingComment } = createPropertyChunkExtractor(sourceCode);
|
|
101
|
+
const chunksByProperty = new Map(cssProperties.map((property) => [
|
|
22
102
|
property,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
103
|
+
extractPropertyChunk(extractNode(property)),
|
|
104
|
+
]));
|
|
105
|
+
// Generate fixes for slots whose property has changed
|
|
106
|
+
return cssProperties
|
|
107
|
+
.map((slotProperty, slotIndex) => {
|
|
108
|
+
const sortedProperty = sortedProperties[slotIndex];
|
|
109
|
+
if (!sortedProperty || sortedProperty === slotProperty) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const slotChunk = chunksByProperty.get(slotProperty);
|
|
113
|
+
const sortedChunk = chunksByProperty.get(sortedProperty);
|
|
114
|
+
if (!slotChunk || !sortedChunk) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const slotNode = extractNode(slotProperty);
|
|
118
|
+
// The replacement range spans the slot's whole attachable chunk: from
|
|
119
|
+
// its first leading comment (if any) to the end of its trailing comma
|
|
120
|
+
// and/or same-line trailing comment. This consumes the original
|
|
121
|
+
// comments and punctuation so they are neither left behind nor doubled.
|
|
122
|
+
const rangeStart = slotChunk.leadingCommentsStart ?? slotNode.range[0];
|
|
123
|
+
let rangeEnd = slotNode.range[1];
|
|
124
|
+
const tokenAfter = sourceCode.getTokenAfter(slotNode);
|
|
125
|
+
if (tokenAfter && tokenAfter.value === ',' && tokenAfter.range[1] > rangeEnd) {
|
|
126
|
+
rangeEnd = tokenAfter.range[1];
|
|
127
|
+
}
|
|
128
|
+
if (slotChunk.trailingCommentText !== null) {
|
|
129
|
+
const trailingComment = findTrailingComment(slotNode);
|
|
130
|
+
if (trailingComment && trailingComment.range[1] > rangeEnd) {
|
|
131
|
+
rangeEnd = trailingComment.range[1];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Rebuild the incoming property's text, preserving the slot's comma
|
|
135
|
+
// convention: if the original property in this slot had a trailing
|
|
136
|
+
// comma, keep one; if not (typically the object's last property), don't.
|
|
137
|
+
const slotHadTrailingComma = tokenAfter?.value === ',';
|
|
138
|
+
const replacementText = [
|
|
139
|
+
sortedChunk.leadingCommentsText,
|
|
140
|
+
sortedChunk.nodeText,
|
|
141
|
+
slotHadTrailingComma ? ',' : '',
|
|
142
|
+
sortedChunk.trailingCommentText !== null
|
|
143
|
+
? `${slotHadTrailingComma ? ' ' : ''}${sortedChunk.trailingCommentText}`
|
|
144
|
+
: '',
|
|
145
|
+
].join('');
|
|
146
|
+
return eslintFixer.replaceTextRange([rangeStart, rangeEnd], replacementText);
|
|
37
147
|
})
|
|
38
148
|
.filter((fix) => fix !== null);
|
|
39
149
|
};
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,7 @@ import preferThemeTokensRule from './css-rules/prefer-theme-tokens/index.js';
|
|
|
12
12
|
const vanillaExtract = {
|
|
13
13
|
meta: {
|
|
14
14
|
name: '@antebudimir/eslint-plugin-vanilla-extract',
|
|
15
|
-
version: '
|
|
15
|
+
version: '2.0.0',
|
|
16
16
|
},
|
|
17
17
|
rules: {
|
|
18
18
|
'alphabetical-order': alphabeticalOrderRule,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antebudimir/eslint-plugin-vanilla-extract",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"!dist/css-sample/"
|
|
43
43
|
],
|
|
44
44
|
"engines": {
|
|
45
|
-
"node": ">=
|
|
45
|
+
"node": ">=20.19.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": ">=8.57.0"
|
|
@@ -54,27 +54,28 @@
|
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@eslint/eslintrc": "^3.3.0",
|
|
56
56
|
"@types/node": "^20.17.24",
|
|
57
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
58
|
-
"@typescript-eslint/parser": "^8.
|
|
59
|
-
"@typescript-eslint/rule-tester": "^8.
|
|
60
|
-
"@typescript-eslint/utils": "^8.
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "^8.71.2",
|
|
58
|
+
"@typescript-eslint/parser": "^8.71.2",
|
|
59
|
+
"@typescript-eslint/rule-tester": "^8.71.2",
|
|
60
|
+
"@typescript-eslint/utils": "^8.71.2",
|
|
61
61
|
"@vanilla-extract/css": "^1.17.1",
|
|
62
62
|
"@vanilla-extract/recipes": "^0.5.5",
|
|
63
63
|
"@vanilla-extract/sprinkles": "^1.6.0",
|
|
64
64
|
"@vitest/coverage-v8": "3.0.8",
|
|
65
|
-
"eslint": "^
|
|
65
|
+
"eslint": "^10.13.0",
|
|
66
66
|
"eslint-config-prettier": "^10.1.1",
|
|
67
67
|
"eslint-import-resolver-typescript": "^3.8.5",
|
|
68
68
|
"eslint-plugin-eslint-plugin": "^6.4.0",
|
|
69
|
-
"eslint-plugin-import": "^
|
|
69
|
+
"eslint-plugin-import-x": "^4.17.1",
|
|
70
70
|
"eslint-vitest-rule-tester": "^1.1.0",
|
|
71
71
|
"prettier": "^3.5.3",
|
|
72
72
|
"typescript": "^5.8.2",
|
|
73
|
-
"typescript-eslint": "^8.
|
|
73
|
+
"typescript-eslint": "^8.71.2",
|
|
74
74
|
"vitest": "3.0.8"
|
|
75
75
|
},
|
|
76
76
|
"scripts": {
|
|
77
77
|
"build": "tsc",
|
|
78
|
+
"test:integration": "bash scripts/integration-test.sh",
|
|
78
79
|
"format": "prettier --write .",
|
|
79
80
|
"lint": "eslint src --ext .ts --fix --max-warnings 0",
|
|
80
81
|
"test": "vitest run",
|