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 3d4b801 commit 6faed7b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/babel-generator/src/node/parentheses.ts
Expand Up @@ -276,6 +276,15 @@ export function LogicalExpression(node: any, parent: any): boolean {
}
}

export function Identifier(node: t.Identifier, parent: t.Node): boolean {
// 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.
return (
node.name === "async" && t.isForOfStatement(parent) && node === parent.left
);
}

// Walk up the print stack to determine if our node can come first
// in statement.
function isFirstInStatement(
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 6faed7b

Please sign in to comment.