1 | 'use strict';
|
2 |
|
3 | const valueParser = require('postcss-value-parser');
|
4 |
|
5 | const atRuleParamIndex = require('../../utils/atRuleParamIndex');
|
6 | const declarationValueIndex = require('../../utils/declarationValueIndex');
|
7 | const report = require('../../utils/report');
|
8 | const ruleMessages = require('../../utils/ruleMessages');
|
9 | const validateOptions = require('../../utils/validateOptions');
|
10 |
|
11 | const ruleName = 'number-leading-zero';
|
12 |
|
13 | const messages = ruleMessages(ruleName, {
|
14 | expected: 'Expected a leading zero',
|
15 | rejected: 'Unexpected leading zero',
|
16 | });
|
17 |
|
18 | const meta = {
|
19 | url: 'https://stylelint.io/user-guide/rules/list/number-leading-zero',
|
20 | };
|
21 |
|
22 |
|
23 | const rule = (primary, _secondaryOptions, context) => {
|
24 | return (root, result) => {
|
25 | const validOptions = validateOptions(result, ruleName, {
|
26 | actual: primary,
|
27 | possible: ['always', 'never'],
|
28 | });
|
29 |
|
30 | if (!validOptions) {
|
31 | return;
|
32 | }
|
33 |
|
34 | root.walkAtRules((atRule) => {
|
35 | if (atRule.name.toLowerCase() === 'import') {
|
36 | return;
|
37 | }
|
38 |
|
39 | check(atRule, atRule.params);
|
40 | });
|
41 |
|
42 | root.walkDecls((decl) => check(decl, decl.value));
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 | function check(node, value) {
|
49 |
|
50 | const neverFixPositions = [];
|
51 |
|
52 | const alwaysFixPositions = [];
|
53 |
|
54 |
|
55 | if (!value.includes('.')) {
|
56 | return;
|
57 | }
|
58 |
|
59 | valueParser(value).walk((valueNode) => {
|
60 |
|
61 | if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
|
62 | return false;
|
63 | }
|
64 |
|
65 |
|
66 | if (valueNode.type !== 'word') {
|
67 | return;
|
68 | }
|
69 |
|
70 |
|
71 | if (primary === 'always') {
|
72 | const match = /(?:\D|^)(\.\d+)/.exec(valueNode.value);
|
73 |
|
74 | if (match === null) {
|
75 | return;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | const capturingGroupIndex = match[0].length - match[1].length;
|
82 |
|
83 | const index = valueNode.sourceIndex + match.index + capturingGroupIndex;
|
84 |
|
85 | if (context.fix) {
|
86 | alwaysFixPositions.unshift({
|
87 | index,
|
88 | });
|
89 |
|
90 | return;
|
91 | }
|
92 |
|
93 | const baseIndex =
|
94 | node.type === 'atrule' ? atRuleParamIndex(node) : declarationValueIndex(node);
|
95 |
|
96 | complain(messages.expected, node, baseIndex + index);
|
97 | }
|
98 |
|
99 | if (primary === 'never') {
|
100 | const match = /(?:\D|^)(0+)(\.\d+)/.exec(valueNode.value);
|
101 |
|
102 | if (match === null) {
|
103 | return;
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | const capturingGroupIndex = match[0].length - (match[1].length + match[2].length);
|
110 |
|
111 | const index = valueNode.sourceIndex + match.index + capturingGroupIndex;
|
112 |
|
113 | if (context.fix) {
|
114 | neverFixPositions.unshift({
|
115 | startIndex: index,
|
116 |
|
117 | endIndex: index + match[1].length,
|
118 | });
|
119 |
|
120 | return;
|
121 | }
|
122 |
|
123 | const baseIndex =
|
124 | node.type === 'atrule' ? atRuleParamIndex(node) : declarationValueIndex(node);
|
125 |
|
126 | complain(messages.rejected, node, baseIndex + index);
|
127 | }
|
128 | });
|
129 |
|
130 | if (alwaysFixPositions.length) {
|
131 | for (const fixPosition of alwaysFixPositions) {
|
132 | const index = fixPosition.index;
|
133 |
|
134 | if (node.type === 'atrule') {
|
135 | node.params = addLeadingZero(node.params, index);
|
136 | } else {
|
137 | node.value = addLeadingZero(node.value, index);
|
138 | }
|
139 | }
|
140 | }
|
141 |
|
142 | if (neverFixPositions.length) {
|
143 | for (const fixPosition of neverFixPositions) {
|
144 | const startIndex = fixPosition.startIndex;
|
145 | const endIndex = fixPosition.endIndex;
|
146 |
|
147 | if (node.type === 'atrule') {
|
148 | node.params = removeLeadingZeros(node.params, startIndex, endIndex);
|
149 | } else {
|
150 | node.value = removeLeadingZeros(node.value, startIndex, endIndex);
|
151 | }
|
152 | }
|
153 | }
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 | function complain(message, node, index) {
|
162 | report({
|
163 | result,
|
164 | ruleName,
|
165 | message,
|
166 | node,
|
167 | index,
|
168 | });
|
169 | }
|
170 | };
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | function addLeadingZero(input, index) {
|
179 |
|
180 | return input.slice(0, index) + '0' + input.slice(index);
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function removeLeadingZeros(input, startIndex, endIndex) {
|
190 | return input.slice(0, startIndex) + input.slice(endIndex);
|
191 | }
|
192 |
|
193 | rule.ruleName = ruleName;
|
194 | rule.messages = messages;
|
195 | rule.meta = meta;
|
196 | module.exports = rule;
|