fix(es/lints): Check parameters in the duplicate binding rule (#4288)

This commit is contained in:
Austaras 2022-04-09 16:14:01 +08:00 committed by GitHub
parent ea3d6c1a58
commit e4a565c2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 19 deletions

View File

@ -0,0 +1,3 @@
function foo(a) {
let a;
}

View File

@ -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
`----

View File

@ -79,6 +79,19 @@ impl DuplicateBindings {
Some(VarDeclKind::Const) | Some(VarDeclKind::Let) Some(VarDeclKind::Const) | Some(VarDeclKind::Let)
) )
} }
fn visit_with_kind<V: VisitWith<Self>>(&mut self, e: &V, kind: Option<VarDeclKind>) {
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 { impl Visit for DuplicateBindings {
@ -93,16 +106,7 @@ impl Visit for DuplicateBindings {
} }
fn visit_catch_clause(&mut self, c: &CatchClause) { fn visit_catch_clause(&mut self, c: &CatchClause) {
let old_var_decl_kind = self.var_decl_kind.take(); self.visit_with_kind(c, Some(VarDeclKind::Var))
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) { fn visit_class_decl(&mut self, d: &ClassDecl) {
@ -192,16 +196,11 @@ impl Visit for DuplicateBindings {
} }
fn visit_var_decl(&mut self, d: &VarDecl) { fn visit_var_decl(&mut self, d: &VarDecl) {
let old_var_decl_kind = self.var_decl_kind.take(); self.visit_with_kind(d, Some(d.kind))
let old_is_pat_decl = self.is_pat_decl; }
self.var_decl_kind = Some(d.kind); fn visit_param(&mut self, p: &Param) {
self.is_pat_decl = true; self.visit_with_kind(p, Some(VarDeclKind::Var))
d.visit_children_with(self);
self.is_pat_decl = old_is_pat_decl;
self.var_decl_kind = old_var_decl_kind;
} }
} }