.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / color-hex-case / index.js
1 "use strict";
2
3 const blurFunctionArguments = require("../../utils/blurFunctionArguments");
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-hex-case";
10
11 const messages = ruleMessages(ruleName, {
12   expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`
13 });
14
15 const rule = function(expectation, options, context) {
16   return (root, result) => {
17     const validOptions = validateOptions(result, ruleName, {
18       actual: expectation,
19       possible: ["lower", "upper"]
20     });
21     if (!validOptions) {
22       return;
23     }
24
25     root.walkDecls(decl => {
26       const declString = blurFunctionArguments(decl.toString(), "url");
27       const fixPositions = [];
28
29       styleSearch({ source: declString, target: "#" }, match => {
30         const hexMatch = /^#[0-9A-Za-z]+/.exec(
31           declString.substr(match.startIndex)
32         );
33         if (!hexMatch) {
34           return;
35         }
36
37         const hexValue = hexMatch[0];
38         const hexValueLower = hexValue.toLowerCase();
39         const hexValueUpper = hexValue.toUpperCase();
40         const expectedHex =
41           expectation === "lower" ? hexValueLower : hexValueUpper;
42
43         if (hexValue === expectedHex) {
44           return;
45         }
46
47         if (context.fix) {
48           fixPositions.unshift({
49             expectedHex,
50             currentHex: hexValue,
51             startIndex: match.startIndex
52           });
53
54           return;
55         }
56
57         report({
58           message: messages.expected(hexValue, expectedHex),
59           node: decl,
60           index: match.startIndex,
61           result,
62           ruleName
63         });
64       });
65
66       if (fixPositions.length) {
67         const declProp = decl.prop;
68         const declBetween = decl.raws.between;
69
70         fixPositions.forEach(function(fixPosition) {
71           // 1 — it's a # length
72           decl.value = replaceHex(
73             decl.value,
74             fixPosition.currentHex,
75             fixPosition.expectedHex,
76             fixPosition.startIndex - declProp.length - declBetween.length - 1
77           );
78         });
79       }
80     });
81   };
82 };
83
84 function replaceHex(input, searchString, replaceString, startIndex) {
85   const offset = startIndex + 1;
86   const stringStart = input.slice(0, offset);
87   const stringEnd = input.slice(offset + searchString.length);
88
89   return stringStart + replaceString + stringEnd;
90 }
91
92 rule.ruleName = ruleName;
93 rule.messages = messages;
94 module.exports = rule;