UNPKG

4.27 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const atRuleParamIndex = require('../../utils/atRuleParamIndex');
6const declarationValueIndex = require('../../utils/declarationValueIndex');
7const report = require('../../utils/report');
8const ruleMessages = require('../../utils/ruleMessages');
9const validateOptions = require('../../utils/validateOptions');
10const valueParser = require('postcss-value-parser');
11
12const ruleName = 'number-leading-zero';
13
14const messages = ruleMessages(ruleName, {
15 expected: 'Expected a leading zero',
16 rejected: 'Unexpected leading zero',
17});
18
19function 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 // Get out quickly if there are no periods
45 if (!value.includes('.')) {
46 return;
47 }
48
49 valueParser(value).walk((valueNode) => {
50 // Ignore `url` function
51 if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
52 return false;
53 }
54
55 // Ignore strings, comments, etc
56 if (valueNode.type !== 'word') {
57 return;
58 }
59
60 // Check leading zero
61 if (expectation === 'always') {
62 const match = /(?:\D|^)(\.\d+)/.exec(valueNode.value);
63
64 if (match === null) {
65 return;
66 }
67
68 // The regexp above consists of 2 capturing groups (or capturing parentheses).
69 // We need the index of the second group. This makes sanse when we have "-.5" as an input
70 // for regex. And we need the index of ".5".
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 // The regexp above consists of 3 capturing groups (or capturing parentheses).
94 // We need the index of the second group. This makes sanse when we have "-00.5"
95 // as an input for regex. And we need the index of "00".
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 // match[1].length is the length of our matched zero(s)
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
152function addLeadingZero(input, index) {
153 // eslint-disable-next-line prefer-template
154 return input.slice(0, index) + '0' + input.slice(index);
155}
156
157function removeLeadingZeros(input, startIndex, endIndex) {
158 return input.slice(0, startIndex) + input.slice(endIndex);
159}
160
161rule.ruleName = ruleName;
162rule.messages = messages;
163module.exports = rule;