Repository: babel/babel (TypeScript)

Issue:
[Bug]: (Babel transpilation fail) For await shadowing
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

### Input code

```js
( async () => {
  for await ( let x of [ ] ) { let x; }
} ) ;
```

### Configuration file name

_No response_

### Configuration

_No response_

### Current and expected behavior

The input code has no problem, but Babel throws error while transpiling the code.
```
$ node
Welcome to Node.js v18.11.0.
Type ".help" for more information.
> ( async () => {
...   for await ( let x of [ ] ) { let x; }
... } ) ;
[AsyncFunction (anonymous)]
```

### Environment

[Reproduction on Babel's own REPL](https://babeljs.io/repl#?browsers=&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=BQAghgzgngdgxiYBKEBeAfCA3gKBCAMwHsAncAdzAEsAXREAGwFM6APEIgkAbRAF0QKLIxYhWAbhABfHFMEhxQA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=script&lineWrap=true&presets=env%2Creact%2Cstage-2&prettier=true&targets=&version=7.20.0&externalPlugins=&assumptions=%7B%7D)

### Possible solution

_No response_

### Additional context

_No response_


Hints:
Hey @p51lee! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.

Patch:
diff --git a/packages/babel-plugin-proposal-async-generator-functions/src/index.ts b/packages/babel-plugin-proposal-async-generator-functions/src/index.ts
--- a/packages/babel-plugin-proposal-async-generator-functions/src/index.ts
+++ b/packages/babel-plugin-proposal-async-generator-functions/src/index.ts
@@ -54,11 +54,13 @@ export default declare(api => {
         // add the value declaration to the new loop body
         if (declar) {
           block.body.push(declar);
+          if (path.node.body.body.length) {
+            block.body.push(t.blockStatement(path.node.body.body));
+          }
+        } else {
+          block.body.push(...path.node.body.body);
         }
 
-        // push the rest of the original loop body onto our new body
-        block.body.push(...path.node.body.body);
-
         t.inherits(loop, node);
         t.inherits(loop.body, node.body);
 


Test patch:
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-arrow/output.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-arrow/output.js
--- a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-arrow/output.js
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-arrow/output.js
@@ -5,7 +5,9 @@
   try {
     for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) {
       let x = _step.value;
-      f(x);
+      {
+        f(x);
+      }
     }
   } catch (err) {
     _didIteratorError = true;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-function/output.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-function/output.js
--- a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-function/output.js
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-function/output.js
@@ -9,7 +9,9 @@ function _f() {
     try {
       for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) {
         let x = _step.value;
-        g(x);
+        {
+          g(x);
+        }
       }
     } catch (err) {
       _didIteratorError = true;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-generator/output.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-generator/output.js
--- a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-generator/output.js
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/async-generator/output.js
@@ -9,7 +9,9 @@ function _g() {
     try {
       for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done; _iteratorAbruptCompletion = false) {
         let x = _step.value;
-        f(x);
+        {
+          f(x);
+        }
       }
     } catch (err) {
       _didIteratorError = true;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js
--- a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js
@@ -9,9 +9,11 @@ function _fn() {
     try {
       for (var _iterator = babelHelpers.asyncIterator([Promise.resolve("ok")]), _step; _iteratorAbruptCompletion = !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done; _iteratorAbruptCompletion = false) {
         const result = _step.value;
-        return {
-          result
-        };
+        {
+          return {
+            result
+          };
+        }
       }
     } catch (err) {
       _didIteratorError = true;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/destructuring/output.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/destructuring/output.js
--- a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/destructuring/output.js
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/destructuring/output.js
@@ -12,7 +12,9 @@ function _f() {
           x,
           y: [z]
         } = _step.value;
-        g(x, z);
+        {
+          g(x, z);
+        }
       }
     } catch (err) {
       _didIteratorError = true;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/exec.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/exec.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/exec.js
@@ -0,0 +1,3 @@
+( async () => {
+  for await ( let x of [ ] ) { let x; }
+} ) ;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/input.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/input.js
@@ -0,0 +1,3 @@
+( async () => {
+  for await ( let x of [ ] ) { let x; }
+} ) ;
diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/output.js b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-proposal-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/output.js
@@ -0,0 +1,26 @@
+/*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
+  var _iteratorAbruptCompletion = false;
+  var _didIteratorError = false;
+  var _iteratorError;
+  try {
+    for (var _iterator = babelHelpers.asyncIterator([]), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) {
+      let x = _step.value;
+      {
+        let x;
+      }
+    }
+  } catch (err) {
+    _didIteratorError = true;
+    _iteratorError = err;
+  } finally {
+    try {
+      if (_iteratorAbruptCompletion && _iterator.return != null) {
+        yield _iterator.return();
+      }
+    } finally {
+      if (_didIteratorError) {
+        throw _iteratorError;
+      }
+    }
+  }
+});