-
Notifications
You must be signed in to change notification settings - Fork 754
/
Copy pathwholePixel.js
85 lines (72 loc) · 2.54 KB
/
wholePixel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const stylelint = require('stylelint');
// eslint-disable-next-line import/no-unresolved -- TS BUG
const postcss = require('postcss');
const ruleName = 'plugin/whole-pixel';
const isString = (s) => typeof s === 'string';
const messages = stylelint.utils.ruleMessages(ruleName, {
expected: (unfixed, fixed) => `Expected "${unfixed}" to be "${fixed}"`,
});
const PX_PER_REM = 16;
const unitRegex = /(px|rem)$/;
const numberRegex = /^([-0-9.]+)/;
module.exports = stylelint.createPlugin(ruleName, (primaryOption, secondaryOptionObject, context) => {
const secondaryOptions = secondaryOptionObject || {};
return (root, result) => {
const validOptions = stylelint.utils.validateOptions(
result,
ruleName,
{
actual: secondaryOptions,
possible: {
pxPerRem: (value) => value % 1 === 0,
ignoreList: [isString],
},
},
);
if (!validOptions) {
return;
}
const pxPerRem = Number(secondaryOptions.pxPerRem) || PX_PER_REM;
const ignoreList = secondaryOptions.ignoreList || [];
const isAutoFixing = Boolean(context.fix);
const isValid = (value, unit) => {
if (unit === 'px') return Number.isInteger(value);
if (unit === 'rem') return Number.isInteger(value * pxPerRem);
return false;
};
const suggestFix = (value, unit) => {
if (unit === 'px') return `${Math.round(value)}px`;
if (unit === 'rem') return `${Math.round(value * pxPerRem) / pxPerRem}rem`;
return undefined;
};
const handleValue = (decl, value) => {
if (!unitRegex.test(value)) return;
const matched = value.match(numberRegex);
if (!matched) return;
const valueNumberString = matched[0];
const valueNumber = parseFloat(valueNumberString);
const unit = value.replace(valueNumberString, '');
if (isValid(valueNumber, unit)) return;
const suggestedValue = suggestFix(valueNumber, unit);
if (isAutoFixing) {
decl.value = decl.value.replace(value, suggestedValue);
} else {
stylelint.utils.report({
ruleName,
result,
node: decl,
message: messages.expected(value, suggestedValue),
word: value,
});
}
};
root.walkDecls((decl) => {
if (!decl.value || ignoreList.includes(decl.prop)) return;
const values = postcss.list.space(decl.value);
if (!values?.length) return;
values.forEach((value) => handleValue(decl, value));
});
};
});
module.exports.ruleName = ruleName;
module.exports.messages = messages;