diff --git a/crates/swc/tests/errors/lints/duplicate-bindings/parameter/input.js b/crates/swc/tests/errors/lints/duplicate-bindings/parameter/input.js new file mode 100644 index 00000000000..77ce2684976 --- /dev/null +++ b/crates/swc/tests/errors/lints/duplicate-bindings/parameter/input.js @@ -0,0 +1,3 @@ +function foo(a) { + let a; +} diff --git a/crates/swc/tests/errors/lints/duplicate-bindings/parameter/output.swc-stderr b/crates/swc/tests/errors/lints/duplicate-bindings/parameter/output.swc-stderr new file mode 100644 index 00000000000..46fdd150b50 --- /dev/null +++ b/crates/swc/tests/errors/lints/duplicate-bindings/parameter/output.swc-stderr @@ -0,0 +1,10 @@ + + x the name `a` is defined multiple times + ,-[1:1] + 1 | function foo(a) { + : | + : `-- previous definition of `a` here + 2 | let a; + : | + : `-- `a` 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 7e104ec8871..1320c34948b 100644 --- a/crates/swc_ecma_lints/src/rules/duplicate_bindings.rs +++ b/crates/swc_ecma_lints/src/rules/duplicate_bindings.rs @@ -79,6 +79,19 @@ impl DuplicateBindings { Some(VarDeclKind::Const) | Some(VarDeclKind::Let) ) } + + fn visit_with_kind>(&mut self, e: &V, kind: Option) { + let old_var_decl_kind = self.var_decl_kind.take(); + let old_is_pat_decl = self.is_pat_decl; + + self.var_decl_kind = kind; + self.is_pat_decl = true; + + e.visit_children_with(self); + + self.is_pat_decl = old_is_pat_decl; + self.var_decl_kind = old_var_decl_kind; + } } impl Visit for DuplicateBindings { @@ -93,16 +106,7 @@ 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; + self.visit_with_kind(c, Some(VarDeclKind::Var)) } fn visit_class_decl(&mut self, d: &ClassDecl) { @@ -192,16 +196,11 @@ impl Visit for DuplicateBindings { } 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; + self.visit_with_kind(d, Some(d.kind)) + } - self.var_decl_kind = Some(d.kind); - self.is_pat_decl = true; - - d.visit_children_with(self); - - self.is_pat_decl = old_is_pat_decl; - self.var_decl_kind = old_var_decl_kind; + fn visit_param(&mut self, p: &Param) { + self.visit_with_kind(p, Some(VarDeclKind::Var)) } }