UNPKG

1.61 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const isStandardSyntaxCombinator = require('../../utils/isStandardSyntaxCombinator');
5const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
6const parseSelector = require('../../utils/parseSelector');
7const report = require('../../utils/report');
8const ruleMessages = require('../../utils/ruleMessages');
9const validateOptions = require('../../utils/validateOptions');
10
11const ruleName = 'selector-combinator-blacklist';
12
13const messages = ruleMessages(ruleName, {
14 rejected: (combinator) => `Unexpected combinator "${combinator}"`,
15});
16
17function rule(blacklist) {
18 return (root, result) => {
19 const validOptions = validateOptions(result, ruleName, {
20 actual: blacklist,
21 possible: [_.isString],
22 });
23
24 if (!validOptions) {
25 return;
26 }
27
28 root.walkRules((rule) => {
29 if (!isStandardSyntaxRule(rule)) {
30 return;
31 }
32
33 const selector = rule.selector;
34
35 parseSelector(selector, result, rule, (fullSelector) => {
36 fullSelector.walkCombinators((combinatorNode) => {
37 if (!isStandardSyntaxCombinator(combinatorNode)) {
38 return;
39 }
40
41 const value = normalizeCombinator(combinatorNode.value);
42
43 if (!blacklist.includes(value)) {
44 return;
45 }
46
47 report({
48 result,
49 ruleName,
50 message: messages.rejected(value),
51 node: rule,
52 index: combinatorNode.sourceIndex,
53 });
54 });
55 });
56 });
57 };
58}
59
60function normalizeCombinator(value) {
61 return value.replace(/\s+/g, ' ');
62}
63
64rule.primaryOptionArray = true;
65
66rule.ruleName = ruleName;
67rule.messages = messages;
68module.exports = rule;