forked from stylelint/stylelint
/
index.js
180 lines (153 loc) · 4.9 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
'use strict';
const _ = require('lodash');
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const isStandardSyntaxSelector = require('../../utils/isStandardSyntaxSelector');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const ruleName = 'string-quotes';
const messages = ruleMessages(ruleName, {
expected: (q) => `Expected ${q} quotes`,
});
const singleQuote = `'`;
const doubleQuote = `"`;
const rule = function(expectation, secondary, context) {
const correctQuote = expectation === 'single' ? singleQuote : doubleQuote;
const erroneousQuote = expectation === 'single' ? doubleQuote : singleQuote;
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ['single', 'double'],
},
{
actual: secondary,
possible: {
avoidEscape: _.isBoolean,
},
optional: true,
},
);
if (!validOptions) {
return;
}
const avoidEscape = _.get(secondary, 'avoidEscape', true);
root.walk((node) => {
switch (node.type) {
case 'atrule':
checkDeclOrAtRule(node, node.params, atRuleParamIndex);
break;
case 'decl':
checkDeclOrAtRule(node, node.value, declarationValueIndex);
break;
case 'rule':
checkRule(node);
break;
}
});
function checkRule(rule) {
if (!isStandardSyntaxRule(rule) || !isStandardSyntaxSelector(rule.selector)) {
return;
}
if (!rule.selector.includes('[') || !rule.selector.includes('=')) {
return;
}
const fixPositions = [];
parseSelector(rule.selector, result, rule, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
if (attributeNode.quoted && attributeNode.value.includes(erroneousQuote)) {
const needsEscape = attributeNode.value.includes(correctQuote);
if (avoidEscape && needsEscape) {
// don't consider this an error
return;
}
const openIndex =
// index of the start of our attribute node in our source
attributeNode.sourceIndex +
// length of our attribute
attributeNode.attribute.length +
// length of our operator , ie '='
attributeNode.operator.length +
// and the length of the quote
erroneousQuote.length;
// we currently don't fix escapes
if (context.fix && !needsEscape) {
const closeIndex =
// our initial index
openIndex +
// the length of our value
attributeNode.value.length -
// with the length of our quote subtracted
erroneousQuote.length;
fixPositions.push(openIndex, closeIndex);
} else {
report({
message: messages.expected(expectation),
node: rule,
index: openIndex,
result,
ruleName,
});
}
}
});
});
fixPositions.forEach((fixIndex) => {
rule.selector = replaceQuote(rule.selector, fixIndex, correctQuote);
});
}
function checkDeclOrAtRule(node, value, getIndex) {
const fixPositions = [];
// Get out quickly if there are no erroneous quotes
if (!value.includes(erroneousQuote)) {
return;
} else if (node.type === 'atrule' && node.name === 'charset') {
// allow @charset rules to have double quotes, in spite of the configuration
// TODO: @charset should always use double-quotes, see https://github.com/stylelint/stylelint/issues/2788
return;
}
valueParser(value).walk((valueNode) => {
if (valueNode.type === 'string' && valueNode.quote === erroneousQuote) {
const needsEscape = valueNode.value.includes(correctQuote);
if (avoidEscape && needsEscape) {
// don't consider this an error
return;
}
const openIndex = valueNode.sourceIndex;
// we currently don't fix escapes
if (context.fix && !needsEscape) {
const closeIndex = openIndex + valueNode.value.length + erroneousQuote.length;
fixPositions.push(openIndex, closeIndex);
} else {
report({
message: messages.expected(expectation),
node,
index: getIndex(node) + openIndex,
result,
ruleName,
});
}
}
});
fixPositions.forEach((fixIndex) => {
if (node.type === 'atrule') {
node.params = replaceQuote(node.params, fixIndex, correctQuote);
} else {
node.value = replaceQuote(node.value, fixIndex, correctQuote);
}
});
}
};
};
function replaceQuote(string, index, replace) {
return string.substring(0, index) + replace + string.substring(index + replace.length);
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;