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
|
@@ -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
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
if (fileScopeClassMatch) {
|
|
50
|
+
// Capture the class name without the dot
|
|
51
|
+
fileScopeClass = fileScopeClassMatch[0].substring(1);
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
57
56
|
});
|
|
57
|
+
}
|
|
58
58
|
|
|
59
|
-
|
|
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
|
|
64
|
-
|
|
65
|
+
// Skip selector if ignored via rule options
|
|
66
|
+
if (isSelectorIgnored(rule, secondaryOptions.ignoreSelectors || [])) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
rule.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
//
|
|
112
|
-
function
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
//
|
|
122
|
-
function
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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);
|