Skip to content

Commit

Permalink
fix(es/resolver): Handle TsInterfaceDecl and UsingDecl correctly (#…
Browse files Browse the repository at this point in the history
…8403)

**Related issue**:
 - Closes #7967
  • Loading branch information
l4yton authored Dec 8, 2023
1 parent 595f13c commit f8ce316
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
53 changes: 28 additions & 25 deletions crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,35 +344,17 @@ impl<'a> Resolver<'a> {

if self.in_type {
self.current.declared_types.insert(ident.sym.clone());
let mark = self.current.mark;

ident.span = if mark == Mark::root() {
ident.span
} else {
let span = ident.span.apply_mark(mark);
if cfg!(debug_assertions) && LOG {
debug!("\t-> {:?}", span.ctxt());
}
span
};
return;
} else {
self.current
.declared_symbols
.insert(ident.sym.clone(), kind);
}

let mark = self.current.mark;

self.current
.declared_symbols
.insert(ident.sym.clone(), kind);

ident.span = if mark == Mark::root() {
ident.span
} else {
let span = ident.span.apply_mark(mark);
if cfg!(debug_assertions) && LOG {
debug!("\t-> {:?}", span.ctxt());
}
span
};
if mark != Mark::root() {
ident.span = ident.span.apply_mark(mark);
}
}

fn mark_block(&mut self, span: &mut Span) {
Expand Down Expand Up @@ -1312,11 +1294,16 @@ impl<'a> VisitMut for Resolver<'a> {
fn visit_mut_ts_interface_decl(&mut self, n: &mut TsInterfaceDecl) {
// always resolve the identifier for type stripping purposes
let old_in_type = self.in_type;
let old_ident_type = self.ident_type;

self.in_type = true;
self.ident_type = IdentType::Ref;

self.modify(&mut n.id, DeclKind::Type);

if !self.config.handle_types {
self.in_type = old_in_type;
self.ident_type = old_ident_type;
return;
}

Expand All @@ -1327,7 +1314,9 @@ impl<'a> VisitMut for Resolver<'a> {
n.extends.visit_mut_with(child);
n.body.visit_mut_with(child);
});

self.in_type = old_in_type;
self.ident_type = old_ident_type;
}

fn visit_mut_ts_mapped_type(&mut self, n: &mut TsMappedType) {
Expand Down Expand Up @@ -1484,6 +1473,13 @@ impl<'a> VisitMut for Resolver<'a> {
params.visit_mut_children_with(self);
}

fn visit_mut_using_decl(&mut self, decl: &mut UsingDecl) {
let old_kind = self.decl_kind;
self.decl_kind = DeclKind::Lexical;
decl.decls.visit_mut_with(self);
self.decl_kind = old_kind;
}

fn visit_mut_var_decl(&mut self, decl: &mut VarDecl) {
let old_kind = self.decl_kind;
self.decl_kind = decl.kind.into();
Expand Down Expand Up @@ -1660,6 +1656,10 @@ impl VisitMut for Hoister<'_, '_> {
if self.resolver.config.handle_types {
match decl {
Decl::TsInterface(i) => {
if self.in_block {
return;
}

let old_in_type = self.resolver.in_type;
self.resolver.in_type = true;
self.resolver.modify(&mut i.id, DeclKind::Type);
Expand Down Expand Up @@ -1833,6 +1833,9 @@ impl VisitMut for Hoister<'_, '_> {
#[inline]
fn visit_mut_ts_module_block(&mut self, _: &mut TsModuleBlock) {}

#[inline]
fn visit_mut_using_decl(&mut self, _: &mut UsingDecl) {}

fn visit_mut_var_decl(&mut self, node: &mut VarDecl) {
if self.in_block {
match node.kind {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
interface Foo { }
}

{
interface Foo { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
interface Foo__3 {
}
}{
interface Foo__4 {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ using foo = null }
{ using foo = null }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
using foo__3 = null
}{
using foo__4 = null
}

0 comments on commit f8ce316

Please sign in to comment.