Skip to content

Commit

Permalink
Prevent ForOfStatement from printing the forbidden sequence "for ( as…
Browse files Browse the repository at this point in the history
…ync of"
  • Loading branch information
Zalathar committed Apr 26, 2021
1 parent 10f4d08 commit 287b118
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/babel-generator/src/generators/statements.ts
Expand Up @@ -82,16 +82,26 @@ export function WhileStatement(this: Printer, node: t.WhileStatement) {
this.printBlock(node);
}

const buildForXStatement = function (op) {
return function (node: any) {
const buildForXStatement = function (op: "in" | "of") {
return function (this: Printer, node: t.ForInStatement | t.ForOfStatement) {
this.word("for");
this.space();
if (op === "of" && node.await) {
if (op === "of" && (node as t.ForOfStatement).await) {
this.word("await");
this.space();
}
this.token("(");

// ECMAScript specifically forbids a for-of loop from starting with the
// token sequence "for ( async of", because it would be ambiguous with
// "for (async of => {};;)", so we need to add extra parentheses.
const leftNeedsParens =
op === "of" && t.isIdentifier(node.left) && node.left.name === "async";

if (leftNeedsParens) this.token("(");
this.print(node.left, node);
if (leftNeedsParens) this.token(")");

this.space();
this.word(op);
this.space();
Expand Down
@@ -0,0 +1,7 @@
for ((async) of []);

for ((async) of async) async;

for (\u0061sync of []);

for (async in []);
@@ -0,0 +1,7 @@
for ((async) of []);

for ((async) of async) async;

for ((async) of []);

for (async in []);

0 comments on commit 287b118

Please sign in to comment.