Skip to content

Commit

Permalink
perf(linter/react): find class node by symbols in get_parent_es6_comp…
Browse files Browse the repository at this point in the history
…onent (#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
  • Loading branch information
Dunqing authored Dec 13, 2023
1 parent 864176a commit 0080638
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
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> + '_ {
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

0 comments on commit 0080638

Please sign in to comment.