UNPKG

911 BJavaScriptView Raw
1"use strict";
2
3/**
4 * @param { import("../lib/css-analyzer") } analyzer
5 */
6function rule(analyzer) {
7 analyzer.setMetric("qualifiedSelectors");
8
9 // @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS
10 analyzer.on("selector", (_, selector, expressions) => {
11 var hasId = expressions.some((expr) => expr.name === "id"),
12 hasTag = expressions.some((expr) => expr.type === "tag"),
13 hasClass = expressions.some((expr) => expr.name === "class");
14
15 // console.log(selector, expressions, {hasId, hasTag, hasClass});
16
17 if (
18 // tag#id
19 (hasId && hasTag) ||
20 // .class#id
21 (hasId && hasClass) ||
22 // tag.class
23 (hasClass && hasTag)
24 ) {
25 analyzer.incrMetric("qualifiedSelectors");
26 analyzer.addOffender("qualifiedSelectors", selector);
27 }
28 });
29}
30
31rule.description = "Reports qualified selectors";
32module.exports = rule;