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