1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const isStandardSyntaxCombinator = require('../../utils/isStandardSyntaxCombinator');
|
5 | const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
|
6 | const parseSelector = require('../../utils/parseSelector');
|
7 | const report = require('../../utils/report');
|
8 | const ruleMessages = require('../../utils/ruleMessages');
|
9 | const validateOptions = require('../../utils/validateOptions');
|
10 |
|
11 | const ruleName = 'selector-combinator-blacklist';
|
12 |
|
13 | const messages = ruleMessages(ruleName, {
|
14 | rejected: (combinator) => `Unexpected combinator "${combinator}"`,
|
15 | });
|
16 |
|
17 | function 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 |
|
60 | function normalizeCombinator(value) {
|
61 | return value.replace(/\s+/g, ' ');
|
62 | }
|
63 |
|
64 | rule.primaryOptionArray = true;
|
65 |
|
66 | rule.ruleName = ruleName;
|
67 | rule.messages = messages;
|
68 | module.exports = rule;
|