Skip to content

Commit

Permalink
fix(es/typescript): Remove empty statements that const enum decls are…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
CPunisher authored Mar 1, 2025
1 parent a3a3a11 commit 7bea830
Show file tree
Hide file tree
Showing 33 changed files with 86 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .changeset/gold-hotels-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_transforms_typescript: patch
---

fix(es/typescript): Remove empty statements that const enum decls are folded into
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ var Foo2 = /*#__PURE__*/ function(Foo2) {
}(Foo2 || {});
console.log(42, "42");
console.log("hello", "こんにちは", "안녕하세요", "你好");
;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
;
var x;
var ref;
ref = void 0, x = ref === void 0 ? 42 : ref;
Expand Down
1 change: 0 additions & 1 deletion crates/swc/tests/tsc-references/constEnum1.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
// An enum declaration that specifies a const modifier is a constant enum declaration.
// In a constant enum declaration, all members must have constant values and
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
;
1 change: 0 additions & 1 deletion crates/swc/tests/tsc-references/constEnum3.1.normal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [constEnum3.ts]
;
function f1(f) {}
function f2(f) {}
f1(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//// [constEnumNoObjectPrototypePropertyAccess.ts]
// https://github.com/microsoft/TypeScript/issues/55421
;
console.log(Bebra.constructor);
console.log(Bebra.hasOwnProperty);
console.log(Bebra.isPrototypeOf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// constant enum declarations are completely erased in the emitted JavaScript code.
// it is an error to reference a constant enum object in any other context
// than a property access that selects one of the enum's members
;
var o = {
1: true
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// constant enum declarations are completely erased in the emitted JavaScript code.
// it is an error to reference a constant enum object in any other context
// than a property access that selects one of the enum's members
;
// Error from referring constant enum in any other context than a property access
var z = G;
var z1 = G[1];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [constEnumPropertyAccess3.ts]
;
(-2).toString();
(-1).toString();
(-3).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ var E = /*#__PURE__*/ function(E) {
E[E["b"] = 1] = "b";
return E;
}(E || {});
;
function foo1(...a) {}
foo1(1, 2, 3, 0);
foo1(1, 2, 3, 0, 1);
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ var E = /*#__PURE__*/ function(E) {
E[E["b"] = 1] = "b";
return E;
}(E || {});
;
function foo1() {
for(var _len = arguments.length, a = new Array(_len), _key = 0; _key < _len; _key++){
a[_key] = arguments[_key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ var E = /*#__PURE__*/ function(E) {
E[E["b"] = 1] = "b";
return E;
}(E || {});
;
function foo1(...a) {}
foo1(1, 2, 3, 0);
foo1(1, 2, 3, 0, 1);
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [enumLiteralTypes1.ts]
;
function f1() {
var a;
var a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [enumLiteralTypes2.ts]
;
function f1() {
var a;
var a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [enumLiteralTypes3.ts]
;
function f1(a, b, c, d) {
a = a;
a = b;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//// [intersectionOfUnionOfUnitTypes.ts]
// @strict
;
var x0; // 'a' | 'b' | 'c'
var x1; // 'b' | 'c'
var x2; // 'c'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var x2 = {
};
x1[k] = 'bar'; // Error
x2[k] = 'bar'; // Error
;
;
s1 = s2;
s2 = s1;
t1 = t2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var x2 = {
};
x1[k] = 'bar'; // Error
x2[k] = 'bar'; // Error
;
;
s1 = s2;
s2 = s1;
t1 = t2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var Options = function Options() {
"use strict";
_class_call_check(this, Options);
};
;
function getProperty(obj, key) {
return obj[key];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [stringEnumLiteralTypes1.ts]
;
function f1() {
var a;
var a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [stringEnumLiteralTypes2.ts]
;
function f1() {
var a;
var a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//// [stringEnumLiteralTypes3.ts]
;
function f1(a, b, c, d) {
a = a;
a = b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//// [external.ts]
export class Reflect {
}
;
export default class {
}
//// [locals.ts]
Expand Down Expand Up @@ -44,7 +43,6 @@ C._ = [
_get(_get_prototype_of(C), "w", C).call(C);
})(),
(()=>{
;
_get(_get_prototype_of(C), "w", C).call(C);
})(),
(()=>{
Expand Down Expand Up @@ -96,10 +94,7 @@ C._ = [
;
_get(_get_prototype_of(C), "w", C).call(C);
})();
(()=>{
;
_get(_get_prototype_of(C), "w", C).call(C);
})();
_get(_get_prototype_of(C), "w", C).call(C);
_get(_get_prototype_of(C), "w", C).call(C);
_get(_get_prototype_of(C), "w", C).call(C);
(()=>{
Expand Down Expand Up @@ -243,15 +238,13 @@ export { };
//// [constEnumInContainingScopeStaticField.ts]
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
;
class C extends B {
}
C._ = _get(_get_prototype_of(C), "w", C).call(C);
export { };
//// [constEnumInContainingScopeStaticBlock.ts]
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
;
class C extends B {
}
_get(_get_prototype_of(C), "w", C).call(C);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//// [external.ts]
export class Reflect {
}
;
export default class {
}
//// [locals.ts]
Expand Down Expand Up @@ -44,7 +43,6 @@ C._ = [
_get(_get_prototype_of(C), "w", C).call(C);
})(),
(()=>{
;
_get(_get_prototype_of(C), "w", C).call(C);
})(),
(()=>{
Expand Down Expand Up @@ -96,10 +94,7 @@ C._ = [
;
_get(_get_prototype_of(C), "w", C).call(C);
})();
(()=>{
;
_get(_get_prototype_of(C), "w", C).call(C);
})();
_get(_get_prototype_of(C), "w", C).call(C);
_get(_get_prototype_of(C), "w", C).call(C);
_get(_get_prototype_of(C), "w", C).call(C);
(()=>{
Expand Down Expand Up @@ -243,15 +238,13 @@ export { };
//// [constEnumInContainingScopeStaticField.ts]
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
;
class C extends B {
}
C._ = _get(_get_prototype_of(C), "w", C).call(C);
export { };
//// [constEnumInContainingScopeStaticBlock.ts]
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
;
class C extends B {
}
_get(_get_prototype_of(C), "w", C).call(C);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//// [external.ts]
export class Reflect {
}
;
export default class {
}
//// [locals.ts]
Expand Down Expand Up @@ -42,7 +41,6 @@ class C extends B {
super.w();
})(),
(()=>{
;
super.w();
})(),
(()=>{
Expand Down Expand Up @@ -96,7 +94,6 @@ class C extends B {
super.w();
}
static{
;
super.w();
}
static{
Expand Down Expand Up @@ -245,15 +242,13 @@ class C extends B {
}
export { };
//// [constEnumInContainingScopeStaticField.ts]
;
class C extends B {
static{
this._ = super.w();
}
}
export { };
//// [constEnumInContainingScopeStaticBlock.ts]
;
class C extends B {
static{
super.w();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export var Reflect = function Reflect() {
"use strict";
_class_call_check(this, Reflect);
};
;
var _default = function _default() {
"use strict";
_class_call_check(this, _default);
Expand Down Expand Up @@ -62,7 +61,6 @@ C._ = [
_get(_get_prototype_of(C), "w", C).call(C);
}(),
function() {
;
_get(_get_prototype_of(C), "w", C).call(C);
}(),
function() {
Expand Down Expand Up @@ -118,10 +116,7 @@ C._ = [
;
_get(_get_prototype_of(C), "w", C).call(C);
})();
(function() {
;
_get(_get_prototype_of(C), "w", C).call(C);
})();
_get(_get_prototype_of(C), "w", C).call(C);
_get(_get_prototype_of(C), "w", C).call(C);
_get(_get_prototype_of(C), "w", C).call(C);
(function() {
Expand Down Expand Up @@ -412,7 +407,6 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
;
var C = /*#__PURE__*/ function(B1) {
"use strict";
_inherits(C, B1);
Expand All @@ -430,7 +424,6 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
;
var C = /*#__PURE__*/ function(B1) {
"use strict";
_inherits(C, B1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//// [external.ts]
export class Reflect {
}
;
export default class {
}
//// [locals.ts]
Expand Down Expand Up @@ -42,7 +41,6 @@ class C extends B {
super.w();
})(),
(()=>{
;
super.w();
})(),
(()=>{
Expand Down Expand Up @@ -96,7 +94,6 @@ class C extends B {
super.w();
}
static{
;
super.w();
}
static{
Expand Down Expand Up @@ -245,15 +242,13 @@ class C extends B {
}
export { };
//// [constEnumInContainingScopeStaticField.ts]
;
class C extends B {
static{
this._ = super.w();
}
}
export { };
//// [constEnumInContainingScopeStaticBlock.ts]
;
class C extends B {
static{
super.w();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//// [templateLiteralTypes4.ts]
// infer from number
;
;
;
p.getIndex(0); // ok, 0 is a valid index
p.getIndex(1); // ok, 1 is a valid index
p.getIndex(2); // error, 2 is not a valid index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ if (isNonBlank(value)) {
} else {
doThat(value);
}
;
if (isNonBlank2(value)) {
doThis2(value);
} else {
Expand Down
Loading

0 comments on commit 7bea830

Please sign in to comment.