Skip to content

Commit

Permalink
refactor(es/typescript): Simplifying enum and namespace transforms (#…
Browse files Browse the repository at this point in the history
…9558)

**Related issue:**

- Closes #4453
- Closes #9385
  • Loading branch information
magic-akari authored Oct 4, 2024
1 parent ec0a62c commit 2480bb0
Show file tree
Hide file tree
Showing 690 changed files with 3,049 additions and 3,337 deletions.
8 changes: 8 additions & 0 deletions .changeset/tiny-games-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
swc_ecma_transforms_typescript: patch
swc_ecma_utils: breaking
swc_core: patch
swc_ecma_transforms: patch
---

refactor(es/typescript): Simplifying enum and namespace transforms
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var _define_property = require("@swc/helpers/_/_define_property");
var _ts_decorate = require("@swc/helpers/_/_ts_decorate");
var _ts_metadata = require("@swc/helpers/_/_ts_metadata");
var MyEnum;
(function(MyEnum) {
var MyEnum = /*#__PURE__*/ function(MyEnum) {
MyEnum["x"] = "xxx";
MyEnum["y"] = "yyy";
})(MyEnum || (MyEnum = {}));
return MyEnum;
}(MyEnum || {});
class Xpto {
constructor(){
_define_property._(this, "value", void 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ServiceError extends Error {
}
}
(function(ServiceError) {
let Code;
(function(Code) {
Code[Code["serviceNotFound"] = 404] = "serviceNotFound";
Code[Code["serviceNotCompatible"] = 426] = "serviceNotCompatible";
Expand All @@ -24,7 +23,7 @@ class ServiceError extends Error {
Code[Code["timedOut"] = 504] = "timedOut";
Code[Code["badRequest"] = 400] = "badRequest";
Code[Code["badResponse"] = 422] = "badResponse";
})(Code = ServiceError.Code || (ServiceError.Code = {}));
})(ServiceError.Code || (ServiceError.Code = {}));
class ServiceNotFound extends ServiceError {
constructor(...args){
super(...args), // Service was probably not registered, or using the wrong channel
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-2xxx/2214/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
export var Test;
(function(Test) {
Test.content = /*#__PURE__*/ React.createElement("div", null, "Content");
})(Test || (Test = {}));
export var Test;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-2xxx/2310/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var A;
(function(A) {
A.Foo = function() {
return /*#__PURE__*/ React.createElement("div", null);
};
})(A || (A = {}));
var A;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-2xxx/2423/output/index.map
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": ";UAAUA;MACOC,IAAI;IACV,SAASC;QACZC,QAAQC,GAAG,GAACH;IAChB;MAFgBC,IAAAA;AAGpB,GALUF,MAAAA",
"mappings": "UAAUA;MACOC,IAAI;IACV,SAASC;QACZC,QAAQC,GAAG,GAACH;IAChB;MAFgBC,IAAAA;AAGpB,GALUF,MAAAA",
"names": [
"A",
"v",
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-2xxx/2423/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var A;
(function(A) {
A.v = 25;
function a() {
console.log(A.v);
}
A.a = a;
})(A || (A = {}));
var A;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-2xxx/2636/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export var RuleInterpreterHelper;
(function(RuleInterpreterHelper) {
RuleInterpreterHelper.fieldNameMap = [];
RuleInterpreterHelper.fieldNameHashMap = new Map(RuleInterpreterHelper.fieldNameMap);
})(RuleInterpreterHelper || (RuleInterpreterHelper = {}));
export var RuleInterpreterHelper;
8 changes: 3 additions & 5 deletions crates/swc/tests/fixture/issues-2xxx/2988/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export var Test;
(function(t) {
let e;
(function(t) {
t["Test"] = "1";
t["Test2"] = "2";
t["Test3"] = "3";
})(e = t.testEnum || (t.testEnum = {}));
let s;
})(t.testEnum || (t.testEnum = {}));
(function(t) {
t["Test"] = "1";
t["Test2"] = "2";
t["Test3"] = "3";
})(s = t.testEnum2 || (t.testEnum2 = {}));
})(t.testEnum2 || (t.testEnum2 = {}));
})(Test || (Test = {}));
export var Test;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var n;
(function(n) {
var ref;
ref = {
a: 1
}, n.a = ref.a, ref;
})(n || (n = {}));
var n;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Foo;
(function(Foo) {
var ref;
ref = {
a: 1,
b: 2
}, Foo.A = ref.a, Foo.B = ref.b, ref;
})(Foo || (Foo = {}));
var Foo;
3 changes: 1 addition & 2 deletions crates/swc/tests/fixture/issues-3xxx/3686/1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ServiceError = _ts_decorate._([
CD
], ServiceError);
(function(ServiceError) {
let Code;
(function(Code) {
Code[Code["serviceNotFound"] = 404] = "serviceNotFound";
Code[Code["serviceNotCompatible"] = 426] = "serviceNotCompatible";
Expand All @@ -31,7 +30,7 @@ ServiceError = _ts_decorate._([
Code[Code["timedOut"] = 504] = "timedOut";
Code[Code["badRequest"] = 400] = "badRequest";
Code[Code["badResponse"] = 422] = "badResponse";
})(Code = ServiceError.Code || (ServiceError.Code = {}));
})(ServiceError.Code || (ServiceError.Code = {}));
class ServiceNotFound extends ServiceError {
code = 404;
name = "ServiceError.ServiceNotFound";
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-4xxx/4021/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var _ts_decorate = require("@swc/helpers/_/_ts_decorate");
var N;
(function(N) {
class C1 {
}
Expand All @@ -12,3 +11,4 @@ class C2 {
C2 = _ts_decorate._([
foo
], C2);
var N;
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export var getErrorForTransaction = /*#__PURE__*/ function() {
return _ref.apply(this, arguments);
};
}();
export var SequenceType;
(function(SequenceType) {
export var SequenceType = /*#__PURE__*/ function(SequenceType) {
SequenceType[SequenceType["Sequential"] = 0] = "Sequential";
SequenceType[SequenceType["Parallel"] = 1] = "Parallel";
SequenceType[SequenceType["StopOnFailure"] = 2] = "StopOnFailure";
})(SequenceType || (SequenceType = {}));
return SequenceType;
}({});
export function sendTransactionsWithManualRetry(connection, wallet, instructions, signers) {
return _sendTransactionsWithManualRetry.apply(this, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var foo;
(function(foo) {
foo.bar = 0;
})(foo || (foo = {}));
export { foo };
var foo;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-4xxx/4897/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ define([
function boo() {
return Foo.Bar * 2;
}
var Foo;
(function(Foo) {
Foo.Bar = 1234;
})(Foo || (Foo = {}));
var Foo;
});
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-6xxx/6791/1/output/input.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var o;
import { test as r } from "test";
(o || (o = {})).Hello = "World!", r("World!");
import { test as o } from "test";
var r, l = ((r = l || {}).Hello = "World!", r);
o("World!");
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-7xxx/7218/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export var Test;
(function(Test) {
export var Test = /*#__PURE__*/ function(Test) {
Test[Test["A"] = 1073741824] = "A";
Test[Test["B"] = -2147483648] = "B";
Test[Test["C"] = -1073741824] = "C";
})(Test || (Test = {}));
return Test;
}({});
var a = 1 << 30; // 1073741824
var b = 1 << 31; // -2147483648
var c = 1 << 30 | 1 << 31; // -1073741824
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-7xxx/7681/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var Foo;
(function(Foo) {
var Foo = /*#__PURE__*/ function(Foo) {
Foo[Foo["a"] = 0] = "a";
Foo[Foo["b"] = 1] = "b";
Foo[Foo["c"] = 0] = "c";
Foo[Foo["d"] = 0] = "d";
Foo[Foo["e"] = 1] = "e";
Foo[Foo["f"] = 2] = "f";
})(Foo || (Foo = {}));
return Foo;
}(Foo || {});
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-8xxx/8204/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Bar;
(function(Bar) {
var Bar = /*#__PURE__*/ function(Bar) {
Bar[Bar["VALUE"] = 1] = "VALUE";
})(Bar || (Bar = {}));
return Bar;
}(Bar || {});
export default Bar;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8551/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ _computedKey = Symbol.iterator;
class C {
[_computedKey]() {}
}
var NS;
(function(NS) {
function f() {}
NS.f = f;
})(NS || (NS = {}));
var NS;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8594/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
export var FooNs;
(function(FooNs) {
FooNs.Shared = ()=>'I\'m shared component';
FooNs.Main = ()=>/*#__PURE__*/ React.createElement(FooNs.Shared, null);
})(FooNs || (FooNs = {}));
export var FooNs;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8640/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
export var Ns;
(function(Ns) {
Ns.Context = /*#__PURE__*/ React.createContext();
Ns.Component = ()=>/*#__PURE__*/ React.createElement(Ns.Context.Provider, null);
})(Ns || (Ns = {}));
export var Ns;
43 changes: 19 additions & 24 deletions crates/swc/tests/fixture/issues-9xxx/9531/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
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 = {}));
}
if (true) var A = /*#__PURE__*/ function(A) {
return A;
}(A || {});
do var B = /*#__PURE__*/ function(B) {
return B;
}(B || {});
while (false)
while(false)var C = /*#__PURE__*/ function(C) {
return C;
}(C || {});
;
for(; false;){
var D;
(function(D) {})(D || (D = {}));
}
for(; false;)var D = /*#__PURE__*/ function(D) {
return D;
}(D || {});
;
for(const a in {}){
var E;
(function(E) {})(E || (E = {}));
}
for(const a in {})var E = /*#__PURE__*/ function(E) {
return E;
}(E || {});
;
for (const a of []){
var F;
(function(F) {})(F || (F = {}));
}
for (const a of [])var F = /*#__PURE__*/ function(F) {
return F;
}(F || {});
;
14 changes: 7 additions & 7 deletions crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var Foo;
(function(Foo) {
var Foo = /*#__PURE__*/ function(Foo) {
Foo[Foo["hello"] = 42] = "hello";
})(Foo || (Foo = {}));
var Foo2;
(function(Foo2) {
return Foo;
}(Foo || {});
var Foo2 = /*#__PURE__*/ function(Foo2) {
Foo2["hello"] = "42";
})(Foo2 || (Foo2 = {}));
return Foo2;
}(Foo2 || {});
console.log(42, "42");
console.log("hello", "こんにちは", "안녕하세요", "你好");
var Hello;
;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Foo;
;
var x;
var ref;
ref = void 0, x = ref === void 0 ? 42 : ref;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var Foo;
(function(Foo) {
var Foo = /*#__PURE__*/ function(Foo) {
Foo[Foo["hello"] = 42] = "hello";
})(Foo || (Foo = {}));
var Foo2;
(function(Foo2) {
return Foo;
}(Foo || {});
var Foo2 = /*#__PURE__*/ function(Foo2) {
Foo2["hello"] = "42";
})(Foo2 || (Foo2 = {}));
var Bar;
var Bar2;
return Foo2;
}(Foo2 || {});
;
;
console.log(42, "42");
console.log(42, "42");
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var Foo;
(function(Foo) {
var Foo = /*#__PURE__*/ function(Foo) {
Foo[Foo["hello"] = 42] = "hello";
})(Foo || (Foo = {}));
var Foo2;
(function(Foo2) {
return Foo;
}(Foo || {});
var Foo2 = /*#__PURE__*/ function(Foo2) {
Foo2["hello"] = "42";
})(Foo2 || (Foo2 = {}));
var Bar;
var Bar2;
return Foo2;
}(Foo2 || {});
;
;
console.log(42, "42");
console.log(42, "42");
6 changes: 3 additions & 3 deletions crates/swc/tests/ts-isolated-declaration/simple/output/3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export var Foo;
(function(Foo) {
export var Foo = /*#__PURE__*/ function(Foo) {
Foo["A"] = "foo";
Foo["B"] = "bar";
})(Foo || (Foo = {}));
return Foo;
}({});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [module.d.ts]
//// [classPoint.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A;
(function(A) {
var Point = function Point(x, y) {
"use strict";
Expand All @@ -11,6 +10,7 @@ var A;
};
A.Point = Point;
})(A || (A = {}));
var A;
//// [test.ts]
var p;
var p = A.Point.Origin;
Expand Down
Loading

0 comments on commit 2480bb0

Please sign in to comment.