mirror of
https://github.com/swc-project/swc.git
synced 2024-11-25 22:34:04 +03:00
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:
parent
c7fdd6b69b
commit
84b004387b
6
.changeset/fresh-zoos-act.md
Normal file
6
.changeset/fresh-zoos-act.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
swc_ecma_transforms_typescript: patch
|
||||
swc_core: patch
|
||||
---
|
||||
|
||||
fix(es/typescript): Handle enum in single statement
|
9
crates/swc/tests/fixture/issues-9xxx/9531/input/.swcrc
Normal file
9
crates/swc/tests/fixture/issues-9xxx/9531/input/.swcrc
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript"
|
||||
},
|
||||
"target": "es2022"
|
||||
},
|
||||
"isModule": true
|
||||
}
|
6
crates/swc/tests/fixture/issues-9xxx/9531/input/index.ts
Normal file
6
crates/swc/tests/fixture/issues-9xxx/9531/input/index.ts
Normal 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 {};
|
28
crates/swc/tests/fixture/issues-9xxx/9531/output/index.ts
Normal file
28
crates/swc/tests/fixture/issues-9xxx/9531/output/index.ts
Normal 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 = {}));
|
||||
}
|
||||
;
|
@ -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;
|
||||
}
|
||||
|
@ -1,3 +1 @@
|
||||
//// [constEnum4.ts]
|
||||
const enum A {
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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> {
|
||||
|
@ -41,7 +41,6 @@ fn identity(entry: PathBuf) {
|
||||
|
||||
// TODO: Unignore
|
||||
let postponed = &[
|
||||
"constEnum4.ts",
|
||||
"decoratorOnClassMethod11.ts",
|
||||
"elementAccessChain.3.ts",
|
||||
"awaitUsingDeclarationsInForAwaitOf.ts",
|
||||
|
Loading…
Reference in New Issue
Block a user