fix(es/typescript): Handle enum in single statement (#9532)

**Related issue:**
- Closes https://github.com/swc-project/swc/issues/9531
This commit is contained in:
magic-akari 2024-09-04 19:56:11 +08:00 committed by GitHub
parent c7fdd6b69b
commit 84b004387b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 161 additions and 10 deletions

View File

@ -0,0 +1,6 @@
---
swc_ecma_transforms_typescript: patch
swc_core: patch
---
fix(es/typescript): Handle enum in single statement

View File

@ -0,0 +1,9 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022"
},
"isModule": true
}

View File

@ -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 {};

View File

@ -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 = {}));
}
;

View File

@ -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;
}

View File

@ -1,3 +1 @@
//// [constEnum4.ts]
const enum A {
}

View File

@ -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,
},
};
@ -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 {

View File

@ -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> {

View File

@ -41,7 +41,6 @@ fn identity(entry: PathBuf) {
// TODO: Unignore
let postponed = &[
"constEnum4.ts",
"decoratorOnClassMethod11.ts",
"elementAccessChain.3.ts",
"awaitUsingDeclarationsInForAwaitOf.ts",