Skip to content

Commit

Permalink
chore(es): Add helpers (#9586)
Browse files Browse the repository at this point in the history
**Description:**

This is a part of #9585. I extracted this as a separate PR to make WIP small.
  • Loading branch information
kdy1 authored Sep 25, 2024
1 parent 0a5a2d4 commit b94a0e1
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
});
2 changes: 2 additions & 0 deletions crates/swc_ecma_transforms_base/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: (),
Expand Down
1 change: 1 addition & 0 deletions packages/helpers/esm/_ts_add_disposable_resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { __addDisposableResource as _ } from "tslib";
1 change: 1 addition & 0 deletions packages/helpers/esm/_ts_dispose_resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { __disposeResources as _ } from "tslib";
2 changes: 2 additions & 0 deletions packages/helpers/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 9 additions & 1 deletion packages/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"zx": "^7.2.1"
},
"dependencies": {
"tslib": "^2.4.0"
"tslib": "^2.7.0"
},
"exports": {
"./package.json": "./package.json",
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b94a0e1

Please sign in to comment.