forked from stylelint/stylelint
/
index.js
200 lines (158 loc) · 6.01 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
// @ts-nocheck
'use strict';
const _ = require('lodash');
const beforeBlockString = require('../../utils/beforeBlockString');
const blurComments = require('../../utils/blurComments');
const hasBlock = require('../../utils/hasBlock');
const isCustomProperty = require('../../utils/isCustomProperty');
const isLessVariable = require('../../utils/isLessVariable');
const isMathFunction = require('../../utils/isMathFunction');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const styleSearch = require('style-search');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const ruleName = 'length-zero-no-unit';
const messages = ruleMessages(ruleName, {
rejected: 'Unexpected unit',
});
function rule(actual, secondary, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
if (decl.prop.toLowerCase() === 'line-height') {
return;
}
const stringValue = blurComments(decl.toString());
const ignorableIndexes = new Array(stringValue.length).fill(false);
const parsedValue = valueParser(stringValue);
parsedValue.walk((node, nodeIndex, nodes) => {
if (decl.prop.toLowerCase() === 'font' && node.type === 'div' && node.value === '/') {
const lineHeightNode = nodes[nodeIndex + 1];
const lineHeightNodeValue = valueParser.stringify(lineHeightNode);
for (let i = 0; i < lineHeightNodeValue.length; i++) {
ignorableIndexes[lineHeightNode.sourceIndex + i] = true;
}
return;
}
if (node.type !== 'function') {
return;
}
const stringValue = valueParser.stringify(node);
const ignoreFlag = isMathFunction(node);
for (let i = 0; i < stringValue.length; i++) {
ignorableIndexes[node.sourceIndex + i] = ignoreFlag;
}
});
check(stringValue, decl, ignorableIndexes);
});
root.walkAtRules((atRule) => {
// Ignore Less variables
if (isLessVariable(atRule)) {
return;
}
const source = hasBlock(atRule)
? beforeBlockString(atRule, { noRawBefore: true })
: atRule.toString();
check(source, atRule);
});
function check(value, node, ignorableIndexes = []) {
if (optionsMatches(secondary, 'ignore', 'custom-properties') && isCustomProperty(value)) {
return;
}
const fixPositions = [];
styleSearch({ source: value, target: '0' }, (match) => {
const index = match.startIndex;
// Given a 0 somewhere in the full property value (not in a string, thanks
// to styleSearch) we need to isolate the value that contains the zero.
// To do so, we'll find the last index before the 0 of a character that would
// divide one value in a list from another, and the next index of such a
// character; then we build a substring from those indexes, which we can
// assess.
// If a single value includes multiple 0's (e.g. 100.01px), we don't want
// each 0 to be treated as a separate value, possibly resulting in multiple
// warnings for the same value (e.g. 0.00px).
//
// This check prevents that from happening: we build and check against a
// Set containing all the indexes that are part of a value already validated.
if (ignorableIndexes[index]) {
return;
}
const prevValueBreakIndex = _.findLastIndex(value.substr(0, index), (char) => {
return [' ', ',', ')', '(', '#', ':', '\n', '\t'].includes(char);
});
// Ignore hex colors
if (value[prevValueBreakIndex] === '#') {
return;
}
// If no prev break was found, this value starts at 0
const valueWithZeroStart = prevValueBreakIndex === -1 ? 0 : prevValueBreakIndex + 1;
const nextValueBreakIndex = _.findIndex(value.substr(valueWithZeroStart), (char) => {
return [' ', ',', ')', '/'].includes(char);
});
// If no next break was found, this value ends at the end of the string
const valueWithZeroEnd =
nextValueBreakIndex === -1 ? value.length : nextValueBreakIndex + valueWithZeroStart;
const valueWithZero = value.slice(valueWithZeroStart, valueWithZeroEnd);
const parsedValue = valueParser.unit(valueWithZero);
if (!parsedValue || (parsedValue && !parsedValue.unit)) {
return;
}
if (parsedValue.unit.toLowerCase() === 'fr') {
return;
}
// Add the indexes to ignorableIndexes so the same value will not
// be checked multiple times.
_.range(valueWithZeroStart, valueWithZeroEnd).forEach((i) => (ignorableIndexes[i] = true));
// Only pay attention if the value parses to 0
// and units with lengths
if (
parseFloat(valueWithZero) !== 0 ||
!keywordSets.lengthUnits.has(parsedValue.unit.toLowerCase())
) {
return;
}
if (context.fix) {
fixPositions.unshift({
startIndex: valueWithZeroStart,
length: valueWithZeroEnd - valueWithZeroStart,
});
return;
}
report({
message: messages.rejected,
node,
index: valueWithZeroEnd - parsedValue.unit.length,
result,
ruleName,
});
});
if (fixPositions.length) {
fixPositions.forEach((fixPosition) => {
if (node.type === 'atrule') {
// Use `-1` for `@` character before each at rule
const realIndex =
fixPosition.startIndex - node.name.length - node.raws.afterName.length - 1;
node.params = replaceZero(node.params, realIndex, fixPosition.length);
} else {
const realIndex = fixPosition.startIndex - node.prop.length - node.raws.between.length;
node.value = replaceZero(node.value, realIndex, fixPosition.length);
}
});
}
}
};
}
function replaceZero(input, startIndex, length) {
const stringStart = input.slice(0, startIndex);
const stringEnd = input.slice(startIndex + length);
return `${stringStart}0${stringEnd}`;
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;