Skip to content

Commit

Permalink
fix(es/typescript): Handle enum in single statement (#9532)
Browse files Browse the repository at this point in the history
**Related issue:**
- Closes #9531
  • Loading branch information
magic-akari authored Sep 4, 2024
1 parent c7fdd6b commit 84b0043
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/fresh-zoos-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_transforms_typescript: patch
swc_core: patch
---

fix(es/typescript): Handle enum in single statement
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9531/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022"
},
"isModule": true
}
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9531/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if(true) enum A {}
do enum B {} while(false);
while(false) enum C {};
for (;false;) enum D {};
for (const a in {}) enum E {};
for (const a of []) enum F {};
28 changes: 28 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9531/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
if (true) {
var A;
(function(A) {})(A || (A = {}));
}
do {
var B;
(function(B) {})(B || (B = {}));
}while (false)
while(false){
var C;
(function(C) {})(C || (C = {}));
}
;
for(; false;){
var D;
(function(D) {})(D || (D = {}));
}
;
for(const a in {}){
var E;
(function(E) {})(E || (E = {}));
}
;
for (const a of []){
var F;
(function(F) {})(F || (F = {}));
}
;
11 changes: 6 additions & 5 deletions crates/swc/tests/tsc-references/constEnum4.1.normal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//// [constEnum4.ts]
if (1) const enum A {
}
else if (2) const enum B {
}
else const enum C {
if (1) {
var A;
} else if (2) {
var B;
} else {
var C;
}
2 changes: 0 additions & 2 deletions crates/swc/tests/tsc-references/constEnum4.2.minified.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//// [constEnum4.ts]
const enum A {
}
103 changes: 101 additions & 2 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
config::TsImportExportAssignConfig,
ts_enum::{EnumValueComputer, InlineEnum, TsEnumRecord, TsEnumRecordKey, TsEnumRecordValue},
utils::{
assign_value_to_this_private_prop, assign_value_to_this_prop, AsCollapsibleDecl,
AsEnumOrModule, Factory,
assign_value_to_this_private_prop, assign_value_to_this_prop, stmt_is_enum,
AsCollapsibleDecl, AsEnumOrModule, Factory,
},
};

Expand Down Expand Up @@ -287,6 +287,105 @@ impl VisitMut for Transform {

self.namespace_id = old_id;
}

fn visit_mut_if_stmt(&mut self, node: &mut IfStmt) {
if stmt_is_enum(&node.cons) {
node.cons = BlockStmt {
stmts: vec![*node.cons.take()],
..Default::default()
}
.into();
}

if let Some(ref mut alt) = node.alt {
if stmt_is_enum(alt) {
*alt = BlockStmt {
stmts: vec![*alt.take()],
..Default::default()
}
.into();
}
}

node.visit_mut_children_with(self);
}

fn visit_mut_for_of_stmt(&mut self, node: &mut ForOfStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}

fn visit_mut_for_in_stmt(&mut self, node: &mut ForInStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}

fn visit_mut_for_stmt(&mut self, node: &mut ForStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}

fn visit_mut_while_stmt(&mut self, node: &mut WhileStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}

fn visit_mut_do_while_stmt(&mut self, node: &mut DoWhileStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}

fn visit_mut_labeled_stmt(&mut self, node: &mut LabeledStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}

fn visit_mut_with_stmt(&mut self, node: &mut WithStmt) {
if stmt_is_enum(&node.body) {
node.body = BlockStmt {
stmts: vec![*node.body.take()],
..Default::default()
}
.into();
}
node.visit_mut_children_with(self);
}
}

impl Transform {
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ impl AsEnumOrModule for ModuleItem {
}
}

#[inline]
pub(crate) fn stmt_is_enum(stmt: &Stmt) -> bool {
matches!(stmt, Stmt::Decl(Decl::TsEnum(..)))
}

///
/// this.prop = value
pub(crate) fn assign_value_to_this_prop(prop_name: PropName, value: Expr) -> Box<Expr> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn identity(entry: PathBuf) {

// TODO: Unignore
let postponed = &[
"constEnum4.ts",
"decoratorOnClassMethod11.ts",
"elementAccessChain.3.ts",
"awaitUsingDeclarationsInForAwaitOf.ts",
Expand Down

0 comments on commit 84b0043

Please sign in to comment.