mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 09:38:16 +03:00
feat(es/lints): Mark catch params as binding patterns while checking duplicates (#3981)
This commit is contained in:
parent
28bc1e23a7
commit
f28134fe77
@ -0,0 +1,4 @@
|
||||
try {
|
||||
} catch (foo) {
|
||||
let foo;
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user