1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | function getExpressionsLength(expressions) {
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | return (
|
13 | expressions.filter((item) => {
|
14 | return ["child", "descendant", "adjacent"].includes(item.type);
|
15 | }).length + 1
|
16 | );
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function rule(analyzer) {
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | const redundantChildSelectors = {
|
36 | ul: ["li"],
|
37 | ol: ["li"],
|
38 | select: ["option"],
|
39 | table: ["tr", "th"],
|
40 | tr: ["td", "th"],
|
41 | };
|
42 |
|
43 | analyzer.setMetric("redundantChildNodesSelectors");
|
44 |
|
45 | analyzer.on("selector", (_, selector, expressions) => {
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | if (getExpressionsLength(expressions) < 3) {
|
51 | return;
|
52 | }
|
53 |
|
54 | Object.keys(redundantChildSelectors).forEach((tagName) => {
|
55 |
|
56 | const tagInSelectorIndex = expressions
|
57 | .map((expr) => expr.type == "tag" && expr.name)
|
58 | .indexOf(tagName);
|
59 |
|
60 |
|
61 | if (tagInSelectorIndex < 0) {
|
62 | return;
|
63 | }
|
64 |
|
65 |
|
66 | const selectorNodeNames = expressions
|
67 | .filter((expr) =>
|
68 | [
|
69 | "tag",
|
70 | "descendant" ,
|
71 | "child" ,
|
72 | "adjacent" ,
|
73 | ].includes(expr.type)
|
74 | )
|
75 | .map((expr) =>
|
76 | expr.name ? { tag: expr.name } : { combinator: expr.type }
|
77 | );
|
78 |
|
79 |
|
80 |
|
81 | const tagIndex = selectorNodeNames
|
82 | .map((item) => item.tag)
|
83 | .indexOf(tagName);
|
84 |
|
85 | const nextTagInSelector = selectorNodeNames[tagIndex + 2]?.tag;
|
86 | const nextCombinator = selectorNodeNames[tagIndex + 1]?.combinator;
|
87 | const previousCombinator = selectorNodeNames[tagIndex - 1]?.combinator;
|
88 |
|
89 |
|
90 | const followedByRedundantTag =
|
91 | redundantChildSelectors[tagName].includes(nextTagInSelector);
|
92 | if (!followedByRedundantTag) {
|
93 | return;
|
94 | }
|
95 |
|
96 |
|
97 | if (previousCombinator === "child") {
|
98 | return;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | if (
|
110 | followedByRedundantTag &&
|
111 | ["descendant", "child"].includes(nextCombinator)
|
112 | ) {
|
113 | analyzer.incrMetric("redundantChildNodesSelectors");
|
114 | analyzer.addOffender("redundantChildNodesSelectors", selector);
|
115 | }
|
116 | });
|
117 | });
|
118 | }
|
119 |
|
120 | rule.description = "Reports redundant child nodes selectors";
|
121 | module.exports = rule;
|