@antebudimir/eslint-plugin-vanilla-extract 1.4.7 → 1.5.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 CHANGED
@@ -139,11 +139,20 @@ export const myStyle = style({
139
139
 
140
140
  ### vanilla-extract/custom-order
141
141
 
142
- The `vanilla-extract/custom-order` rule allows you to enforce a custom ordering of CSS properties in your vanilla-extract styles. You can specify an array of property groups in the order you prefer, and the rule will ensure that properties within these groups are sorted according to their position in the concentric CSS model. All other groups that aren't included in the groups array will have their respective properties sorted after the last group in the array. You can choose to sort them either alphabetically or following the concentric CSS order (see list of concentric groups) by setting the `sortRemainingProperties` option to 'alphabetical' or 'concentric' respectively. If not set, `sortRemainingProperties` defaults to 'alphabetical'.
142
+ The `vanilla-extract/custom-order` rule enables you to enforce a custom ordering of CSS properties in your vanilla-extract styles. You can specify an array of property groups in your preferred order, and the rule will ensure that properties within these groups are sorted according to their position in the concentric CSS model.
143
143
 
144
- To configure the rule, add it to your ESLint configuration file with your desired options. You can customize the `groups` array to include any number of available CSS property groups you want to enforce, but minimum of 1 is required.
144
+ Key features of this rule include:
145
145
 
146
- Example usage:
146
+ 1. Custom group ordering: Define your preferred order of CSS property groups.
147
+ 2. Handling of unspecified groups: All groups not included in the custom array will have their properties sorted after the last specified group.
148
+ 3. Flexible sorting options: You can choose to sort remaining properties either alphabetically or following the concentric CSS order by setting the `sortRemainingProperties` option to 'alphabetical' or 'concentric' respectively.
149
+
150
+ Default behavior:
151
+
152
+ - If not set, `sortRemainingProperties` defaults to 'alphabetical'.
153
+ - If no `groupOrder` is specified or an empty array is provided, the rule will default to sorting all properties alphabetically, and `sortRemainingProperties` will be ignored even if set.
154
+
155
+ To configure the rule, add it to your ESLint configuration file with your desired options. You can customize the `groups` array to include any number of available CSS property groups you want to enforce, with a minimum of one group required.
147
156
 
148
157
  ```typescript
149
158
  // ❌ Incorrect (Unordered)
@@ -14,9 +14,8 @@ import { enforceAlphabeticalCSSOrderInStyleObject } from './style-object-process
14
14
  * 3. For each relevant property (e.g., 'base', 'variants'), it applies alphabetical ordering to the CSS properties.
15
15
  */
16
16
  export const enforceAlphabeticalCSSOrderInRecipe = (node, context) => {
17
- if (!node.arguments[0] || node.arguments[0].type !== 'ObjectExpression') {
18
- return;
17
+ if (node.arguments[0]?.type === 'ObjectExpression') {
18
+ const recipeObject = node.arguments[0];
19
+ processRecipeProperties(context, recipeObject, (context, object) => processStyleNode(context, object, enforceAlphabeticalCSSOrderInStyleObject));
19
20
  }
20
- const recipeObject = node.arguments[0];
21
- processRecipeProperties(context, recipeObject, (context, object) => processStyleNode(context, object, enforceAlphabeticalCSSOrderInStyleObject));
22
21
  };
@@ -15,14 +15,13 @@ import { enforceAlphabeticalCSSOrder } from './property-order-enforcer.js';
15
15
  * @param styleObject - The object expression representing the style object to be processed.
16
16
  */
17
17
  export const enforceAlphabeticalCSSOrderInStyleObject = (ruleContext, styleObject) => {
18
- if (!styleObject || styleObject.type !== AST_NODE_TYPES.ObjectExpression) {
19
- return;
20
- }
21
- if (isSelectorsObject(styleObject)) {
18
+ if (styleObject?.type === AST_NODE_TYPES.ObjectExpression) {
19
+ if (isSelectorsObject(styleObject)) {
20
+ processNestedSelectors(ruleContext, styleObject, enforceAlphabeticalCSSOrderInStyleObject);
21
+ return;
22
+ }
23
+ const { regularProperties } = separateProperties(styleObject.properties);
24
+ enforceAlphabeticalCSSOrder(ruleContext, regularProperties);
22
25
  processNestedSelectors(ruleContext, styleObject, enforceAlphabeticalCSSOrderInStyleObject);
23
- return;
24
26
  }
25
- const { regularProperties } = separateProperties(styleObject.properties);
26
- enforceAlphabeticalCSSOrder(ruleContext, regularProperties);
27
- processNestedSelectors(ruleContext, styleObject, enforceAlphabeticalCSSOrderInStyleObject);
28
27
  };
@@ -45,16 +45,15 @@ const compareProperties = (firstProperty, secondProperty) => {
45
45
  * @param cssPropertyInfoList - An array of CSS property information objects to be checked.
46
46
  */
47
47
  export const enforceConcentricCSSOrder = (ruleContext, cssPropertyInfoList) => {
48
- if (cssPropertyInfoList.length <= 1) {
49
- return;
50
- }
51
- // Create pairs of consecutive properties
52
- const propertyPairs = cssPropertyInfoList.slice(0, -1).map((currentProperty, index) => ({
53
- currentProperty,
54
- nextProperty: cssPropertyInfoList[index + 1],
55
- }));
56
- const violatingPair = propertyPairs.find(({ currentProperty, nextProperty }) => compareProperties(currentProperty, nextProperty) > 0);
57
- if (violatingPair) {
58
- reportOrderingIssue(ruleContext, violatingPair.currentProperty, violatingPair.nextProperty, cssPropertyInfoList);
48
+ if (cssPropertyInfoList.length > 1) {
49
+ // Create pairs of consecutive properties
50
+ const propertyPairs = cssPropertyInfoList.slice(0, -1).map((currentProperty, index) => ({
51
+ currentProperty,
52
+ nextProperty: cssPropertyInfoList[index + 1],
53
+ }));
54
+ const violatingPair = propertyPairs.find(({ currentProperty, nextProperty }) => compareProperties(currentProperty, nextProperty) > 0);
55
+ if (violatingPair) {
56
+ reportOrderingIssue(ruleContext, violatingPair.currentProperty, violatingPair.nextProperty, cssPropertyInfoList);
57
+ }
59
58
  }
60
59
  };
@@ -14,9 +14,8 @@ import { enforceConcentricCSSOrderInStyleObject } from './style-object-processor
14
14
  * 3. For each relevant property (e.g., 'base', 'variants'), it applies concentric ordering to the CSS properties.
15
15
  */
16
16
  export const enforceConcentricCSSOrderInRecipe = (ruleContext, callExpression) => {
17
- if (!callExpression.arguments[0] || callExpression.arguments[0].type !== 'ObjectExpression') {
18
- return;
17
+ if (callExpression.arguments[0]?.type === 'ObjectExpression') {
18
+ const recipeObjectExpression = callExpression.arguments[0];
19
+ processRecipeProperties(ruleContext, recipeObjectExpression, (currentContext, styleObject) => processStyleNode(currentContext, styleObject, enforceConcentricCSSOrderInStyleObject));
19
20
  }
20
- const recipeObjectExpression = callExpression.arguments[0];
21
- processRecipeProperties(ruleContext, recipeObjectExpression, (currentContext, styleObject) => processStyleNode(currentContext, styleObject, enforceConcentricCSSOrderInStyleObject));
22
21
  };
@@ -41,19 +41,18 @@ const buildCSSPropertyInfoList = (regularStyleProperties) => {
41
41
  * @param styleObject - The object expression representing the style object to be processed.
42
42
  */
43
43
  export const enforceConcentricCSSOrderInStyleObject = (ruleContext, styleObject) => {
44
- if (!styleObject || styleObject.type !== AST_NODE_TYPES.ObjectExpression) {
45
- return;
44
+ if (styleObject?.type === AST_NODE_TYPES.ObjectExpression) {
45
+ if (isSelectorsObject(styleObject)) {
46
+ styleObject.properties.forEach((property) => {
47
+ if (property.type === AST_NODE_TYPES.Property && property.value.type === AST_NODE_TYPES.ObjectExpression) {
48
+ enforceConcentricCSSOrderInStyleObject(ruleContext, property.value);
49
+ }
50
+ });
51
+ return;
52
+ }
53
+ const { regularProperties } = separateProperties(styleObject.properties);
54
+ const cssPropertyInfoList = buildCSSPropertyInfoList(regularProperties);
55
+ enforceConcentricCSSOrder(ruleContext, cssPropertyInfoList);
56
+ processNestedSelectors(ruleContext, styleObject, enforceConcentricCSSOrderInStyleObject);
46
57
  }
47
- if (isSelectorsObject(styleObject)) {
48
- styleObject.properties.forEach((property) => {
49
- if (property.type === AST_NODE_TYPES.Property && property.value.type === AST_NODE_TYPES.ObjectExpression) {
50
- enforceConcentricCSSOrderInStyleObject(ruleContext, property.value);
51
- }
52
- });
53
- return;
54
- }
55
- const { regularProperties } = separateProperties(styleObject.properties);
56
- const cssPropertyInfoList = buildCSSPropertyInfoList(regularProperties);
57
- enforceConcentricCSSOrder(ruleContext, cssPropertyInfoList);
58
- processNestedSelectors(ruleContext, styleObject, enforceConcentricCSSOrderInStyleObject);
59
58
  };
@@ -15,53 +15,58 @@ import { createCSSPropertyPriorityMap } from '../shared-utils/css-property-prior
15
15
  * with a suggested fix.
16
16
  */
17
17
  export const enforceCustomGroupOrder = (ruleContext, cssPropertyInfoList, userDefinedGroups = [], sortRemainingProperties) => {
18
- if (cssPropertyInfoList.length <= 1) {
19
- return;
20
- }
21
- const cssPropertyPriorityMap = createCSSPropertyPriorityMap(userDefinedGroups);
22
- const compareProperties = (firstProperty, secondProperty) => {
23
- const firstPropertyInfo = cssPropertyPriorityMap.get(firstProperty.name) || {
24
- groupIndex: Infinity,
25
- positionInGroup: Infinity,
26
- inUserGroup: false,
27
- };
28
- const secondPropertyInfo = cssPropertyPriorityMap.get(secondProperty.name) || {
29
- groupIndex: Infinity,
30
- positionInGroup: Infinity,
31
- inUserGroup: false,
32
- };
33
- if (firstPropertyInfo.inUserGroup !== secondPropertyInfo.inUserGroup) {
34
- return firstPropertyInfo.inUserGroup ? -1 : 1;
35
- }
36
- if (firstPropertyInfo.inUserGroup) {
37
- if (firstPropertyInfo.groupIndex !== secondPropertyInfo.groupIndex) {
38
- return firstPropertyInfo.groupIndex - secondPropertyInfo.groupIndex;
18
+ if (cssPropertyInfoList.length > 1) {
19
+ const cssPropertyPriorityMap = createCSSPropertyPriorityMap(userDefinedGroups);
20
+ const compareProperties = (firstProperty, secondProperty) => {
21
+ const firstPropertyInfo = cssPropertyPriorityMap.get(firstProperty.name) || {
22
+ groupIndex: Infinity,
23
+ positionInGroup: Infinity,
24
+ inUserGroup: false,
25
+ };
26
+ const secondPropertyInfo = cssPropertyPriorityMap.get(secondProperty.name) || {
27
+ groupIndex: Infinity,
28
+ positionInGroup: Infinity,
29
+ inUserGroup: false,
30
+ };
31
+ if (firstPropertyInfo.inUserGroup !== secondPropertyInfo.inUserGroup) {
32
+ return firstPropertyInfo.inUserGroup ? -1 : 1;
39
33
  }
40
- return firstPropertyInfo.positionInGroup - secondPropertyInfo.positionInGroup;
41
- }
42
- // For properties not in user-defined groups
43
- if (sortRemainingProperties === 'alphabetical') {
44
- return firstProperty.name.localeCompare(secondProperty.name);
45
- }
46
- else {
47
- return (firstPropertyInfo.groupIndex - secondPropertyInfo.groupIndex ||
48
- firstPropertyInfo.positionInGroup - secondPropertyInfo.positionInGroup);
34
+ if (firstPropertyInfo.inUserGroup) {
35
+ if (firstPropertyInfo.groupIndex !== secondPropertyInfo.groupIndex) {
36
+ return firstPropertyInfo.groupIndex - secondPropertyInfo.groupIndex;
37
+ }
38
+ return firstPropertyInfo.positionInGroup - secondPropertyInfo.positionInGroup;
39
+ }
40
+ // For properties not in user-defined groups
41
+ if (sortRemainingProperties === 'alphabetical') {
42
+ return firstProperty.name.localeCompare(secondProperty.name);
43
+ }
44
+ else {
45
+ return (firstPropertyInfo.groupIndex - secondPropertyInfo.groupIndex ||
46
+ firstPropertyInfo.positionInGroup - secondPropertyInfo.positionInGroup);
47
+ }
48
+ };
49
+ const sortedPropertyList = [...cssPropertyInfoList].sort(compareProperties);
50
+ // Find the first pair that violates the new ordering
51
+ const violatingProperty = cssPropertyInfoList
52
+ .slice(0, -1)
53
+ .find((currentProperty, index) => currentProperty.name !== sortedPropertyList[index]?.name);
54
+ if (violatingProperty) {
55
+ const indexInSorted = cssPropertyInfoList.indexOf(violatingProperty);
56
+ const sortedProperty = sortedPropertyList[indexInSorted];
57
+ // Defensive programming - sortedProperty will always exist and have a name because sortedPropertyList is derived from cssPropertyInfoList and cssPropertyInfoList exists and is non-empty
58
+ // Therefore, we can exclude the next line from coverage because it's unreachable in practice
59
+ /* v8 ignore next */
60
+ const nextPropertyName = sortedProperty?.name ?? '';
61
+ ruleContext.report({
62
+ node: violatingProperty.node,
63
+ messageId: 'incorrectOrder',
64
+ data: {
65
+ currentProperty: violatingProperty.name,
66
+ nextProperty: nextPropertyName,
67
+ },
68
+ fix: (fixer) => generateFixesForCSSOrder(fixer, ruleContext, cssPropertyInfoList, compareProperties, (propertyInfo) => propertyInfo.node),
69
+ });
49
70
  }
50
- };
51
- const sortedPropertyList = [...cssPropertyInfoList].sort(compareProperties);
52
- // Find the first pair that violates the new ordering
53
- const violatingProperty = cssPropertyInfoList
54
- .slice(0, -1)
55
- .find((currentProperty, index) => currentProperty.name !== sortedPropertyList[index]?.name);
56
- if (violatingProperty) {
57
- ruleContext.report({
58
- node: violatingProperty.node,
59
- messageId: 'incorrectOrder',
60
- data: {
61
- currentProperty: violatingProperty.name,
62
- nextProperty: sortedPropertyList[cssPropertyInfoList.indexOf(violatingProperty)]?.name || '',
63
- },
64
- fix: (fixer) => generateFixesForCSSOrder(fixer, ruleContext, cssPropertyInfoList, compareProperties, (propertyInfo) => propertyInfo.node),
65
- });
66
71
  }
67
72
  };
@@ -16,9 +16,8 @@ import { enforceUserDefinedGroupOrderInStyleObject } from './style-object-proces
16
16
  * 4. Processes nested selectors and style objects recursively.
17
17
  */
18
18
  export const enforceUserDefinedGroupOrderInRecipe = (ruleContext, callExpression, userDefinedGroups, sortRemainingPropertiesMethod) => {
19
- if (!callExpression.arguments[0] || callExpression.arguments[0].type !== 'ObjectExpression') {
20
- return;
19
+ if (callExpression.arguments[0]?.type === 'ObjectExpression') {
20
+ const recipeObjectExpression = callExpression.arguments[0];
21
+ processRecipeProperties(ruleContext, recipeObjectExpression, (currentContext, styleObject) => processStyleNode(currentContext, styleObject, (styleContext, styleObjectNode) => enforceUserDefinedGroupOrderInStyleObject(styleContext, styleObjectNode, userDefinedGroups, sortRemainingPropertiesMethod)));
21
22
  }
22
- const recipeObjectExpression = callExpression.arguments[0];
23
- processRecipeProperties(ruleContext, recipeObjectExpression, (currentContext, styleObject) => processStyleNode(currentContext, styleObject, (styleContext, styleObjectNode) => enforceUserDefinedGroupOrderInStyleObject(styleContext, styleObjectNode, userDefinedGroups, sortRemainingPropertiesMethod)));
24
23
  };
@@ -27,8 +27,10 @@ const customGroupOrderRule = {
27
27
  },
28
28
  ],
29
29
  messages: {
30
- incorrectOrder: "Property '{{nextProperty}}' should come before '{{currentProperty}}' according to custom CSS group ordering.",
30
+ // default message for ordering in case no groupOrder is provided
31
+ alphabeticalOrder: "Property '{{nextProperty}}' should come before '{{currentProperty}}' according to alphabetical ordering.",
31
32
  fontFaceOrder: "Properties in fontFace should be ordered with 'src' first, followed by other properties in alphabetical order. Property '{{nextProperty}}' should come before '{{currentProperty}}'.",
33
+ incorrectOrder: "Property '{{nextProperty}}' should come before '{{currentProperty}}' according to custom CSS group ordering.",
32
34
  },
33
35
  },
34
36
  create(ruleContext) {
@@ -21,32 +21,31 @@ import { enforceCustomGroupOrder } from './property-order-enforcer.js';
21
21
  * 6. Recursively processes nested selectors and style objects.
22
22
  */
23
23
  export const enforceUserDefinedGroupOrderInStyleObject = (ruleContext, styleObject, userDefinedGroups, sortRemainingPropertiesMethod = 'concentric') => {
24
- if (!styleObject || styleObject.type !== AST_NODE_TYPES.ObjectExpression) {
25
- return;
26
- }
27
- if (isSelectorsObject(styleObject)) {
28
- styleObject.properties.forEach((property) => {
29
- if (property.type === AST_NODE_TYPES.Property && property.value.type === AST_NODE_TYPES.ObjectExpression) {
30
- enforceUserDefinedGroupOrderInStyleObject(ruleContext, property.value, userDefinedGroups, sortRemainingPropertiesMethod);
31
- }
24
+ if (styleObject?.type === AST_NODE_TYPES.ObjectExpression) {
25
+ if (isSelectorsObject(styleObject)) {
26
+ styleObject.properties.forEach((property) => {
27
+ if (property.type === AST_NODE_TYPES.Property && property.value.type === AST_NODE_TYPES.ObjectExpression) {
28
+ enforceUserDefinedGroupOrderInStyleObject(ruleContext, property.value, userDefinedGroups, sortRemainingPropertiesMethod);
29
+ }
30
+ });
31
+ return;
32
+ }
33
+ const cssPropertyPriorityMap = createCSSPropertyPriorityMap(userDefinedGroups);
34
+ const { regularProperties } = separateProperties(styleObject.properties);
35
+ const cssPropertyInfoList = regularProperties.map((property) => {
36
+ const propertyName = getPropertyName(property);
37
+ const propertyInfo = cssPropertyPriorityMap.get(propertyName);
38
+ const group = userDefinedGroups.find((groupName) => concentricGroups[groupName]?.includes(propertyName)) || 'remaining';
39
+ return {
40
+ name: propertyName,
41
+ node: property,
42
+ priority: propertyInfo?.groupIndex ?? Number.MAX_SAFE_INTEGER,
43
+ positionInGroup: propertyInfo?.positionInGroup ?? Number.MAX_SAFE_INTEGER,
44
+ group,
45
+ inUserGroup: propertyInfo?.inUserGroup ?? false,
46
+ };
32
47
  });
33
- return;
48
+ enforceCustomGroupOrder(ruleContext, cssPropertyInfoList, userDefinedGroups, sortRemainingPropertiesMethod);
49
+ processNestedSelectors(ruleContext, styleObject, (nestedContext, nestedNode) => enforceUserDefinedGroupOrderInStyleObject(nestedContext, nestedNode, userDefinedGroups, sortRemainingPropertiesMethod));
34
50
  }
35
- const cssPropertyPriorityMap = createCSSPropertyPriorityMap(userDefinedGroups);
36
- const { regularProperties } = separateProperties(styleObject.properties);
37
- const cssPropertyInfoList = regularProperties.map((property) => {
38
- const propertyName = getPropertyName(property);
39
- const propertyInfo = cssPropertyPriorityMap.get(propertyName);
40
- const group = userDefinedGroups.find((groupName) => concentricGroups[groupName]?.includes(propertyName)) || 'remaining';
41
- return {
42
- name: propertyName,
43
- node: property,
44
- priority: propertyInfo?.groupIndex ?? Number.MAX_SAFE_INTEGER,
45
- positionInGroup: propertyInfo?.positionInGroup ?? Number.MAX_SAFE_INTEGER,
46
- group,
47
- inUserGroup: propertyInfo?.inUserGroup ?? false,
48
- };
49
- });
50
- enforceCustomGroupOrder(ruleContext, cssPropertyInfoList, userDefinedGroups, sortRemainingPropertiesMethod);
51
- processNestedSelectors(ruleContext, styleObject, (nestedContext, nestedNode) => enforceUserDefinedGroupOrderInStyleObject(nestedContext, nestedNode, userDefinedGroups, sortRemainingPropertiesMethod));
52
51
  };
@@ -33,7 +33,7 @@ export const createNodeVisitors = (ruleContext, orderingStrategy, userDefinedGro
33
33
  return enforceConcentricCSSOrderInStyleObject;
34
34
  case 'userDefinedGroupOrder':
35
35
  if (!userDefinedGroupOrder || userDefinedGroupOrder.length === 0) {
36
- throw new Error('💥 👿 User-defined group order must be provided for userDefinedGroupOrder strategy');
36
+ return enforceAlphabeticalCSSOrderInStyleObject;
37
37
  }
38
38
  return (ruleContext, node) => enforceUserDefinedGroupOrderInStyleObject(ruleContext, node, userDefinedGroupOrder, sortRemainingProperties);
39
39
  default:
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.7',
7
+ version: '1.5.0',
8
8
  },
9
9
  rules: {
10
10
  'alphabetical-order': alphabeticalOrderRule,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antebudimir/eslint-plugin-vanilla-extract",
3
- "version": "1.4.7",
3
+ "version": "1.5.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",