-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
216 lines (167 loc) · 5.86 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
'use strict';
const { tokenize, TokenType } = require('@csstools/css-tokenizer');
const {
parseCommaSeparatedListOfComponentValues,
isFunctionNode,
isSimpleBlockNode,
isTokenNode,
} = require('@csstools/css-parser-algorithms');
const { isMediaFeature, parseFromTokens } = require('@csstools/media-query-list-parser');
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getAtRuleParams = require('../../utils/getAtRuleParams');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const isUnicodeRangeDescriptor = require('../../utils/isUnicodeRangeDescriptor');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');
const { isRegExp, isString } = require('../../utils/validateTypes');
const { units } = require('../../reference/units');
const ruleName = 'unit-no-unknown';
const messages = ruleMessages(ruleName, {
rejected: (unit) => `Unexpected unknown unit "${unit}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/unit-no-unknown',
};
const HAS_DIMENSION_LIKE_VALUES = /\d[\w-]/;
const RESOLUTION_MEDIA_FEATURE_NAME = /^(?:min-|max-)?resolution$/i;
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual: primary },
{
actual: secondaryOptions,
possible: {
ignoreUnits: [isString, isRegExp],
ignoreFunctions: [isString, isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
/**
* @param {string} value
*/
const tokenizeIfValueMightContainUnknownUnits = (value) => {
if (!HAS_DIMENSION_LIKE_VALUES.test(value)) return;
const tokens = tokenize({ css: value });
const hasUnknownUnits = tokens.some((token) => {
return token[0] === TokenType.Dimension && !units.has(token[4].unit.toLowerCase());
});
if (!hasUnknownUnits) return;
return tokens;
};
/**
* @template {import('postcss').AtRule | import('postcss').Declaration} T
* @param {T} node
* @param {(node: T) => number} getIndex
* @param {import('@csstools/css-parser-algorithms').FunctionNode | import('@csstools/css-parser-algorithms').TokenNode} componentValue
* @param {{ ignored: boolean, allowX: boolean }} state
*/
const check = (node, getIndex, componentValue, state) => {
if (isFunctionNode(componentValue)) {
const name = componentValue.getName();
if (
name.toLowerCase() === 'url' ||
optionsMatches(secondaryOptions, 'ignoreFunctions', name)
) {
state.ignored = true;
return;
}
if (
vendor.unprefixed(name.toLowerCase()) === 'image-set' ||
optionsMatches(secondaryOptions, 'ignoreFunctions', name)
) {
state.allowX = true;
return;
}
return;
}
const [tokenType, , , endIndex, tokenValue] = componentValue.value;
if (tokenType !== TokenType.Dimension) return;
if (optionsMatches(secondaryOptions, 'ignoreUnits', tokenValue.unit)) return;
const unit = tokenValue.unit.toLowerCase();
if (unit === 'x' && state.allowX) return;
if (units.has(unit) && unit !== 'x') return;
const startIndex = getIndex(node) + (endIndex + 1) - unit.length;
report({
message: messages.rejected,
messageArgs: [tokenValue.unit],
node,
index: startIndex,
endIndex: startIndex + unit.length,
result,
ruleName,
});
};
root.walkAtRules(/^media$/i, (atRule) => {
if (!isStandardSyntaxAtRule(atRule)) return;
const params = getAtRuleParams(atRule);
const tokens = tokenizeIfValueMightContainUnknownUnits(params);
if (!tokens) return;
parseFromTokens(tokens).forEach((mediaQuery) => {
const state = {
ignored: false,
allowX: false,
};
mediaQuery.walk((entry) => {
if (!entry.state) return;
if (entry.state.ignored) return;
if (isMediaFeature(entry.node)) {
const name = entry.node.getName();
if (RESOLUTION_MEDIA_FEATURE_NAME.test(name)) {
entry.state.allowX = true;
}
} else if (isFunctionNode(entry.node) || isTokenNode(entry.node)) {
check(atRule, atRuleParamIndex, entry.node, entry.state);
}
}, state);
});
});
root.walkDecls((decl) => {
if (!isStandardSyntaxDeclaration(decl)) return;
if (isUnicodeRangeDescriptor(decl)) return;
const value = getDeclarationValue(decl);
if (!isStandardSyntaxValue(value)) return;
const tokens = tokenizeIfValueMightContainUnknownUnits(value);
if (!tokens) return;
const isImageResolutionProp = decl.prop.toLowerCase() === 'image-resolution';
parseCommaSeparatedListOfComponentValues(tokens)
.flatMap((x) => x)
.forEach((componentValue) => {
const state = {
ignored: false,
allowX: isImageResolutionProp,
};
if (isFunctionNode(componentValue) || isTokenNode(componentValue)) {
check(decl, declarationValueIndex, componentValue, state);
}
if (!isFunctionNode(componentValue) && !isSimpleBlockNode(componentValue)) {
return;
}
componentValue.walk((entry) => {
if (!entry.state) return;
if (entry.state.ignored) return;
if (isFunctionNode(entry.node) || isTokenNode(entry.node)) {
check(decl, declarationValueIndex, entry.node, entry.state);
}
}, state);
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;