stylelint-config-crisp 1.0.16 → 1.0.17
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
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
return isRegExp(ignoreItem) ? ignoreItem.test(resolvedSelector) : ignoreItem === resolvedSelector;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (isIgnored) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (context.fix) {
|
|
51
|
+
let newSelector = rule.selector;
|
|
52
|
+
|
|
53
|
+
// Auto-fix for top-level class selector
|
|
54
|
+
if (isTopLevelClassSelector(rule)) {
|
|
55
|
+
const className = rule.selector.substring(1);
|
|
56
|
+
newSelector = `#{$c}`;
|
|
57
|
+
rule.root().prepend(`$c: ".${className}";\n`);
|
|
58
|
+
} else {
|
|
59
|
+
// Auto-fix logic for nested selectors
|
|
60
|
+
newSelector = applyNestedSelectorFixes(rule, newSelector);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
rule.selector = newSelector;
|
|
64
|
+
} else {
|
|
65
|
+
// Reporting logic for selectors not using Sass interpolation
|
|
66
|
+
reportNonInterpolatedSelectors(rule, result, primaryOption);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Helper function to resolve the actual selector name
|
|
73
|
+
function resolveSelector(rule, parentSelector = "") {
|
|
74
|
+
// Skip rules without selectors (e.g., @font-face, @keyframes)
|
|
75
|
+
if (!rule.selector) {
|
|
76
|
+
return "";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (rule.selector.startsWith("&")) {
|
|
80
|
+
const combinedSelector = (parentSelector + " " + rule.selector.replace("&", "")).trim();
|
|
81
|
+
|
|
82
|
+
return rule.parent && rule.parent.type !== "root" ? resolveSelector(rule.parent, combinedSelector) : combinedSelector;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return parentSelector ? (parentSelector + " " + rule.selector).trim() : rule.selector;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Helper function to check if a value is a RegExp
|
|
89
|
+
function isRegExp(value) {
|
|
90
|
+
return value instanceof RegExp;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Helper function to check if a value is a RegExp or a string
|
|
94
|
+
function isStringOrRegExp(value) {
|
|
95
|
+
return isRegExp(value) || typeof value === "string";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Check if the rule is a top-level class selector
|
|
99
|
+
function isTopLevelClassSelector(rule) {
|
|
100
|
+
return rule.parent && rule.parent.type === "root" && rule.selector.startsWith(".");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Apply fixes for nested selectors
|
|
104
|
+
function applyNestedSelectorFixes(rule, selector) {
|
|
105
|
+
let newSelector = selector.replace(/&__([^ ,{]+)/g, "#{$c}__$1");
|
|
106
|
+
const parentMatch = rule.parent && rule.parent.selector ? rule.parent.selector.match(/__([^ ,{]+)/) : null;
|
|
107
|
+
if (parentMatch) {
|
|
108
|
+
newSelector = newSelector.replace(/&-([^ ,{]+)/g, `#{$c}__${parentMatch[1]}-$1`);
|
|
109
|
+
}
|
|
110
|
+
return newSelector;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Report selectors that do not use Sass interpolation
|
|
114
|
+
function reportNonInterpolatedSelectors(rule, result, primaryOption) {
|
|
115
|
+
if (rule.selector.startsWith(".") || rule.selector.startsWith("&__") || rule.selector.startsWith("&.")) {
|
|
116
|
+
const containsInterpolation = /#\{[^}]+\}/.test(rule.selector);
|
|
117
|
+
const expected = primaryOption === true;
|
|
118
|
+
|
|
119
|
+
if (containsInterpolation !== expected) {
|
|
120
|
+
stylelint.utils.report({
|
|
121
|
+
ruleName,
|
|
122
|
+
result,
|
|
123
|
+
node: rule,
|
|
124
|
+
message: messages.expected,
|
|
125
|
+
word: rule.selector
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = stylelint.createPlugin(ruleName, ruleFunction);
|