1 | 'use strict';
|
2 |
|
3 | const atRuleParamIndex = require('../../utils/atRuleParamIndex');
|
4 | const declarationValueIndex = require('../../utils/declarationValueIndex');
|
5 | const report = require('../../utils/report');
|
6 | const ruleMessages = require('../../utils/ruleMessages');
|
7 | const validateOptions = require('../../utils/validateOptions');
|
8 | const valueParser = require('postcss-value-parser');
|
9 |
|
10 | const ruleName = 'number-no-trailing-zeros';
|
11 |
|
12 | const messages = ruleMessages(ruleName, {
|
13 | rejected: 'Unexpected trailing zero(s)',
|
14 | });
|
15 |
|
16 | function rule(actual, secondary, context) {
|
17 | return (root, result) => {
|
18 | const validOptions = validateOptions(result, ruleName, { actual });
|
19 |
|
20 | if (!validOptions) {
|
21 | return;
|
22 | }
|
23 |
|
24 | root.walkAtRules((atRule) => {
|
25 | if (atRule.name.toLowerCase() === 'import') {
|
26 | return;
|
27 | }
|
28 |
|
29 | check(atRule, atRule.params, atRuleParamIndex);
|
30 | });
|
31 |
|
32 | root.walkDecls((decl) => check(decl, decl.value, declarationValueIndex));
|
33 |
|
34 | function check(node, value, getIndex) {
|
35 | const fixPositions = [];
|
36 |
|
37 |
|
38 | if (!value.includes('.')) {
|
39 | return;
|
40 | }
|
41 |
|
42 | valueParser(value).walk((valueNode) => {
|
43 |
|
44 | if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
|
45 | return false;
|
46 | }
|
47 |
|
48 |
|
49 | if (valueNode.type !== 'word') {
|
50 | return;
|
51 | }
|
52 |
|
53 | const match = /\.(\d*?)(0+)(?:\D|$)/.exec(valueNode.value);
|
54 |
|
55 |
|
56 |
|
57 | if (match === null) {
|
58 | return;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const index = valueNode.sourceIndex + match.index + 1 + match[1].length;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | const startIndex = match[1].length > 0 ? index : index - 1;
|
72 |
|
73 |
|
74 | const endIndex = index + match[2].length;
|
75 |
|
76 | if (context.fix) {
|
77 | fixPositions.unshift({
|
78 | startIndex,
|
79 | endIndex,
|
80 | });
|
81 |
|
82 | return;
|
83 | }
|
84 |
|
85 | report({
|
86 | message: messages.rejected,
|
87 | node,
|
88 |
|
89 | index: getIndex(node) + index,
|
90 | result,
|
91 | ruleName,
|
92 | });
|
93 | });
|
94 |
|
95 | if (fixPositions.length) {
|
96 | fixPositions.forEach((fixPosition) => {
|
97 | const startIndex = fixPosition.startIndex;
|
98 | const endIndex = fixPosition.endIndex;
|
99 |
|
100 | if (node.type === 'atrule') {
|
101 | node.params = removeTrailingZeros(node.params, startIndex, endIndex);
|
102 | } else {
|
103 | node.value = removeTrailingZeros(node.value, startIndex, endIndex);
|
104 | }
|
105 | });
|
106 | }
|
107 | }
|
108 | };
|
109 | }
|
110 |
|
111 | function removeTrailingZeros(input, startIndex, endIndex) {
|
112 | return input.slice(0, startIndex) + input.slice(endIndex);
|
113 | }
|
114 |
|
115 | rule.ruleName = ruleName;
|
116 | rule.messages = messages;
|
117 | module.exports = rule;
|