.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / selector-attribute-brackets-space-inside / index.js
1 "use strict";
2
3 const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule");
4 const parseSelector = require("../../utils/parseSelector");
5 const report = require("../../utils/report");
6 const ruleMessages = require("../../utils/ruleMessages");
7 const styleSearch = require("style-search");
8 const validateOptions = require("../../utils/validateOptions");
9
10 const ruleName = "selector-attribute-brackets-space-inside";
11
12 const messages = ruleMessages(ruleName, {
13   expectedOpening: 'Expected single space after "["',
14   rejectedOpening: 'Unexpected whitespace after "["',
15   expectedClosing: 'Expected single space before "]"',
16   rejectedClosing: 'Unexpected whitespace before "]"'
17 });
18
19 const rule = function(expectation) {
20   return (root, result) => {
21     const validOptions = validateOptions(result, ruleName, {
22       actual: expectation,
23       possible: ["always", "never"]
24     });
25     if (!validOptions) {
26       return;
27     }
28
29     root.walkRules(rule => {
30       if (!isStandardSyntaxRule(rule)) {
31         return;
32       }
33       if (rule.selector.indexOf("[") === -1) {
34         return;
35       }
36
37       parseSelector(rule.selector, result, rule, selectorTree => {
38         selectorTree.walkAttributes(attributeNode => {
39           const attributeSelectorString = attributeNode.toString();
40
41           styleSearch(
42             { source: attributeSelectorString, target: "[" },
43             match => {
44               const nextCharIsSpace =
45                 attributeSelectorString[match.startIndex + 1] === " ";
46               const index = attributeNode.sourceIndex + match.startIndex + 1;
47               if (nextCharIsSpace && expectation === "never") {
48                 complain(messages.rejectedOpening, index);
49               }
50               if (!nextCharIsSpace && expectation === "always") {
51                 complain(messages.expectedOpening, index);
52               }
53             }
54           );
55
56           styleSearch(
57             { source: attributeSelectorString, target: "]" },
58             match => {
59               const prevCharIsSpace =
60                 attributeSelectorString[match.startIndex - 1] === " ";
61               const index = attributeNode.sourceIndex + match.startIndex - 1;
62               if (prevCharIsSpace && expectation === "never") {
63                 complain(messages.rejectedClosing, index);
64               }
65               if (!prevCharIsSpace && expectation === "always") {
66                 complain(messages.expectedClosing, index);
67               }
68             }
69           );
70         });
71       });
72
73       function complain(message, index) {
74         report({
75           message,
76           index,
77           result,
78           ruleName,
79           node: rule
80         });
81       }
82     });
83   };
84 };
85
86 rule.ruleName = ruleName;
87 rule.messages = messages;
88 module.exports = rule;