Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(es/resolver): Handle TsInterfaceDecl and UsingDecl correctly #8403

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of this logic seems dangerous to me at that time, but now I'm not sure why I feel it dangerous.

.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
}