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

This way we can get the class node faster. But I don't know if this is a
good way. In `eslint-plugin-react`, they get class node by scope. But
oxc cannot do the same way
This commit is contained in:
Dunqing 2023-12-13 13:48:53 +08:00 committed by GitHub
parent 864176a051
commit 00806384ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 8 deletions

View file

@ -121,7 +121,7 @@ impl Rule for NoStringRefs {
if matches!(member_expr.object(), Expression::ThisExpression(_)) if matches!(member_expr.object(), Expression::ThisExpression(_))
&& member_expr.static_property_name() == Some("refs") && member_expr.static_property_name() == Some("refs")
&& (get_parent_es5_component(node, ctx).is_some() && (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())); ctx.diagnostic(NoStringRefsDiagnostic::ThisRefsDeprecated(member_expr.span()));
} }

View file

@ -6,7 +6,7 @@ use oxc_ast::{
}, },
AstKind, AstKind,
}; };
use oxc_semantic::AstNode; use oxc_semantic::{AstNode, SymbolFlags};
use crate::LintContext; use crate::LintContext;
@ -151,11 +151,15 @@ pub fn get_parent_es5_component<'a, 'b>(
}) })
} }
pub fn get_parent_es6_component<'a, 'b>( pub fn get_parent_es6_component<'a, 'b>(ctx: &'b LintContext<'a>) -> Option<&'b AstNode<'a>> {
node: &'b AstNode<'a>, ctx.semantic().symbols().iter_rev().find_map(|symbol| {
ctx: &'b LintContext<'a>, let flags = ctx.semantic().symbols().get_flag(symbol);
) -> Option<&'b AstNode<'a>> { if flags.contains(SymbolFlags::Class) {
ctx.nodes().ancestors(node.id()).skip(1).find_map(|node_id| { let node = ctx.semantic().symbol_declaration(symbol);
is_es6_component(ctx.nodes().get_node(node_id)).then(|| ctx.nodes().get_node(node_id)) if is_es6_component(node) {
return Some(node);
}
}
None
}) })
} }

View file

@ -39,6 +39,10 @@ impl SymbolTable {
self.spans.iter_enumerated().map(|(symbol_id, _)| symbol_id) self.spans.iter_enumerated().map(|(symbol_id, _)| symbol_id)
} }
pub fn iter_rev(&self) -> impl Iterator<Item = SymbolId> + '_ {
self.spans.iter_enumerated().rev().map(|(symbol_id, _)| symbol_id)
}
pub fn get_symbol_id_from_span(&self, span: &Span) -> Option<SymbolId> { pub fn get_symbol_id_from_span(&self, span: &Span) -> Option<SymbolId> {
self.spans self.spans
.iter_enumerated() .iter_enumerated()