-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
96 lines (83 loc) · 2.19 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const stylelint = require("stylelint");
const ruleName = "plugin/z-index-value-constraint";
const messages = stylelint.utils.ruleMessages(ruleName, {
largerThanMax: expected =>
`Expected z-index to have maximum value of ${expected}.`,
smallerThanMin: expected =>
`Expected z-index to have minimum value of ${expected}.`
});
function isNumber(value) {
return typeof value === "number";
}
function isNegative(value) {
return value < 0;
}
const _isNaN =
Number.isNaN ||
function (value) {
return value !== value;
};
function possibleValueTest(value) {
return isNumber(value) && !isNegative(value);
}
module.exports = stylelint.createPlugin(
ruleName,
function (options, secondary) {
return function (cssRoot, result) {
const validOptions = stylelint.utils.validateOptions(
result,
ruleName,
{
actual: options,
possible: {
min: possibleValueTest,
max: possibleValueTest
}
},
{
actual: secondary,
possible: {
ignoreValues: [isNumber]
},
optional: true
}
);
if (!validOptions) {
return;
}
cssRoot.walkDecls("z-index", function (decl) {
const value = Number(decl.value);
if (
_isNaN(value) ||
(secondary &&
Array.isArray(secondary.ignoreValues) &&
secondary.ignoreValues.indexOf(value) > -1)
) {
return;
}
if (options.max && Math.abs(value) > options.max) {
stylelint.utils.report({
ruleName,
result,
node: decl,
message: messages.largerThanMax(
isNegative(value) ? options.max * -1 : options.max
)
});
}
if (options.min && Math.abs(value) < options.min) {
stylelint.utils.report({
ruleName,
result,
node: decl,
message: messages.smallerThanMin(
isNegative(value) ? options.min * -1 : options.min
)
});
}
});
};
}
);
module.exports.ruleName = ruleName;
module.exports.messages = messages;