forked from stylelint/stylelint
/
index.js
190 lines (158 loc) · 4.51 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
'use strict';
const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
const isCustomProperty = require('../../utils/isCustomProperty');
const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isString } = require('../../utils/validateTypes');
const vendor = require('../../utils/vendor');
const ruleName = 'declaration-block-no-duplicate-properties';
const messages = ruleMessages(ruleName, {
rejected: (property) => `Unexpected duplicate "${property}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/declaration-block-no-duplicate-properties',
fixable: true,
};
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual: primary },
{
actual: secondaryOptions,
possible: {
ignore: [
'consecutive-duplicates',
'consecutive-duplicates-with-different-values',
'consecutive-duplicates-with-same-prefixless-values',
],
ignoreProperties: [isString],
},
optional: true,
},
);
if (!validOptions) {
return;
}
const ignoreDuplicates = optionsMatches(secondaryOptions, 'ignore', 'consecutive-duplicates');
const ignoreDiffValues = optionsMatches(
secondaryOptions,
'ignore',
'consecutive-duplicates-with-different-values',
);
const ignorePrefixlessSameValues = optionsMatches(
secondaryOptions,
'ignore',
'consecutive-duplicates-with-same-prefixless-values',
);
eachDeclarationBlock(root, (eachDecl) => {
/** @type {import('postcss').Declaration[]} */
const decls = [];
eachDecl((decl) => {
const prop = decl.prop;
const lowerProp = decl.prop.toLowerCase();
const value = decl.value;
const important = decl.important;
if (!isStandardSyntaxProperty(prop)) {
return;
}
if (isCustomProperty(prop)) {
return;
}
// Return early if the property is to be ignored
if (optionsMatches(secondaryOptions, 'ignoreProperties', prop)) {
return;
}
// Ignore the src property as commonly duplicated in at-fontface
if (lowerProp === 'src') {
return;
}
const indexDuplicate = decls.findIndex((d) => d.prop.toLowerCase() === lowerProp);
if (indexDuplicate === -1) {
decls.push(decl);
}
const duplicateDecl = decls[indexDuplicate];
if (!duplicateDecl) {
return;
}
const duplicateValue = duplicateDecl.value || '';
const duplicateImportant = duplicateDecl.important || false;
const duplicateIsMoreImportant = !important && duplicateImportant;
const duplicatesAreConsecutive = indexDuplicate === decls.length - 1;
if (ignoreDiffValues || ignorePrefixlessSameValues) {
if (!duplicatesAreConsecutive) {
if (!context.fix) {
return report({
message: messages.rejected(prop),
node: decl,
result,
ruleName,
word: prop,
});
}
if (duplicateIsMoreImportant) {
return decl.remove();
}
return duplicateDecl.remove();
}
if (ignorePrefixlessSameValues) {
// fails if values of consecutive, unprefixed duplicates are equal
if (vendor.unprefixed(value) !== vendor.unprefixed(duplicateValue)) {
if (!context.fix) {
return report({
message: messages.rejected(prop),
node: decl,
result,
ruleName,
word: prop,
});
}
if (duplicateIsMoreImportant) {
return decl.remove();
}
return duplicateDecl.remove();
}
}
if (value !== duplicateValue) {
return;
}
if (context.fix) {
return duplicateDecl.remove();
}
return report({
message: messages.rejected(prop),
node: decl,
result,
ruleName,
word: prop,
});
}
if (ignoreDuplicates && duplicatesAreConsecutive) {
return;
}
if (!context.fix) {
return report({
message: messages.rejected(prop),
node: decl,
result,
ruleName,
word: prop,
});
}
if (duplicateIsMoreImportant) {
return decl.remove();
}
duplicateDecl.remove();
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;