stylelint-config-crisp 1.0.19 → 1.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-config-crisp",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "Custom Stylelint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -10,7 +10,7 @@ const ruleFunction = (primaryOption, secondaryOptions = {}, context) => {
10
10
  // Validate options for the rule
11
11
  const validOptions = stylelint.utils.validateOptions(result, ruleName, {
12
12
  actual: primaryOption,
13
- possible: [true, false]
13
+ possible: [true]
14
14
  }, {
15
15
  actual: secondaryOptions,
16
16
  possible: {
@@ -23,60 +23,73 @@ const ruleFunction = (primaryOption, secondaryOptions = {}, context) => {
23
23
  return;
24
24
  }
25
25
 
26
- root.walkRules(rule => {
27
- // Skip non-Vue files
28
- if (!root.source.input.file.endsWith(".vue")) {
29
- return;
30
- }
26
+ // Skip non-Vue files
27
+ if (!root.source.input.file.endsWith(".vue")) {
28
+ return;
29
+ }
30
+
31
+ let fileScopeClass;
32
+
33
+ // First pass (identify the parent selector - via variable)
34
+ root.walkDecls(decl => {
35
+ if (decl.prop === "$c") {
36
+ // Remove leading `.` and `"`
37
+ fileScopeClass = decl.value.replace(/^["']?\./, "").replace(/["']?$/, "");
31
38
 
32
- // Skip rules without selectors (e.g., @font-face, @keyframes)
33
- if (!rule.selector) {
34
39
  return;
35
40
  }
41
+ });
36
42
 
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
- }
43
+ if (!fileScopeClass) {
44
+ // Second pass (identify the parent selector - via first class)
45
+ root.walkRules(rule => {
46
+ if (rule.parent && rule.parent.type === "root") {
47
+ const fileScopeClassMatch = rule.selector.match(/^\.[\w-]+/);
50
48
 
51
- // It's a regular string
52
- return ignoreItem === resolvedSelector;
53
- }
49
+ if (fileScopeClassMatch) {
50
+ // Capture the class name without the dot
51
+ fileScopeClass = fileScopeClassMatch[0].substring(1);
54
52
 
55
- // It's a real RegExp
56
- return ignoreItem.test(resolvedSelector);
53
+ return;
54
+ }
55
+ }
57
56
  });
57
+ }
58
58
 
59
- if (isIgnored) {
59
+ root.walkRules(rule => {
60
+ // Skip rules without selectors (e.g. @font-face, @keyframes, variables)
61
+ if (!rule.selector) {
60
62
  return;
61
63
  }
62
64
 
63
- if (context.fix) {
64
- let newSelector = rule.selector;
65
+ // Skip selector if ignored via rule options
66
+ if (isSelectorIgnored(rule, secondaryOptions.ignoreSelectors || [])) {
67
+ return;
68
+ }
65
69
 
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);
70
+ // Skip selectors other than `.*`, `&__*` , `&-*` or `&.*`
71
+ if (!rule.selector.startsWith(".") &&
72
+ !rule.selector.startsWith("&__") &&
73
+ !(rule.selector.startsWith("&-") && !rule.selector.startsWith("&--")) &&
74
+ !rule.selector.startsWith("&.")
75
+ ) {
76
+ return;
77
+ }
78
+
79
+ let isInterpolated = isInterpolatedClassSelector(rule);
80
+
81
+ if (!isInterpolated) {
82
+ // Skip classes elements outside of CSS file scope
83
+ if (rule.selector.startsWith(".") &&
84
+ !rule.selector.startsWith(`.${fileScopeClass}`)) {
85
+ return;
74
86
  }
75
87
 
76
- rule.selector = newSelector;
77
- } else {
78
- // Reporting logic for selectors not using Sass interpolation
79
- reportNonInterpolatedSelectors(rule, result, primaryOption);
88
+ if (context.fix) {
89
+ fixSelector(rule);
90
+ } else {
91
+ reportSelector(rule, result);
92
+ }
80
93
  }
81
94
  });
82
95
  };
@@ -84,7 +97,7 @@ const ruleFunction = (primaryOption, secondaryOptions = {}, context) => {
84
97
 
85
98
  // Helper function to resolve the actual selector name
86
99
  function resolveSelector(rule, parentSelector = "") {
87
- // Skip rules without selectors (e.g., @font-face, @keyframes)
100
+ // Skip rules without selectors (e.g. @font-face, @keyframes)
88
101
  if (!rule.selector) {
89
102
  return "";
90
103
  }
@@ -92,7 +105,12 @@ function resolveSelector(rule, parentSelector = "") {
92
105
  if (rule.selector.startsWith("&")) {
93
106
  const combinedSelector = (parentSelector + " " + rule.selector.replace("&", "")).trim();
94
107
 
95
- return rule.parent && rule.parent.type !== "root" ? resolveSelector(rule.parent, combinedSelector) : combinedSelector;
108
+ // Recurse?
109
+ if (rule.parent && rule.parent.type !== "root") {
110
+ return resolveSelector(rule.parent, combinedSelector);
111
+ }
112
+
113
+ return combinedSelector;
96
114
  }
97
115
 
98
116
  return parentSelector ? (parentSelector + " " + rule.selector).trim() : rule.selector;
@@ -103,37 +121,99 @@ function isStringOrRegExp(value) {
103
121
  return value instanceof RegExp || typeof value === "string";
104
122
  }
105
123
 
124
+ // Check if selector is ignored via options
125
+ function isSelectorIgnored(rule, ignoreSelectors = []) {
126
+ // Resolve the actual selector name
127
+ const resolvedSelector = resolveSelector(rule);
128
+
129
+ const isIgnored = ignoreSelectors.some(ignoreItem => {
130
+ if (typeof ignoreItem === "string") {
131
+ // Check if it's a stringified regex (e.g. "/pattern/")
132
+ if (ignoreItem.startsWith("/") && ignoreItem.endsWith("/")) {
133
+ const regexPattern = ignoreItem.slice(1, -1);
134
+ const regex = new RegExp(regexPattern);
135
+
136
+ return regex.test(resolvedSelector);
137
+ }
138
+
139
+ // It's a regular string
140
+ return ignoreItem === resolvedSelector;
141
+ }
142
+
143
+ // It's a real RegExp
144
+ return ignoreItem.test(resolvedSelector);
145
+ });
146
+
147
+ return isIgnored;
148
+ }
149
+
106
150
  // Check if the rule is a top-level class selector
107
151
  function isTopLevelClassSelector(rule) {
108
152
  return rule.parent && rule.parent.type === "root" && rule.selector.startsWith(".");
109
153
  }
110
154
 
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`);
155
+ // Check if selector uses Sass interpolation
156
+ function isInterpolatedClassSelector(rule) {
157
+ return /#\{[^}]+\}/.test(rule.selector);
158
+ }
159
+
160
+ // Apply fixes for nested selector
161
+ function fixNestedSelector(rule, selector) {
162
+ let newSelector = selector;
163
+
164
+ if (selector.startsWith("&__")) {
165
+ // Transform `&__foo` to `#{$c}__foo`
166
+ selector.replace(/&__([^ ,{]+)/g, "#{$c}__$1");
167
+ }
168
+
169
+ if (selector.startsWith("&-")) {
170
+ const parentMatch = (rule.parent && rule.parent.selector)
171
+ ? rule.parent.selector.match(/__([^ ,{]+)/)
172
+ : null;
173
+
174
+ if (parentMatch) {
175
+ // Transform
176
+ // ```
177
+ // &__foo {
178
+ // &-bar {
179
+ // ```
180
+ // to
181
+ // ```
182
+ // #{$c}__foo {
183
+ // #{$c}__foo-bar {
184
+ // ```
185
+ newSelector = newSelector.replace(/&-([^ ,{]+)/g, `#{$c}__${parentMatch[1]}-$1`);
186
+ }
117
187
  }
188
+
118
189
  return newSelector;
119
190
  }
120
191
 
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
- }
192
+ // Fix selector
193
+ function fixSelector(rule) {
194
+ let newSelector = rule.selector;
195
+
196
+ if (isTopLevelClassSelector(rule)) {
197
+ newSelector = `#{$c}`;
198
+
199
+ rule.root().prepend(`$c: "${rule.selector}";\n`);
200
+ } else {
201
+ // Auto-fix nested selector
202
+ newSelector = fixNestedSelector(rule, newSelector);
136
203
  }
204
+
205
+ rule.selector = newSelector;
206
+ }
207
+
208
+ // Report selectors
209
+ function reportSelector(rule, result) {
210
+ stylelint.utils.report({
211
+ ruleName,
212
+ result,
213
+ node: rule,
214
+ message: messages.expected,
215
+ word: rule.selector
216
+ });
137
217
  }
138
218
 
139
219
  module.exports = stylelint.createPlugin(ruleName, ruleFunction);