-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
82 lines (65 loc) · 2.05 KB
/
index.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
import { parse } from "postcss-values-parser";
import stylelint from "stylelint";
import createRuleMessages from "../../utils/createRuleMessages.js";
import findScaleByUnit from "../../utils/findScaleByUnit.js";
import getClosest from "../../utils/getClosest.js";
import getValue from "../../utils/getValue.js";
import hasScalesWithUnits from "../../utils/hasScalesWithUnits.js";
import isOnNumericScale from "../../utils/isOnNumericScale.js";
import setValue from "../../utils/setValue.js";
const {
createPlugin,
utils: { report, validateOptions },
} = stylelint;
const ruleName = "scales/border-widths";
const messages = createRuleMessages(ruleName);
const meta = {
url: "https://github.com/jeddy3/stylelint-scales/blob/main/lib/rules/border-widths/README.md",
fixable: true,
};
const propertyFilter =
/^border$|^border.*(width$|top$|right$|bottom$|left$|block$|inline$|start$|end$)/;
const rule = (primary, secondary, { fix }) => {
return (root, result) => {
if (
!validateOptions(result, ruleName, {
actual: primary,
possible: hasScalesWithUnits,
})
)
return;
root.walkDecls(propertyFilter, (decl) => {
const value = getValue(decl);
let hasFix = false;
const valueRoot = parse(value, {
ignoreUnknownWords: true,
});
valueRoot.walkNumerics((node) => {
check(node);
});
function check(node) {
const { value, unit } = node;
const scale = findScaleByUnit(primary, unit);
if (isOnNumericScale(scale, value)) return;
if (fix) {
node.value = getClosest(scale, value);
hasFix = true;
return;
}
report({
message: messages.expected(value, scale.join(", "), unit),
node: decl,
result,
ruleName,
word: value,
});
}
if (hasFix) setValue(decl, valueRoot.toString());
});
};
};
rule.primaryOptionArray = true;
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
export default createPlugin(ruleName, rule);