-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Description:** This is a part of #9585. I extracted this as a separate PR to make WIP small.
- Loading branch information
Showing
8 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
crates/swc_ecma_transforms_base/src/helpers/_ts_add_disposable_resource.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
32 changes: 32 additions & 0 deletions
32
crates/swc_ecma_transforms_base/src/helpers/_ts_dispose_resources.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { __addDisposableResource as _ } from "tslib"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { __disposeResources as _ } from "tslib"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters