-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 1.83 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
"use strict";
const styleSearch = require('style-search');
const stylelint = require('stylelint');
const findColor = require('happytree').findColor;
const { report, ruleMessages, validateOptions } = stylelint.utils;
const ruleName = "inturn/inturn-color-no-hex";
const messages = ruleMessages(ruleName, {
rejected: hex => `Unexpected hex color "${hex}"`
});
const rule = function(actual) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
if (!validOptions) {
return;
}
root.walkDecls(decl => {
const declString = decl.toString();
styleSearch({ source: declString, target: "#" }, match => {
// If there's not a colon, comma, or whitespace character before, we'll assume this is
// not intended to be a hex color, but is instead something like the
// hash in a url() argument
if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
return;
}
const hexMatch = /^#[0-9A-Za-z]+/.exec(
declString.substr(match.startIndex)
);
if (!hexMatch) {
return;
}
const hexValue = hexMatch[0];
const happyTreeValue = findColor(hexValue);
const message = Array.isArray(happyTreeValue)
? `Expected getColor('${happyTreeValue[0]}', ${Number(happyTreeValue[1])}) in ${hexValue}`
: `Expected getColor in ${hexValue}. There is no match.
Please consult specs or check with design for a color within our palette.`;
report({
message: messages.rejected(message),
node: decl,
index: match.startIndex,
result,
ruleName
});
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = {
default: stylelint.createPlugin(ruleName, rule),
rule,
};