Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(es/typescript): Reimplement TypeScript pass #7202

Merged
merged 11 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions bindings/binding_core_wasm/__tests__/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ describe("parse", () => {
"optional": false,
"span": Object {
"ctxt": 2,
"end": 394,
"start": 391,
"end": 289,
"start": 286,
},
"type": "Identifier",
"value": "Foo",
Expand All @@ -119,8 +119,8 @@ describe("parse", () => {
"isAbstract": false,
"span": Object {
"ctxt": 0,
"end": 397,
"start": 385,
"end": 292,
"start": 280,
},
"superClass": null,
"superTypeParams": null,
Expand All @@ -131,8 +131,8 @@ describe("parse", () => {
"interpreter": null,
"span": Object {
"ctxt": 0,
"end": 397,
"start": 385,
"end": 292,
"start": 280,
},
"type": "Module",
}
Expand All @@ -156,8 +156,8 @@ describe("parse", () => {
"optional": false,
"span": Object {
"ctxt": 2,
"end": 407,
"start": 404,
"end": 302,
"start": 299,
},
"type": "Identifier",
"value": "Foo",
Expand All @@ -166,8 +166,8 @@ describe("parse", () => {
"isAbstract": false,
"span": Object {
"ctxt": 0,
"end": 410,
"start": 398,
"end": 305,
"start": 293,
},
"superClass": null,
"superTypeParams": null,
Expand All @@ -178,8 +178,8 @@ describe("parse", () => {
"interpreter": null,
"span": Object {
"ctxt": 0,
"end": 410,
"start": 398,
"end": 305,
"start": 293,
},
"type": "Module",
}
Expand Down
25 changes: 15 additions & 10 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use swc_ecma_transforms::{
},
react::{self, default_pragma, default_pragma_frag},
resolver,
typescript::{self, TsEnumConfig, TsImportExportAssignConfig},
typescript::{self, TsImportExportAssignConfig},
Assumptions,
};
use swc_ecma_transforms_compat::es2015::regenerator;
Expand Down Expand Up @@ -618,6 +618,8 @@ impl Options {
_ => TsImportExportAssignConfig::Classic,
};

let verbatim_module_syntax = transform.verbatim_module_syntax.into_bool();

let charset = cfg.jsc.output.charset.or_else(|| {
if js_minify.as_ref()?.format.ascii_only {
Some(OutputCharset::Ascii)
Expand Down Expand Up @@ -803,9 +805,14 @@ impl Options {
// keep_import_assertions is false.
Optional::new(import_assertions(), !keep_import_attributes),
Optional::new(
typescript::strip_with_jsx::<Option<&dyn Comments>>(
typescript::tsx::<Option<&dyn Comments>>(
cm.clone(),
typescript::Config {
import_export_assign_config,
verbatim_module_syntax,
..Default::default()
},
typescript::TsxConfig {
pragma: Some(
transform
.react
Expand All @@ -820,14 +827,6 @@ impl Options {
.clone()
.unwrap_or_else(default_pragma_frag)
),
ts_enum_config: TsEnumConfig {
treat_const_enum_as_enum: transform
.treat_const_enum_as_enum
.into_bool(),
ts_enum_is_readonly: assumptions.ts_enum_is_readonly,
},
import_export_assign_config,
..Default::default()
},
comments.map(|v| v as _),
top_level_mark
Expand Down Expand Up @@ -1721,11 +1720,17 @@ pub struct TransformConfig {
pub regenerator: regenerator::Config,

#[serde(default)]
#[deprecated]
pub treat_const_enum_as_enum: BoolConfig<false>,

/// https://www.typescriptlang.org/tsconfig#useDefineForClassFields
#[serde(default)]
pub use_define_for_class_fields: BoolConfig<true>,

/// https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax
#[serde(default)]
pub verbatim_module_syntax: BoolConfig<false>,

#[serde(default)]
pub decorator_version: Option<DecoratorVersion>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import homura = require("homura");

export import madoka = require("madoka");



// avoid unused import be stripped
export { homura };
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
2 |
3 | export import madoka = require("madoka");
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 |
5 |
`----
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ServiceError extends Error {
_define_property._(this, "name", "ServiceError.BadResponse");
}
}
(function(ServiceError1) {
(function(ServiceError) {
let Code;
(function(Code) {
Code[Code["serviceNotFound"] = 404] = "serviceNotFound";
Expand All @@ -26,7 +26,7 @@ class ServiceError extends Error {
Code[Code["timedOut"] = 504] = "timedOut";
Code[Code["badRequest"] = 400] = "badRequest";
Code[Code["badResponse"] = 422] = "badResponse";
})(Code = ServiceError1.Code || (ServiceError1.Code = {}));
})(Code = ServiceError.Code || (ServiceError.Code = {}));
class ServiceNotFound extends ServiceError {
constructor(...args){
super(...args);
Expand All @@ -35,11 +35,11 @@ class ServiceError extends Error {
_define_property._(this, "name", "ServiceError.ServiceNotFound");
}
}
ServiceError1.ServiceNotFound = ServiceNotFound;
ServiceError.ServiceNotFound = ServiceNotFound;
function toMessageBody(error) {
return {
code: ServiceError.Code.implementation
};
}
ServiceError1.toMessageBody = toMessageBody;
ServiceError.toMessageBody = toMessageBody;
})(ServiceError || (ServiceError = {}));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": ";AAAA,IAAUA;;QACOC,MAAAA,IAAI;IACV,SAASC;QACZC,QAAQC,GAAG,CAACH;IAChB;MAFgBC,IAAAA;GAFVF,MAAAA",
"mappings": ";;UAAUA;IACC,MAAMC,MAAAA,IAAI;IACV,SAASC;QACZC,QAAQC,GAAG,CAACH;IAChB;MAFgBC,IAAAA;AAGpB,GALUF,MAAAA",
"names": [
"A",
"v",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
var A;
(function(A) {
var v = A.v = 25;
const v = A.v = 25;
function a() {
console.log(v);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-3xxx/3073/1/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var n;
(function(n) {
var ref;
ref = {
var a = (ref = {
a: 1
}, n.a = ref.a, ref;
}, n.a = ref.a, ref).a;
})(n || (n = {}));
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-3xxx/3073/2/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Foo;
(function(Foo) {
var ref;
ref = {
var _ref = (ref = {
a: 1,
b: 2
}, Foo.A = ref.a, Foo.B = ref.b, ref;
}, Foo.A = ref.a, Foo.B = ref.b, ref), A = _ref.a, B = _ref.b;
})(Foo || (Foo = {}));
8 changes: 4 additions & 4 deletions crates/swc/tests/fixture/issues-3xxx/3686/1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _ts_decorate._([
ServiceError = _ts_decorate._([
CD
], ServiceError);
(function(ServiceError1) {
(function(ServiceError) {
let Code;
(function(Code) {
Code[Code["serviceNotFound"] = 404] = "serviceNotFound";
Expand All @@ -31,17 +31,17 @@ ServiceError = _ts_decorate._([
Code[Code["timedOut"] = 504] = "timedOut";
Code[Code["badRequest"] = 400] = "badRequest";
Code[Code["badResponse"] = 422] = "badResponse";
})(Code = ServiceError1.Code || (ServiceError1.Code = {}));
})(Code = ServiceError.Code || (ServiceError.Code = {}));
class ServiceNotFound extends ServiceError {
// Service was probably not registered, or using the wrong channel
code = 404;
name = "ServiceError.ServiceNotFound";
}
ServiceError1.ServiceNotFound = ServiceNotFound;
ServiceError.ServiceNotFound = ServiceNotFound;
function toMessageBody(error) {
return {
code: ServiceError.Code.implementation
};
}
ServiceError1.toMessageBody = toMessageBody;
ServiceError.toMessageBody = toMessageBody;
})(ServiceError || (ServiceError = {}));
10 changes: 5 additions & 5 deletions crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function _sendTransactionsWithManualRetry() {
case 4:
return [
4,
sendTransactions(connection, wallet, instructions, filteredSigners, SequenceType.StopOnFailure, "single")
sendTransactions(connection, wallet, instructions, filteredSigners, 2, "single")
];
case 5:
txs = _state.sent().txs;
Expand Down Expand Up @@ -196,7 +196,7 @@ export var sendTransactions = function() {
connection: connection,
signedTransaction: signedTxns[i1]
});
if (!(sequenceType !== SequenceType.Parallel)) return [
if (!(sequenceType !== 1)) return [
3,
7
];
Expand Down Expand Up @@ -227,7 +227,7 @@ export var sendTransactions = function() {
console.log("Failed at txn index:", i1);
console.log("Caught failure:", e);
failCallback(signedTxns[i1], i1);
if (!(sequenceType === SequenceType.StopOnFailure)) return [
if (!(sequenceType === 2)) return [
3,
5
];
Expand Down Expand Up @@ -264,7 +264,7 @@ export var sendTransactions = function() {
}
});
};
sequenceType = _arguments.length > 4 && _arguments[4] !== void 0 ? _arguments[4] : SequenceType.Parallel, commitment = _arguments.length > 5 && _arguments[5] !== void 0 ? _arguments[5] : "singleGossip", successCallback = _arguments.length > 6 && _arguments[6] !== void 0 ? _arguments[6] : function(txid, ind) {}, failCallback = _arguments.length > 7 && _arguments[7] !== void 0 ? _arguments[7] : function(txid, ind) {
sequenceType = _arguments.length > 4 && _arguments[4] !== void 0 ? _arguments[4] : 1, commitment = _arguments.length > 5 && _arguments[5] !== void 0 ? _arguments[5] : "singleGossip", successCallback = _arguments.length > 6 && _arguments[6] !== void 0 ? _arguments[6] : function(txid, ind) {}, failCallback = _arguments.length > 7 && _arguments[7] !== void 0 ? _arguments[7] : function(txid, ind) {
return false;
}, block = _arguments.length > 8 ? _arguments[8] : void 0, beforeTransactions = _arguments.length > 9 && _arguments[9] !== void 0 ? _arguments[9] : [], afterTransactions = _arguments.length > 10 && _arguments[10] !== void 0 ? _arguments[10] : [];
if (!wallet.publicKey) throw new WalletNotConnectedError();
Expand Down Expand Up @@ -327,7 +327,7 @@ export var sendTransactions = function() {
4
];
case 7:
if (!(sequenceType !== SequenceType.Parallel)) return [
if (!(sequenceType !== 1)) return [
3,
9
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ define([
}
var Foo;
(function(Foo) {
var Bar = Foo.Bar = 1234;
const Bar = Foo.Bar = 1234;
})(Foo || (Foo = {}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Object.defineProperty(exports, "flakySuite", {
});
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
const _testUtils = /*#__PURE__*/ _interop_require_wildcard._(require("vs/base/test/common/testUtils"));
var flakySuite = _testUtils.flakySuite;
const flakySuite = _testUtils.flakySuite;
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 l;
import { test as o } from "test";
(l || (l = {})).Hello = "World!", o(l.Hello);
var o;
import { test as r } from "test";
(o || (o = {})).Hello = "World!", r("World!");
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7681/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum Foo {
a,
b = 1,
c = Foo["a"],
d = Foo.a,
e,
f,
}
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7681/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var Foo;
(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 = {}));
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7791/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es5"
}
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7791/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a?.b!.c ?? 0;
a?.b.c ?? 0;
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7791/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var _a, _a1;
var _a_b_c;
(_a_b_c = (_a = a) === null || _a === void 0 ? void 0 : _a.b.c) !== null && _a_b_c !== void 0 ? _a_b_c : 0;
var _a_b_c1;
(_a_b_c1 = (_a1 = a) === null || _a1 === void 0 ? void 0 : _a1.b.c) !== null && _a_b_c1 !== void 0 ? _a_b_c1 : 0;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading