-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
191 lines (157 loc) · 5.72 KB
/
index.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import stylelint from 'stylelint';
import { physicalProp, physical2Prop, physical4Prop, physicalValue } from './lib/maps';
import { validateRuleWithProps } from './lib/validate';
import ruleName from './lib/rule-name';
import messages from './lib/messages';
import walk from './lib/walk';
const reportedDecls = new WeakMap();
function ruleFunc(method, opts, context) {
const propExceptions = [].concat(Object(opts).except || []);
const isAutofix = isContextAutofixing(context);
const dir = /^rtl$/i.test(Object(opts).direction) ? 'rtl' : 'ltr';
return (root, result) => {
// validate the method
const isMethodValid = stylelint.utils.validateOptions(result, ruleName, {
actual: method,
possible() {
return isMethodIndifferent(method) ||
isMethodAlways(method)
}
});
const reportUnexpectedProperty = (decl, logicalProperty) => stylelint.utils.report({
message: messages.unexpectedProp(decl.prop, logicalProperty),
node: decl,
result,
ruleName
});
const reportUnexpectedValue = (node, value) => stylelint.utils.report({
message: messages.unexpectedValue(node.prop, node.value, value),
node,
result,
ruleName
});
if (isMethodValid && isMethodAlways(method)) {
walk(root, node => {
// validate or autofix 4 physical properties as logical shorthands
physical4Prop.forEach(([ props, prop ]) => {
validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex, blockEndDecl, blockEndIndex, inlineEndDecl, inlineEndIndex) => { // eslint-disable-line
if (
isDeclAnException(blockStartDecl, propExceptions) ||
isDeclAnException(inlineStartDecl, propExceptions) ||
isDeclAnException(blockEndDecl, propExceptions) ||
isDeclAnException(inlineEndDecl, propExceptions)
) {
return;
}
const firstInlineDecl = blockStartDecl;
if (isAutofix) {
const values = [ blockStartDecl.value, inlineStartDecl.value, blockEndDecl.value, inlineEndDecl.value ];
if (values[1] === values[3]) {
values.pop();
if (values[2] === values[1]) {
values.pop();
if (values[1] === values[0]) {
values.pop();
}
}
}
firstInlineDecl.cloneBefore({
prop,
value: values.length <= 2 ? values.join(' ') : `logical ${values.join(' ')}`
});
blockStartDecl.remove();
inlineStartDecl.remove();
blockEndDecl.remove();
inlineEndDecl.remove();
} else if (!isDeclReported(blockStartDecl) && !isDeclReported(inlineStartDecl) && !isDeclReported(blockEndDecl) && !isDeclReported(inlineEndDecl)) {
reportUnexpectedProperty(firstInlineDecl, prop);
reportedDecls.set(blockStartDecl);
reportedDecls.set(inlineStartDecl);
reportedDecls.set(blockEndDecl);
reportedDecls.set(inlineEndDecl);
}
});
});
// validate or autofix 2 physical properties as logical shorthands
physical2Prop().forEach(([ props, prop ]) => {
validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex) => { // eslint-disable-line
if (
isDeclAnException(blockStartDecl, propExceptions) ||
isDeclAnException(inlineStartDecl, propExceptions)
) {
return;
}
const firstInlineDecl = blockStartIndex < inlineStartIndex
? blockStartDecl
: inlineStartDecl;
if (isAutofix) {
firstInlineDecl.cloneBefore({
prop,
value: blockStartDecl.value === inlineStartDecl.value
? blockStartDecl.value
: [ blockStartDecl.value, inlineStartDecl.value ].join(' ')
});
blockStartDecl.remove();
inlineStartDecl.remove();
} else if (!isDeclReported(blockStartDecl) && !isDeclReported(inlineStartDecl)) {
reportUnexpectedProperty(firstInlineDecl, prop);
reportedDecls.set(blockStartDecl);
reportedDecls.set(inlineStartDecl);
}
});
});
// validate or autofix physical properties as logical
physicalProp(dir).forEach(([ props, prop ]) => {
validateRuleWithProps(node, props, physicalDecl => {
if (isDeclAnException(physicalDecl, propExceptions)) {
return;
}
if (isAutofix) {
physicalDecl.prop = prop;
} else if (!isDeclReported(physicalDecl)) {
reportUnexpectedProperty(physicalDecl, prop);
reportedDecls.set(physicalDecl);
}
});
});
// validate or autofix physical values as logical
physicalValue(dir).forEach(([ regexp, props ]) => {
if (!isNodeMatchingDecl(node, regexp)) {
return;
}
if (isDeclAnException(node, propExceptions)) {
return;
}
const valuekey = node.value.toLowerCase();
if (valuekey in props) {
const value = props[valuekey];
if (isAutofix) {
node.value = value;
} else {
reportUnexpectedValue(node, value);
reportedDecls.set(node);
}
}
});
});
}
};
};
ruleFunc.ruleName = ruleName;
export default stylelint.createPlugin(ruleName, ruleFunc);
const isMethodIndifferent = method => method === 'ignore' || method === false || method === null;
const isMethodAlways = method => method === 'always' || method === true;
const isContextAutofixing = context => Boolean(Object(context).fix);
const isNodeMatchingDecl = (decl, regexp) => decl.type === 'decl' && regexp.test(decl.prop);
const isDeclAnException = (decl, propExceptions) => {
if (!decl || decl.type !== 'decl') {
return false;
}
return propExceptions.some((match) => {
if (match instanceof RegExp) {
return match.test(decl.prop);
}
return String(match || '').toLowerCase() === String(decl.prop || '').toLowerCase();
});
}
const isDeclReported = decl => reportedDecls.has(decl);