-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.mjs
166 lines (134 loc) · 3.61 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import stylelint from 'stylelint';
import is from './lib/is.mjs';
import areRulesPotentialNestingAtRule from './lib/are-rules-potential-nesting-at-rule.mjs';
import areRulesPotentialNestingMediaRule from './lib/are-rules-potential-nesting-media-rule.mjs';
import areRulesPotentialNestingRule from './lib/are-rules-potential-nesting-rule.mjs';
import fixNestingAtRule from './lib/fix-nesting-at-rule.mjs';
import fixNestingMediaRule from './lib/fix-nesting-media-rule.mjs';
import fixNestingRule from './lib/fix-nesting-rule.mjs';
const ruleName = 'csstools/use-nesting';
const meta = {
url: 'https://github.com/csstools/stylelint-use-nesting',
fixable: true,
};
const ruleFunction = (action, opts) => {
return async (root, result) => {
// validate the action
const isActionValid = stylelint.utils.validateOptions(result, ruleName, {
actual: action,
possible() {
return is(action, [ 'always', 'ignore', true, false, null ]);
}
});
if (isActionValid) {
if (is(action, [ 'always', true ])) {
result.root.walk(rule => {
let isProcessing = true;
while (isProcessing) {
isProcessing = false;
const prev = rule.prev();
const isRuleValid = rule && (rule.type === 'rule' || rule.type === 'atrule');
const isPrevValid = prev && (prev.type === 'rule' || prev.type === 'atrule');
// if the previous node is also a rule
if (!isRuleValid || !isPrevValid) {
continue;
}
if (areRulesPotentialNestingRule(rule, prev, opts)) {
report(
rule,
prev,
result,
() => {
isProcessing = true;
fixNestingRule(rule, prev)
}
);
continue;
}
if (areRulesPotentialNestingRule(prev, rule, opts)) {
report(
prev,
rule,
result,
() => {
isProcessing = true;
fixNestingRule(prev, rule)
}
);
continue;
}
if (areRulesPotentialNestingAtRule(rule, prev, opts)) {
report(
rule,
prev,
result,
() => {
isProcessing = true;
fixNestingAtRule(rule, prev, opts);
}
);
continue;
}
if (areRulesPotentialNestingAtRule(prev, rule, opts)) {
report(
prev,
rule,
result,
() => {
isProcessing = true;
fixNestingAtRule(prev, rule, opts);
}
);
continue;
}
if (areRulesPotentialNestingMediaRule(rule, prev, opts)) {
report(
rule,
prev,
result,
() => {
isProcessing = true;
fixNestingMediaRule(rule, prev);
}
);
continue;
}
if (areRulesPotentialNestingMediaRule(prev, rule, opts)) {
report(
prev,
rule,
result,
() => {
isProcessing = true;
fixNestingMediaRule(prev, rule);
}
);
continue;
}
}
});
}
}
};
}
const messages = stylelint.utils.ruleMessages(ruleName, {
expected: (node, prev) => {
const outside = prev.type === 'atrule' ? `@${prev.name} ${prev.params}` : node.selector;
const inside = prev.type === 'atrule' ? node.selector : prev.selector;
const message = `Expected "${outside}" inside "${inside}".`;
return message;
}
});
const report = (rule1, rule2, result, fix) => {
stylelint.utils.report({
message: messages.expected(rule1, rule2),
node: rule1,
result,
ruleName,
fix
});
};
ruleFunction.ruleName = ruleName;
ruleFunction.meta = meta;
ruleFunction.messages = messages;
export default stylelint.createPlugin(ruleName, ruleFunction);