-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
294 lines (236 loc) · 8.54 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
293
294
'use strict';
const valueParser = require('postcss-value-parser');
const arrayEqual = require('../../utils/arrayEqual');
const { basicKeywords } = require('../../reference/keywords');
const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const { longhandSubPropertiesOfShorthandProperties } = require('../../reference/properties');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');
const { isRegExp, isString } = require('../../utils/validateTypes');
const ruleName = 'declaration-block-no-redundant-longhand-properties';
const messages = ruleMessages(ruleName, {
expected: (props) => `Expected shorthand property "${props}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/declaration-block-no-redundant-longhand-properties',
fixable: true,
};
/** @typedef {import('postcss').Declaration} Declaration */
/** @type {Map<string, (decls: Map<string, Declaration>) => (string | undefined)>} */
const customResolvers = new Map([
[
'grid-column',
(decls) => {
const start = decls.get('grid-column-start')?.value.trim();
const end = decls.get('grid-column-end')?.value.trim();
if (!start || !end) return;
return `${start} / ${end}`;
},
],
[
'grid-row',
(decls) => {
const start = decls.get('grid-row-start')?.value.trim();
const end = decls.get('grid-row-end')?.value.trim();
if (!start || !end) return;
return `${start} / ${end}`;
},
],
[
'grid-template',
(decls) => {
const areas = decls.get('grid-template-areas')?.value.trim();
const columns = decls.get('grid-template-columns')?.value.trim();
const rows = decls.get('grid-template-rows')?.value.trim();
if (!(areas && columns && rows)) return;
const splitAreas = [...areas.matchAll(/"[^"]+"/g)].map((x) => x[0]);
const splitRows = rows.split(' ');
if (splitAreas.length === 0 || splitRows.length === 0) return;
if (splitAreas.length !== splitRows.length) return;
const zipped = splitAreas.map((area, i) => `${area} ${splitRows[i]}`).join(' ');
return `${zipped} / ${columns}`;
},
],
[
'transition',
(decls) => {
/** @type {(input: string | undefined) => string[]} */
const commaSeparated = (input = '') => {
let trimmedInput = input.trim();
if (!trimmedInput) return [];
if (trimmedInput.indexOf(',') === -1) return [trimmedInput];
/** @type {import('postcss-value-parser').ParsedValue} */
let parsedValue = valueParser(trimmedInput);
/** @type {Array<Array<import('postcss-value-parser').Node>>} */
let valueParts = [];
{
/** @type {Array<import('postcss-value-parser').Node>} */
let currentListItem = [];
parsedValue.nodes.forEach((node) => {
if (node.type === 'div' && node.value === ',') {
valueParts.push(currentListItem);
currentListItem = [];
return;
}
currentListItem.push(node);
});
valueParts.push(currentListItem);
}
return valueParts.map((s) => valueParser.stringify(s).trim()).filter((s) => s.length > 0);
};
const delays = commaSeparated(decls.get('transition-delay')?.value);
const durations = commaSeparated(decls.get('transition-duration')?.value);
const timingFunctions = commaSeparated(decls.get('transition-timing-function')?.value);
const properties = commaSeparated(decls.get('transition-property')?.value);
if (!(delays.length && durations.length && timingFunctions.length && properties.length)) {
return;
}
// transition-property is the canonical list of the number of properties;
// see spec: https://w3c.github.io/csswg-drafts/css-transitions/#transition-property-property
// if there are more transition-properties than duration/delay/timings,
// the other properties are computed cyclically -- ex with %
// see spec example #3: https://w3c.github.io/csswg-drafts/css-transitions/#example-d94cbd75
return properties
.map((property, i) => {
return [
property,
durations[i % durations.length],
timingFunctions[i % timingFunctions.length],
delays[i % delays.length],
]
.filter(isString)
.join(' ');
})
.join(', ');
},
],
]);
/**
* @param {string} prefixedShorthandProperty
* @param {string[]} prefixedShorthandData
* @param {Map<string, Declaration>} transformedDeclarationNodes
* @returns {string | undefined}
*/
const resolveShorthandValue = (
prefixedShorthandProperty,
prefixedShorthandData,
transformedDeclarationNodes,
) => {
const resolver = customResolvers.get(prefixedShorthandProperty);
if (resolver === undefined) {
// the "default" resolver: sort the longhand values in the order
// of their properties
const values = prefixedShorthandData
.map((p) => transformedDeclarationNodes.get(p)?.value.trim())
.filter(Boolean);
return values.length > 0 ? values.join(' ') : undefined;
}
return resolver(transformedDeclarationNodes);
};
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual: primary },
{
actual: secondaryOptions,
possible: {
ignoreShorthands: [isString, isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
/** @type {Map<string, import('stylelint').ShorthandProperties[]>} */
const longhandToShorthands = new Map();
for (const [shorthand, longhandProps] of longhandSubPropertiesOfShorthandProperties.entries()) {
if (optionsMatches(secondaryOptions, 'ignoreShorthands', shorthand)) {
continue;
}
for (const longhand of longhandProps) {
const shorthands = longhandToShorthands.get(longhand) || [];
shorthands.push(shorthand);
longhandToShorthands.set(longhand, shorthands);
}
}
eachDeclarationBlock(root, (eachDecl) => {
/** @type {Map<string, string[]>} */
const longhandDeclarations = new Map();
/** @type {Map<string, Declaration[]>} */
const longhandDeclarationNodes = new Map();
eachDecl((decl) => {
// basic keywords are not allowed in shorthand properties
if (basicKeywords.has(decl.value)) {
return;
}
const prop = decl.prop.toLowerCase();
const unprefixedProp = vendor.unprefixed(prop);
const prefix = vendor.prefix(prop);
const shorthandProperties = longhandToShorthands.get(unprefixedProp);
if (!shorthandProperties) {
return;
}
for (const shorthandProperty of shorthandProperties) {
const prefixedShorthandProperty = prefix + shorthandProperty;
const longhandDeclaration = longhandDeclarations.get(prefixedShorthandProperty) || [];
const longhandDeclarationNode =
longhandDeclarationNodes.get(prefixedShorthandProperty) || [];
longhandDeclaration.push(prop);
longhandDeclarations.set(prefixedShorthandProperty, longhandDeclaration);
longhandDeclarationNode.push(decl);
longhandDeclarationNodes.set(prefixedShorthandProperty, longhandDeclarationNode);
const shorthandProps = longhandSubPropertiesOfShorthandProperties.get(shorthandProperty);
const prefixedShorthandData = Array.from(shorthandProps || []).map(
(item) => prefix + item,
);
const copiedPrefixedShorthandData = [...prefixedShorthandData];
if (!arrayEqual(copiedPrefixedShorthandData.sort(), longhandDeclaration.sort())) {
continue;
}
if (context.fix) {
const declNodes = longhandDeclarationNodes.get(prefixedShorthandProperty) || [];
const [firstDeclNode] = declNodes;
if (firstDeclNode) {
const transformedDeclarationNodes = new Map(
declNodes.map((d) => [d.prop.toLowerCase(), d]),
);
const resolvedShorthandValue = resolveShorthandValue(
prefixedShorthandProperty,
prefixedShorthandData,
transformedDeclarationNodes,
);
if (resolvedShorthandValue) {
const newShorthandDeclarationNode = firstDeclNode.clone({
prop: prefixedShorthandProperty,
value: resolvedShorthandValue,
});
firstDeclNode.replaceWith(newShorthandDeclarationNode);
declNodes.forEach((node) => node.remove());
return;
}
}
}
report({
ruleName,
result,
node: decl,
word: decl.prop,
message: messages.expected,
messageArgs: [prefixedShorthandProperty],
});
}
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;