-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.cjs
192 lines (157 loc) · 4.88 KB
/
index.cjs
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
// NOTICE: This file is generated by Rollup. To modify it,
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';
const valueParser = require('postcss-value-parser');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration.cjs');
const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty.cjs');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue.cjs');
const report = require('../../utils/report.cjs');
const ruleMessages = require('../../utils/ruleMessages.cjs');
const validateOptions = require('../../utils/validateOptions.cjs');
const vendor = require('../../utils/vendor.cjs');
const typeGuards = require('../../utils/typeGuards.cjs');
const ruleName = 'shorthand-property-no-redundant-values';
const messages = ruleMessages(ruleName, {
rejected: (unexpected, expected) => `Expected "${unexpected}" to be "${expected}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/shorthand-property-no-redundant-values',
fixable: true,
};
const propertiesWithShorthandNotation = new Set([
'margin',
'padding',
'border-color',
'border-radius',
'border-style',
'border-width',
'grid-gap',
'inset',
]);
/**
* @param {string} property
* @returns {boolean}
*/
function isShorthandProperty(property) {
return propertiesWithShorthandNotation.has(property);
}
/**
* @param {string} top
* @param {string} right
* @param {string} bottom
* @param {string} left
* @returns {string[]}
*/
function canCondense(top, right, bottom, left) {
const lowerTop = top.toLowerCase();
const lowerRight = right.toLowerCase();
const lowerBottom = bottom && bottom.toLowerCase();
const lowerLeft = left && left.toLowerCase();
if (canCondenseToOneValue(lowerTop, lowerRight, lowerBottom, lowerLeft)) {
return [top];
}
if (canCondenseToTwoValues(lowerTop, lowerRight, lowerBottom, lowerLeft)) {
return [top, right];
}
if (canCondenseToThreeValues(lowerTop, lowerRight, lowerBottom, lowerLeft)) {
return [top, right, bottom];
}
return [top, right, bottom, left];
}
/**
* @param {string} top
* @param {string} right
* @param {string} bottom
* @param {string} left
* @returns {boolean}
*/
function canCondenseToOneValue(top, right, bottom, left) {
if (top !== right) {
return false;
}
return (top === bottom && (bottom === left || !left)) || (!bottom && !left);
}
/**
* @param {string} top
* @param {string} right
* @param {string} bottom
* @param {string} left
* @returns {boolean}
*/
function canCondenseToTwoValues(top, right, bottom, left) {
return (top === bottom && right === left) || (top === bottom && !left && top !== right);
}
/**
* @param {string} _top
* @param {string} right
* @param {string} _bottom
* @param {string} left
* @returns {boolean}
*/
function canCondenseToThreeValues(_top, right, _bottom, left) {
return right === left;
}
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary });
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
if (!isStandardSyntaxDeclaration(decl)) return;
const { prop, value } = decl;
if (!isStandardSyntaxProperty(prop)) return;
if (!isStandardSyntaxValue(value)) return;
const normalizedProp = vendor.unprefixed(prop.toLowerCase());
if (!isShorthandProperty(normalizedProp)) return;
/** @type {string[]} */
const valuesToShorthand = [];
let shouldIgnore = false;
for (const valueNode of valueParser(value).nodes) {
if (typeGuards.isValueDiv(valueNode)) {
shouldIgnore = true;
break;
}
// `var()` should be ignored. E.g.
//
// --padding: 20px 5px;
// padding: 0 var(--padding) 0;
//
if (typeGuards.isValueFunction(valueNode) && valueNode.value.toLowerCase() === 'var') {
shouldIgnore = true;
break;
}
if (typeGuards.isValueWord(valueNode) || typeGuards.isValueFunction(valueNode)) {
valuesToShorthand.push(valueParser.stringify(valueNode));
}
}
if (shouldIgnore) return;
if (valuesToShorthand.length <= 1 || valuesToShorthand.length > 4) {
return;
}
const [top, right, bottom, left] = valuesToShorthand;
const shortestForm = canCondense(top ?? '', right ?? '', bottom ?? '', left ?? '');
const shortestFormString = shortestForm.filter(Boolean).join(' ');
const valuesFormString = valuesToShorthand.join(' ');
if (shortestFormString.toLowerCase() === valuesFormString.toLowerCase()) {
return;
}
if (context.fix) {
decl.value = decl.value.replace(value, shortestFormString);
} else {
report({
message: messages.rejected,
messageArgs: [value, shortestFormString],
node: decl,
result,
ruleName,
});
}
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;