From 65c302532c782a3cfddb6ae3f522ede77b87a80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 25 Sep 2024 15:56:32 +0900 Subject: [PATCH] helper --- .../helpers/_ts_add_disposable_resource.js | 33 +++++++++++++++++++ .../src/helpers/_ts_dispose_resources.js | 32 ++++++++++++++++++ .../src/helpers/mod.rs | 2 ++ .../esm/_ts_add_disposable_resource.js | 1 + packages/helpers/esm/_ts_dispose_resources.js | 1 + packages/helpers/esm/index.js | 2 ++ packages/helpers/package.json | 10 +++++- yarn.lock | 9 ++++- 8 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 crates/swc_ecma_transforms_base/src/helpers/_ts_add_disposable_resource.js create mode 100644 crates/swc_ecma_transforms_base/src/helpers/_ts_dispose_resources.js create mode 100644 packages/helpers/esm/_ts_add_disposable_resource.js create mode 100644 packages/helpers/esm/_ts_dispose_resources.js diff --git a/crates/swc_ecma_transforms_base/src/helpers/_ts_add_disposable_resource.js b/crates/swc_ecma_transforms_base/src/helpers/_ts_add_disposable_resource.js new file mode 100644 index 000000000000..992a8587d9c9 --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_ts_add_disposable_resource.js @@ -0,0 +1,33 @@ +function _ts_add_disposable_resource(env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function () { + try { + inner.call(this); + } catch (e) { + return Promise.reject(e); + } + }; + env.stack.push({ + value: value, + dispose: dispose, + async: async + }); + } else if (async) { + env.stack.push({ + async: true + }); + } + return value; +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_base/src/helpers/_ts_dispose_resources.js b/crates/swc_ecma_transforms_base/src/helpers/_ts_dispose_resources.js new file mode 100644 index 000000000000..685b40e2985f --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_ts_dispose_resources.js @@ -0,0 +1,32 @@ +function _ts_dispose_resources(SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, + s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function (e) { + fail(e); + return next(); + }); + } else s |= 1; + } catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +} (typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); \ No newline at end of file diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 89b1b267a858..18fb5907dbe8 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -410,6 +410,8 @@ define_helpers!(Helpers { ts_metadata: (), ts_param: (), ts_values: (), + ts_add_disposable_resource: (), + ts_dispose_resources: (), apply_decs_2203_r: (), identity: (), diff --git a/packages/helpers/esm/_ts_add_disposable_resource.js b/packages/helpers/esm/_ts_add_disposable_resource.js new file mode 100644 index 000000000000..62fd1cb30bdc --- /dev/null +++ b/packages/helpers/esm/_ts_add_disposable_resource.js @@ -0,0 +1 @@ +export { __addDisposableResource as _ } from "tslib"; diff --git a/packages/helpers/esm/_ts_dispose_resources.js b/packages/helpers/esm/_ts_dispose_resources.js new file mode 100644 index 000000000000..5d06d4a5f87e --- /dev/null +++ b/packages/helpers/esm/_ts_dispose_resources.js @@ -0,0 +1 @@ +export { __disposeResources as _ } from "tslib"; diff --git a/packages/helpers/esm/index.js b/packages/helpers/esm/index.js index 522cc47b4633..fe511005bd78 100644 --- a/packages/helpers/esm/index.js +++ b/packages/helpers/esm/index.js @@ -89,7 +89,9 @@ export { _ as _to_array } from "./_to_array.js"; export { _ as _to_consumable_array } from "./_to_consumable_array.js"; export { _ as _to_primitive } from "./_to_primitive.js"; export { _ as _to_property_key } from "./_to_property_key.js"; +export { _ as _ts_add_disposable_resource } from "./_ts_add_disposable_resource.js"; export { _ as _ts_decorate } from "./_ts_decorate.js"; +export { _ as _ts_dispose_resources } from "./_ts_dispose_resources.js"; export { _ as _ts_generator } from "./_ts_generator.js"; export { _ as _ts_metadata } from "./_ts_metadata.js"; export { _ as _ts_param } from "./_ts_param.js"; diff --git a/packages/helpers/package.json b/packages/helpers/package.json index 637a6cbcccbe..bc7f7a9cf37c 100644 --- a/packages/helpers/package.json +++ b/packages/helpers/package.json @@ -36,7 +36,7 @@ "zx": "^7.2.1" }, "dependencies": { - "tslib": "^2.4.0" + "tslib": "^2.7.0" }, "exports": { "./package.json": "./package.json", @@ -403,10 +403,18 @@ "import": "./esm/_to_property_key.js", "default": "./cjs/_to_property_key.cjs" }, + "./_/_ts_add_disposable_resource": { + "import": "./esm/_ts_add_disposable_resource.js", + "default": "./cjs/_ts_add_disposable_resource.cjs" + }, "./_/_ts_decorate": { "import": "./esm/_ts_decorate.js", "default": "./cjs/_ts_decorate.cjs" }, + "./_/_ts_dispose_resources": { + "import": "./esm/_ts_dispose_resources.js", + "default": "./cjs/_ts_dispose_resources.cjs" + }, "./_/_ts_generator": { "import": "./esm/_ts_generator.js", "default": "./cjs/_ts_generator.cjs" diff --git a/yarn.lock b/yarn.lock index 3ea818274ea3..dacc1b37e32b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4639,7 +4639,7 @@ __metadata: "@ast-grep/napi": "npm:^0.3.1" dprint: "npm:^0.35.3" magic-string: "npm:^0.30.0" - tslib: "npm:^2.4.0" + tslib: "npm:^2.7.0" zx: "npm:^7.2.1" languageName: unknown linkType: soft @@ -16822,6 +16822,13 @@ __metadata: languageName: node linkType: hard +"tslib@npm:^2.7.0": + version: 2.7.0 + resolution: "tslib@npm:2.7.0" + checksum: 9a5b47ddac65874fa011c20ff76db69f97cf90c78cff5934799ab8894a5342db2d17b4e7613a087046bc1d133d21547ddff87ac558abeec31ffa929c88b7fce6 + languageName: node + linkType: hard + "tsx@npm:^3.13.0": version: 3.14.0 resolution: "tsx@npm:3.14.0"