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