Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(linter/react): find class node by symbols in get_parent_es6_component #1657

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/no_string_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
18 changes: 11 additions & 7 deletions crates/oxc_linter/src/utils/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_ast::{
},
AstKind,
};
use oxc_semantic::AstNode;
use oxc_semantic::{AstNode, SymbolFlags};

use crate::LintContext;

Expand Down Expand Up @@ -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
})
}
4 changes: 4 additions & 0 deletions crates/oxc_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl SymbolTable {
self.spans.iter_enumerated().map(|(symbol_id, _)| symbol_id)
}

pub fn iter_rev(&self) -> impl Iterator<Item = SymbolId> + '_ {
Boshen marked this conversation as resolved.
Show resolved Hide resolved
self.spans.iter_enumerated().rev().map(|(symbol_id, _)| symbol_id)
}

pub fn get_symbol_id_from_span(&self, span: &Span) -> Option<SymbolId> {
self.spans
.iter_enumerated()
Expand Down