diff --git a/packages/babel-plugin-proposal-explicit-resource-management/src/index.ts b/packages/babel-plugin-proposal-explicit-resource-management/src/index.ts index 564cdff06eff..e561540e1edb 100644 --- a/packages/babel-plugin-proposal-explicit-resource-management/src/index.ts +++ b/packages/babel-plugin-proposal-explicit-resource-management/src/index.ts @@ -37,7 +37,10 @@ export default declare(api => { ]), ); }, - BlockStatement(path, state) { + "BlockStatement|StaticBlock"( + path: NodePath, + state, + ) { let stackId: t.Identifier | null = null; let needsAwait = false; @@ -97,10 +100,11 @@ export default declare(api => { if ( parentPath.isFunction() || parentPath.isTryStatement() || - parentPath.isCatchClause() || - parentPath.isStaticBlock() + parentPath.isCatchClause() ) { path.replaceWith(t.blockStatement([replacement])); + } else if (path.isStaticBlock()) { + path.node.body = [replacement]; } else { path.replaceWith(replacement); } diff --git a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/input.js b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/input.js index 326085daa898..0ed05eeaeae2 100644 --- a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/input.js +++ b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/input.js @@ -1,4 +1,4 @@ -if (test) { +function fn() { using x = obj; - doSomethingWith(x); + return doSomethingWith(x); } diff --git a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/output.js b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/output.js index 1893ea294ba5..70932ce58965 100644 --- a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/output.js +++ b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/output.js @@ -1,10 +1,12 @@ -if (test) try { - var _stack = []; - const x = babelHelpers.using(_stack, obj); - doSomethingWith(x); -} catch (_) { - var _error = _; - var _hasError = true; -} finally { - babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== "undefined" && SuppressedError); +function fn() { + try { + var _stack = []; + const x = babelHelpers.using(_stack, obj); + return doSomethingWith(x); + } catch (_) { + var _error = _; + var _hasError = true; + } finally { + babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== "undefined" && SuppressedError); + } } diff --git a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/input.js b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/input.js index 0ed05eeaeae2..326085daa898 100644 --- a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/input.js +++ b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/input.js @@ -1,4 +1,4 @@ -function fn() { +if (test) { using x = obj; - return doSomethingWith(x); + doSomethingWith(x); } diff --git a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/output.js b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/output.js index 70932ce58965..1893ea294ba5 100644 --- a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/output.js +++ b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/output.js @@ -1,12 +1,10 @@ -function fn() { - try { - var _stack = []; - const x = babelHelpers.using(_stack, obj); - return doSomethingWith(x); - } catch (_) { - var _error = _; - var _hasError = true; - } finally { - babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== "undefined" && SuppressedError); - } +if (test) try { + var _stack = []; + const x = babelHelpers.using(_stack, obj); + doSomethingWith(x); +} catch (_) { + var _error = _; + var _hasError = true; +} finally { + babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== "undefined" && SuppressedError); } diff --git a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/static-block/input.js b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/static-block/input.js new file mode 100644 index 000000000000..c965473f4842 --- /dev/null +++ b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/static-block/input.js @@ -0,0 +1,6 @@ +class A { + static { + using x = y; + doSomethingWith(x); + } +} diff --git a/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/static-block/output.js b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/static-block/output.js new file mode 100644 index 000000000000..60e9a2fb61f0 --- /dev/null +++ b/packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/transform-sync/static-block/output.js @@ -0,0 +1,14 @@ +class A { + static { + try { + var _stack = []; + const x = babelHelpers.using(_stack, y); + doSomethingWith(x); + } catch (_) { + var _error = _; + var _hasError = true; + } finally { + babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== "undefined" && SuppressedError); + } + } +}