Skip to content

Commit

Permalink
fix: support await as for-of-lhs (#15134)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 5, 2022
1 parent c171a75 commit ce09a26
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for (await of []);
for (await of [0]);
Original file line number Diff line number Diff line change
@@ -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": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
for (await o\u0066 [0];;);
Original file line number Diff line number Diff line change
@@ -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": []
}
}

0 comments on commit ce09a26

Please sign in to comment.