-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
195 lines (161 loc) · 4.97 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
'use strict';
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const functionArgumentsSearch = require('../../utils/functionArgumentsSearch');
const getAtRuleParams = require('../../utils/getAtRuleParams');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxUrl = require('../../utils/isStandardSyntaxUrl');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const ruleName = 'function-url-quotes';
const messages = ruleMessages(ruleName, {
expected: (functionName) => `Expected quotes around "${functionName}" function argument`,
rejected: (functionName) => `Unexpected quotes around "${functionName}" function argument`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/function-url-quotes',
fixable: true,
};
const URL_FUNC_REGEX = /url\(/i;
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: ['always', 'never'],
},
{
actual: secondaryOptions,
possible: {
except: ['empty'],
},
optional: true,
},
);
if (!validOptions) {
return;
}
const exceptEmpty = optionsMatches(secondaryOptions, 'except', 'empty');
const emptyArgumentPatterns = new Set(['', "''", '""']);
root.walkAtRules(checkAtRuleParams);
root.walkDecls(checkDeclParams);
/**
* @param {import('postcss').Declaration} decl
*/
function checkDeclParams(decl) {
if (!URL_FUNC_REGEX.test(decl.value)) return;
if (!isStandardSyntaxDeclaration(decl)) return;
const value = getDeclarationValue(decl);
const startIndex = declarationValueIndex(decl);
const parsed = functionArgumentsSearch(value, /^url$/i, (args, index, funcNode) => {
checkArgs(decl, args, startIndex + index, funcNode);
});
if (context.fix) {
decl.value = parsed.toString();
}
}
/**
* @param {import('postcss').AtRule} atRule
*/
function checkAtRuleParams(atRule) {
const params = getAtRuleParams(atRule);
const startIndex = atRuleParamIndex(atRule);
let hasUrlFunction = false;
const parsed = functionArgumentsSearch(params, /^url$/i, (args, index, funcNode) => {
hasUrlFunction = true;
checkArgs(atRule, args, startIndex + index, funcNode);
});
if (!hasUrlFunction) return;
if (context.fix) {
atRule.params = parsed.toString();
}
}
/**
* @param {import('postcss-value-parser').FunctionNode} funcNode
*/
function addQuotes(funcNode) {
for (const argNode of funcNode.nodes) {
if (argNode.type === 'word') {
argNode.value = `"${argNode.value}"`;
}
}
}
/**
* @param {import('postcss-value-parser').FunctionNode} funcNode
*/
function removeQuotes(funcNode) {
for (const argNode of funcNode.nodes) {
if (argNode.type === 'string') {
// NOTE: We can ignore this error because the test passes.
// @ts-expect-error -- TS2322: Type '"word"' is not assignable to type '"string"'.
argNode.type = 'word';
}
}
}
/**
* @param {import('postcss').Declaration | import('postcss').AtRule} node
* @param {string} args
* @param {number} index
* @param {import('postcss-value-parser').FunctionNode} funcNode
*/
function checkArgs(node, args, index, funcNode) {
const functionName = funcNode.value.toLowerCase();
let shouldHasQuotes = primary === 'always';
const leftTrimmedArgs = args.trimStart();
if (!isStandardSyntaxUrl(leftTrimmedArgs)) {
return;
}
const complaintIndex = index + args.length - leftTrimmedArgs.length;
const complaintEndIndex = index + args.length;
const hasQuotes = leftTrimmedArgs.startsWith("'") || leftTrimmedArgs.startsWith('"');
if (exceptEmpty && emptyArgumentPatterns.has(args.trim())) {
shouldHasQuotes = !shouldHasQuotes;
}
if (shouldHasQuotes) {
if (hasQuotes) {
return;
}
if (context.fix) {
addQuotes(funcNode);
return;
}
complain(messages.expected(functionName), node, complaintIndex, complaintEndIndex);
} else {
if (!hasQuotes) {
return;
}
if (context.fix) {
removeQuotes(funcNode);
return;
}
complain(messages.rejected(functionName), node, complaintIndex, complaintEndIndex);
}
}
/**
* @param {string} message
* @param {import('postcss').Node} node
* @param {number} index
* @param {number} endIndex
*/
function complain(message, node, index, endIndex) {
report({
message,
node,
index,
endIndex,
result,
ruleName,
});
}
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;