Skip to content

Commit

Permalink
refactor(linter): eslint/no_redeclare rule use run_on_symbol not …
Browse files Browse the repository at this point in the history
…`run_once` (#5201)

`eslint/no_redeclare` rule iterates over all symbols. Use `run_on_symbols` for this, instead of `run_once`.
  • Loading branch information
overlookmotel committed Aug 25, 2024
1 parent 8ff6f2c commit 2a91ef1
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions crates/oxc_linter/src/rules/eslint/no_redeclare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oxc_ast::{
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::symbol::SymbolId;

use crate::{context::LintContext, rule::Rule};

Expand Down Expand Up @@ -56,33 +57,31 @@ impl Rule for NoRedeclare {
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();

for symbol_id in ctx.symbols().iter() {
let decl = symbol_table.get_declaration(symbol_id);
let symbol_name = symbol_table.get_name(symbol_id);
match ctx.nodes().kind(decl) {
AstKind::VariableDeclarator(var) => {
if let BindingPatternKind::BindingIdentifier(ident) = &var.id.kind {
if symbol_name == ident.name.as_str() {
for span in ctx.symbols().get_redeclarations(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
let decl_node_id = symbol_table.get_declaration(symbol_id);
match ctx.nodes().kind(decl_node_id) {
AstKind::VariableDeclarator(var) => {
if let BindingPatternKind::BindingIdentifier(ident) = &var.id.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);
}
}
}
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);
}
}
}
_ => {}
}
_ => {}
}
}
}
Expand Down

0 comments on commit 2a91ef1

Please sign in to comment.