UNPKG

1.88 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-blacklist';
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 result.warn(
31 `'${ruleName}' has been deprecated. Instead use 'selector-combinator-disallowed-list'.`,
32 {
33 stylelintType: 'deprecation',
34 stylelintReference: `https://github.com/stylelint/stylelint/blob/13.7.0/lib/rules/${ruleName}/README.md`,
35 },
36 );
37
38 root.walkRules((rule) => {
39 if (!isStandardSyntaxRule(rule)) {
40 return;
41 }
42
43 const selector = rule.selector;
44
45 parseSelector(selector, result, rule, (fullSelector) => {
46 fullSelector.walkCombinators((combinatorNode) => {
47 if (!isStandardSyntaxCombinator(combinatorNode)) {
48 return;
49 }
50
51 const value = normalizeCombinator(combinatorNode.value);
52
53 if (!list.includes(value)) {
54 return;
55 }
56
57 report({
58 result,
59 ruleName,
60 message: messages.rejected(value),
61 node: rule,
62 index: combinatorNode.sourceIndex,
63 });
64 });
65 });
66 });
67 };
68}
69
70function normalizeCombinator(value) {
71 return value.replace(/\s+/g, ' ');
72}
73
74rule.primaryOptionArray = true;
75
76rule.ruleName = ruleName;
77rule.messages = messages;
78module.exports = rule;