From d8a18dfd9e9e67da0ef19c5f44e847534790e58f Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Wed, 14 Apr 2021 01:42:07 -0700 Subject: [PATCH] fix(es/transforms/compat): Fix block scoping of class declarations (#1569) --- .../transforms/base/src/resolver/mod.rs | 3 ++ .../transforms/base/src/resolver/tests.rs | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ecmascript/transforms/base/src/resolver/mod.rs b/ecmascript/transforms/base/src/resolver/mod.rs index 0dcfda3f21cc..d30489cd1aad 100644 --- a/ecmascript/transforms/base/src/resolver/mod.rs +++ b/ecmascript/transforms/base/src/resolver/mod.rs @@ -1245,6 +1245,9 @@ impl VisitMut for Hoister<'_, '_> { } fn visit_mut_class_decl(&mut self, node: &mut ClassDecl) { + if self.in_block { + return; + } self.resolver.in_type = false; self.resolver .visit_mut_binding_ident(&mut node.ident, Some(VarDeclKind::Let)); diff --git a/ecmascript/transforms/base/src/resolver/tests.rs b/ecmascript/transforms/base/src/resolver/tests.rs index 27af359e84db..05bc14bf467d 100644 --- a/ecmascript/transforms/base/src/resolver/tests.rs +++ b/ecmascript/transforms/base/src/resolver/tests.rs @@ -2646,3 +2646,31 @@ to!( })(); "# ); + +to!( + block_scope_class, + r#" + const g = 20; + + function baz() { + { + class g {} + console.log(g); + } + + return g; + } + "#, + r#" + const g = 20; + + function baz() { + { + class g1 {} + console.log(g1); + } + + return g; + } + "# +);