diff --git a/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js index f460cbf43846..903d446b4600 100644 --- a/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js +++ b/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js @@ -1,12 +1,12 @@ import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function baz() { - return bar; -} try { var _usingCtx = _using_ctx(); var foo = _usingCtx.u(null); const bar = 1; console.log(baz()); + function baz() { + return bar; + } } catch (_) { _usingCtx.e = _; } finally{ diff --git a/crates/swc/tests/fixture/issues-8xxx/8895/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8895/input/.swcrc new file mode 100644 index 000000000000..dcc3f09edb78 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8895/input/.swcrc @@ -0,0 +1,20 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript", + "jsx": false, + "explicitResourceManagement": true + }, + "target": "es2022", + "loose": false, + "minify": { + "compress": false, + "mangle": false + } + }, + "module": { + "type": "commonjs" + }, + "minify": false, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8895/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8895/input/1.js new file mode 100644 index 000000000000..27128f641108 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8895/input/1.js @@ -0,0 +1,16 @@ +class File { + read() { + return 'content'; + } + [Symbol.dispose]() { + console.log(`closing the file ...`); + } +} +function main() { + using file = new File(); + function readFile() { + file.read(); + // ... + } + readFile(); +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8895/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8895/output/1.js new file mode 100644 index 000000000000..fa627b77a735 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8895/output/1.js @@ -0,0 +1,27 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +const _using_ctx = require("@swc/helpers/_/_using_ctx"); +class File { + read() { + return 'content'; + } + [Symbol.dispose]() { + console.log(`closing the file ...`); + } +} +function main() { + try { + var _usingCtx = _using_ctx._(); + const file = _usingCtx.u(new File()); + function readFile() { + file.read(); + } + readFile(); + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } +} diff --git a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2015).1.normal.js index 7ced32f4f868..d35d1106376d 100644 --- a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2015).1.normal.js @@ -3,49 +3,6 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; import { _ as _await_async_generator } from "@swc/helpers/_/_await_async_generator"; import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function af() { - return _af.apply(this, arguments); -} -function _af() { - _af = _async_to_generator(function*() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.a({ - [Symbol.asyncDispose] () { - return _async_to_generator(function*() {})(); - } - }); - yield null; - } catch (_) { - _usingCtx.e = _; - } finally{ - yield _usingCtx.d(); - } - }); - return _af.apply(this, arguments); -} -function ag() { - return _ag.apply(this, arguments); -} -function _ag() { - _ag = _wrap_async_generator(function*() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.a({ - [Symbol.asyncDispose] () { - return _async_to_generator(function*() {})(); - } - }); - yield; - yield _await_async_generator(null); - } catch (_) { - _usingCtx.e = _; - } finally{ - yield _await_async_generator(_usingCtx.d()); - } - }); - return _ag.apply(this, arguments); -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.a({ @@ -53,6 +10,49 @@ try { return _async_to_generator(function*() {})(); } }); + function af() { + return _af.apply(this, arguments); + } + function _af() { + _af = _async_to_generator(function*() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.a({ + [Symbol.asyncDispose] () { + return _async_to_generator(function*() {})(); + } + }); + yield null; + } catch (_) { + _usingCtx.e = _; + } finally{ + yield _usingCtx.d(); + } + }); + return _af.apply(this, arguments); + } + function ag() { + return _ag.apply(this, arguments); + } + function _ag() { + _ag = _wrap_async_generator(function*() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.a({ + [Symbol.asyncDispose] () { + return _async_to_generator(function*() {})(); + } + }); + yield; + yield _await_async_generator(null); + } catch (_) { + _usingCtx.e = _; + } finally{ + yield _await_async_generator(_usingCtx.d()); + } + }); + return _ag.apply(this, arguments); + } const a = function() { var _ref = _async_to_generator(function*() { try { diff --git a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2017).1.normal.js b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2017).1.normal.js index 5cb90efe0440..4ff13560ef08 100644 --- a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2017).1.normal.js +++ b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2017).1.normal.js @@ -1,37 +1,37 @@ //// [awaitUsingDeclarations.1.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -async function af() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.a({ - async [Symbol.asyncDispose] () {} - }); - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} -async function* ag() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.a({ - async [Symbol.asyncDispose] () {} - }); - yield; - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.a({ async [Symbol.asyncDispose] () {} }); + async function af() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.a({ + async [Symbol.asyncDispose] () {} + }); + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } + async function* ag() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.a({ + async [Symbol.asyncDispose] () {} + }); + yield; + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } const a = async ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2022).1.normal.js b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2022).1.normal.js index 5f28d335d27f..f4fb101df113 100644 --- a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2022).1.normal.js +++ b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es2022).1.normal.js @@ -1,37 +1,37 @@ //// [awaitUsingDeclarations.1.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -async function af() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.a({ - async [Symbol.asyncDispose] () {} - }); - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} -async function* ag() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.a({ - async [Symbol.asyncDispose] () {} - }); - yield; - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.a({ async [Symbol.asyncDispose] () {} }); + async function af() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.a({ + async [Symbol.asyncDispose] () {} + }); + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } + async function* ag() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.a({ + async [Symbol.asyncDispose] () {} + }); + yield; + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } const a = async ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es5).1.normal.js b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es5).1.normal.js index d00226235d98..ebf8514584d5 100644 --- a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es5).1.normal.js +++ b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=es5).1.normal.js @@ -6,134 +6,13 @@ import { _ as _define_property } from "@swc/helpers/_/_define_property"; import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator"; import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function af() { - return _af.apply(this, arguments); -} -function _af() { - _af = _async_to_generator(function() { - var _usingCtx, d3, _; - return _ts_generator(this, function(_state) { - switch(_state.label){ - case 0: - _state.trys.push([ - 0, - 2, - 3, - 5 - ]); - _usingCtx = _using_ctx(); - d3 = _usingCtx.a(_define_property({}, Symbol.asyncDispose, function() { - return _async_to_generator(function() { - return _ts_generator(this, function(_state) { - return [ - 2 - ]; - }); - })(); - })); - return [ - 4, - null - ]; - case 1: - _state.sent(); - return [ - 3, - 5 - ]; - case 2: - _ = _state.sent(); - _usingCtx.e = _; - return [ - 3, - 5 - ]; - case 3: - return [ - 4, - _usingCtx.d() - ]; - case 4: - _state.sent(); - return [ - 7 - ]; - case 5: - return [ - 2 - ]; - } - }); - }); - return _af.apply(this, arguments); -} -function ag() { - return _ag.apply(this, arguments); -} -function _ag() { - _ag = _wrap_async_generator(function() { - var _usingCtx, d5, _; - return _ts_generator(this, function(_state) { - switch(_state.label){ - case 0: - _state.trys.push([ - 0, - 3, - 4, - 6 - ]); - _usingCtx = _using_ctx(); - d5 = _usingCtx.a(_define_property({}, Symbol.asyncDispose, function() { - return _async_to_generator(function() { - return _ts_generator(this, function(_state) { - return [ - 2 - ]; - }); - })(); - })); - return [ - 4 - ]; - case 1: - _state.sent(); - return [ - 4, - _await_async_generator(null) - ]; - case 2: - _state.sent(); - return [ - 3, - 6 - ]; - case 3: - _ = _state.sent(); - _usingCtx.e = _; - return [ - 3, - 6 - ]; - case 4: - return [ - 4, - _await_async_generator(_usingCtx.d()) - ]; - case 5: - _state.sent(); - return [ - 7 - ]; - case 6: - return [ - 2 - ]; - } - }); - }); - return _ag.apply(this, arguments); -} try { + var af = function af() { + return _af.apply(this, arguments); + }; + var ag = function ag() { + return _ag.apply(this, arguments); + }; var _usingCtx = _using_ctx(); var d1 = _usingCtx.a(_define_property({}, Symbol.asyncDispose, function() { return _async_to_generator(function() { @@ -144,6 +23,127 @@ try { }); })(); })); + function _af() { + _af = _async_to_generator(function() { + var _usingCtx, d3, _; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + _state.trys.push([ + 0, + 2, + 3, + 5 + ]); + _usingCtx = _using_ctx(); + d3 = _usingCtx.a(_define_property({}, Symbol.asyncDispose, function() { + return _async_to_generator(function() { + return _ts_generator(this, function(_state) { + return [ + 2 + ]; + }); + })(); + })); + return [ + 4, + null + ]; + case 1: + _state.sent(); + return [ + 3, + 5 + ]; + case 2: + _ = _state.sent(); + _usingCtx.e = _; + return [ + 3, + 5 + ]; + case 3: + return [ + 4, + _usingCtx.d() + ]; + case 4: + _state.sent(); + return [ + 7 + ]; + case 5: + return [ + 2 + ]; + } + }); + }); + return _af.apply(this, arguments); + } + function _ag() { + _ag = _wrap_async_generator(function() { + var _usingCtx, d5, _; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + _state.trys.push([ + 0, + 3, + 4, + 6 + ]); + _usingCtx = _using_ctx(); + d5 = _usingCtx.a(_define_property({}, Symbol.asyncDispose, function() { + return _async_to_generator(function() { + return _ts_generator(this, function(_state) { + return [ + 2 + ]; + }); + })(); + })); + return [ + 4 + ]; + case 1: + _state.sent(); + return [ + 4, + _await_async_generator(null) + ]; + case 2: + _state.sent(); + return [ + 3, + 6 + ]; + case 3: + _ = _state.sent(); + _usingCtx.e = _; + return [ + 3, + 6 + ]; + case 4: + return [ + 4, + _await_async_generator(_usingCtx.d()) + ]; + case 5: + _state.sent(); + return [ + 7 + ]; + case 6: + return [ + 2 + ]; + } + }); + }); + return _ag.apply(this, arguments); + } var a = function() { var _ref = _async_to_generator(function() { var _usingCtx, d6, _; diff --git a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=esnext).1.normal.js b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=esnext).1.normal.js index 5f28d335d27f..f4fb101df113 100644 --- a/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=esnext).1.normal.js +++ b/crates/swc/tests/tsc-references/awaitUsingDeclarations.1(target=esnext).1.normal.js @@ -1,37 +1,37 @@ //// [awaitUsingDeclarations.1.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -async function af() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.a({ - async [Symbol.asyncDispose] () {} - }); - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} -async function* ag() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.a({ - async [Symbol.asyncDispose] () {} - }); - yield; - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.a({ async [Symbol.asyncDispose] () {} }); + async function af() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.a({ + async [Symbol.asyncDispose] () {} + }); + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } + async function* ag() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.a({ + async [Symbol.asyncDispose] () {} + }); + yield; + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } const a = async ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/awaitUsingDeclarations.13.1.normal.js b/crates/swc/tests/tsc-references/awaitUsingDeclarations.13.1.normal.js index 8c3319033d6b..5c4f259e5fe0 100644 --- a/crates/swc/tests/tsc-references/awaitUsingDeclarations.13.1.normal.js +++ b/crates/swc/tests/tsc-references/awaitUsingDeclarations.13.1.normal.js @@ -1,18 +1,18 @@ //// [awaitUsingDeclarations.13.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function f() { - try { - var _usingCtx = _using_ctx(); - const x = _usingCtx.a(null); - } catch (_) { - _usingCtx.e = _; - } finally{ - await _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var x = _usingCtx.a(null); + function f() { + try { + var _usingCtx = _using_ctx(); + const x = _usingCtx.a(null); + } catch (_) { + _usingCtx.e = _; + } finally{ + await _usingCtx.d(); + } + } } catch (_) { _usingCtx.e = _; } finally{ diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2015).1.normal.js index e62ebe0f866e..a864e97ccdb1 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2015).1.normal.js @@ -3,75 +3,75 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; import { _ as _await_async_generator } from "@swc/helpers/_/_await_async_generator"; import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function f() { - try { - var _usingCtx = _using_ctx(); - const d2 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -function af() { - return _af.apply(this, arguments); -} -function _af() { - _af = _async_to_generator(function*() { +try { + var _usingCtx = _using_ctx(); + var d1 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + function f() { try { var _usingCtx = _using_ctx(); - const d3 = _usingCtx.u({ + const d2 = _usingCtx.u({ [Symbol.dispose] () {} }); - yield null; } catch (_) { _usingCtx.e = _; } finally{ _usingCtx.d(); } - }); - return _af.apply(this, arguments); -} -function* g() { - try { - var _usingCtx = _using_ctx(); - const d4 = _usingCtx.u({ - [Symbol.dispose] () {} + } + function af() { + return _af.apply(this, arguments); + } + function _af() { + _af = _async_to_generator(function*() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } }); - yield; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); + return _af.apply(this, arguments); } -} -function ag() { - return _ag.apply(this, arguments); -} -function _ag() { - _ag = _wrap_async_generator(function*() { + function* g() { try { var _usingCtx = _using_ctx(); - const d5 = _usingCtx.u({ + const d4 = _usingCtx.u({ [Symbol.dispose] () {} }); yield; - yield _await_async_generator(null); } catch (_) { _usingCtx.e = _; } finally{ _usingCtx.d(); } - }); - return _ag.apply(this, arguments); -} -try { - var _usingCtx = _using_ctx(); - var d1 = _usingCtx.u({ - [Symbol.dispose] () {} - }); + } + function ag() { + return _ag.apply(this, arguments); + } + function _ag() { + _ag = _wrap_async_generator(function*() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + yield _await_async_generator(null); + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + }); + return _ag.apply(this, arguments); + } const a = ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2017).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2017).1.normal.js index 35dcd9bcd498..9ea76e46367c 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2017).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2017).1.normal.js @@ -1,62 +1,62 @@ //// [usingDeclarations.1.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function f() { - try { - var _usingCtx = _using_ctx(); - const d2 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -async function af() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -function* g() { - try { - var _usingCtx = _using_ctx(); - const d4 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - yield; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -async function* ag() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - yield; - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.u({ [Symbol.dispose] () {} }); + function f() { + try { + var _usingCtx = _using_ctx(); + const d2 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + async function af() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + function* g() { + try { + var _usingCtx = _using_ctx(); + const d4 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + async function* ag() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } const a = ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2022).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2022).1.normal.js index 02cff72474a0..ebd4bb752b3b 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2022).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es2022).1.normal.js @@ -1,62 +1,62 @@ //// [usingDeclarations.1.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function f() { - try { - var _usingCtx = _using_ctx(); - const d2 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -async function af() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -function* g() { - try { - var _usingCtx = _using_ctx(); - const d4 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - yield; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -async function* ag() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - yield; - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.u({ [Symbol.dispose] () {} }); + function f() { + try { + var _usingCtx = _using_ctx(); + const d2 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + async function af() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + function* g() { + try { + var _usingCtx = _using_ctx(); + const d4 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + async function* ag() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } const a = ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js index bfd9eb040547..2ffd41628d86 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js @@ -10,22 +10,22 @@ import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator import { _ as _create_super } from "@swc/helpers/_/_create_super"; import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function f() { - try { - var _usingCtx = _using_ctx(); - var d2 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -function af() { - return _af.apply(this, arguments); -} -function _af() { - _af = _async_to_generator(function() { - var _usingCtx, d3, _; +try { + var f = function f() { + try { + var _usingCtx = _using_ctx(); + var d2 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + }; + var af = function af() { + return _af.apply(this, arguments); + }; + var g = function g() { + var _usingCtx, d4, _; return _ts_generator(this, function(_state) { switch(_state.label){ case 0: @@ -36,10 +36,9 @@ function _af() { 4 ]); _usingCtx = _using_ctx(); - d3 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); + d4 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); return [ - 4, - null + 4 ]; case 1: _state.sent(); @@ -65,106 +64,107 @@ function _af() { ]; } }); - }); - return _af.apply(this, arguments); -} -function g() { - var _usingCtx, d4, _; - return _ts_generator(this, function(_state) { - switch(_state.label){ - case 0: - _state.trys.push([ - 0, - 2, - 3, - 4 - ]); - _usingCtx = _using_ctx(); - d4 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); - return [ - 4 - ]; - case 1: - _state.sent(); - return [ - 3, - 4 - ]; - case 2: - _ = _state.sent(); - _usingCtx.e = _; - return [ - 3, - 4 - ]; - case 3: - _usingCtx.d(); - return [ - 7 - ]; - case 4: - return [ - 2 - ]; - } - }); -} -function ag() { - return _ag.apply(this, arguments); -} -function _ag() { - _ag = _wrap_async_generator(function() { - var _usingCtx, d5, _; - return _ts_generator(this, function(_state) { - switch(_state.label){ - case 0: - _state.trys.push([ - 0, - 3, - 4, - 5 - ]); - _usingCtx = _using_ctx(); - d5 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); - return [ - 4 - ]; - case 1: - _state.sent(); - return [ - 4, - _await_async_generator(null) - ]; - case 2: - _state.sent(); - return [ - 3, - 5 - ]; - case 3: - _ = _state.sent(); - _usingCtx.e = _; - return [ - 3, - 5 - ]; - case 4: - _usingCtx.d(); - return [ - 7 - ]; - case 5: - return [ - 2 - ]; - } - }); - }); - return _ag.apply(this, arguments); -} -try { + }; + var ag = function ag() { + return _ag.apply(this, arguments); + }; var _usingCtx = _using_ctx(); var d1 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); + function _af() { + _af = _async_to_generator(function() { + var _usingCtx, d3, _; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + _state.trys.push([ + 0, + 2, + 3, + 4 + ]); + _usingCtx = _using_ctx(); + d3 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); + return [ + 4, + null + ]; + case 1: + _state.sent(); + return [ + 3, + 4 + ]; + case 2: + _ = _state.sent(); + _usingCtx.e = _; + return [ + 3, + 4 + ]; + case 3: + _usingCtx.d(); + return [ + 7 + ]; + case 4: + return [ + 2 + ]; + } + }); + }); + return _af.apply(this, arguments); + } + function _ag() { + _ag = _wrap_async_generator(function() { + var _usingCtx, d5, _; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + _state.trys.push([ + 0, + 3, + 4, + 5 + ]); + _usingCtx = _using_ctx(); + d5 = _usingCtx.u(_define_property({}, Symbol.dispose, function() {})); + return [ + 4 + ]; + case 1: + _state.sent(); + return [ + 4, + _await_async_generator(null) + ]; + case 2: + _state.sent(); + return [ + 3, + 5 + ]; + case 3: + _ = _state.sent(); + _usingCtx.e = _; + return [ + 3, + 5 + ]; + case 4: + _usingCtx.d(); + return [ + 7 + ]; + case 5: + return [ + 2 + ]; + } + }); + }); + return _ag.apply(this, arguments); + } var a = function() { try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=esnext).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=esnext).1.normal.js index 02cff72474a0..ebd4bb752b3b 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=esnext).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=esnext).1.normal.js @@ -1,62 +1,62 @@ //// [usingDeclarations.1.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; -function f() { - try { - var _usingCtx = _using_ctx(); - const d2 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -async function af() { - try { - var _usingCtx = _using_ctx(); - const d3 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -function* g() { - try { - var _usingCtx = _using_ctx(); - const d4 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - yield; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} -async function* ag() { - try { - var _usingCtx = _using_ctx(); - const d5 = _usingCtx.u({ - [Symbol.dispose] () {} - }); - yield; - await null; - } catch (_) { - _usingCtx.e = _; - } finally{ - _usingCtx.d(); - } -} try { var _usingCtx = _using_ctx(); var d1 = _usingCtx.u({ [Symbol.dispose] () {} }); + function f() { + try { + var _usingCtx = _using_ctx(); + const d2 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + async function af() { + try { + var _usingCtx = _using_ctx(); + const d3 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + function* g() { + try { + var _usingCtx = _using_ctx(); + const d4 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } + async function* ag() { + try { + var _usingCtx = _using_ctx(); + const d5 = _usingCtx.u({ + [Symbol.dispose] () {} + }); + yield; + await null; + } catch (_) { + _usingCtx.e = _; + } finally{ + _usingCtx.d(); + } + } const a = ()=>{ try { var _usingCtx = _using_ctx(); diff --git a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=amd).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=amd).1.normal.js index 29254e094e4b..999774e3ca9b 100644 --- a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=amd).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=amd).1.normal.js @@ -14,9 +14,6 @@ define([ return y; } }); - function f() { - console.log(y, z); - } try { var _usingCtx = _using_ctx._(); var z = _usingCtx.u({ @@ -25,6 +22,9 @@ define([ if (false) { var y = 1; } + function f() { + console.log(y, z); + } } catch (_) { _usingCtx.e = _; } finally{ diff --git a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=commonjs).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=commonjs).1.normal.js index e0c7677ce190..947f97c17a2c 100644 --- a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=commonjs).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=commonjs).1.normal.js @@ -10,9 +10,6 @@ Object.defineProperty(exports, "y", { } }); const _using_ctx = require("@swc/helpers/_/_using_ctx"); -function f() { - console.log(y, z); -} try { var _usingCtx = _using_ctx._(); var z = _usingCtx.u({ @@ -21,6 +18,9 @@ try { if (false) { var y = 1; } + function f() { + console.log(y, z); + } } catch (_) { _usingCtx.e = _; } finally{ diff --git a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=es6).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=es6).1.normal.js index f89557661729..26971dbd5b13 100644 --- a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=es6).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=es6).1.normal.js @@ -1,9 +1,6 @@ //// [usingDeclarationsTopLevelOfModule.3.ts] import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; export { y }; -function f() { - console.log(y, z); -} try { var _usingCtx = _using_ctx(); var z = _usingCtx.u({ @@ -12,6 +9,9 @@ try { if (false) { var y = 1; } + function f() { + console.log(y, z); + } } catch (_) { _usingCtx.e = _; } finally{ diff --git a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=system).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=system).1.normal.js index e0d29f964924..6020805d8211 100644 --- a/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=system).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarationsTopLevelOfModule.3(module=system).1.normal.js @@ -4,9 +4,6 @@ System.register([ ], function(_export, _context) { "use strict"; var _using_ctx; - function f() { - console.log(y, z); - } return { setters: [ function(_using_ctx1) { @@ -22,6 +19,9 @@ System.register([ if (false) { var y = 1; } + function f() { + console.log(y, z); + } } catch (_) { _usingCtx.e = _; } finally{ diff --git a/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs b/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs index fd51c2aa1e82..6c64c6178c0a 100644 --- a/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs +++ b/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs @@ -85,7 +85,7 @@ impl ExplicitResourceManagement { for stmt in stmts.take() { match stmt.try_into_stmt() { Ok(stmt @ Stmt::Decl(Decl::Fn(..))) => { - new.push(T::from_stmt(stmt)); + try_body.push(stmt); } Ok(Stmt::Decl(Decl::Var(var))) => { // var.kind = VarDeclKind::Var; diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs index 022a0f510e9d..e09f3d2ca214 100644 --- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs +++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs @@ -1,20 +1,20 @@ import { doSomething } from "somewhere"; export * from "somewhere else"; export * as ns from "somewhere else"; -function f() { - a; - B; -} -function h() { - b; - A; -} var _g; export { f }; var _b; var _B; try { var _usingCtx = _using_ctx(); + function f() { + a; + B; + } + function h() { + b; + A; + } function g() { c; }