.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / selector-attribute-quotes / index.js
1 "use strict";
2
3 const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule");
4 const isStandardSyntaxSelector = require("../../utils/isStandardSyntaxSelector");
5 const parseSelector = require("../../utils/parseSelector");
6 const report = require("../../utils/report");
7 const ruleMessages = require("../../utils/ruleMessages");
8 const validateOptions = require("../../utils/validateOptions");
9
10 const ruleName = "selector-attribute-quotes";
11
12 const messages = ruleMessages(ruleName, {
13   expected: value => `Expected quotes around "${value}"`,
14   rejected: value => `Unexpected quotes around "${value}"`
15 });
16
17 const rule = function(expectation) {
18   return (root, result) => {
19     const validOptions = validateOptions(result, ruleName, {
20       actual: expectation,
21       possible: ["always", "never"]
22     });
23     if (!validOptions) {
24       return;
25     }
26
27     root.walkRules(rule => {
28       if (!isStandardSyntaxRule(rule)) {
29         return;
30       }
31       if (!isStandardSyntaxSelector(rule.selector)) {
32         return;
33       }
34       if (
35         rule.selector.indexOf("[") === -1 ||
36         rule.selector.indexOf("=") === -1
37       ) {
38         return;
39       }
40       parseSelector(rule.selector, result, rule, selectorTree => {
41         selectorTree.walkAttributes(attributeNode => {
42           if (!attributeNode.operator) {
43             return;
44           }
45
46           const attributeSelectorString = attributeNode.toString();
47
48           if (!attributeNode.quoted && expectation === "always") {
49             complain(
50               messages.expected(attributeNode.raws.unquoted),
51               attributeNode.sourceIndex +
52                 attributeSelectorString.indexOf(attributeNode.value)
53             );
54           }
55
56           if (attributeNode.quoted && expectation === "never") {
57             complain(
58               messages.rejected(attributeNode.raws.unquoted),
59               attributeNode.sourceIndex +
60                 attributeSelectorString.indexOf(attributeNode.value)
61             );
62           }
63         });
64       });
65
66       function complain(message, index) {
67         report({
68           message,
69           index,
70           result,
71           ruleName,
72           node: rule
73         });
74       }
75     });
76   };
77 };
78
79 rule.ruleName = ruleName;
80 rule.messages = messages;
81 module.exports = rule;