forked from csstools/stylelint-use-logical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (150 loc) · 7.88 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
import stylelint from 'stylelint';
import { physicalProp, physical2Prop, physical4Prop, physicalValueUnsupported, physicalValueSupported } 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();
export default stylelint.createPlugin(ruleName, (method, opts, context) => {
const propExceptions = [].concat(Object(opts).except || []);
const isProp2ConversionEnabled = !isProp2Disabled(opts);
const isProp4ConversionEnabled = !isProp4Disabled(opts);
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
});
const reportUnexpectedValueMixin = (node, value) => stylelint.utils.report({
message: messages.unexpectedValueMixin(node.prop, node.value, value),
node,
result,
ruleName
});
const validateOrAutofixPhysicalValuesAsLogical = (node, physValMap, reportFn) =>
physValMap(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 (isAutofix) {
node.value = value;
} else {
reportFn(node, value);
reportedDecls.set(node);
}
}
}
});
if (isMethodValid && isMethodAlways(method)) {
walk(root, node => {
// validate or autofix 4 physical properties as logical shorthands
if (isProp4ConversionEnabled) {
physical4Prop.filter(([physProps, logicalProp]) => { // eslint-disable-line
return !physProps.some(physProp => propExceptions.includes(physProp));
}).forEach(([props, prop]) => {
validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex, blockEndDecl, blockEndIndex, inlineEndDecl, inlineEndIndex) => { // eslint-disable-line
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
if (isProp2ConversionEnabled) {
physical2Prop(dir).filter(([physProps, logicalProp]) => { // eslint-disable-line
return !physProps.some(physProp => propExceptions.includes(physProp));
}).forEach(([props, prop]) => {
validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex) => { // eslint-disable-line
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)) {
if (isAutofix) {
physicalDecl.prop = prop;
} else if (!isDeclReported(physicalDecl)) {
reportUnexpectedProperty(physicalDecl, prop);
reportedDecls.set(physicalDecl);
}
}
});
});
// validate or autofix physical values as logical
validateOrAutofixPhysicalValuesAsLogical(node, physicalValueSupported, reportUnexpectedValue);
validateOrAutofixPhysicalValuesAsLogical(node, physicalValueUnsupported, reportUnexpectedValueMixin);
});
}
};
});
export { ruleName }
const isMethodIndifferent = method => method === 'ignore' || method === false || method === null;
const isMethodAlways = method => method === 'always' || method === true;
const isContextAutofixing = context => Boolean(Object(context).fix);
const isProp2Disabled = opts => Boolean(Object(opts).disableProp2);
const isProp4Disabled = opts => Boolean(Object(opts).disableProp4);
const isNodeMatchingDecl = (decl, regexp) => decl.type === 'decl' && regexp.test(decl.prop);
const isDeclAnException = (decl, propExceptions) => propExceptions.some(match => match instanceof RegExp
? match.test(decl.prop)
: String(match || '').toLowerCase() === String(decl.prop || '').toLowerCase());
const isDeclReported = decl => reportedDecls.has(decl);