From 0268c8256fc0a7bda1bea3d582ea3eea36f6fa07 Mon Sep 17 00:00:00 2001 From: neoki Date: Sat, 1 Jun 2024 14:38:45 +0900 Subject: [PATCH] refactor --- .../no_shorthand_property_overrides.rs | 71 +++++++++++-------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/crates/biome_css_analyze/src/lint/nursery/no_shorthand_property_overrides.rs b/crates/biome_css_analyze/src/lint/nursery/no_shorthand_property_overrides.rs index 26842d94c212..7d8c1ae85abc 100644 --- a/crates/biome_css_analyze/src/lint/nursery/no_shorthand_property_overrides.rs +++ b/crates/biome_css_analyze/src/lint/nursery/no_shorthand_property_overrides.rs @@ -39,9 +39,15 @@ declare_rule! { } } +#[derive(Default)] +struct PriorProperty { + original: String, + lowercase: String, +} + #[derive(Default)] struct NoDeclarationBlockShorthandPropertyOverridesVisitor { - prior_properties_in_block: Vec<(String, String)>, + prior_properties_in_block: Vec, } impl Visitor for NoDeclarationBlockShorthandPropertyOverridesVisitor { @@ -53,53 +59,58 @@ impl Visitor for NoDeclarationBlockShorthandPropertyOverridesVisitor { mut ctx: VisitorContext, ) { match event { - WalkEvent::Enter(node) => match node.kind() { - CssSyntaxKind::CSS_DECLARATION_OR_RULE_BLOCK => { - self.prior_properties_in_block.clear(); - } - CssSyntaxKind::CSS_GENERIC_PROPERTY => { - if let Some(property_name_node) = node - .clone() - .cast::() - .and_then(|property_node| property_node.name().ok()) - { - let property = property_name_node.text(); - let property_lower = property.to_lowercase(); - - let vendor_prefix = vender_prefix(&property_lower); - let unprefixed_property = property_lower[vendor_prefix.len()..].to_string(); - - let longhand_sub_properties = - get_longhand_sub_properties(&unprefixed_property); - let reset_to_initial_properties = - get_reset_to_initial_properties(&unprefixed_property); - - longhand_sub_properties + WalkEvent::Enter(node) => { + match node.kind() { + CssSyntaxKind::CSS_DECLARATION_OR_RULE_BLOCK => { + self.prior_properties_in_block.clear(); + } + CssSyntaxKind::CSS_GENERIC_PROPERTY => { + if let Some(property_node) = node + .clone() + .cast::() + .and_then(|property_node| property_node.name().ok()) + { + let property_original = property_node.text(); + let property_lowercase = property_original.to_lowercase(); + + let vendor_prefix = vender_prefix(&property_lowercase); + let unprefixed_property = + property_lowercase[vendor_prefix.len()..].to_string(); + + let longhand_sub_properties = + get_longhand_sub_properties(&unprefixed_property); + let reset_to_initial_properties = + get_reset_to_initial_properties(&unprefixed_property); + + longhand_sub_properties .iter() .chain(reset_to_initial_properties.iter()) .for_each(|override_property| { self.prior_properties_in_block .iter() .for_each(|prior_property| { - if prior_property.1 + if prior_property.lowercase == (vendor_prefix.clone() + override_property) { ctx.match_query( NoDeclarationBlockShorthandPropertyOverridesQuery { - property_node: property_name_node.clone(), - override_property: prior_property.0.to_string(), + property_node: property_node.clone(), + override_property: prior_property.original.clone(), }, ); } }); }); - self.prior_properties_in_block - .push((property, property_lower)); + self.prior_properties_in_block.push(PriorProperty { + original: property_original, + lowercase: property_lowercase, + }); + } } + _ => {} } - _ => {} - }, + } _ => {} } }