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