Skip to content

Commit

Permalink
perf(linter): Replace ToString::to_string with CompactStr in rema…
Browse files Browse the repository at this point in the history
…ining rules
  • Loading branch information
camchenry committed Oct 9, 2024
1 parent b11d9e3 commit 213dde4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 40 deletions.
14 changes: 6 additions & 8 deletions crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -71,10 +71,10 @@ pub struct AltText(Box<AltTextConfig>);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AltTextConfig {
img: Option<Vec<String>>,
object: Option<Vec<String>>,
area: Option<Vec<String>>,
input_type_image: Option<Vec<String>>,
img: Option<Vec<CompactStr>>,
object: Option<Vec<CompactStr>>,
area: Option<Vec<CompactStr>>,
input_type_image: Option<Vec<CompactStr>>,
}

impl std::ops::Deref for AltText {
Expand Down Expand Up @@ -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)));
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -23,7 +23,7 @@ pub struct HeadingHasContent(Box<HeadingHasContentConfig>);

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct HeadingHasContentConfig {
components: Option<Vec<String>>,
components: Option<Vec<CompactStr>>,
}

impl std::ops::Deref for HeadingHasContent {
Expand Down Expand Up @@ -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()
}),
}))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,15 +28,15 @@ pub struct MouseEventsHaveKeyEvents(Box<MouseEventsHaveKeyEventsConfig>);

#[derive(Debug, Clone)]
pub struct MouseEventsHaveKeyEventsConfig {
hover_in_handlers: Vec<String>,
hover_out_handlers: Vec<String>,
hover_in_handlers: Vec<CompactStr>,
hover_out_handlers: Vec<CompactStr>,
}

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()],
}
}
}
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = role_values
.value
.split_whitespace()
.map(std::string::ToString::to_string)
.collect();
let roles = role_values.value.split_whitespace().collect::<Vec<_>>();
for role in &roles {
let exceptions = DEFAULT_ROLE_EXCEPTIONS.get(&component);
if exceptions.map_or(false, |set| set.contains(role)) {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/promise/spec_only.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,7 +16,7 @@ pub struct SpecOnly(Box<SpecOnlyConfig>);

#[derive(Debug, Default, Clone)]
pub struct SpecOnlyConfig {
allowed_methods: Option<FxHashSet<String>>,
allowed_methods: Option<FxHashSet<CompactStr>>,
}

impl std::ops::Deref for SpecOnly {
Expand Down Expand Up @@ -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 }))
Expand Down
8 changes: 3 additions & 5 deletions crates/oxc_linter/src/rules/react/jsx_boolean_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -42,7 +42,7 @@ pub enum EnforceBooleanAttribute {
#[derive(Debug, Default, Clone)]
pub struct JsxBooleanValueConfig {
pub enforce_boolean_attribute: EnforceBooleanAttribute,
pub exceptions: FxHashSet<String>,
pub exceptions: FxHashSet<CompactStr>,
pub assume_undefined_is_false: bool,
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String>,
allowed_names: FxHashSet<CompactStr>,
allow_higher_order_functions: bool,
allow_iifes: bool,
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 213dde4

Please sign in to comment.