From 7bea830a0e6f73ab0ba5032d13d5e58e4674bc72 Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Sat, 1 Mar 2025 19:34:30 +0800 Subject: [PATCH] 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 https://github.com/swc-project/swc/issues/10097 --- .changeset/gold-hotels-camp.md | 6 +++ .../ts-inline-enum/default/output/index.ts | 1 - .../ts-inline-enum/lhs/output/index.ts | 1 - .../tsc-references/constEnum1.1.normal.js | 1 - .../tsc-references/constEnum3.1.normal.js | 1 - ...oObjectPrototypePropertyAccess.1.normal.js | 1 - .../constEnumPropertyAccess1.1.normal.js | 1 - .../constEnumPropertyAccess2.1.normal.js | 1 - .../constEnumPropertyAccess3.1.normal.js | 1 - ...turingParameterDeclaration3ES5.1.normal.js | 1 - ...rameterDeclaration3ES5iterable.1.normal.js | 1 - ...turingParameterDeclaration3ES6.1.normal.js | 1 - .../enumLiteralTypes1.1.normal.js | 1 - .../enumLiteralTypes2.1.normal.js | 1 - .../enumLiteralTypes3.1.normal.js | 1 - ...intersectionOfUnionOfUnitTypes.1.normal.js | 1 - .../intersectionReduction.1.normal.js | 2 - .../intersectionReductionStrict.1.normal.js | 2 - .../keyofAndIndexedAccess.1.normal.js | 1 - .../stringEnumLiteralTypes1.1.normal.js | 1 - .../stringEnumLiteralTypes2.1.normal.js | 1 - .../stringEnumLiteralTypes3.1.normal.js | 1 - ...nStaticMembers1(target=es2015).1.normal.js | 9 +--- ...nStaticMembers1(target=es2021).1.normal.js | 9 +--- ...nStaticMembers1(target=es2022).1.normal.js | 5 -- ...erInStaticMembers1(target=es5).1.normal.js | 9 +--- ...nStaticMembers1(target=esnext).1.normal.js | 5 -- .../templateLiteralTypes4.1.normal.js | 3 -- ...rdNarrowsPrimitiveIntersection.1.normal.js | 1 - .../src/transform.rs | 14 ++++- .../tests/strip.rs/ts_enum_is_mutable_true.js | 3 -- .../tests/fixture/issue-10097/input.ts | 51 +++++++++++++++++++ .../tests/fixture/issue-10097/output.js | 14 +++++ 33 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 .changeset/gold-hotels-camp.md create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/input.ts create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/output.js diff --git a/.changeset/gold-hotels-camp.md b/.changeset/gold-hotels-camp.md new file mode 100644 index 000000000000..c5c0864d8304 --- /dev/null +++ b/.changeset/gold-hotels-camp.md @@ -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 diff --git a/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts b/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts index 1a9e88f68f99..7bd8fb3f6162 100644 --- a/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts +++ b/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts @@ -8,4 +8,3 @@ var Foo2 = /*#__PURE__*/ function(Foo2) { }(Foo2 || {}); console.log(42, "42"); console.log("hello", "こんにちは", "안녕하세요", "你好"); -; diff --git a/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts b/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts index 98e01778d09c..5188677879d1 100644 --- a/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts +++ b/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts @@ -1,4 +1,3 @@ -; var x; var ref; ref = void 0, x = ref === void 0 ? 42 : ref; diff --git a/crates/swc/tests/tsc-references/constEnum1.1.normal.js b/crates/swc/tests/tsc-references/constEnum1.1.normal.js index dd5459d80a98..55e66098a8d7 100644 --- a/crates/swc/tests/tsc-references/constEnum1.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnum1.1.normal.js @@ -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. -; diff --git a/crates/swc/tests/tsc-references/constEnum3.1.normal.js b/crates/swc/tests/tsc-references/constEnum3.1.normal.js index c9416a46c65b..0a6f230a8a0f 100644 --- a/crates/swc/tests/tsc-references/constEnum3.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnum3.1.normal.js @@ -1,5 +1,4 @@ //// [constEnum3.ts] -; function f1(f) {} function f2(f) {} f1(0); diff --git a/crates/swc/tests/tsc-references/constEnumNoObjectPrototypePropertyAccess.1.normal.js b/crates/swc/tests/tsc-references/constEnumNoObjectPrototypePropertyAccess.1.normal.js index e582a37fcfea..74af9bc68610 100644 --- a/crates/swc/tests/tsc-references/constEnumNoObjectPrototypePropertyAccess.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumNoObjectPrototypePropertyAccess.1.normal.js @@ -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); diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess1.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess1.1.normal.js index 1b3e7c965849..25e21ce4deb2 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess1.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess1.1.normal.js @@ -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 }; diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess2.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess2.1.normal.js index 0e9418633478..fa167d11292c 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess2.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess2.1.normal.js @@ -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]; diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess3.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess3.1.normal.js index 5695cf2a2348..14a3d6f4f33b 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess3.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess3.1.normal.js @@ -1,5 +1,4 @@ //// [constEnumPropertyAccess3.ts] -; (-2).toString(); (-1).toString(); (-3).toString(); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5.1.normal.js index ac3f7ebb3058..9a1d3df98bc1 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5.1.normal.js @@ -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); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable.1.normal.js index 2df03ccc1c64..3a2e18f2b27e 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable.1.normal.js @@ -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]; diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6.1.normal.js index 5dd67455ff69..a9efe13fcfea 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6.1.normal.js @@ -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); diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes1.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes1.1.normal.js index 48bf4ca471ee..7296c87b370b 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes1.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes1.1.normal.js @@ -1,5 +1,4 @@ //// [enumLiteralTypes1.ts] -; function f1() { var a; var a; diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes2.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes2.1.normal.js index c95ef9eccba3..6ded29093691 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes2.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes2.1.normal.js @@ -1,5 +1,4 @@ //// [enumLiteralTypes2.ts] -; function f1() { var a; var a; diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes3.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes3.1.normal.js index 5df15728beed..b341948afc76 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes3.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes3.1.normal.js @@ -1,5 +1,4 @@ //// [enumLiteralTypes3.ts] -; function f1(a, b, c, d) { a = a; a = b; diff --git a/crates/swc/tests/tsc-references/intersectionOfUnionOfUnitTypes.1.normal.js b/crates/swc/tests/tsc-references/intersectionOfUnionOfUnitTypes.1.normal.js index 443e718edbe1..7f4494dc79bf 100644 --- a/crates/swc/tests/tsc-references/intersectionOfUnionOfUnitTypes.1.normal.js +++ b/crates/swc/tests/tsc-references/intersectionOfUnionOfUnitTypes.1.normal.js @@ -1,6 +1,5 @@ //// [intersectionOfUnionOfUnitTypes.ts] // @strict -; var x0; // 'a' | 'b' | 'c' var x1; // 'b' | 'c' var x2; // 'c' diff --git a/crates/swc/tests/tsc-references/intersectionReduction.1.normal.js b/crates/swc/tests/tsc-references/intersectionReduction.1.normal.js index 084f47a3a02f..a768c2c2660b 100644 --- a/crates/swc/tests/tsc-references/intersectionReduction.1.normal.js +++ b/crates/swc/tests/tsc-references/intersectionReduction.1.normal.js @@ -14,8 +14,6 @@ var x2 = { }; x1[k] = 'bar'; // Error x2[k] = 'bar'; // Error -; -; s1 = s2; s2 = s1; t1 = t2; diff --git a/crates/swc/tests/tsc-references/intersectionReductionStrict.1.normal.js b/crates/swc/tests/tsc-references/intersectionReductionStrict.1.normal.js index 7986015b4980..9e9b9ef0b29a 100644 --- a/crates/swc/tests/tsc-references/intersectionReductionStrict.1.normal.js +++ b/crates/swc/tests/tsc-references/intersectionReductionStrict.1.normal.js @@ -12,8 +12,6 @@ var x2 = { }; x1[k] = 'bar'; // Error x2[k] = 'bar'; // Error -; -; s1 = s2; s2 = s1; t1 = t2; diff --git a/crates/swc/tests/tsc-references/keyofAndIndexedAccess.1.normal.js b/crates/swc/tests/tsc-references/keyofAndIndexedAccess.1.normal.js index 3ada1b9a44e7..2381955ae890 100644 --- a/crates/swc/tests/tsc-references/keyofAndIndexedAccess.1.normal.js +++ b/crates/swc/tests/tsc-references/keyofAndIndexedAccess.1.normal.js @@ -23,7 +23,6 @@ var Options = function Options() { "use strict"; _class_call_check(this, Options); }; -; function getProperty(obj, key) { return obj[key]; } diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes1.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes1.1.normal.js index 3cbc57409340..9e383d7b5a01 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes1.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes1.1.normal.js @@ -1,5 +1,4 @@ //// [stringEnumLiteralTypes1.ts] -; function f1() { var a; var a; diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes2.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes2.1.normal.js index 740d062d9f71..ae4e639bf0fb 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes2.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes2.1.normal.js @@ -1,5 +1,4 @@ //// [stringEnumLiteralTypes2.ts] -; function f1() { var a; var a; diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes3.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes3.1.normal.js index 46057948504b..a9add30c9d79 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes3.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes3.1.normal.js @@ -1,5 +1,4 @@ //// [stringEnumLiteralTypes3.ts] -; function f1(a, b, c, d) { a = a; a = b; diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js index 417f892bb763..3c532a8e5094 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js @@ -2,7 +2,6 @@ //// [external.ts] export class Reflect { } -; export default class { } //// [locals.ts] @@ -44,7 +43,6 @@ C._ = [ _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ - ; _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ @@ -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); (()=>{ @@ -243,7 +238,6 @@ 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); @@ -251,7 +245,6 @@ 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); diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js index 417f892bb763..3c532a8e5094 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js @@ -2,7 +2,6 @@ //// [external.ts] export class Reflect { } -; export default class { } //// [locals.ts] @@ -44,7 +43,6 @@ C._ = [ _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ - ; _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ @@ -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); (()=>{ @@ -243,7 +238,6 @@ 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); @@ -251,7 +245,6 @@ 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); diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js index 1f3a216184a5..6ad247ff78e5 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js @@ -2,7 +2,6 @@ //// [external.ts] export class Reflect { } -; export default class { } //// [locals.ts] @@ -42,7 +41,6 @@ class C extends B { super.w(); })(), (()=>{ - ; super.w(); })(), (()=>{ @@ -96,7 +94,6 @@ class C extends B { super.w(); } static{ - ; super.w(); } static{ @@ -245,7 +242,6 @@ class C extends B { } export { }; //// [constEnumInContainingScopeStaticField.ts] -; class C extends B { static{ this._ = super.w(); @@ -253,7 +249,6 @@ class C extends B { } export { }; //// [constEnumInContainingScopeStaticBlock.ts] -; class C extends B { static{ super.w(); diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js index 6bb67745751d..b7776fdcd339 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js @@ -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); @@ -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() { @@ -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() { @@ -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); @@ -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); diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js index 1f3a216184a5..6ad247ff78e5 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js @@ -2,7 +2,6 @@ //// [external.ts] export class Reflect { } -; export default class { } //// [locals.ts] @@ -42,7 +41,6 @@ class C extends B { super.w(); })(), (()=>{ - ; super.w(); })(), (()=>{ @@ -96,7 +94,6 @@ class C extends B { super.w(); } static{ - ; super.w(); } static{ @@ -245,7 +242,6 @@ class C extends B { } export { }; //// [constEnumInContainingScopeStaticField.ts] -; class C extends B { static{ this._ = super.w(); @@ -253,7 +249,6 @@ class C extends B { } export { }; //// [constEnumInContainingScopeStaticBlock.ts] -; class C extends B { static{ super.w(); diff --git a/crates/swc/tests/tsc-references/templateLiteralTypes4.1.normal.js b/crates/swc/tests/tsc-references/templateLiteralTypes4.1.normal.js index 1e794a991684..b8417bbcb556 100644 --- a/crates/swc/tests/tsc-references/templateLiteralTypes4.1.normal.js +++ b/crates/swc/tests/tsc-references/templateLiteralTypes4.1.normal.js @@ -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 diff --git a/crates/swc/tests/tsc-references/typeGuardNarrowsPrimitiveIntersection.1.normal.js b/crates/swc/tests/tsc-references/typeGuardNarrowsPrimitiveIntersection.1.normal.js index 76a74d992424..a1c629e1551c 100644 --- a/crates/swc/tests/tsc-references/typeGuardNarrowsPrimitiveIntersection.1.normal.js +++ b/crates/swc/tests/tsc-references/typeGuardNarrowsPrimitiveIntersection.1.normal.js @@ -5,7 +5,6 @@ if (isNonBlank(value)) { } else { doThat(value); } -; if (isNonBlank2(value)) { doThis2(value); } else { diff --git a/crates/swc_ecma_transforms_typescript/src/transform.rs b/crates/swc_ecma_transforms_typescript/src/transform.rs index a4d63c4dd70b..99ef7ad1fe27 100644 --- a/crates/swc_ecma_transforms_typescript/src/transform.rs +++ b/crates/swc_ecma_transforms_typescript/src/transform.rs @@ -252,7 +252,12 @@ impl VisitMut for Transform { fn visit_mut_module_items(&mut self, node: &mut Vec) { let var_list = self.var_list.take(); - node.visit_mut_children_with(self); + node.retain_mut(|item| { + let is_empty = item.as_stmt().map(Stmt::is_empty).unwrap_or(false); + item.visit_mut_with(self); + // Remove those folded into Empty + is_empty || !item.as_stmt().map(Stmt::is_empty).unwrap_or(false) + }); let var_list = mem::replace(&mut self.var_list, var_list); if !var_list.is_empty() { @@ -347,7 +352,12 @@ impl VisitMut for Transform { fn visit_mut_stmts(&mut self, node: &mut Vec) { let var_list = self.var_list.take(); - node.visit_mut_children_with(self); + node.retain_mut(|stmt| { + let is_empty = stmt.is_empty(); + stmt.visit_mut_with(self); + // Remove those folded into Empty + is_empty || !stmt.is_empty() + }); let var_list = mem::replace(&mut self.var_list, var_list); if !var_list.is_empty() { let decls = var_list.into_iter().map(id_to_var_declarator).collect(); diff --git a/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/ts_enum_is_mutable_true.js b/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/ts_enum_is_mutable_true.js index 4dac34a2886e..0ba6cd848efd 100644 --- a/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/ts_enum_is_mutable_true.js +++ b/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/ts_enum_is_mutable_true.js @@ -5,9 +5,7 @@ var D = /*#__PURE__*/ function(D) { }(D || {}); D.A = 5; console.log(D.A); -; console.log(1); -; var G = /*#__PURE__*/ function(G) { G[G["A"] = 2] = "A"; return G; @@ -17,5 +15,4 @@ var H = /*#__PURE__*/ function(H) { H[H["A"] = 2] = "A"; return H; }(H || {}); -; console.log(2); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/input.ts new file mode 100644 index 000000000000..a847f50ab8f4 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/input.ts @@ -0,0 +1,51 @@ +const enum A { + A, +} + +export enum B { + B, +} + +{ + const enum C { + C, + } +} + +label: const enum D { + D, +} + +if (Math.random()) + const enum E { + E, + } +else + const enum F { + F, + } + +while (Math.random()) + const enum G { + G, + } + +do + const enum H { + H, + } +while (Math.random()); + +for (const _ of Math.random()) + const enum I { + I, + } + +switch (Math.random()) { + case 1: + const enum J { + J, + } +} + +; diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/output.js new file mode 100644 index 000000000000..1bc7dd26cce8 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-10097/output.js @@ -0,0 +1,14 @@ +export var B = /*#__PURE__*/ function(B) { + B[B["B"] = 0] = "B"; + return B; +}({}); +{}label: ; +if (Math.random()) ; +else ; +while(Math.random()); +do ; +while (Math.random()) +for (const _ of Math.random()); +switch(Math.random()){ + case 1: +}