Skip to content

Commit 80c9a3a

Browse files
authored
fix: handle shadowed binding in for using of body (#17319)
1 parent 7ed9f5e commit 80c9a3a

7 files changed

Lines changed: 40 additions & 3 deletions

File tree

packages/babel-plugin-proposal-explicit-resource-management/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"babel-plugin"
1818
],
1919
"dependencies": {
20-
"@babel/helper-plugin-utils": "workspace:^"
20+
"@babel/helper-plugin-utils": "workspace:^",
21+
"@babel/plugin-transform-destructuring": "workspace:^"
2122
},
2223
"peerDependencies": {
2324
"@babel/core": "^7.0.0-0"

packages/babel-plugin-proposal-explicit-resource-management/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { declare } from "@babel/helper-plugin-utils";
2+
import { unshiftForXStatementBody } from "@babel/plugin-transform-destructuring";
23
import { types as t, template, traverse } from "@babel/core";
34
import type { NodePath, Visitor, PluginPass } from "@babel/core";
45

@@ -51,11 +52,11 @@ export default declare(api => {
5152
left.kind = "const";
5253

5354
path.ensureBlock();
54-
path.node.body.body.unshift(
55+
unshiftForXStatementBody(path, [
5556
t.variableDeclaration("using", [
5657
t.variableDeclarator(id, t.cloneNode(tmpId)),
5758
]),
58-
);
59+
]);
5960
},
6061
"BlockStatement|StaticBlock"(
6162
path: NodePath<t.BlockStatement | t.StaticBlock>,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let log = [];
2+
{
3+
for (using x of [null]) {
4+
const x = undefined;
5+
log.push(x);
6+
}
7+
}
8+
9+
expect(log).toEqual([undefined]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let log = [];
2+
{
3+
for (using x of [null]) {
4+
const x = undefined;
5+
log.push(x);
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let log = [];
2+
{
3+
for (const _x of [null]) try {
4+
var _usingCtx = babelHelpers.usingCtx();
5+
const x = _usingCtx.u(_x);
6+
{
7+
const x = undefined;
8+
log.push(x);
9+
}
10+
} catch (_) {
11+
_usingCtx.e = _;
12+
} finally {
13+
_usingCtx.d();
14+
}
15+
}

packages/babel-plugin-proposal-explicit-resource-management/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
{
1515
"path": "../../packages/babel-helper-plugin-utils"
1616
},
17+
{
18+
"path": "../../packages/babel-plugin-transform-destructuring"
19+
},
1720
{
1821
"path": "../../packages/babel-core"
1922
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,7 @@ __metadata:
14701470
"@babel/core": "workspace:^"
14711471
"@babel/helper-plugin-test-runner": "workspace:^"
14721472
"@babel/helper-plugin-utils": "workspace:^"
1473+
"@babel/plugin-transform-destructuring": "workspace:^"
14731474
peerDependencies:
14741475
"@babel/core": ^7.0.0-0
14751476
languageName: unknown

0 commit comments

Comments
 (0)