refactor(linter): eslint/no_redeclare rule use run_on_symbol not run_once (#5201)

`eslint/no_redeclare` rule iterates over all symbols. Use `run_on_symbols` for this, instead of `run_once`.
This commit is contained in:
overlookmotel 2024-08-25 21:40:58 +00:00
parent 8ff6f2cb86
commit 2a91ef10ea

View file

@ -5,6 +5,7 @@ use oxc_ast::{
use oxc_diagnostics::OxcDiagnostic; use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint; use oxc_macros::declare_oxc_lint;
use oxc_span::Span; use oxc_span::Span;
use oxc_syntax::symbol::SymbolId;
use crate::{context::LintContext, rule::Rule}; use crate::{context::LintContext, rule::Rule};
@ -56,33 +57,31 @@ impl Rule for NoRedeclare {
Self { built_in_globals } Self { built_in_globals }
} }
fn run_once(&self, ctx: &LintContext) { fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext) {
let symbol_table = ctx.semantic().symbols(); let symbol_table = ctx.semantic().symbols();
let decl_node_id = symbol_table.get_declaration(symbol_id);
for symbol_id in ctx.symbols().iter() { match ctx.nodes().kind(decl_node_id) {
let decl = symbol_table.get_declaration(symbol_id); AstKind::VariableDeclarator(var) => {
let symbol_name = symbol_table.get_name(symbol_id); if let BindingPatternKind::BindingIdentifier(ident) = &var.id.kind {
match ctx.nodes().kind(decl) { let symbol_name = symbol_table.get_name(symbol_id);
AstKind::VariableDeclarator(var) => { if symbol_name == ident.name.as_str() {
if let BindingPatternKind::BindingIdentifier(ident) = &var.id.kind { for span in ctx.symbols().get_redeclarations(symbol_id) {
if symbol_name == ident.name.as_str() { self.report_diagnostic(ctx, *span, ident);
for span in ctx.symbols().get_redeclarations(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
} }
} }
} }
AstKind::FormalParameter(param) => {
if let BindingPatternKind::BindingIdentifier(ident) = &param.pattern.kind {
if symbol_name == ident.name.as_str() {
for span in ctx.symbols().get_redeclarations(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
}
}
}
_ => {}
} }
AstKind::FormalParameter(param) => {
if let BindingPatternKind::BindingIdentifier(ident) = &param.pattern.kind {
let symbol_name = symbol_table.get_name(symbol_id);
if symbol_name == ident.name.as_str() {
for span in ctx.symbols().get_redeclarations(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
}
}
}
_ => {}
} }
} }
} }