stylelint-config-crisp 1.0.16 → 1.0.18

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
@@ -85,7 +85,7 @@ This is the list of configured syntaxes, plugins and rules used by Stylelint Con
85
85
  #### Crisp rules
86
86
  - [at-rule-conditional-parentheses](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/at-rule-conditional-parentheses.js): Enforces parentheses in conditions of at-rules (`@if`, `@elseif`, `@while`)
87
87
  - [rule-empty-line-before](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/rule-empty-line-before.js): Requires an empty line before multi-line rules the same way [rule-empty-line-before](https://stylelint.io/user-guide/rules/rule-empty-line-before/) does, but properly handles Sass interpolation `#{}` in selectors
88
- - [selector-class-interpolation](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/selector-class-interpolation.js): Enforces class selectors to use Sass interpolation
88
+ - [selector-class-interpolation](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/selector-class-interpolation.js): Enforces class selectors to use Sass interpolation (this rule is only applied to Vue files)
89
89
 
90
90
  ## License
91
91
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-config-crisp",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Custom Stylelint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -0,0 +1,139 @@
1
+ const stylelint = require("stylelint");
2
+
3
+ const ruleName = "crisp/selector-class-interpolation";
4
+ const messages = stylelint.utils.ruleMessages(ruleName, {
5
+ expected: "Expected Sass interpolation in class selector"
6
+ });
7
+
8
+ const ruleFunction = (primaryOption, secondaryOptions = {}, context) => {
9
+ return (root, result) => {
10
+ // Validate options for the rule
11
+ const validOptions = stylelint.utils.validateOptions(result, ruleName, {
12
+ actual: primaryOption,
13
+ possible: [true, false]
14
+ }, {
15
+ actual: secondaryOptions,
16
+ possible: {
17
+ ignoreSelectors: [isStringOrRegExp]
18
+ },
19
+ optional: true
20
+ });
21
+
22
+ if (!validOptions) {
23
+ return;
24
+ }
25
+
26
+ root.walkRules(rule => {
27
+ // Skip non-Vue files
28
+ if (!root.source.input.file.endsWith(".vue")) {
29
+ return;
30
+ }
31
+
32
+ // Skip rules without selectors (e.g., @font-face, @keyframes)
33
+ if (!rule.selector) {
34
+ return;
35
+ }
36
+
37
+ // Resolve the actual selector name
38
+ const resolvedSelector = resolveSelector(rule);
39
+ const ignoreSelectors = secondaryOptions.ignoreSelectors || [];
40
+
41
+ // Determine if the resolved selector should be ignored
42
+ const isIgnored = ignoreSelectors.some(ignoreItem => {
43
+ if (typeof ignoreItem === "string") {
44
+ // Check if it's a stringified regex (e.g., "/pattern/")
45
+ if (ignoreItem.startsWith("/") && ignoreItem.endsWith("/")) {
46
+ const regexPattern = ignoreItem.slice(1, -1);
47
+ const regex = new RegExp(regexPattern);
48
+ return regex.test(resolvedSelector);
49
+ }
50
+
51
+ // It's a regular string
52
+ return ignoreItem === resolvedSelector;
53
+ }
54
+
55
+ // It's a real RegExp
56
+ return ignoreItem.test(resolvedSelector);
57
+ });
58
+
59
+ if (isIgnored) {
60
+ return;
61
+ }
62
+
63
+ if (context.fix) {
64
+ let newSelector = rule.selector;
65
+
66
+ // Auto-fix for top-level class selector
67
+ if (isTopLevelClassSelector(rule)) {
68
+ const className = rule.selector.substring(1);
69
+ newSelector = `#{$c}`;
70
+ rule.root().prepend(`$c: ".${className}";\n`);
71
+ } else {
72
+ // Auto-fix logic for nested selectors
73
+ newSelector = applyNestedSelectorFixes(rule, newSelector);
74
+ }
75
+
76
+ rule.selector = newSelector;
77
+ } else {
78
+ // Reporting logic for selectors not using Sass interpolation
79
+ reportNonInterpolatedSelectors(rule, result, primaryOption);
80
+ }
81
+ });
82
+ };
83
+ };
84
+
85
+ // Helper function to resolve the actual selector name
86
+ function resolveSelector(rule, parentSelector = "") {
87
+ // Skip rules without selectors (e.g., @font-face, @keyframes)
88
+ if (!rule.selector) {
89
+ return "";
90
+ }
91
+
92
+ if (rule.selector.startsWith("&")) {
93
+ const combinedSelector = (parentSelector + " " + rule.selector.replace("&", "")).trim();
94
+
95
+ return rule.parent && rule.parent.type !== "root" ? resolveSelector(rule.parent, combinedSelector) : combinedSelector;
96
+ }
97
+
98
+ return parentSelector ? (parentSelector + " " + rule.selector).trim() : rule.selector;
99
+ }
100
+
101
+ // Helper function to check if a value is a RegExp or a string
102
+ function isStringOrRegExp(value) {
103
+ return value instanceof RegExp || typeof value === "string";
104
+ }
105
+
106
+ // Check if the rule is a top-level class selector
107
+ function isTopLevelClassSelector(rule) {
108
+ return rule.parent && rule.parent.type === "root" && rule.selector.startsWith(".");
109
+ }
110
+
111
+ // Apply fixes for nested selectors
112
+ function applyNestedSelectorFixes(rule, selector) {
113
+ let newSelector = selector.replace(/&__([^ ,{]+)/g, "#{$c}__$1");
114
+ const parentMatch = rule.parent && rule.parent.selector ? rule.parent.selector.match(/__([^ ,{]+)/) : null;
115
+ if (parentMatch) {
116
+ newSelector = newSelector.replace(/&-([^ ,{]+)/g, `#{$c}__${parentMatch[1]}-$1`);
117
+ }
118
+ return newSelector;
119
+ }
120
+
121
+ // Report selectors that do not use Sass interpolation
122
+ function reportNonInterpolatedSelectors(rule, result, primaryOption) {
123
+ if (rule.selector.startsWith(".") || rule.selector.startsWith("&__") || rule.selector.startsWith("&.")) {
124
+ const containsInterpolation = /#\{[^}]+\}/.test(rule.selector);
125
+ const expected = primaryOption === true;
126
+
127
+ if (containsInterpolation !== expected) {
128
+ stylelint.utils.report({
129
+ ruleName,
130
+ result,
131
+ node: rule,
132
+ message: messages.expected,
133
+ word: rule.selector
134
+ });
135
+ }
136
+ }
137
+ }
138
+
139
+ module.exports = stylelint.createPlugin(ruleName, ruleFunction);