-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
292 lines (254 loc) · 9.25 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import stylelint from 'stylelint';
import { physicalProp, physical2Prop, physicalShorthandProp, physical4Prop, physicalValue, migrationNoneSpec, propsThatContainPropsInValue } from './lib/maps';
import { validateRuleWithProps } from './lib/validate';
import { cssValueSplit } from './lib/value-split';
import ruleName from './lib/rule-name';
import messages from './lib/messages';
import walk from './lib/walk';
const reportedDecls = new WeakMap();
// Ignore autofix on those expression value
const expressionRegex = /^\$\{.*\}$/g;
function ruleFunc(method, opts, context) {
const propExceptions = [].concat(Object(opts).except || []);
const isAutofixable = (node) => isContextAutofixing(context) && !expressionRegex.test(node.value);
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 reportUnsupportedProp = (decl, logicalProperty) => stylelint.utils.report({
message: messages.unsupportedProp(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 => {
migrationNoneSpec.forEach(([prop, props]) => {
validateRuleWithProps(node, prop, (outDateDecl) => {
console.warn(`Property ${prop[0]} is not part of Logical standards.`);
if (isAutofixable(node)) {
console.warn(`Migrating ${prop[0]} to Logical standards.`);
const value = outDateDecl.value;
outDateDecl.cloneBefore({
prop: props[0],
value
});
outDateDecl.cloneAfter({
prop: props[1],
value
});
outDateDecl.remove();
} else if (!isDeclReported(outDateDecl)) {
reportUnsupportedProp(outDateDecl, props);
reportedDecls.set(outDateDecl);
}
})
})
/* logical shorthands do not work yet in browsers */
/* but we can still use shorthand if all values are the same */
// 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
const firstInlineDecl = blockStartDecl;
const values = shorthandValueShorten([blockStartDecl.value, inlineStartDecl.value, blockEndDecl.value, inlineEndDecl.value]);
if (
!isDeclAnException(blockStartDecl, propExceptions) &&
!isDeclAnException(inlineStartDecl, propExceptions) &&
!isDeclAnException(blockEndDecl, propExceptions) &&
!isDeclAnException(inlineEndDecl, propExceptions) &&
values.length === 1 // only report issues if there is 1 value after shortening
) {
if (isAutofixable(node)) {
firstInlineDecl.cloneBefore({
prop,
value: 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 shorthand properties that are not supported
physicalShorthandProp.forEach((prop) => {
validateRuleWithProps(node, [prop], physicalDecl => { // eslint-disable-line
const inputValues = cssValueSplit(physicalDecl.value);
if (
!isDeclAnException(physicalDecl, propExceptions) &&
inputValues.length !== 1
) {
if (isAutofixable(node)) {
let outputValues = convertShorthandValues(inputValues, dir);
['block', 'inline'].forEach(type => {
physicalDecl.cloneBefore({
prop: prop + "-" + type,
value: outputValues[type]
});
})
physicalDecl.remove();
} else if (!isDeclReported(physicalDecl)) {
reportUnexpectedProperty(physicalDecl, `${prop}-block and ${prop}-inline`);
reportedDecls.set(physicalDecl);
}
}
});
});
// validate or autofix 2 physical properties as logical shorthands
physical2Prop(dir).forEach(([props, prop]) => {
validateRuleWithProps(node, props, (startDecl, startIndex, endDecl, endStartIndex) => { // eslint-disable-line
const firstInlineDecl = startIndex < endStartIndex
? startDecl
: endDecl;
if (!isDeclAnException(startDecl, propExceptions) && !isDeclAnException(endDecl, propExceptions)) {
if (isAutofixable(node)) {
firstInlineDecl.cloneBefore({
prop,
value: startDecl.value === endDecl.value
? startDecl.value
: [startDecl.value, endDecl.value].join(' ')
});
startDecl.remove();
endDecl.remove();
} else if (!isDeclReported(startDecl) && !isDeclReported(endDecl)) {
reportUnexpectedProperty(firstInlineDecl, prop);
reportedDecls.set(startDecl);
reportedDecls.set(endDecl);
}
}
});
});
// validate or autofix physical properties as logical
physicalProp(dir).forEach(([props, prop]) => {
validateRuleWithProps(node, props, physicalDecl => {
if (!isDeclAnException(physicalDecl, propExceptions)) {
if (isAutofixable(node)) {
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) && !isDeclAnException(node, propExceptions)) {
const valuekey = node.value.toLowerCase();
if (valuekey in props) {
const value = props[valuekey];
if (isAutofixable(node)) {
node.value = value;
} else {
reportUnexpectedValue(node, value);
reportedDecls.set(node);
}
}
}
});
// validate or autofix physical values containing properties as logical
if (isNodeMatchingDecl(node, propsThatContainPropsInValue)) {
const originalValue = node.value.toLowerCase();
let value = originalValue;
physicalProp(dir).forEach(([props, prop]) => {
if (!isDeclAnException(node, propExceptions)) {
props.forEach(searchProp => {
if (!isValueAnException(searchProp, propExceptions)) {
let regex = new RegExp("(?<!-)" + searchProp + "(?!-)", "g");
if (regex.test(value)) {
value = value.replace(regex, prop);
}
}
})
}
});
if (value !== originalValue) {
if (isAutofixable(node)) {
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) => isValueAnException(decl.prop, propExceptions);
const isValueAnException = (value, propExceptions) => propExceptions.some(match => match instanceof RegExp
? match.test(value)
: String(match || '').toLowerCase() === String(value || '').toLowerCase());
const isDeclReported = decl => reportedDecls.has(decl);
const shorthandValueShorten = values => {
const map = [[1, 0], [0, 2], [1, 3]];
for (let x = values.length - 2; x >= 0; x--)
{
if (values[map[x][0]] !== values[map[x][1]]) {
break;
}
values.pop();
}
return values;
};
const convertShorthandValues = (input, dir) => {
let block, inline;
if (input.length === 1) {
block = input[0];
inline = input[0];
}
if (input.length === 2) {
block = input[0];
inline = input[1];
}
if (input.length === 3) {
block = input[0] + ' ' + input[2];
inline = input[1];
}
if (input.length === 4) {
block = input[0] + ' ' + input[2];
inline = dir === 'ltr' ? input[3] + ' ' + input[1] : input[1] + ' ' + input[3];
}
return {
block: optimizeCssValues(block),
inline: optimizeCssValues(inline)
};
};
const optimizeCssValues = (value) => {
const values = cssValueSplit(value);
if (values.length === 2 && values[0] === values[1]) {
return values[0];
}
return value;
}