1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const beforeBlockString = require('../../utils/beforeBlockString');
|
5 | const blurComments = require('../../utils/blurComments');
|
6 | const hasBlock = require('../../utils/hasBlock');
|
7 | const isCustomProperty = require('../../utils/isCustomProperty');
|
8 | const isLessVariable = require('../../utils/isLessVariable');
|
9 | const isMathFunction = require('../../utils/isMathFunction');
|
10 | const keywordSets = require('../../reference/keywordSets');
|
11 | const optionsMatches = require('../../utils/optionsMatches');
|
12 | const report = require('../../utils/report');
|
13 | const ruleMessages = require('../../utils/ruleMessages');
|
14 | const styleSearch = require('style-search');
|
15 | const validateOptions = require('../../utils/validateOptions');
|
16 | const valueParser = require('postcss-value-parser');
|
17 |
|
18 | const ruleName = 'length-zero-no-unit';
|
19 |
|
20 | const messages = ruleMessages(ruleName, {
|
21 | rejected: 'Unexpected unit',
|
22 | });
|
23 |
|
24 | function rule(actual, secondary, context) {
|
25 | return (root, result) => {
|
26 | const validOptions = validateOptions(result, ruleName, { actual });
|
27 |
|
28 | if (!validOptions) {
|
29 | return;
|
30 | }
|
31 |
|
32 | root.walkDecls((decl) => {
|
33 | if (decl.prop.toLowerCase() === 'line-height') {
|
34 | return;
|
35 | }
|
36 |
|
37 | const stringValue = blurComments(decl.toString());
|
38 | const ignorableIndexes = new Array(stringValue.length).fill(false);
|
39 | const parsedValue = valueParser(stringValue);
|
40 |
|
41 | parsedValue.walk((node, nodeIndex) => {
|
42 | if (decl.prop.toLowerCase() === 'font' && node.type === 'div' && node.value === '/') {
|
43 | const lineHeightNode = parsedValue.nodes[nodeIndex + 1];
|
44 | const lineHeightNodeValue = valueParser.stringify(lineHeightNode);
|
45 |
|
46 | for (let i = 0; i < lineHeightNodeValue.length; i++) {
|
47 | ignorableIndexes[lineHeightNode.sourceIndex + i] = true;
|
48 | }
|
49 |
|
50 | return;
|
51 | }
|
52 |
|
53 | if (node.type !== 'function') {
|
54 | return;
|
55 | }
|
56 |
|
57 | const stringValue = valueParser.stringify(node);
|
58 | const ignoreFlag = isMathFunction(node);
|
59 |
|
60 | for (let i = 0; i < stringValue.length; i++) {
|
61 | ignorableIndexes[node.sourceIndex + i] = ignoreFlag;
|
62 | }
|
63 | });
|
64 |
|
65 | check(stringValue, decl, ignorableIndexes);
|
66 | });
|
67 |
|
68 | root.walkAtRules((atRule) => {
|
69 |
|
70 | if (isLessVariable(atRule)) {
|
71 | return;
|
72 | }
|
73 |
|
74 | const source = hasBlock(atRule)
|
75 | ? beforeBlockString(atRule, { noRawBefore: true })
|
76 | : atRule.toString();
|
77 |
|
78 | check(source, atRule);
|
79 | });
|
80 |
|
81 | function check(value, node, ignorableIndexes = []) {
|
82 | if (optionsMatches(secondary, 'ignore', 'custom-properties') && isCustomProperty(value)) {
|
83 | return;
|
84 | }
|
85 |
|
86 | const fixPositions = [];
|
87 |
|
88 | styleSearch({ source: value, target: '0' }, (match) => {
|
89 | const index = match.startIndex;
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | if (ignorableIndexes[index]) {
|
105 | return;
|
106 | }
|
107 |
|
108 | const prevValueBreakIndex = _.findLastIndex(value.substr(0, index), (char) => {
|
109 | return [' ', ',', ')', '(', '#', ':', '\n', '\t'].includes(char);
|
110 | });
|
111 |
|
112 |
|
113 | if (value[prevValueBreakIndex] === '#') {
|
114 | return;
|
115 | }
|
116 |
|
117 |
|
118 | const valueWithZeroStart = prevValueBreakIndex === -1 ? 0 : prevValueBreakIndex + 1;
|
119 |
|
120 | const nextValueBreakIndex = _.findIndex(value.substr(valueWithZeroStart), (char) => {
|
121 | return [' ', ',', ')', '/'].includes(char);
|
122 | });
|
123 |
|
124 |
|
125 | const valueWithZeroEnd =
|
126 | nextValueBreakIndex === -1 ? value.length : nextValueBreakIndex + valueWithZeroStart;
|
127 |
|
128 | const valueWithZero = value.slice(valueWithZeroStart, valueWithZeroEnd);
|
129 | const parsedValue = valueParser.unit(valueWithZero);
|
130 |
|
131 | if (!parsedValue || (parsedValue && !parsedValue.unit)) {
|
132 | return;
|
133 | }
|
134 |
|
135 | if (parsedValue.unit.toLowerCase() === 'fr') {
|
136 | return;
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 | _.range(valueWithZeroStart, valueWithZeroEnd).forEach((i) => (ignorableIndexes[i] = true));
|
142 |
|
143 |
|
144 |
|
145 | if (
|
146 | parseFloat(valueWithZero) !== 0 ||
|
147 | !keywordSets.lengthUnits.has(parsedValue.unit.toLowerCase())
|
148 | ) {
|
149 | return;
|
150 | }
|
151 |
|
152 | if (context.fix) {
|
153 | fixPositions.unshift({
|
154 | startIndex: valueWithZeroStart,
|
155 | length: valueWithZeroEnd - valueWithZeroStart,
|
156 | });
|
157 |
|
158 | return;
|
159 | }
|
160 |
|
161 | report({
|
162 | message: messages.rejected,
|
163 | node,
|
164 | index: valueWithZeroEnd - parsedValue.unit.length,
|
165 | result,
|
166 | ruleName,
|
167 | });
|
168 | });
|
169 |
|
170 | if (fixPositions.length) {
|
171 | fixPositions.forEach((fixPosition) => {
|
172 | if (node.type === 'atrule') {
|
173 |
|
174 | const realIndex =
|
175 | fixPosition.startIndex - node.name.length - node.raws.afterName.length - 1;
|
176 |
|
177 | node.params = replaceZero(node.params, realIndex, fixPosition.length);
|
178 | } else {
|
179 | const realIndex = fixPosition.startIndex - node.prop.length - node.raws.between.length;
|
180 |
|
181 | node.value = replaceZero(node.value, realIndex, fixPosition.length);
|
182 | }
|
183 | });
|
184 | }
|
185 | }
|
186 | };
|
187 | }
|
188 |
|
189 | function replaceZero(input, startIndex, length) {
|
190 | const stringStart = input.slice(0, startIndex);
|
191 | const stringEnd = input.slice(startIndex + length);
|
192 |
|
193 | return `${stringStart}0${stringEnd}`;
|
194 | }
|
195 |
|
196 | rule.ruleName = ruleName;
|
197 | rule.messages = messages;
|
198 | module.exports = rule;
|