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 62ed6a158e87d..e34211c2c61db 100644 --- a/crates/oxc_linter/src/rules/react/no_string_refs.rs +++ b/crates/oxc_linter/src/rules/react/no_string_refs.rs @@ -121,7 +121,7 @@ impl Rule for NoStringRefs { 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_es6_component(ctx).is_some()) { ctx.diagnostic(NoStringRefsDiagnostic::ThisRefsDeprecated(member_expr.span())); } diff --git a/crates/oxc_linter/src/utils/react.rs b/crates/oxc_linter/src/utils/react.rs index e64fb894dd845..3c9b8c550e254 100644 --- a/crates/oxc_linter/src/utils/react.rs +++ b/crates/oxc_linter/src/utils/react.rs @@ -6,7 +6,7 @@ use oxc_ast::{ }, AstKind, }; -use oxc_semantic::AstNode; +use oxc_semantic::{AstNode, SymbolFlags}; use crate::LintContext; @@ -151,11 +151,15 @@ pub fn get_parent_es5_component<'a, 'b>( }) } -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()).skip(1).find_map(|node_id| { - is_es6_component(ctx.nodes().get_node(node_id)).then(|| ctx.nodes().get_node(node_id)) +pub fn get_parent_es6_component<'a, 'b>(ctx: &'b LintContext<'a>) -> Option<&'b AstNode<'a>> { + ctx.semantic().symbols().iter_rev().find_map(|symbol| { + let flags = ctx.semantic().symbols().get_flag(symbol); + if flags.contains(SymbolFlags::Class) { + let node = ctx.semantic().symbol_declaration(symbol); + if is_es6_component(node) { + return Some(node); + } + } + None }) } diff --git a/crates/oxc_semantic/src/symbol.rs b/crates/oxc_semantic/src/symbol.rs index 1ad78c98a4d37..76ae6c6501d30 100644 --- a/crates/oxc_semantic/src/symbol.rs +++ b/crates/oxc_semantic/src/symbol.rs @@ -39,6 +39,10 @@ impl SymbolTable { self.spans.iter_enumerated().map(|(symbol_id, _)| symbol_id) } + pub fn iter_rev(&self) -> impl Iterator + '_ { + self.spans.iter_enumerated().rev().map(|(symbol_id, _)| symbol_id) + } + pub fn get_symbol_id_from_span(&self, span: &Span) -> Option { self.spans .iter_enumerated()