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");
10 const ruleName = "selector-attribute-brackets-space-inside";
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 "]"'
19 const rule = function(expectation) {
20 return (root, result) => {
21 const validOptions = validateOptions(result, ruleName, {
23 possible: ["always", "never"]
29 root.walkRules(rule => {
30 if (!isStandardSyntaxRule(rule)) {
33 if (rule.selector.indexOf("[") === -1) {
37 parseSelector(rule.selector, result, rule, selectorTree => {
38 selectorTree.walkAttributes(attributeNode => {
39 const attributeSelectorString = attributeNode.toString();
42 { source: attributeSelectorString, target: "[" },
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);
50 if (!nextCharIsSpace && expectation === "always") {
51 complain(messages.expectedOpening, index);
57 { source: attributeSelectorString, target: "]" },
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);
65 if (!prevCharIsSpace && expectation === "always") {
66 complain(messages.expectedClosing, index);
73 function complain(message, index) {
86 rule.ruleName = ruleName;
87 rule.messages = messages;
88 module.exports = rule;