-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (58 loc) · 1.75 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
// Abbreviated example
var stylelint = require("stylelint");
var isColor = require("is-color");
var ruleName = "restrict/color-variables-per-files";
var messages = stylelint.utils.ruleMessages(ruleName, {
expected: "Expected ..."
});
module.exports = stylelint.createPlugin(ruleName, function(
primaryOption,
secondaryOptionObject
) {
return function(postcssRoot, postcssResult) {
var validOptions = stylelint.utils.validateOptions(
postcssResult,
ruleName,
primaryOption
);
if (!validOptions) {
return;
}
const fileName = postcssRoot.source.input.file;
const css = postcssRoot.source.input.css;
// Find variables only inside the files that are not allowed to have them
if (!primaryOption.files.some(f => fileName.indexOf(f) > -1)) {
const regex = /\$.*:.*;/gm;
let m;
let counter = 0;
while ((m = regex.exec(css)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
let onlyValue = match.replace(/\$.*: /gm, "").replace(";", "");
if (isColor(onlyValue)) {
counter++;
}
});
}
if (counter > 0) {
stylelint.utils.report({
message:
"Variables can only be defined in " +
primaryOption.files +
", found " +
counter +
" variables.",
ruleName: ruleName,
result: postcssResult,
node: postcssRoot
});
}
}
};
});
module.exports.ruleName = ruleName;
module.exports.messages = messages;