-
-
Notifications
You must be signed in to change notification settings - Fork 934
/
index.js
160 lines (128 loc) · 4.16 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
'use strict';
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const ruleName = 'number-leading-zero';
const messages = ruleMessages(ruleName, {
expected: 'Expected a leading zero',
rejected: 'Unexpected leading zero',
});
const rule = function(expectation, secondary, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: ['always', 'never'],
});
if (!validOptions) {
return;
}
root.walkAtRules((atRule) => {
if (atRule.name.toLowerCase() === 'import') {
return;
}
check(atRule, atRule.params, atRuleParamIndex);
});
root.walkDecls((decl) => check(decl, decl.value, declarationValueIndex));
function check(node, value, getIndex) {
const neverFixPositions = [];
const alwaysFixPositions = [];
// Get out quickly if there are no periods
if (!value.includes('.')) {
return;
}
valueParser(value).walk((valueNode) => {
// Ignore `url` function
if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
return false;
}
// Ignore strings, comments, etc
if (valueNode.type !== 'word') {
return;
}
// Check leading zero
if (expectation === 'always') {
const match = /(?:\D|^)(\.\d+)/.exec(valueNode.value);
if (match === null) {
return;
}
// The regexp above consists of 2 capturing groups (or capturing parentheses).
// We need the index of the second group. This makes sanse when we have "-.5" as an input
// for regex. And we need the index of ".5".
const capturingGroupIndex = match[0].length - match[1].length;
const index = valueNode.sourceIndex + match.index + capturingGroupIndex;
if (context.fix) {
alwaysFixPositions.unshift({
index,
});
return;
} else {
complain(messages.expected, node, getIndex(node) + index);
}
}
if (expectation === 'never') {
const match = /(?:\D|^)(0+)(\.\d+)/.exec(valueNode.value);
if (match === null) {
return;
}
// The regexp above consists of 3 capturing groups (or capturing parentheses).
// We need the index of the second group. This makes sanse when we have "-00.5"
// as an input for regex. And we need the index of "00".
const capturingGroupIndex = match[0].length - (match[1].length + match[2].length);
const index = valueNode.sourceIndex + match.index + capturingGroupIndex;
if (context.fix) {
neverFixPositions.unshift({
startIndex: index,
// match[1].length is the length of our matched zero(s)
endIndex: index + match[1].length,
});
return;
} else {
complain(messages.rejected, node, getIndex(node) + index);
}
}
});
if (alwaysFixPositions.length) {
alwaysFixPositions.forEach(function(fixPosition) {
const index = fixPosition.index;
if (node.type === 'atrule') {
node.params = addLeadingZero(node.params, index);
} else {
node.value = addLeadingZero(node.value, index);
}
});
}
if (neverFixPositions.length) {
neverFixPositions.forEach(function(fixPosition) {
const startIndex = fixPosition.startIndex;
const endIndex = fixPosition.endIndex;
if (node.type === 'atrule') {
node.params = removeLeadingZeros(node.params, startIndex, endIndex);
} else {
node.value = removeLeadingZeros(node.value, startIndex, endIndex);
}
});
}
}
function complain(message, node, index) {
report({
result,
ruleName,
message,
node,
index,
});
}
};
};
function addLeadingZero(input, index) {
return input.slice(0, index) + '0' + input.slice(index);
}
function removeLeadingZeros(input, startIndex, endIndex) {
return input.slice(0, startIndex) + input.slice(endIndex);
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;