UNPKG

1.66 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const isStandardSyntaxCombinator = require('../../utils/isStandardSyntaxCombinator');
6const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
7const parseSelector = require('../../utils/parseSelector');
8const report = require('../../utils/report');
9const ruleMessages = require('../../utils/ruleMessages');
10const validateOptions = require('../../utils/validateOptions');
11const { isString } = require('../../utils/validateTypes');
12
13const ruleName = 'selector-combinator-allowed-list';
14
15const messages = ruleMessages(ruleName, {
16 rejected: (combinator) => `Unexpected combinator "${combinator}"`,
17});
18
19function rule(list) {
20 return (root, result) => {
21 const validOptions = validateOptions(result, ruleName, {
22 actual: list,
23 possible: [isString],
24 });
25
26 if (!validOptions) {
27 return;
28 }
29
30 root.walkRules((ruleNode) => {
31 if (!isStandardSyntaxRule(ruleNode)) {
32 return;
33 }
34
35 const selector = ruleNode.selector;
36
37 parseSelector(selector, result, ruleNode, (fullSelector) => {
38 fullSelector.walkCombinators((combinatorNode) => {
39 if (!isStandardSyntaxCombinator(combinatorNode)) {
40 return;
41 }
42
43 const value = normalizeCombinator(combinatorNode.value);
44
45 if (list.includes(value)) {
46 return;
47 }
48
49 report({
50 result,
51 ruleName,
52 message: messages.rejected(value),
53 node: ruleNode,
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;