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