From b469fd2ab83760ed8ca1f54cc9e5d1afae9a660f Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 5 Dec 2023 15:14:31 +0800 Subject: [PATCH] refactor(css/lints): Support stable rust --- crates/swc_css_lints/src/lib.rs | 1 - .../rules/font_family_no_duplicate_names.rs | 17 ++--- .../no_invalid_position_at_import_rule.rs | 73 +++++++++---------- .../src/rules/unit_no_unknown.rs | 19 +++-- 4 files changed, 51 insertions(+), 59 deletions(-) diff --git a/crates/swc_css_lints/src/lib.rs b/crates/swc_css_lints/src/lib.rs index 39064afb8b06..f2fb077adb17 100644 --- a/crates/swc_css_lints/src/lib.rs +++ b/crates/swc_css_lints/src/lib.rs @@ -1,5 +1,4 @@ #![deny(clippy::all)] -#![feature(box_patterns)] mod config; mod dataset; diff --git a/crates/swc_css_lints/src/rules/font_family_no_duplicate_names.rs b/crates/swc_css_lints/src/rules/font_family_no_duplicate_names.rs index 3ab1f736b5a8..ea20c6e9a906 100644 --- a/crates/swc_css_lints/src/rules/font_family_no_duplicate_names.rs +++ b/crates/swc_css_lints/src/rules/font_family_no_duplicate_names.rs @@ -47,7 +47,8 @@ impl FontFamilyNoDuplicateNames { Option::<(String, Span)>::None, ), |(mut fonts, last_identifier), item| match item { - ComponentValue::Ident(box Ident { value, span, .. }) => { + ComponentValue::Ident(ident) => { + let Ident { value, span, .. } = &**ident; if let Some((mut identifier, last_span)) = last_identifier { identifier.push(' '); identifier.push_str(value); @@ -56,18 +57,12 @@ impl FontFamilyNoDuplicateNames { (fonts, Some((value.to_string(), *span))) } } - ComponentValue::Str(box Str { - raw: Some(raw), - span, - .. - }) => { - fonts.push((FontNameKind::from(raw), *span)); + ComponentValue::Str(s) if s.raw.is_some() => { + let raw = s.raw.as_ref().unwrap(); + fonts.push((FontNameKind::from(raw), s.span)); (fonts, None) } - ComponentValue::Delimiter(box Delimiter { - value: DelimiterValue::Comma, - .. - }) => { + ComponentValue::Delimiter(delimiter) if delimiter.value.is_comma() => { if let Some((identifier, span)) = last_identifier { fonts.push((FontNameKind::from(identifier), span)); } diff --git a/crates/swc_css_lints/src/rules/no_invalid_position_at_import_rule.rs b/crates/swc_css_lints/src/rules/no_invalid_position_at_import_rule.rs index e51f11a43e3b..04770cd5a872 100644 --- a/crates/swc_css_lints/src/rules/no_invalid_position_at_import_rule.rs +++ b/crates/swc_css_lints/src/rules/no_invalid_position_at_import_rule.rs @@ -43,51 +43,50 @@ impl Visit for NoInvalidPositionAtImportRule { fn visit_stylesheet(&mut self, stylesheet: &Stylesheet) { stylesheet.rules.iter().fold(false, |seen, rule| { if seen - && matches!( - rule, - Rule::AtRule(box AtRule { - prelude: Some(box AtRulePrelude::ImportPrelude(_)), - .. - }) - ) + && rule + .as_at_rule() + .and_then(|at_rule| at_rule.prelude.as_ref()) + .map(|prelude| prelude.is_import_prelude()) + .unwrap_or_default() { self.ctx.report(rule, MESSAGE); } // TODO improve me https://www.w3.org/TR/css-cascade-5/#layer-empty - @import and @namespace rules must be consecutive - match rule { - Rule::AtRule( - box AtRule { - prelude: Some(box AtRulePrelude::CharsetPrelude(_)), - .. - } - | box AtRule { - prelude: Some(box AtRulePrelude::ImportPrelude(_)), - .. - }, - ) => seen, - Rule::AtRule(box AtRule { - prelude: Some(box AtRulePrelude::LayerPrelude(_)), - block, - .. - }) => match block { - Some(block) if block.value.is_empty() => seen, - None => seen, - _ => true, - }, - Rule::AtRule(box AtRule { name, .. }) => { - let name = match name { - AtRuleName::DashedIdent(dashed_ident) => &dashed_ident.value, - AtRuleName::Ident(ident) => &ident.value, - }; + let Rule::AtRule(at_rule) = rule else { + return true; + }; + + let AtRule { + name, + prelude, + block, + .. + } = &**at_rule; - if self.ignored.iter().any(|item| item.is_match(name)) { - seen - } else { - true + if let Some(prelude) = prelude { + match &**prelude { + AtRulePrelude::CharsetPrelude(_) | AtRulePrelude::ImportPrelude(_) => { + return seen } + AtRulePrelude::LayerPrelude(_) => match block { + Some(block) if block.value.is_empty() => return seen, + None => return seen, + _ => return true, + }, + _ => {} } - _ => true, + }; + + let name = match name { + AtRuleName::DashedIdent(dashed_ident) => &dashed_ident.value, + AtRuleName::Ident(ident) => &ident.value, + }; + + if self.ignored.iter().any(|item| item.is_match(name)) { + seen + } else { + true } }); diff --git a/crates/swc_css_lints/src/rules/unit_no_unknown.rs b/crates/swc_css_lints/src/rules/unit_no_unknown.rs index 1dc832834c32..edeb837febc0 100644 --- a/crates/swc_css_lints/src/rules/unit_no_unknown.rs +++ b/crates/swc_css_lints/src/rules/unit_no_unknown.rs @@ -50,16 +50,15 @@ impl Visit for UnitNoUnknown { } fn visit_component_value(&mut self, component_value: &ComponentValue) { - if let ComponentValue::PreservedToken( - token_and_span @ box TokenAndSpan { - token: Token::Dimension(box DimensionToken { unit, .. }), - .. - }, - ) = component_value - { - if self.ignored_units.iter().all(|item| !item.is_match(unit)) { - let message = format!("Unexpected unknown unit \"{}\".", unit); - self.ctx.report(token_and_span, message); + if let Some(token_and_span) = component_value.as_preserved_token() { + if let Some(unit) = match &token_and_span.token { + Token::Dimension(dimension_token) => Some(dimension_token.unit.as_ref()), + _ => None, + } { + if self.ignored_units.iter().all(|item| !item.is_match(unit)) { + let message = format!("Unexpected unknown unit \"{}\".", unit); + self.ctx.report(token_and_span, message); + } } }