.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / number-no-trailing-zeros / index.js
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 const rule = function(actual, secondary, context) {
17   return (root, result) => {
18     const validOptions = validateOptions(result, ruleName, { actual });
19     if (!validOptions) {
20       return;
21     }
22
23     root.walkAtRules(atRule => {
24       if (atRule.name.toLowerCase() === "import") {
25         return;
26       }
27
28       check(atRule, atRule.params, atRuleParamIndex);
29     });
30
31     root.walkDecls(decl => check(decl, decl.value, declarationValueIndex));
32
33     function check(node, value, getIndex) {
34       const fixPositions = [];
35
36       // Get out quickly if there are no periods
37       if (value.indexOf(".") === -1) {
38         return;
39       }
40
41       valueParser(value).walk(valueNode => {
42         // Ignore `url` function
43         if (
44           valueNode.type === "function" &&
45           valueNode.value.toLowerCase() === "url"
46         ) {
47           return false;
48         }
49
50         // Ignore strings, comments, etc
51         if (valueNode.type !== "word") {
52           return;
53         }
54
55         const match = /\.(\d*?)(0+)(?:\D|$)/.exec(valueNode.value);
56         // match[1] is any numbers between the decimal and our trailing zero, could be empty
57         // match[2] is our trailing zero(s)
58         if (match === null) {
59           return;
60         }
61
62         // our index is:
63         //  the index of our valueNode +
64         //  the index of our match +
65         //  1 for our decimal +
66         //  the length of our potential non-zero number match (match[1])
67         const index = valueNode.sourceIndex + match.index + 1 + match[1].length;
68
69         // our startIndex is identical to our index except when we have only
70         // trailing zeros after our decimal. in that case we don't need the decimal
71         // either so we move our index back by 1.
72         const startIndex = match[1].length > 0 ? index : index - 1;
73
74         // our end index is our original index + the length of our trailing zeros
75         const endIndex = index + match[2].length;
76
77         if (context.fix) {
78           fixPositions.unshift({
79             startIndex,
80             endIndex
81           });
82           return;
83         } else {
84           report({
85             message: messages.rejected,
86             node,
87             // this is the index of the _first_ trailing zero
88             index: getIndex(node) + index,
89             result,
90             ruleName
91           });
92         }
93       });
94
95       if (fixPositions.length) {
96         fixPositions.forEach(function(fixPosition) {
97           const startIndex = fixPosition.startIndex;
98           const endIndex = fixPosition.endIndex;
99           if (node.type === "atrule") {
100             node.params = removeTrailingZeros(
101               node.params,
102               startIndex,
103               endIndex
104             );
105           } else {
106             node.value = removeTrailingZeros(node.value, startIndex, endIndex);
107           }
108         });
109       }
110     }
111   };
112 };
113
114 function removeTrailingZeros(input, startIndex, endIndex) {
115   return input.slice(0, startIndex) + input.slice(endIndex);
116 }
117
118 rule.ruleName = ruleName;
119 rule.messages = messages;
120 module.exports = rule;