From a1a2721b1e398102e3ce43dfedfc8cdb0f2efa18 Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Thu, 10 Oct 2024 02:45:24 +0000 Subject: [PATCH] perf(linter): Replace `ToString::to_string` with `CompactStr` in remaining rules (#6407) --- crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs | 14 ++++++-------- .../src/rules/jsx_a11y/heading_has_content.rs | 9 +++------ .../rules/jsx_a11y/mouse_events_have_key_events.rs | 14 +++++++------- .../src/rules/jsx_a11y/no_redundant_roles.rs | 6 +----- crates/oxc_linter/src/rules/promise/spec_only.rs | 6 +++--- .../src/rules/react/jsx_boolean_value.rs | 8 +++----- .../typescript/explicit_function_return_type.rs | 9 +++------ 7 files changed, 26 insertions(+), 40 deletions(-) diff --git a/crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs b/crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs index 38a16fe2e..21c09194c 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs @@ -4,7 +4,7 @@ use oxc_ast::{ }; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::Span; +use oxc_span::{CompactStr, Span}; use crate::{ context::LintContext, @@ -71,10 +71,10 @@ pub struct AltText(Box); #[derive(Debug, Clone, PartialEq, Eq)] pub struct AltTextConfig { - img: Option>, - object: Option>, - area: Option>, - input_type_image: Option>, + img: Option>, + object: Option>, + area: Option>, + input_type_image: Option>, } impl std::ops::Deref for AltText { @@ -160,9 +160,7 @@ impl Rule for AltText { if let (Some(tags), Some(elements)) = (tags, config.get(field).and_then(|v| v.as_array())) { - tags.extend( - elements.iter().filter_map(|v| v.as_str().map(ToString::to_string)), - ); + tags.extend(elements.iter().filter_map(|v| v.as_str().map(CompactStr::from))); } } } diff --git a/crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs b/crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs index ea8432998..04d6a4adc 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs @@ -1,7 +1,7 @@ use oxc_ast::AstKind; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::Span; +use oxc_span::{CompactStr, Span}; use crate::{ context::LintContext, @@ -23,7 +23,7 @@ pub struct HeadingHasContent(Box); #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct HeadingHasContentConfig { - components: Option>, + components: Option>, } impl std::ops::Deref for HeadingHasContent { @@ -74,10 +74,7 @@ impl Rule for HeadingHasContent { .and_then(|v| v.get("components")) .and_then(serde_json::Value::as_array) .map(|v| { - v.iter() - .filter_map(serde_json::Value::as_str) - .map(ToString::to_string) - .collect() + v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect() }), })) } diff --git a/crates/oxc_linter/src/rules/jsx_a11y/mouse_events_have_key_events.rs b/crates/oxc_linter/src/rules/jsx_a11y/mouse_events_have_key_events.rs index 4aec7a36c..a1b10afc7 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/mouse_events_have_key_events.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/mouse_events_have_key_events.rs @@ -1,7 +1,7 @@ use oxc_ast::{ast::JSXAttributeValue, AstKind}; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::{GetSpan, Span}; +use oxc_span::{CompactStr, GetSpan, Span}; use crate::{ context::LintContext, @@ -28,15 +28,15 @@ pub struct MouseEventsHaveKeyEvents(Box); #[derive(Debug, Clone)] pub struct MouseEventsHaveKeyEventsConfig { - hover_in_handlers: Vec, - hover_out_handlers: Vec, + hover_in_handlers: Vec, + hover_out_handlers: Vec, } impl Default for MouseEventsHaveKeyEventsConfig { fn default() -> Self { Self { - hover_in_handlers: vec!["onMouseOver".to_string()], - hover_out_handlers: vec!["onMouseOut".to_string()], + hover_in_handlers: vec!["onMouseOver".into()], + hover_out_handlers: vec!["onMouseOut".into()], } } } @@ -78,7 +78,7 @@ impl Rule for MouseEventsHaveKeyEvents { config.hover_in_handlers = hover_in_handlers_config .iter() .filter_map(serde_json::Value::as_str) - .map(ToString::to_string) + .map(CompactStr::from) .collect(); } @@ -90,7 +90,7 @@ impl Rule for MouseEventsHaveKeyEvents { config.hover_out_handlers = hover_out_handlers_config .iter() .filter_map(serde_json::Value::as_str) - .map(ToString::to_string) + .map(CompactStr::from) .collect(); } diff --git a/crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs b/crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs index bf1183646..f4d53b514 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs @@ -67,11 +67,7 @@ impl Rule for NoRedundantRoles { if let Some(JSXAttributeItem::Attribute(attr)) = has_jsx_prop_ignore_case(jsx_el, "role") { if let Some(JSXAttributeValue::StringLiteral(role_values)) = &attr.value { - let roles: Vec = role_values - .value - .split_whitespace() - .map(std::string::ToString::to_string) - .collect(); + let roles = role_values.value.split_whitespace().collect::>(); for role in &roles { let exceptions = DEFAULT_ROLE_EXCEPTIONS.get(&component); if exceptions.map_or(false, |set| set.contains(role)) { diff --git a/crates/oxc_linter/src/rules/promise/spec_only.rs b/crates/oxc_linter/src/rules/promise/spec_only.rs index e9ce43322..081f92256 100644 --- a/crates/oxc_linter/src/rules/promise/spec_only.rs +++ b/crates/oxc_linter/src/rules/promise/spec_only.rs @@ -1,7 +1,7 @@ use oxc_ast::AstKind; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::{GetSpan, Span}; +use oxc_span::{CompactStr, GetSpan, Span}; use rustc_hash::FxHashSet; use crate::{context::LintContext, rule::Rule, utils::PROMISE_STATIC_METHODS, AstNode}; @@ -16,7 +16,7 @@ pub struct SpecOnly(Box); #[derive(Debug, Default, Clone)] pub struct SpecOnlyConfig { - allowed_methods: Option>, + allowed_methods: Option>, } impl std::ops::Deref for SpecOnly { @@ -58,7 +58,7 @@ impl Rule for SpecOnly { .and_then(|v| v.get("allowedMethods")) .and_then(serde_json::Value::as_array) .map(|v| { - v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect() + v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect() }); Self(Box::new(SpecOnlyConfig { allowed_methods })) diff --git a/crates/oxc_linter/src/rules/react/jsx_boolean_value.rs b/crates/oxc_linter/src/rules/react/jsx_boolean_value.rs index e0c970ba5..b762e7710 100644 --- a/crates/oxc_linter/src/rules/react/jsx_boolean_value.rs +++ b/crates/oxc_linter/src/rules/react/jsx_boolean_value.rs @@ -4,7 +4,7 @@ use oxc_ast::{ }; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::Span; +use oxc_span::{CompactStr, Span}; use rustc_hash::FxHashSet; use crate::{ @@ -42,7 +42,7 @@ pub enum EnforceBooleanAttribute { #[derive(Debug, Default, Clone)] pub struct JsxBooleanValueConfig { pub enforce_boolean_attribute: EnforceBooleanAttribute, - pub exceptions: FxHashSet, + pub exceptions: FxHashSet, pub assume_undefined_is_false: bool, } @@ -94,9 +94,7 @@ impl Rule for JsxBooleanValue { let exceptions = config .and_then(|c| c.get(attribute_name)) .and_then(serde_json::Value::as_array) - .map(|v| { - v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect() - }) + .map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect()) .unwrap_or_default(); Self(Box::new(JsxBooleanValueConfig { diff --git a/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs b/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs index ac0675428..3281d198f 100644 --- a/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs +++ b/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs @@ -7,7 +7,7 @@ use oxc_ast::{ }; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::Span; +use oxc_span::{CompactStr, Span}; use oxc_syntax::operator::UnaryOperator; use rustc_hash::FxHashSet; @@ -31,7 +31,7 @@ pub struct ExplicitFunctionReturnTypeConfig { allow_direct_const_assertion_in_arrow_functions: bool, allow_concise_arrow_function_expressions_starting_with_void: bool, allow_functions_without_type_parameters: bool, - allowed_names: FxHashSet, + allowed_names: FxHashSet, allow_higher_order_functions: bool, allow_iifes: bool, } @@ -143,10 +143,7 @@ impl Rule for ExplicitFunctionReturnType { .and_then(|x| x.get("allowedNames")) .and_then(serde_json::Value::as_array) .map(|v| { - v.iter() - .filter_map(serde_json::Value::as_str) - .map(ToString::to_string) - .collect() + v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect() }) .unwrap_or_default(), allow_higher_order_functions: options