UNPKG

3.7 kBJavaScriptView Raw
1'use strict';
2
3const valueParser = require('postcss-value-parser');
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');
10
11const ruleName = 'number-no-trailing-zeros';
12
13const messages = ruleMessages(ruleName, {
14 rejected: 'Unexpected trailing zero(s)',
15});
16
17const meta = {
18 url: 'https://stylelint.io/user-guide/rules/list/number-no-trailing-zeros',
19};
20
21/** @type {import('stylelint').Rule} */
22const 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 * @param {import('postcss').AtRule | import('postcss').Declaration} node
42 * @param {string} value
43 */
44 function check(node, value) {
45 /** @type {Array<{ startIndex: number, endIndex: number }>} */
46 const fixPositions = [];
47
48 // Get out quickly if there are no periods
49 if (!value.includes('.')) {
50 return;
51 }
52
53 valueParser(value).walk((valueNode) => {
54 // Ignore `url` function
55 if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
56 return false;
57 }
58
59 // Ignore strings, comments, etc
60 if (valueNode.type !== 'word') {
61 return;
62 }
63
64 const match = /\.(\d{0,100}?)(0+)(?:\D|$)/.exec(valueNode.value);
65
66 // match[1] is any numbers between the decimal and our trailing zero, could be empty
67 // match[2] is our trailing zero(s)
68 if (match === null) {
69 return;
70 }
71
72 // our index is:
73 // the index of our valueNode +
74 // the index of our match +
75 // 1 for our decimal +
76 // the length of our potential non-zero number match (match[1])
77 const index = valueNode.sourceIndex + match.index + 1 + match[1].length;
78
79 // our startIndex is identical to our index except when we have only
80 // trailing zeros after our decimal. in that case we don't need the decimal
81 // either so we move our index back by 1.
82 const startIndex = match[1].length > 0 ? index : index - 1;
83
84 // our end index is our original index + the length of our trailing zeros
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 // this is the index of the _first_ trailing zero
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 * @param {string} input
127 * @param {number} startIndex
128 * @param {number} endIndex
129 * @returns {string}
130 */
131function removeTrailingZeros(input, startIndex, endIndex) {
132 return input.slice(0, startIndex) + input.slice(endIndex);
133}
134
135rule.ruleName = ruleName;
136rule.messages = messages;
137rule.meta = meta;
138module.exports = rule;