chore(es): Add helpers (#9586)
Some checks failed
CI / Cargo fmt (push) Has been cancelled
CI / Cargo clippy (push) Has been cancelled
CI / Check license of dependencies (push) Has been cancelled
CI / Check (macos-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CI / Test wasm (binding_core_wasm) (push) Has been cancelled
CI / Test wasm (binding_minifier_wasm) (push) Has been cancelled
CI / Test wasm (binding_typescript_wasm) (push) Has been cancelled
CI / List crates (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Has been cancelled
CI / Test with @swc/cli (push) Has been cancelled
CI / Miri (better_scoped_tls) (push) Has been cancelled
CI / Miri (string_enum) (push) Has been cancelled
CI / Miri (swc) (push) Has been cancelled
CI / Miri (swc_bundler) (push) Has been cancelled
CI / Miri (swc_ecma_codegen) (push) Has been cancelled
CI / Miri (swc_ecma_minifier) (push) Has been cancelled
Benchmark / Bench everything (push) Has been cancelled
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Has been cancelled
CI / Done (push) Has been cancelled

**Description:**

This is a part of https://github.com/swc-project/swc/pull/9585. I extracted this as a separate PR to make WIP small.
This commit is contained in:
Donny/강동윤 2024-09-25 15:58:19 +09:00 committed by GitHub
parent 0a5a2d44cb
commit b94a0e1fd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 88 additions and 2 deletions

View File

@ -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;
}

View File

@ -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;
});

View File

@ -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: (),

View File

@ -0,0 +1 @@
export { __addDisposableResource as _ } from "tslib";

View File

@ -0,0 +1 @@
export { __disposeResources as _ } from "tslib";

View File

@ -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";

View File

@ -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"

View File

@ -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"