Skip to content

Commit 7bea830

Browse files
authored
fix(es/typescript): Remove empty statements that const enum decls are folded into (#10128)
**Description:** By searching `asEmbeddedStatement` in https://github.com/microsoft/TypeScript/blob/main/src/compiler/factory/nodeFactory.ts, we can know there are four cases where typescript **doesn't** insert an empty statement: 1. Stmt in ModuleItem 2. Stmts in Script's body 3. Stmts in BlockStmt 4. Stmts in SwitchCase Case 2, 3, 4 can be merged in `visit_mut_stmts`, while case 1 should be handled in `visit_mut_module_items` **Related issue:** - Closes #10097
1 parent a3a3a11 commit 7bea830

33 files changed

+86
-66
lines changed

.changeset/gold-hotels-camp.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_transforms_typescript: patch
4+
---
5+
6+
fix(es/typescript): Remove empty statements that const enum decls are folded into

crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ var Foo2 = /*#__PURE__*/ function(Foo2) {
88
}(Foo2 || {});
99
console.log(42, "42");
1010
console.log("hello", "こんにちは", "안녕하세요", "你好");
11-
;

crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
;
21
var x;
32
var ref;
43
ref = void 0, x = ref === void 0 ? 42 : ref;

crates/swc/tests/tsc-references/constEnum1.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
// An enum declaration that specifies a const modifier is a constant enum declaration.
33
// In a constant enum declaration, all members must have constant values and
44
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
5-
;

crates/swc/tests/tsc-references/constEnum3.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//// [constEnum3.ts]
2-
;
32
function f1(f) {}
43
function f2(f) {}
54
f1(0);

crates/swc/tests/tsc-references/constEnumNoObjectPrototypePropertyAccess.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//// [constEnumNoObjectPrototypePropertyAccess.ts]
22
// https://github.com/microsoft/TypeScript/issues/55421
3-
;
43
console.log(Bebra.constructor);
54
console.log(Bebra.hasOwnProperty);
65
console.log(Bebra.isPrototypeOf);

crates/swc/tests/tsc-references/constEnumPropertyAccess1.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// constant enum declarations are completely erased in the emitted JavaScript code.
33
// it is an error to reference a constant enum object in any other context
44
// than a property access that selects one of the enum's members
5-
;
65
var o = {
76
1: true
87
};

crates/swc/tests/tsc-references/constEnumPropertyAccess2.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// constant enum declarations are completely erased in the emitted JavaScript code.
33
// it is an error to reference a constant enum object in any other context
44
// than a property access that selects one of the enum's members
5-
;
65
// Error from referring constant enum in any other context than a property access
76
var z = G;
87
var z1 = G[1];

crates/swc/tests/tsc-references/constEnumPropertyAccess3.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//// [constEnumPropertyAccess3.ts]
2-
;
32
(-2).toString();
43
(-1).toString();
54
(-3).toString();

crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5.1.normal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ var E = /*#__PURE__*/ function(E) {
7171
E[E["b"] = 1] = "b";
7272
return E;
7373
}(E || {});
74-
;
7574
function foo1(...a) {}
7675
foo1(1, 2, 3, 0);
7776
foo1(1, 2, 3, 0, 1);

0 commit comments

Comments
 (0)