|
| 1 | +use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource}; |
| 2 | +use biome_console::markup; |
| 3 | +use biome_css_syntax::{ |
| 4 | + AnyCssPseudoClassNth, CssPseudoClassFunctionSelectorList, CssPseudoClassNthSelector, |
| 5 | +}; |
| 6 | +use biome_rowan::{AstNode, SyntaxNodeCast}; |
| 7 | + |
| 8 | +declare_rule! { |
| 9 | + /// Disallow unmatchable An+B selectors. |
| 10 | + /// |
| 11 | + /// Selectors that always evaluate to 0 will not match any elements. |
| 12 | + /// For more details about the An+B syntax, see: |
| 13 | + /// https://www.w3.org/TR/css-syntax-3/#anb-microsyntax |
| 14 | + /// |
| 15 | + /// ## Examples |
| 16 | + /// |
| 17 | + /// ### Invalid |
| 18 | + /// |
| 19 | + /// ```css,expect_diagnostic |
| 20 | + /// a:nth-child(0) {} |
| 21 | + /// ``` |
| 22 | + /// |
| 23 | + /// ```css,expect_diagnostic |
| 24 | + /// a:nth-last-child(0n) {} |
| 25 | + /// ``` |
| 26 | + /// |
| 27 | + /// ```css,expect_diagnostic |
| 28 | + /// a:nth-of-type(0n+0) {} |
| 29 | + /// ``` |
| 30 | + /// |
| 31 | + /// ```css,expect_diagnostic |
| 32 | + /// a:nth-last-of-type(0 of a) {} |
| 33 | + /// ``` |
| 34 | + /// |
| 35 | + /// ### Valid |
| 36 | + /// |
| 37 | + /// ```css |
| 38 | + /// a:nth-child(1) {} |
| 39 | + /// ``` |
| 40 | + /// |
| 41 | + /// ```css |
| 42 | + /// a:nth-last-child(1n) {} |
| 43 | + /// ``` |
| 44 | + /// |
| 45 | + /// ```css |
| 46 | + /// a:nth-of-type(1n+0) {} |
| 47 | + /// ``` |
| 48 | + /// |
| 49 | + /// ```css |
| 50 | + /// a:nth-last-of-type(1 of a) {} |
| 51 | + /// ``` |
| 52 | + /// |
| 53 | + pub NoUnmatchableAnbSelector { |
| 54 | + version: "next", |
| 55 | + name: "noUnmatchableAnbSelector", |
| 56 | + recommended: true, |
| 57 | + sources: &[RuleSource::Stylelint("selector-anb-no-unmatchable")], |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl Rule for NoUnmatchableAnbSelector { |
| 62 | + type Query = Ast<CssPseudoClassNthSelector>; |
| 63 | + type State = CssPseudoClassNthSelector; |
| 64 | + type Signals = Option<Self::State>; |
| 65 | + type Options = (); |
| 66 | + |
| 67 | + fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { |
| 68 | + let node = ctx.query(); |
| 69 | + let nth = node.nth().ok()?; |
| 70 | + if is_unmatchable(&nth) && !is_within_not_pseudo_class(&nth) { |
| 71 | + return Some(node.clone()); |
| 72 | + } |
| 73 | + None |
| 74 | + } |
| 75 | + |
| 76 | + fn diagnostic(_: &RuleContext<Self>, node: &Self::State) -> Option<RuleDiagnostic> { |
| 77 | + let span = node.range(); |
| 78 | + Some( |
| 79 | + RuleDiagnostic::new( |
| 80 | + rule_category!(), |
| 81 | + span, |
| 82 | + markup! { |
| 83 | + "This selector will never match any elements." |
| 84 | + }, |
| 85 | + ) |
| 86 | + .note(markup! { |
| 87 | + "Avoid using An+B selectors that always evaluate to 0." |
| 88 | + }).note(markup! { |
| 89 | + "For more details, see "<Hyperlink href="https://www.w3.org/TR/css-syntax-3/#anb-microsyntax">"the official spec for An+B selectors"</Hyperlink>"." |
| 90 | + }) |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn is_unmatchable(nth: &AnyCssPseudoClassNth) -> bool { |
| 96 | + match nth { |
| 97 | + AnyCssPseudoClassNth::CssPseudoClassNthIdentifier(_) => false, |
| 98 | + AnyCssPseudoClassNth::CssPseudoClassNth(nth) => { |
| 99 | + let coefficient = nth.value(); |
| 100 | + let constant = nth.offset(); |
| 101 | + match (coefficient, constant) { |
| 102 | + (Some(a), Some(b)) => a.text() == "0" && b.text() == "0", |
| 103 | + (Some(a), None) => a.text() == "0", |
| 104 | + _ => false, |
| 105 | + } |
| 106 | + } |
| 107 | + AnyCssPseudoClassNth::CssPseudoClassNthNumber(nth) => nth.text() == "0", |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Check if the nth selector is effective within a `not` pseudo class |
| 112 | +// Example: a:not(:nth-child(0)) returns true |
| 113 | +// a:not(:not(:nth-child(0))) returns false |
| 114 | +fn is_within_not_pseudo_class(node: &AnyCssPseudoClassNth) -> bool { |
| 115 | + let number_of_not = node |
| 116 | + .syntax() |
| 117 | + .ancestors() |
| 118 | + .filter_map(|n| n.cast::<CssPseudoClassFunctionSelectorList>()) |
| 119 | + .filter_map(|n| n.name().ok()) |
| 120 | + .filter(|n| n.text() == "not") |
| 121 | + .count(); |
| 122 | + number_of_not % 2 == 1 |
| 123 | +} |
0 commit comments