Repository: babel/babel (TypeScript)

Issue:
[Bug]: Babel crashes when using `await` as an identifier in ForInOfHead
### 💻

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

### How are you using Babel?

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

### Input code

```js
for( await of [1, 2, 3] )  { console.log(await) }
```

### Configuration file name

_No response_

### Configuration

_No response_

### Current and expected behavior

Hello,

The input code prints `1`, `2` and `3`:
```console
$ node --version
v18.11.0
$ node input.js
1
2
3
```
However, Babel fails to transpile the input code:
<img width="446" alt="image" src="https://user-images.githubusercontent.com/68288688/200038280-5710d26b-d188-433c-b675-e83a27e5ef13.png">


### Environment

[Reproduction on Babel's own REPL](https://babeljs.io/repl#?browsers=since%202015&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=GYewTgFAhg7lCWAXABCYyDaBGANAJhwGYBdASmWQG9kBjEAOwGcQAbAUwDoWQBzaOJOQC-AKCA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=script&lineWrap=true&presets=env%2Creact%2Cstage-2&prettier=false&targets=&version=7.20.1&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-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts
--- a/packages/babel-parser/src/parser/expression.ts
+++ b/packages/babel-parser/src/parser/expression.ts
@@ -2891,11 +2891,12 @@ export default abstract class ExpressionParser extends LValParser {
     const { type } = this.state;
     return (
       // All the following expressions are ambiguous:
-      //   await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await ``
+      //   await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await ``, await of []
       type === tt.plusMin ||
       type === tt.parenL ||
       type === tt.bracketL ||
       tokenIsTemplate(type) ||
+      (type === tt._of && !this.state.containsEsc) ||
       // Sometimes the tokenizer generates tt.slash for regexps, and this is
       // handler by parseExprAtom
       type === tt.regexp ||


Test patch:
diff --git a/packages/babel-parser/test/fixtures/es2015/for-of/valid-script-await-as-lhs/input.js b/packages/babel-parser/test/fixtures/es2015/for-of/valid-script-await-as-lhs/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/es2015/for-of/valid-script-await-as-lhs/input.js
@@ -0,0 +1,2 @@
+for (await of []);
+for (await of [0]);
diff --git a/packages/babel-parser/test/fixtures/es2015/for-of/valid-script-await-as-lhs/output.json b/packages/babel-parser/test/fixtures/es2015/for-of/valid-script-await-as-lhs/output.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/es2015/for-of/valid-script-await-as-lhs/output.json
@@ -0,0 +1,61 @@
+{
+  "type": "File",
+  "start":0,"end":38,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":19,"index":38}},
+  "program": {
+    "type": "Program",
+    "start":0,"end":38,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":19,"index":38}},
+    "sourceType": "script",
+    "interpreter": null,
+    "body": [
+      {
+        "type": "ForOfStatement",
+        "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}},
+        "await": false,
+        "left": {
+          "type": "Identifier",
+          "start":5,"end":10,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":10,"index":10},"identifierName":"await"},
+          "name": "await"
+        },
+        "right": {
+          "type": "ArrayExpression",
+          "start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16}},
+          "elements": []
+        },
+        "body": {
+          "type": "EmptyStatement",
+          "start":17,"end":18,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":18,"index":18}}
+        }
+      },
+      {
+        "type": "ForOfStatement",
+        "start":19,"end":38,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":19,"index":38}},
+        "await": false,
+        "left": {
+          "type": "Identifier",
+          "start":24,"end":29,"loc":{"start":{"line":2,"column":5,"index":24},"end":{"line":2,"column":10,"index":29},"identifierName":"await"},
+          "name": "await"
+        },
+        "right": {
+          "type": "ArrayExpression",
+          "start":33,"end":36,"loc":{"start":{"line":2,"column":14,"index":33},"end":{"line":2,"column":17,"index":36}},
+          "elements": [
+            {
+              "type": "NumericLiteral",
+              "start":34,"end":35,"loc":{"start":{"line":2,"column":15,"index":34},"end":{"line":2,"column":16,"index":35}},
+              "extra": {
+                "rawValue": 0,
+                "raw": "0"
+              },
+              "value": 0
+            }
+          ]
+        },
+        "body": {
+          "type": "EmptyStatement",
+          "start":37,"end":38,"loc":{"start":{"line":2,"column":18,"index":37},"end":{"line":2,"column":19,"index":38}}
+        }
+      }
+    ],
+    "directives": []
+  }
+}
diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-for-await-expression-init/input.js b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-for-await-expression-init/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-for-await-expression-init/input.js
@@ -0,0 +1 @@
+for (await o\u0066 [0];;);
diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-for-await-expression-init/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-for-await-expression-init/output.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-for-await-expression-init/output.json
@@ -0,0 +1,49 @@
+{
+  "type": "File",
+  "start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":26,"index":26}},
+  "errors": [
+    "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules. (1:5)"
+  ],
+  "program": {
+    "type": "Program",
+    "start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":26,"index":26}},
+    "sourceType": "script",
+    "interpreter": null,
+    "body": [
+      {
+        "type": "ForStatement",
+        "start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":26,"index":26}},
+        "init": {
+          "type": "AwaitExpression",
+          "start":5,"end":22,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":22,"index":22}},
+          "argument": {
+            "type": "MemberExpression",
+            "start":11,"end":22,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":22,"index":22}},
+            "object": {
+              "type": "Identifier",
+              "start":11,"end":18,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":18,"index":18},"identifierName":"of"},
+              "name": "of"
+            },
+            "computed": true,
+            "property": {
+              "type": "NumericLiteral",
+              "start":20,"end":21,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":21,"index":21}},
+              "extra": {
+                "rawValue": 0,
+                "raw": "0"
+              },
+              "value": 0
+            }
+          }
+        },
+        "test": null,
+        "update": null,
+        "body": {
+          "type": "EmptyStatement",
+          "start":25,"end":26,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":26,"index":26}}
+        }
+      }
+    ],
+    "directives": []
+  }
+}