Skip to content
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
27 changes: 27 additions & 0 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::hash::{Hash, Hasher};

use oxc_ast::ast::BindingIdentifier;
use oxc_ast::AstKind;
use oxc_semantic::{AstNode, SymbolId};
use oxc_span::{GetSpan, Span};
Expand Down Expand Up @@ -239,6 +240,18 @@ pub fn outermost_paren_parent<'a, 'b>(
.find(|parent| !matches!(parent.kind(), AstKind::ParenthesizedExpression(_)))
}

pub fn nth_outermost_paren_parent<'a, 'b>(
node: &'b AstNode<'a>,
ctx: &'b LintContext<'a>,
n: usize,
) -> Option<&'b AstNode<'a>> {
ctx.nodes()
.iter_parents(node.id())
.skip(1)
.filter(|parent| !matches!(parent.kind(), AstKind::ParenthesizedExpression(_)))
.nth(n)
}

pub fn get_declaration_of_variable<'a, 'b>(
ident: &IdentifierReference,
ctx: &'b LintContext<'a>,
Expand Down Expand Up @@ -393,3 +406,17 @@ pub fn is_function_node(node: &AstNode) -> bool {
_ => false,
}
}

pub fn get_function_like_declaration<'b>(
node: &AstNode<'b>,
ctx: &LintContext<'b>,
) -> Option<&'b BindingIdentifier<'b>> {
let parent = outermost_paren_parent(node, ctx)?;

if let AstKind::VariableDeclarator(decl) = parent.kind() {
let ident = decl.id.get_binding_identifier()?;
Some(ident)
} else {
None
}
}
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ mod react_perf {

mod unicorn {
pub mod catch_error_name;
pub mod consistent_function_scoping;
pub mod empty_brace_spaces;
pub mod error_message;
pub mod escape_case;
Expand Down Expand Up @@ -643,6 +644,7 @@ oxc_macros::declare_all_lint_rules! {
jest::valid_expect,
jest::valid_title,
unicorn::catch_error_name,
unicorn::consistent_function_scoping,
unicorn::empty_brace_spaces,
unicorn::error_message,
unicorn::escape_case,
Expand Down
16 changes: 1 addition & 15 deletions crates/oxc_linter/src/rules/oxc/only_used_in_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{
ast_util::outermost_paren_parent, context::LintContext, fixer::Fix, rule::Rule, AstNode,
ast_util::get_function_like_declaration, context::LintContext, fixer::Fix, rule::Rule, AstNode,
};

fn only_used_in_recursion_diagnostic(span0: Span, x1: &str) -> OxcDiagnostic {
Expand Down Expand Up @@ -162,20 +162,6 @@ impl Rule for OnlyUsedInRecursion {
}
}

fn get_function_like_declaration<'b>(
node: &AstNode<'b>,
ctx: &LintContext<'b>,
) -> Option<&'b BindingIdentifier<'b>> {
let parent = outermost_paren_parent(node, ctx)?;

if let AstKind::VariableDeclarator(decl) = parent.kind() {
let ident = decl.id.get_binding_identifier()?;
Some(ident)
} else {
None
}
}

fn is_argument_only_used_in_recursion<'a>(
function_id: &'a BindingIdentifier,
arg: &'a BindingIdentifier,
Expand Down
Loading