From 90a7d206971e05b9f7b246a885b00284345fc666 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sun, 8 Sep 2024 19:26:22 +0100 Subject: [PATCH] perf(linter): `react/no_set_state` + `react/no_string_refs` rules reduce iteration over ancestors --- .../src/rules/react/no_set_state.rs | 10 ++------- .../src/rules/react/no_string_refs.rs | 10 ++------- crates/oxc_linter/src/utils/react.rs | 21 +++++++------------ 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/crates/oxc_linter/src/rules/react/no_set_state.rs b/crates/oxc_linter/src/rules/react/no_set_state.rs index 18e84b83278a36..da2acc7a221ab7 100644 --- a/crates/oxc_linter/src/rules/react/no_set_state.rs +++ b/crates/oxc_linter/src/rules/react/no_set_state.rs @@ -3,12 +3,7 @@ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_span::{GetSpan, Span}; -use crate::{ - context::LintContext, - rule::Rule, - utils::{get_parent_es5_component, get_parent_es6_component}, - AstNode, -}; +use crate::{context::LintContext, rule::Rule, utils::get_parent_component, AstNode}; fn no_set_state_diagnostic(span: Span) -> OxcDiagnostic { OxcDiagnostic::warn("Do not use setState").with_label(span) @@ -63,8 +58,7 @@ impl Rule for NoSetState { if !matches!(member_expr.object(), Expression::ThisExpression(_)) || !member_expr.static_property_name().is_some_and(|str| str == "setState") - || !(get_parent_es5_component(node, ctx).is_some() - || get_parent_es6_component(node, ctx).is_some()) + || get_parent_component(node, ctx).is_none() { return; } diff --git a/crates/oxc_linter/src/rules/react/no_string_refs.rs b/crates/oxc_linter/src/rules/react/no_string_refs.rs index 60791f688f51ea..33e3237832c342 100644 --- a/crates/oxc_linter/src/rules/react/no_string_refs.rs +++ b/crates/oxc_linter/src/rules/react/no_string_refs.rs @@ -9,12 +9,7 @@ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_span::{GetSpan, Span}; -use crate::{ - context::LintContext, - rule::Rule, - utils::{get_parent_es5_component, get_parent_es6_component}, - AstNode, -}; +use crate::{context::LintContext, rule::Rule, utils::get_parent_component, AstNode}; fn this_refs_deprecated(span: Span) -> OxcDiagnostic { OxcDiagnostic::warn("Using this.refs is deprecated.") @@ -121,8 +116,7 @@ impl Rule for NoStringRefs { AstKind::MemberExpression(member_expr) => { if matches!(member_expr.object(), Expression::ThisExpression(_)) && member_expr.static_property_name() == Some("refs") - && (get_parent_es5_component(node, ctx).is_some() - || get_parent_es6_component(node, ctx).is_some()) + && get_parent_component(node, ctx).is_some() { ctx.diagnostic(this_refs_deprecated(member_expr.span())); } diff --git a/crates/oxc_linter/src/utils/react.rs b/crates/oxc_linter/src/utils/react.rs index a560ea60c3d512..dbb47a8fbe5fd8 100644 --- a/crates/oxc_linter/src/utils/react.rs +++ b/crates/oxc_linter/src/utils/react.rs @@ -190,22 +190,17 @@ pub fn is_es6_component(node: &AstNode) -> bool { false } -pub fn get_parent_es5_component<'a, 'b>( +pub fn get_parent_component<'a, 'b>( node: &'b AstNode<'a>, ctx: &'b LintContext<'a>, ) -> Option<&'b AstNode<'a>> { - ctx.nodes().ancestors(node.id()).skip(1).find_map(|node_id| { - is_es5_component(ctx.nodes().get_node(node_id)).then(|| ctx.nodes().get_node(node_id)) - }) -} - -pub fn get_parent_es6_component<'a, 'b>( - node: &'b AstNode<'a>, - ctx: &'b LintContext<'a>, -) -> Option<&'b AstNode<'a>> { - ctx.nodes().ancestors(node.id()).find_map(|node_id| { - is_es6_component(ctx.nodes().get_node(node_id)).then(|| ctx.nodes().get_node(node_id)) - }) + for node_id in ctx.nodes().ancestors(node.id()) { + let node = ctx.nodes().get_node(node_id); + if is_es5_component(node) || is_es6_component(node) { + return Some(node); + } + } + None } /// Resolve element type(name) using jsx-a11y settings