.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / color-no-hex / index.js
1 "use strict";
2
3 const report = require("../../utils/report");
4 const ruleMessages = require("../../utils/ruleMessages");
5 const styleSearch = require("style-search");
6 const validateOptions = require("../../utils/validateOptions");
7
8 const ruleName = "color-no-hex";
9
10 const messages = ruleMessages(ruleName, {
11   rejected: hex => `Unexpected hex color "${hex}"`
12 });
13
14 const rule = function(actual) {
15   return (root, result) => {
16     const validOptions = validateOptions(result, ruleName, { actual });
17     if (!validOptions) {
18       return;
19     }
20
21     root.walkDecls(decl => {
22       const declString = decl.toString();
23
24       styleSearch({ source: declString, target: "#" }, match => {
25         // If there's not a colon, comma, or whitespace character before, we'll assume this is
26         // not intended to be a hex color, but is instead something like the
27         // hash in a url() argument
28         if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
29           return;
30         }
31
32         const hexMatch = /^#[0-9A-Za-z]+/.exec(
33           declString.substr(match.startIndex)
34         );
35         if (!hexMatch) {
36           return;
37         }
38         const hexValue = hexMatch[0];
39
40         report({
41           message: messages.rejected(hexValue),
42           node: decl,
43           index: match.startIndex,
44           result,
45           ruleName
46         });
47       });
48     });
49   };
50 };
51
52 rule.ruleName = ruleName;
53 rule.messages = messages;
54 module.exports = rule;