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");
10 const ruleName = "number-leading-zero";
12 const messages = ruleMessages(ruleName, {
13 expected: "Expected a leading zero",
14 rejected: "Unexpected leading zero"
17 const rule = function(expectation, secondary, context) {
18 return (root, result) => {
19 const validOptions = validateOptions(result, ruleName, {
21 possible: ["always", "never"]
27 root.walkAtRules(atRule => {
28 if (atRule.name.toLowerCase() === "import") {
32 check(atRule, atRule.params, atRuleParamIndex);
35 root.walkDecls(decl => check(decl, decl.value, declarationValueIndex));
37 function check(node, value, getIndex) {
38 const neverFixPositions = [];
39 const alwaysFixPositions = [];
41 // Get out quickly if there are no periods
42 if (value.indexOf(".") === -1) {
46 valueParser(value).walk(valueNode => {
47 // Ignore `url` function
49 valueNode.type === "function" &&
50 valueNode.value.toLowerCase() === "url"
55 // Ignore strings, comments, etc
56 if (valueNode.type !== "word") {
61 if (expectation === "always") {
62 const match = /(?:\D|^)(\.\d+)/.exec(valueNode.value);
67 // The regexp above consists of 2 capturing groups (or capturing parentheses).
68 // We need the index of the second group. This makes sanse when we have "-.5" as an input
69 // for regex. And we need the index of ".5".
70 const capturingGroupIndex = match[0].length - match[1].length;
73 valueNode.sourceIndex + match.index + capturingGroupIndex;
76 alwaysFixPositions.unshift({
81 complain(messages.expected, node, getIndex(node) + index);
85 if (expectation === "never") {
86 const match = /(?:\D|^)(0+)(\.\d+)/.exec(valueNode.value);
92 // The regexp above consists of 3 capturing groups (or capturing parentheses).
93 // We need the index of the second group. This makes sanse when we have "-00.5"
94 // as an input for regex. And we need the index of "00".
95 const capturingGroupIndex =
96 match[0].length - (match[1].length + match[2].length);
99 valueNode.sourceIndex + match.index + capturingGroupIndex;
102 neverFixPositions.unshift({
104 // match[1].length is the length of our matched zero(s)
105 endIndex: index + match[1].length
109 complain(messages.rejected, node, getIndex(node) + index);
114 if (alwaysFixPositions.length) {
115 alwaysFixPositions.forEach(function(fixPosition) {
116 const index = fixPosition.index;
117 if (node.type === "atrule") {
118 node.params = addLeadingZero(node.params, index);
120 node.value = addLeadingZero(node.value, index);
125 if (neverFixPositions.length) {
126 neverFixPositions.forEach(function(fixPosition) {
127 const startIndex = fixPosition.startIndex;
128 const endIndex = fixPosition.endIndex;
129 if (node.type === "atrule") {
130 node.params = removeLeadingZeros(node.params, startIndex, endIndex);
132 node.value = removeLeadingZeros(node.value, startIndex, endIndex);
138 function complain(message, node, index) {
150 function addLeadingZero(input, index) {
151 return input.slice(0, index) + "0" + input.slice(index);
154 function removeLeadingZeros(input, startIndex, endIndex) {
155 return input.slice(0, startIndex) + input.slice(endIndex);
158 rule.ruleName = ruleName;
159 rule.messages = messages;
160 module.exports = rule;