feat(es/lints): Mark catch params as binding patterns while checking duplicates (#3981)

This commit is contained in:
Donny/강동윤 2022-03-12 12:02:22 +09:00 committed by GitHub
parent 28bc1e23a7
commit f28134fe77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 23 deletions

View File

@ -0,0 +1,4 @@
try {
} catch (foo) {
let foo;
}

View File

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

View File

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

View File

@ -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);
}
});