diff --git a/crates/swc/tests/errors/lints/duplicate-bindings/catch-clause/1/input.js b/crates/swc/tests/errors/lints/duplicate-bindings/catch-clause/1/input.js new file mode 100644 index 00000000000..44251e5133e --- /dev/null +++ b/crates/swc/tests/errors/lints/duplicate-bindings/catch-clause/1/input.js @@ -0,0 +1,4 @@ +try { +} catch (foo) { + let foo; +} \ No newline at end of file diff --git a/crates/swc/tests/errors/lints/duplicate-bindings/catch-clause/1/output.swc-stderr b/crates/swc/tests/errors/lints/duplicate-bindings/catch-clause/1/output.swc-stderr new file mode 100644 index 00000000000..89efdf56db6 --- /dev/null +++ b/crates/swc/tests/errors/lints/duplicate-bindings/catch-clause/1/output.swc-stderr @@ -0,0 +1,8 @@ +error: the name `foo` is defined multiple times + + | +2 | } catch (foo) { + | --- previous definition of `foo` here +3 | let foo; + | ^^^ `foo` redefined here + diff --git a/crates/swc_ecma_lints/src/rules/duplicate_bindings.rs b/crates/swc_ecma_lints/src/rules/duplicate_bindings.rs index 265e175cbc0..c7cbc7dbd41 100644 --- a/crates/swc_ecma_lints/src/rules/duplicate_bindings.rs +++ b/crates/swc_ecma_lints/src/rules/duplicate_bindings.rs @@ -84,23 +84,6 @@ impl DuplicateBindings { impl Visit for DuplicateBindings { noop_visit_type!(); - fn visit_module(&mut self, m: &Module) { - m.visit_with(&mut TypeCollector { - type_bindings: &mut self.type_bindings, - }); - - self.is_module = true; - m.visit_children_with(self); - } - - fn visit_script(&mut self, s: &Script) { - s.visit_with(&mut TypeCollector { - type_bindings: &mut self.type_bindings, - }); - - s.visit_children_with(self); - } - fn visit_assign_pat_prop(&mut self, p: &AssignPatProp) { p.visit_children_with(self); @@ -109,6 +92,19 @@ impl Visit for DuplicateBindings { } } + fn visit_catch_clause(&mut self, c: &CatchClause) { + let old_var_decl_kind = self.var_decl_kind.take(); + let old_is_pat_decl = self.is_pat_decl; + + self.var_decl_kind = Some(VarDeclKind::Var); + self.is_pat_decl = true; + + c.visit_children_with(self); + + self.is_pat_decl = old_is_pat_decl; + self.var_decl_kind = old_var_decl_kind; + } + fn visit_class_decl(&mut self, d: &ClassDecl) { self.add(&d.ident, true); @@ -160,6 +156,15 @@ impl Visit for DuplicateBindings { } } + fn visit_module(&mut self, m: &Module) { + m.visit_with(&mut TypeCollector { + type_bindings: &mut self.type_bindings, + }); + + self.is_module = true; + m.visit_children_with(self); + } + fn visit_pat(&mut self, p: &Pat) { p.visit_children_with(self); @@ -170,6 +175,14 @@ impl Visit for DuplicateBindings { } } + fn visit_script(&mut self, s: &Script) { + s.visit_with(&mut TypeCollector { + type_bindings: &mut self.type_bindings, + }); + + s.visit_children_with(self); + } + fn visit_var_decl(&mut self, d: &VarDecl) { let old_var_decl_kind = self.var_decl_kind.take(); let old_is_pat_decl = self.is_pat_decl; diff --git a/crates/swc_ecma_lints/tests/fixture.rs b/crates/swc_ecma_lints/tests/fixture.rs index d76f3e05782..f0c1709e902 100644 --- a/crates/swc_ecma_lints/tests/fixture.rs +++ b/crates/swc_ecma_lints/tests/fixture.rs @@ -4,6 +4,7 @@ use swc_common::{input::SourceFileInput, Mark, SyntaxContext}; use swc_ecma_ast::{EsVersion, Program}; use swc_ecma_lints::{ config::LintConfig, + rule::Rule, rules::{all, LintParams}, }; use swc_ecma_parser::{lexer::Lexer, Parser, Syntax}; @@ -53,7 +54,7 @@ fn pass(input: PathBuf) { let config = LintConfig::default(); - let rules = all(LintParams { + let mut rules = all(LintParams { program: &program, lint_config: &config, top_level_ctxt, @@ -61,11 +62,12 @@ fn pass(input: PathBuf) { source_map: cm, }); - HANDLER.set(handler, || { - if let Program::Module(m) = &program { - for mut rule in rules { - rule.lint_module(m); - } + HANDLER.set(handler, || match &program { + Program::Module(m) => { + rules.lint_module(m); + } + Program::Script(s) => { + rules.lint_script(s); } });