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-no-trailing-zeros';
|
12 |
|
13 | const messages = ruleMessages(ruleName, {
|
14 | rejected: 'Unexpected trailing zero(s)',
|
15 | });
|
16 |
|
17 | const meta = {
|
18 | url: 'https://stylelint.io/user-guide/rules/list/number-no-trailing-zeros',
|
19 | };
|
20 |
|
21 |
|
22 | const rule = (primary, _secondaryOptions, context) => {
|
23 | return (root, result) => {
|
24 | const validOptions = validateOptions(result, ruleName, { actual: primary });
|
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);
|
36 | });
|
37 |
|
38 | root.walkDecls((decl) => check(decl, decl.value));
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 | function check(node, value) {
|
45 |
|
46 | const fixPositions = [];
|
47 |
|
48 |
|
49 | if (!value.includes('.')) {
|
50 | return;
|
51 | }
|
52 |
|
53 | valueParser(value).walk((valueNode) => {
|
54 |
|
55 | if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
|
56 | return false;
|
57 | }
|
58 |
|
59 |
|
60 | if (valueNode.type !== 'word') {
|
61 | return;
|
62 | }
|
63 |
|
64 | const match = /\.(\d{0,100}?)(0+)(?:\D|$)/.exec(valueNode.value);
|
65 |
|
66 |
|
67 |
|
68 | if (match === null) {
|
69 | return;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | const index = valueNode.sourceIndex + match.index + 1 + match[1].length;
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | const startIndex = match[1].length > 0 ? index : index - 1;
|
83 |
|
84 |
|
85 | const endIndex = index + match[2].length;
|
86 |
|
87 | if (context.fix) {
|
88 | fixPositions.unshift({
|
89 | startIndex,
|
90 | endIndex,
|
91 | });
|
92 |
|
93 | return;
|
94 | }
|
95 |
|
96 | const baseIndex =
|
97 | node.type === 'atrule' ? atRuleParamIndex(node) : declarationValueIndex(node);
|
98 |
|
99 | report({
|
100 | message: messages.rejected,
|
101 | node,
|
102 |
|
103 | index: baseIndex + index,
|
104 | result,
|
105 | ruleName,
|
106 | });
|
107 | });
|
108 |
|
109 | if (fixPositions.length) {
|
110 | for (const fixPosition of fixPositions) {
|
111 | const startIndex = fixPosition.startIndex;
|
112 | const endIndex = fixPosition.endIndex;
|
113 |
|
114 | if (node.type === 'atrule') {
|
115 | node.params = removeTrailingZeros(node.params, startIndex, endIndex);
|
116 | } else {
|
117 | node.value = removeTrailingZeros(node.value, startIndex, endIndex);
|
118 | }
|
119 | }
|
120 | }
|
121 | }
|
122 | };
|
123 | };
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function removeTrailingZeros(input, startIndex, endIndex) {
|
132 | return input.slice(0, startIndex) + input.slice(endIndex);
|
133 | }
|
134 |
|
135 | rule.ruleName = ruleName;
|
136 | rule.messages = messages;
|
137 | rule.meta = meta;
|
138 | module.exports = rule;
|