From 76960c650593a6a3da23f9605f00f73c69d8e06c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 13 Mar 2025 11:47:46 +0000 Subject: [PATCH] test(transformer/using): add failing test for scopes (#9701) Add a test case for explicit resource management (`using` declarations) which demonstrates a problem with scopes. See: https://github.com/oxc-project/oxc/pull/9310#discussion_r1988348266 --- .../snapshots/oxc.snap.md | 18 ++++++++++++++++-- .../function-with-scopes-in-params/input.js | 5 +++++ .../options.json | 3 +++ .../function-with-scopes-in-params/output.js | 11 +++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/input.js create mode 100644 tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/options.json create mode 100644 tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/output.js diff --git a/tasks/transform_conformance/snapshots/oxc.snap.md b/tasks/transform_conformance/snapshots/oxc.snap.md index 8a807f7e8baef..2a4ea0e809080 100644 --- a/tasks/transform_conformance/snapshots/oxc.snap.md +++ b/tasks/transform_conformance/snapshots/oxc.snap.md @@ -1,6 +1,6 @@ commit: 578ac4df -Passed: 138/226 +Passed: 138/227 # All Passed: * babel-plugin-transform-class-static-block @@ -320,7 +320,7 @@ x Output mismatch x Output mismatch -# babel-plugin-proposal-explicit-resource-management (0/2) +# babel-plugin-proposal-explicit-resource-management (0/3) * export-class-name/input.js x Output mismatch @@ -335,6 +335,20 @@ Scope parent mismatch: after transform: ScopeId(3): Some(ScopeId(1)) rebuilt : ScopeId(4): Some(ScopeId(3)) +* function-with-scopes-in-params/input.js +Scope children mismatch: +after transform: ScopeId(1): [ScopeId(5), ScopeId(6), ScopeId(8)] +rebuilt : ScopeId(1): [ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(6), ScopeId(8)] +Scope parent mismatch: +after transform: ScopeId(2): Some(ScopeId(5)) +rebuilt : ScopeId(2): Some(ScopeId(1)) +Scope parent mismatch: +after transform: ScopeId(3): Some(ScopeId(5)) +rebuilt : ScopeId(3): Some(ScopeId(1)) +Scope children mismatch: +after transform: ScopeId(5): [ScopeId(2), ScopeId(3), ScopeId(4)] +rebuilt : ScopeId(4): [ScopeId(5)] + # legacy-decorators (2/66) * oxc/metadata/bound-type-reference/input.ts diff --git a/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/input.js b/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/input.js new file mode 100644 index 0000000000000..95bfbc508e8cc --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/input.js @@ -0,0 +1,5 @@ +// The arrow functions in this test case are to make sure that scopes are re-parented correctly +function f(a = () => {}, b = () => {}) { + using x = a(), y = b(); + doSomethingWith(x, y, () => {}); +} diff --git a/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/options.json b/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/options.json new file mode 100644 index 0000000000000..2bc3ea3845be0 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-explicit-resource-management"] +} diff --git a/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/output.js b/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/output.js new file mode 100644 index 0000000000000..89942e2d8c519 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-proposal-explicit-resource-management/test/fixtures/function-with-scopes-in-params/output.js @@ -0,0 +1,11 @@ +function f(a = () => {}, b = () => {}) { + try { + var _usingCtx = babelHelpers.usingCtx(); + const x = _usingCtx.u(a()), y = _usingCtx.u(b()); + doSomethingWith(x, y, () => {}); + } catch (_) { + _usingCtx.e = _; + } finally { + _usingCtx.d(); + } +}