-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
236 lines (194 loc) · 6.17 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
// @ts-nocheck
'use strict';
const _ = require('lodash');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
const isCounterIncrementCustomIdentValue = require('../../utils/isCounterIncrementCustomIdentValue');
const isCounterResetCustomIdentValue = require('../../utils/isCounterResetCustomIdentValue');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const keywordSets = require('../../reference/keywordSets');
const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const ruleName = 'value-keyword-case';
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
});
// Operators are interpreted as "words" by the value parser, so we want to make sure to ignore them.
const ignoredCharacters = new Set(['+', '-', '/', '*', '%']);
const gridRowProps = new Set(['grid-row', 'grid-row-start', 'grid-row-end']);
const gridColumnProps = new Set(['grid-column', 'grid-column-start', 'grid-column-end']);
const mapLowercaseKeywordsToCamelCase = new Map();
keywordSets.camelCaseKeywords.forEach((func) => {
mapLowercaseKeywordsToCamelCase.set(func.toLowerCase(), func);
});
function rule(expectation, options, context) {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ['lower', 'upper'],
},
{
actual: options,
possible: {
ignoreProperties: [_.isString, _.isRegExp],
ignoreKeywords: [_.isString, _.isRegExp],
ignoreFunctions: [_.isString, _.isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
const prop = decl.prop;
const propLowerCase = decl.prop.toLowerCase();
const value = decl.value;
const parsed = valueParser(getDeclarationValue(decl));
let needFix = false;
parsed.walk((node) => {
const valueLowerCase = node.value.toLowerCase();
// Ignore system colors
if (keywordSets.systemColors.has(valueLowerCase)) {
return;
}
// Ignore keywords within `url` and `var` function
if (
node.type === 'function' &&
(valueLowerCase === 'url' ||
valueLowerCase === 'var' ||
valueLowerCase === 'counter' ||
valueLowerCase === 'counters' ||
valueLowerCase === 'attr')
) {
return false;
}
// ignore keywords within ignoreFunctions functions
const ignoreFunctions = (options && options.ignoreFunctions) || [];
if (
node.type === 'function' &&
ignoreFunctions.length > 0 &&
matchesStringOrRegExp(valueLowerCase, ignoreFunctions)
) {
return false;
}
const keyword = node.value;
// Ignore css variables, and hex values, and math operators, and sass interpolation
if (
node.type !== 'word' ||
!isStandardSyntaxValue(node.value) ||
value.includes('#') ||
ignoredCharacters.has(keyword) ||
getUnitFromValueNode(node)
) {
return;
}
if (
propLowerCase === 'animation' &&
!keywordSets.animationShorthandKeywords.has(valueLowerCase) &&
!keywordSets.animationNameKeywords.has(valueLowerCase)
) {
return;
}
if (
propLowerCase === 'animation-name' &&
!keywordSets.animationNameKeywords.has(valueLowerCase)
) {
return;
}
if (
propLowerCase === 'font' &&
!keywordSets.fontShorthandKeywords.has(valueLowerCase) &&
!keywordSets.fontFamilyKeywords.has(valueLowerCase)
) {
return;
}
if (
propLowerCase === 'font-family' &&
!keywordSets.fontFamilyKeywords.has(valueLowerCase)
) {
return;
}
if (
propLowerCase === 'counter-increment' &&
isCounterIncrementCustomIdentValue(valueLowerCase)
) {
return;
}
if (propLowerCase === 'counter-reset' && isCounterResetCustomIdentValue(valueLowerCase)) {
return;
}
if (gridRowProps.has(propLowerCase) && !keywordSets.gridRowKeywords.has(valueLowerCase)) {
return;
}
if (
gridColumnProps.has(propLowerCase) &&
!keywordSets.gridColumnKeywords.has(valueLowerCase)
) {
return;
}
if (propLowerCase === 'grid-area' && !keywordSets.gridAreaKeywords.has(valueLowerCase)) {
return;
}
if (
propLowerCase === 'list-style' &&
!keywordSets.listStyleShorthandKeywords.has(valueLowerCase) &&
!keywordSets.listStyleTypeKeywords.has(valueLowerCase)
) {
return;
}
if (
propLowerCase === 'list-style-type' &&
!keywordSets.listStyleTypeKeywords.has(valueLowerCase)
) {
return;
}
const ignoreKeywords = (options && options.ignoreKeywords) || [];
const ignoreProperties = (options && options.ignoreProperties) || [];
if (ignoreKeywords.length > 0 && matchesStringOrRegExp(keyword, ignoreKeywords)) {
return;
}
if (ignoreProperties.length > 0 && matchesStringOrRegExp(prop, ignoreProperties)) {
return;
}
const keywordLowerCase = keyword.toLocaleLowerCase();
let expectedKeyword = null;
if (expectation === 'lower' && mapLowercaseKeywordsToCamelCase.has(keywordLowerCase)) {
expectedKeyword = mapLowercaseKeywordsToCamelCase.get(keywordLowerCase);
} else if (expectation === 'lower') {
expectedKeyword = keyword.toLowerCase();
} else {
expectedKeyword = keyword.toUpperCase();
}
if (keyword === expectedKeyword) {
return;
}
if (context.fix) {
needFix = true;
node.value = expectedKeyword;
return;
}
report({
message: messages.expected(keyword, expectedKeyword),
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName,
});
});
if (context.fix && needFix) {
decl.value = parsed.toString();
}
});
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;