@antebudimir/eslint-plugin-vanilla-extract 1.4.0 → 1.4.7

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 CHANGED
@@ -1,9 +1,13 @@
1
1
  # @antebudimir/eslint-plugin-vanilla-extract
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@antebudimir/eslint-plugin-vanilla-extract.svg)](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract)
3
+ [![npm version](https://img.shields.io/npm/v/@antebudimir/eslint-plugin-vanilla-extract.svg)](https://www.npmjs.com/package/@antebudimir/eslint-plugin-vanilla-extract) [![CI](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml/badge.svg)](https://github.com/antebudimir/eslint-plugin-vanilla-extract/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/antebudimir/eslint-plugin-vanilla-extract/badge.svg?branch=main)](https://coveralls.io/github/antebudimir/eslint-plugin-vanilla-extract?branch=main)
4
4
 
5
5
  An ESLint plugin for enforcing best practices in [vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract) CSS styles, including CSS property ordering and additional linting rules. Available presets are for alphabetical and [concentric](https://rhodesmill.org/brandon/2011/concentric-css/) CSS ordering. The plugin also supports a custom group ordering option based on groups available in [concentric CSS](src/css-rules/concentric-order/concentric-groups.ts).
6
6
 
7
+ ## Demo
8
+
9
+ ![Plugin Demo](https://github.com/user-attachments/assets/93ba118d-84df-4da0-ac68-fdb429e581d6)
10
+
7
11
  ## Features
8
12
 
9
13
  - Enforces CSS property ordering in vanilla-extract style objects with two available presets:
@@ -253,14 +257,14 @@ The roadmap outlines the project's current status and future plans:
253
257
  - Initial release with support for alphabetical, concentric, and custom group CSS ordering.
254
258
  - Auto-fix capability integrated into ESLint.
255
259
  - Support for multiple vanilla-extract APIs (e.g., `style`, `styleVariants`, `recipe`, `globalStyle`, `fontFace`, etc.).
260
+ - Rules tested.
256
261
 
257
262
  ### Current Work
258
263
 
259
- - Test coverage.
264
+ - `no-empty-blocks` rule to disallow empty blocks.
260
265
 
261
266
  ### Upcoming Features
262
267
 
263
- - `no-empty-blocks` rule to disallow empty blocks.
264
268
  - `no-unknown-units` rule to disallow unknown units.
265
269
  - `no-number-trailing-zeros` rule to disallow trailing zeros in numbers.
266
270
  - `no-zero-unit` rule to disallow units when the value is zero.
@@ -8,9 +8,10 @@ import type { Rule } from 'eslint';
8
8
  * @returns An object with visitor functions for the ESLint rule.
9
9
  *
10
10
  * This function sets up visitors for the following cases:
11
- * 1. Style-related functions: 'style', 'styleVariants', 'createVar', 'createTheme', 'createThemeContract'
12
- * 2. The 'globalStyle' function
13
- * 3. The 'recipe' function
11
+ * 1. The fontFace and globalFontFace functions.
12
+ * 2. Style-related functions: 'keyframes', 'style', 'styleVariants'.
13
+ * 3. The 'globalStyle' and 'globalKeyframes' function
14
+ * 4. The 'recipe' function
14
15
  *
15
16
  * Each visitor applies the appropriate ordering strategy to the style objects in these function calls.
16
17
  */
@@ -16,9 +16,10 @@ import { processStyleNode } from './style-node-processor.js';
16
16
  * @returns An object with visitor functions for the ESLint rule.
17
17
  *
18
18
  * This function sets up visitors for the following cases:
19
- * 1. Style-related functions: 'style', 'styleVariants', 'createVar', 'createTheme', 'createThemeContract'
20
- * 2. The 'globalStyle' function
21
- * 3. The 'recipe' function
19
+ * 1. The fontFace and globalFontFace functions.
20
+ * 2. Style-related functions: 'keyframes', 'style', 'styleVariants'.
21
+ * 3. The 'globalStyle' and 'globalKeyframes' function
22
+ * 4. The 'recipe' function
22
23
  *
23
24
  * Each visitor applies the appropriate ordering strategy to the style objects in these function calls.
24
25
  */
@@ -58,7 +59,7 @@ export const createNodeVisitors = (ruleContext, orderingStrategy, userDefinedGro
58
59
  return;
59
60
  }
60
61
  // Handle style-related functions
61
- if (['createThemeContract', 'createVar', 'createTheme', 'keyframes', 'style', 'styleVariants'].includes(node.callee.name)) {
62
+ if (['keyframes', 'style', 'styleVariants'].includes(node.callee.name)) {
62
63
  if (node.arguments.length > 0) {
63
64
  const styleArguments = node.arguments[0];
64
65
  processStyleNode(ruleContext, styleArguments, processProperty);
@@ -6,13 +6,10 @@ import { TSESTree } from '@typescript-eslint/utils';
6
6
  * @param processProperty A function to process each object expression.
7
7
  */
8
8
  export const processStyleNode = (ruleContext, node, processProperty) => {
9
- if (!node) {
10
- return;
11
- }
12
- if (node.type === 'ObjectExpression') {
9
+ if (node?.type === 'ObjectExpression') {
13
10
  processProperty(ruleContext, node);
14
11
  }
15
- if (node.type === 'ArrayExpression') {
12
+ if (node?.type === 'ArrayExpression') {
16
13
  node.elements.forEach((element) => {
17
14
  if (element && element.type === 'ObjectExpression') {
18
15
  processProperty(ruleContext, element);
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import customOrderRule from './css-rules/custom-order/rule-definition.js';
4
4
  export const vanillaExtract = {
5
5
  meta: {
6
6
  name: '@antebudimir/eslint-plugin-vanilla-extract',
7
- version: '1.4.0',
7
+ version: '1.4.7',
8
8
  },
9
9
  rules: {
10
10
  'alphabetical-order': alphabeticalOrderRule,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.4.0",
4
- "description": "ESLint plugin for enforcing CSS ordering in vanilla-extract styles",
3
+ "version": "1.4.7",
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",
7
7
  "keywords": [
@@ -38,6 +38,7 @@
38
38
  "dist",
39
39
  "LICENSE",
40
40
  "README.md",
41
+ "!dist/**/__tests__",
41
42
  "!dist/css-sample/"
42
43
  ],
43
44
  "engines": {
@@ -51,22 +52,29 @@
51
52
  "@types/node": "^20.17.19",
52
53
  "@typescript-eslint/eslint-plugin": "^8.25.0",
53
54
  "@typescript-eslint/parser": "^8.25.0",
55
+ "@typescript-eslint/rule-tester": "^8.26.0",
54
56
  "@typescript-eslint/utils": "^8.25.0",
55
57
  "@vanilla-extract/css": "^1.17.1",
56
58
  "@vanilla-extract/recipes": "^0.5.5",
59
+ "@vitest/coverage-v8": "3.0.8",
57
60
  "eslint": "^9.21.0",
58
61
  "eslint-config-prettier": "^10.0.2",
59
62
  "eslint-import-resolver-typescript": "^3.8.3",
60
63
  "eslint-plugin-eslint-plugin": "^6.4.0",
61
64
  "eslint-plugin-import": "^2.31.0",
65
+ "eslint-vitest-rule-tester": "^1.1.0",
62
66
  "prettier": "^3.5.2",
63
67
  "typescript": "^5.7.3",
64
- "typescript-eslint": "^8.25.0"
68
+ "typescript-eslint": "^8.25.0",
69
+ "vitest": "^3.0.8"
65
70
  },
66
71
  "scripts": {
67
72
  "build": "tsc",
68
73
  "format": "prettier --write .",
69
74
  "lint": "eslint src --ext .ts --fix --max-warnings 0",
75
+ "test": "vitest run",
76
+ "test:watch": "vitest",
77
+ "test:coverage": "vitest run --coverage",
70
78
  "typecheck": "tsc --noEmit",
71
79
  "version:update": "node scripts/update-version.mjs"
72
80
  }
@@ -1,10 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- import { TSESTree } from '@typescript-eslint/utils';
3
- /**
4
- * Processes a font face style object to enforce special ordering:
5
- * 'src' first, then other properties alphabetically.
6
- *
7
- * @param ruleContext The ESLint rule context for reporting and fixing issues.
8
- * @param styleObject The object expression representing the style object.
9
- */
10
- export declare const enforceFontFaceOrderInStyleObject: (ruleContext: Rule.RuleContext, styleObject: TSESTree.ObjectExpression) => void;
@@ -1,17 +0,0 @@
1
- import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
- import { separateProperties } from '../shared-utils/property-separator.js';
3
- import { enforceFontFaceOrder } from './font-face-property-order-enforcer.js';
4
- /**
5
- * Processes a font face style object to enforce special ordering:
6
- * 'src' first, then other properties alphabetically.
7
- *
8
- * @param ruleContext The ESLint rule context for reporting and fixing issues.
9
- * @param styleObject The object expression representing the style object.
10
- */
11
- export const enforceFontFaceOrderInStyleObject = (ruleContext, styleObject) => {
12
- // if (!styleObject || styleObject.type !== AST_NODE_TYPES.ObjectExpression) {
13
- // return;
14
- // }
15
- // const { regularProperties } = separateProperties(styleObject.properties);
16
- // enforceFontFaceOrder(ruleContext, regularProperties);
17
- };
@@ -1,12 +0,0 @@
1
- import { TSESTree } from '@typescript-eslint/utils';
2
- export type ThemeValue = string | number | null;
3
- export type ThemeObject = {
4
- [key: string]: ThemeValue | ThemeObject;
5
- };
6
- export interface ThemeTokenMatch {
7
- token: string;
8
- path: string[];
9
- }
10
- export declare function isVanillaExtractStyleCall(node: TSESTree.CallExpression): boolean;
11
- export declare function findThemeTokenForValue(value: ThemeValue, themeObj: ThemeObject | null, path?: string[]): ThemeTokenMatch | null;
12
- export declare function findThemeTokenForPropertyValue(property: string, value: ThemeValue, themeObj: ThemeObject, path?: string[]): ThemeTokenMatch | null;
@@ -1,80 +0,0 @@
1
- // import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
2
- // export function isVanillaExtractStyleCall(node: TSESTree.CallExpression): boolean {
3
- // return (
4
- // node.callee.type === AST_NODE_TYPES.Identifier &&
5
- // ['style', 'createVar', 'styleVariants', 'globalStyle'].includes(node.callee.name) &&
6
- // node.arguments.length > 0 &&
7
- // node.arguments[0] !== undefined &&
8
- // node.arguments[0].type === AST_NODE_TYPES.ObjectExpression
9
- // );
10
- // }
11
- // type ThemeValue = string | number | null;
12
- // type ThemeObject = { [key: string]: ThemeValue | ThemeObject };
13
- // interface ThemeTokenMatch {
14
- // token: string;
15
- // path: string[];
16
- // }
17
- // export function findThemeTokenForValue(
18
- // value: ThemeValue,
19
- // themeObj: ThemeObject | null,
20
- // path: string[] = [],
21
- // ): ThemeTokenMatch | null {
22
- // if (!themeObj || typeof themeObj !== 'object') {
23
- // return null;
24
- // }
25
- // for (const [key, val] of Object.entries(themeObj)) {
26
- // const currentPath = [...path, key];
27
- // if (String(val) === String(value)) {
28
- // return { token: currentPath.join('.'), path: currentPath };
29
- // }
30
- // if (typeof val === 'object' && val !== null) {
31
- // const result = findThemeTokenForValue(value, val, currentPath);
32
- // if (result) {
33
- // return result;
34
- // }
35
- // }
36
- // }
37
- // return null;
38
- // }
39
- // TODO 2nd
40
- import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
41
- export function isVanillaExtractStyleCall(node) {
42
- return (node.callee.type === AST_NODE_TYPES.Identifier &&
43
- ['style', 'createVar', 'styleVariants', 'globalStyle', 'createThemeContract', 'createTheme', 'keyframes'].includes(node.callee.name) &&
44
- node.arguments.length > 0 &&
45
- (node.arguments[0]?.type === AST_NODE_TYPES.ObjectExpression ||
46
- node.arguments[0]?.type === AST_NODE_TYPES.ArrayExpression));
47
- }
48
- export function findThemeTokenForValue(value, themeObj, path = []) {
49
- if (!themeObj || typeof themeObj !== 'object') {
50
- return null;
51
- }
52
- for (const [key, val] of Object.entries(themeObj)) {
53
- const currentPath = [...path, key];
54
- if (String(val) === String(value)) {
55
- return {
56
- token: currentPath.join('.'),
57
- path: currentPath,
58
- };
59
- }
60
- if (typeof val === 'object' && val !== null) {
61
- const result = findThemeTokenForValue(value, val, currentPath);
62
- if (result) {
63
- return result;
64
- }
65
- }
66
- }
67
- return null;
68
- }
69
- export function findThemeTokenForPropertyValue(property, value, themeObj, path = []) {
70
- if (themeObj && typeof themeObj === 'object' && themeObj[property]) {
71
- const group = themeObj[property];
72
- if (typeof group === 'object' && group !== null) {
73
- const result = findThemeTokenForValue(value, group, [property]);
74
- if (result) {
75
- return result;
76
- }
77
- }
78
- }
79
- return findThemeTokenForValue(value, themeObj, path);
80
- }
@@ -1,2 +0,0 @@
1
- import preferThemeTokensRule from './rule-definition.js';
2
- export default preferThemeTokensRule;
@@ -1,2 +0,0 @@
1
- import preferThemeTokensRule from './rule-definition.js';
2
- export default preferThemeTokensRule;
@@ -1,3 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- declare const preferThemeTokensRule: Rule.RuleModule;
3
- export default preferThemeTokensRule;
@@ -1,29 +0,0 @@
1
- import { createThemeTokenVisitor } from './theme-token-visitor.js';
2
- const preferThemeTokensRule = {
3
- meta: {
4
- type: 'suggestion',
5
- docs: {
6
- description: 'enforce the use of theme tokens instead of direct values in vanilla-extract styles',
7
- category: 'Best Practices',
8
- recommended: true,
9
- },
10
- fixable: 'code',
11
- schema: [
12
- {
13
- type: 'object',
14
- properties: {
15
- themeImportPath: { type: 'string' },
16
- themeVarName: { type: 'string' },
17
- },
18
- additionalProperties: false,
19
- },
20
- ],
21
- messages: {
22
- preferThemeTokens: 'Use theme token "{{token}}" instead of direct value "{{value}}"',
23
- },
24
- },
25
- create(context) {
26
- return createThemeTokenVisitor(context);
27
- },
28
- };
29
- export default preferThemeTokensRule;
@@ -1,10 +0,0 @@
1
- type ThemeValue = string | number | null;
2
- type ThemeObject = {
3
- [key: string]: ThemeValue | ThemeObject;
4
- };
5
- export declare function loadThemeTokens(themeFilePath: string): ThemeObject | null;
6
- export declare function findThemeTokenForValue(value: ThemeValue, themeObj: ThemeObject, path?: string[]): {
7
- token: string;
8
- path: string[];
9
- } | null;
10
- export {};
@@ -1,71 +0,0 @@
1
- /* eslint-disable no-console */
2
- import fs from 'fs';
3
- import path from 'path';
4
- export function loadThemeTokens(themeFilePath) {
5
- try {
6
- console.log(`Attempting to load theme from: ${themeFilePath}`);
7
- const resolvedPath = path.resolve(process.cwd(), themeFilePath);
8
- console.log(`Resolved path: ${resolvedPath}`);
9
- if (!fs.existsSync(resolvedPath)) {
10
- console.error(`Theme file not found: ${resolvedPath}`);
11
- return null;
12
- }
13
- const content = fs.readFileSync(resolvedPath, 'utf8');
14
- console.log(`File content loaded. Length: ${content.length} characters`);
15
- const themeMatch = content.match(/createTheme\([^,]+,\s*(\{[\s\S]+?\})\s*\);/);
16
- if (!themeMatch || !themeMatch[1]) {
17
- console.error('No theme implementation found in the file');
18
- return null;
19
- }
20
- const themeStr = themeMatch[1];
21
- console.log(`Theme implementation extracted. Length: ${themeStr.length} characters`);
22
- // Use a safe evaluation method to parse the theme object
23
- const themeObj = Function(`return ${themeStr}`)();
24
- console.log('Parsed theme structure:', JSON.stringify(themeObj, null, 2));
25
- return themeObj;
26
- }
27
- catch (error) {
28
- console.error('Error loading theme tokens:', error);
29
- return null;
30
- }
31
- }
32
- // function parseThemeObject(str: string): ThemeObject {
33
- // const result: ThemeObject = {};
34
- // const lines = str.split('\n');
35
- // const stack: ThemeObject[] = [result];
36
- // for (const line of lines) {
37
- // const trimmed = line.trim();
38
- // if (!trimmed || trimmed.startsWith('//')) {
39
- // continue;
40
- // }
41
- // if (trimmed.endsWith('{')) {
42
- // const key = trimmed.slice(0, -1).trim();
43
- // const newObj: ThemeObject = {};
44
- // stack[stack.length - 1]![key] = newObj;
45
- // stack.push(newObj);
46
- // } else if (trimmed === '},') {
47
- // stack.pop();
48
- // } else {
49
- // const [key, value] = trimmed.split(':').map((s) => s.trim());
50
- // if (key && value) {
51
- // stack[stack.length - 1]![key] = value === 'null' ? null : value.replace(/,$/, '');
52
- // }
53
- // }
54
- // }
55
- // return result;
56
- // }
57
- export function findThemeTokenForValue(value, themeObj, path = []) {
58
- for (const [key, val] of Object.entries(themeObj)) {
59
- const currentPath = [...path, key];
60
- if (String(val) === String(value)) {
61
- return { token: currentPath.join('.'), path: currentPath };
62
- }
63
- if (typeof val === 'object' && val !== null) {
64
- const result = findThemeTokenForValue(value, val, currentPath);
65
- if (result) {
66
- return result;
67
- }
68
- }
69
- }
70
- return null;
71
- }
@@ -1,2 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- export declare function createThemeTokenVisitor(context: Rule.RuleContext): Rule.RuleListener;
@@ -1,113 +0,0 @@
1
- // /* eslint-disable no-console */
2
- // import type { Rule } from 'eslint';
3
- // import { TSESTree } from '@typescript-eslint/utils';
4
- // import { isVanillaExtractStyleCall, findThemeTokenForValue } from './ast-helpers.js';
5
- // import { loadThemeTokens } from './theme-loader.js';
6
- import { TSESTree } from '@typescript-eslint/utils';
7
- import { createNodeVisitors } from '../shared-utils/order-strategy-visitor-creator.js';
8
- import { isVanillaExtractStyleCall, findThemeTokenForValue, findThemeTokenForPropertyValue, } from './ast-helpers.js';
9
- import { loadThemeTokens } from './theme-loader.js';
10
- /**
11
- * Helper to extract a static property name from a node’s key.
12
- */
13
- function getStaticPropertyKey(key) {
14
- if (key.type === 'Identifier') {
15
- return key.name;
16
- }
17
- if (key.type === 'Literal' && typeof key.value === 'string') {
18
- return key.value;
19
- }
20
- return null;
21
- }
22
- /**
23
- * Recursively traverse an AST node representing a style definition.
24
- */
25
- function traverseStyleNode(node, themeTokens, context, themeVarName, propertyContext) {
26
- switch (node.type) {
27
- case 'ObjectExpression':
28
- node.properties.forEach((prop) => {
29
- if (prop.type === 'Property') {
30
- const propKey = getStaticPropertyKey(prop.key);
31
- const newContext = propKey && !propKey.startsWith(':') && !propKey.startsWith('@') ? propKey : propertyContext;
32
- if (prop.value.type === 'Literal') {
33
- const value = prop.value.value;
34
- if (typeof value === 'string' || typeof value === 'number') {
35
- const matchingToken = newContext && themeTokens
36
- ? findThemeTokenForPropertyValue(newContext, value, themeTokens)
37
- : findThemeTokenForValue(value, themeTokens);
38
- if (matchingToken) {
39
- context.report({
40
- node: prop.value,
41
- messageId: 'preferThemeTokens',
42
- data: {
43
- token: matchingToken.token,
44
- value: String(value),
45
- },
46
- fix(fixer) {
47
- const tokenRef = `${themeVarName}.${matchingToken.token}`;
48
- return fixer.replaceText(prop.value, tokenRef);
49
- },
50
- });
51
- }
52
- }
53
- }
54
- else {
55
- traverseStyleNode(prop.value, themeTokens, context, themeVarName, newContext);
56
- }
57
- }
58
- });
59
- break;
60
- case 'ArrayExpression':
61
- node.elements.forEach((element) => {
62
- if (element) {
63
- traverseStyleNode(element, themeTokens, context, themeVarName, propertyContext);
64
- }
65
- });
66
- break;
67
- case 'ConditionalExpression':
68
- traverseStyleNode(node.consequent, themeTokens, context, themeVarName, propertyContext);
69
- traverseStyleNode(node.alternate, themeTokens, context, themeVarName, propertyContext);
70
- break;
71
- case 'LogicalExpression':
72
- traverseStyleNode(node.left, themeTokens, context, themeVarName, propertyContext);
73
- traverseStyleNode(node.right, themeTokens, context, themeVarName, propertyContext);
74
- break;
75
- default:
76
- break;
77
- }
78
- }
79
- export function createThemeTokenVisitor(context) {
80
- const options = context.options[0] || {};
81
- const themeImportPath = options.themeImportPath ?? './src/css-sample/themes/light-theme.css.ts';
82
- const themeVarName = options.themeVarName ?? 'theme';
83
- const orderingStrategy = options.orderingStrategy ?? 'alphabetical';
84
- const userDefinedGroupOrder = options.userDefinedGroupOrder;
85
- const sortRemainingProperties = options.sortRemainingProperties;
86
- let themeTokens = null;
87
- const nodeVisitors = createNodeVisitors(context, orderingStrategy, userDefinedGroupOrder, sortRemainingProperties);
88
- return {
89
- ...nodeVisitors,
90
- Program() {
91
- themeTokens = loadThemeTokens(themeImportPath);
92
- },
93
- CallExpression(node) {
94
- if (node.callee.type !== 'Identifier') {
95
- return;
96
- }
97
- if (isVanillaExtractStyleCall(node)) {
98
- const styleArg = node.arguments[0];
99
- if (styleArg) {
100
- traverseStyleNode(styleArg, themeTokens, context, themeVarName);
101
- }
102
- }
103
- if ((node.callee.name === 'globalKeyframes' || node.callee.name === 'globalStyle') &&
104
- node.arguments.length >= 2) {
105
- const styleArg = node.arguments[1];
106
- if (styleArg) {
107
- traverseStyleNode(styleArg, themeTokens, context, themeVarName);
108
- }
109
- }
110
- nodeVisitors.CallExpression?.(node);
111
- },
112
- };
113
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};