UNPKG

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