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");
11 const ruleName = "selector-pseudo-class-parentheses-space-inside";
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 ")"'
20 const rule = function(expectation) {
21 return (root, result) => {
22 const validOptions = validateOptions(result, ruleName, {
24 possible: ["always", "never"]
30 root.walkRules(rule => {
31 if (!isStandardSyntaxRule(rule)) {
34 if (rule.selector.indexOf("(") === -1) {
38 parseSelector(rule.selector, result, rule, selectorTree => {
39 selectorTree.walkPseudos(pseudoNode => {
40 if (_.get(pseudoNode, "parent.parent.type") === "pseudo") {
44 const pseudoSelectorString = pseudoNode.toString();
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);
53 if (!nextCharIsSpace && expectation === "always") {
54 complain(messages.expectedOpening, index);
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);
65 if (!prevCharIsSpace && expectation === "always") {
66 complain(messages.expectedClosing, index);
72 function complain(message, index) {
85 rule.ruleName = ruleName;
86 rule.messages = messages;
87 module.exports = rule;