-
Notifications
You must be signed in to change notification settings - Fork 12
/
no-negative-var.js
47 lines (38 loc) · 1.19 KB
/
no-negative-var.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
const declarationValueIndex = require("stylelint/lib/utils/declarationValueIndex");
const isStandardSyntaxFunction = require("stylelint/lib/utils/isStandardSyntaxFunction");
const report = require("stylelint/lib/utils/report");
const valueParser = require("postcss-value-parser");
const ruleName = "swissquote/no-negative-var";
const messages = {
rejected: `Using "-" in front of "var()" doesn't work, use "calc(var(...) * -1)".`
};
const negativeVar = "-var";
const rule = function() {
return (root, result) => {
root.walkDecls(decl => {
const value = decl.value;
valueParser(value).walk(node => {
if (node.type !== "function") {
return;
}
if (!isStandardSyntaxFunction(node)) {
return;
}
// Remove prefixes and check if it's a negativeVar
if (node.value.replace(/^-\w+-/, "") !== negativeVar) {
return;
}
report({
message: messages.rejected,
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName
});
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;