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-exhaustive-deps): use stack of AstTypes instead of AstKinds #8522

Merged
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
34 changes: 15 additions & 19 deletions crates/oxc_linter/src/rules/react/exhaustive_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oxc_ast::{
},
match_expression,
visit::walk::walk_function_body,
AstKind, Visit,
AstKind, AstType, Visit,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
Expand Down Expand Up @@ -973,7 +973,7 @@ fn func_call_without_react_namespace<'a>(

struct ExhaustiveDepsVisitor<'a, 'b> {
semantic: &'b Semantic<'a>,
stack: Vec<AstKind<'a>>,
stack: Vec<AstType>,
skip_reporting_dependency: bool,
set_state_call: bool,
found_dependencies: FxHashSet<Dependency<'a>>,
Expand All @@ -999,7 +999,7 @@ impl<'a, 'b> ExhaustiveDepsVisitor<'a, 'b> {

impl<'a> Visit<'a> for ExhaustiveDepsVisitor<'a, '_> {
fn enter_node(&mut self, kind: AstKind<'a>) {
self.stack.push(kind);
self.stack.push(kind.ty());
}

fn leave_node(&mut self, _kind: AstKind<'a>) {
Expand Down Expand Up @@ -1041,10 +1041,8 @@ impl<'a> Visit<'a> for ExhaustiveDepsVisitor<'a, '_> {
return;
}

let is_parent_call_expr = self
.stack
.get(self.stack.len() - 2)
.is_some_and(|kind| matches!(kind, AstKind::CallExpression(_)));
let is_parent_call_expr =
self.stack.get(self.stack.len() - 2).is_some_and(|&ty| ty == AstType::CallExpression);

match analyze_property_chain(&it.object, self.semantic) {
Ok(source) => {
Expand Down Expand Up @@ -1110,30 +1108,28 @@ impl<'a> Visit<'a> for ExhaustiveDepsVisitor<'a, '_> {
};

if is_set_state_call
&& self.stack.iter().all(|kind| {
!matches!(kind, AstKind::Function(_) | AstKind::ArrowFunctionExpression(_))
})
&& self
.stack
.iter()
.all(|&ty| !matches!(ty, AstType::Function | AstType::ArrowFunctionExpression))
{
self.set_state_call = true;
}
}
}
}

fn is_inside_effect_cleanup(stack: &[AstKind<'_>]) -> bool {
fn is_inside_effect_cleanup(stack: &[AstType]) -> bool {
let mut iter = stack.iter().rev();
let mut is_in_returned_function = false;

while let Some(cur) = iter.next() {
match cur {
AstKind::Function(_) | AstKind::ArrowFunctionExpression(_) => {
if let Some(parent) = iter.next() {
if matches!(parent, AstKind::ReturnStatement(_)) {
is_in_returned_function = true;
}
while let Some(&cur) = iter.next() {
if matches!(cur, AstType::Function | AstType::ArrowFunctionExpression) {
if let Some(&parent) = iter.next() {
if parent == AstType::ReturnStatement {
is_in_returned_function = true;
}
}
_ => {}
}
}

Expand Down
Loading