Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 11 additions & 6 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,17 +1074,22 @@ impl Transform {
return FoldedDecl::Empty;
}

let opaque = member_list
.iter()
.any(|item| matches!(item.value, TsEnumRecordValue::Opaque(..)));
let namespace_export = self.namespace_id.is_some() && is_export;
let iife = !is_first || namespace_export;

let mut opaque = false;

let stmts = member_list
.into_iter()
.filter(|item| !ts_enum_safe_remove || !item.is_const())
.map(|item| item.build_assign(&id.to_id()));
.map(|mut item| {
if let TsEnumRecordValue::Opaque(ref mut expr) = item.value {
opaque = true;
expr.visit_mut_with(self);
}

let namespace_export = self.namespace_id.is_some() && is_export;
let iife = !is_first || namespace_export;
item.build_assign(&id.to_id())
});

let body = if !iife {
let return_stmt: Stmt = ReturnStmt {
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/ts_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ impl EnumValueComputer<'_> {
Expr::Bin(e) => self.compute_bin(e),
Expr::Member(e) => self.compute_member(e),
Expr::Tpl(e) => self.compute_tpl(e),
// Handle TypeScript type expressions by stripping them
// and computing the inner expression
Expr::TsAs(TsAsExpr { expr, .. })
| Expr::TsNonNull(TsNonNullExpr { expr, .. })
| Expr::TsTypeAssertion(TsTypeAssertion { expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { expr, .. })
| Expr::TsInstantiation(TsInstantiation { expr, .. })
| Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => self.compute_rec(expr),
_ => TsEnumRecordValue::Opaque(expr),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Foo = function(Foo) {
Foo[Foo["a"] = (class {
constructor(b){
this.b = b;
}
}, 0)] = "a";
return Foo;
}(Foo || {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var Foo = function(Foo) {
Foo[Foo["a"] = (()=>{
(function(Bar) {
Bar["a"] = "a";
Bar["b"] = "b";
})(Bar);
return 0;
})()] = "a";
return Foo;
}(Foo || {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var Foo = function(Foo) {
Foo[Foo["a"] = foo('x')] = "a";
return Foo;
}(Foo || {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var RefType = /*#__PURE__*/ function(RefType) {
RefType["property"] = "11";
RefType["event"] = "22";
return RefType;
}(RefType || {});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Foo = function(Foo) {
Foo[Foo["a"] = 0] = "a";
Foo[Foo["b"] = 0] = "b";
Foo[Foo["c"] = 1] = "c";
Foo[Foo["d"] = 1 + Foo.c * x] = "d";
Foo[Foo["d"] = 1 + 1 * x] = "d";
Foo[Foo["e"] = 2 * Foo.d] = "e";
Foo[Foo["f"] = Foo.e * 10] = "f";
return Foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ var Baz = function(Baz) {
Baz[Baz["a"] = 0] = "a";
Baz[Baz["b"] = 1] = "b";
// @ts-ignore
Baz[Baz["x"] = Baz.a.toString()] = "x";
Baz[Baz["x"] = 0..toString()] = "x";
return Baz;
}(Baz || {});
37 changes: 37 additions & 0 deletions crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,43 @@ to!(
}"
);

to!(
ts_enum_with_type_assertion,
"enum RefType {
property = '11' as any,
event = '22' as any,
}"
);

to!(
ts_enum_with_opaque_expr,
"enum Foo {
a = foo('x' as any),
}"
);

to!(
ts_enum_with_nested_class,
"enum Foo {
a = (class {
constructor(public b: string) { }
}, 0)
}"
);

to!(
ts_enum_with_nested_enum,
"enum Foo {
a = (() => {
enum Bar {
a = 'a',
b = 'b',
}
return 0;
})(),
}"
);

to!(module_01, "module 'foo'{ }");

to!(declare_01, "declare var env: FOO");
Expand Down
Loading