Skip to content

Commit

Permalink
Print parentheses around identifier let where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed May 6, 2021
1 parent c11ec58 commit fd19bad
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
37 changes: 34 additions & 3 deletions packages/babel-generator/src/node/parentheses.ts
Expand Up @@ -292,7 +292,28 @@ export function LogicalExpression(node: any, parent: any): boolean {
}
}

export function Identifier(node: t.Identifier, parent: t.Node): boolean {
export function Identifier(
node: t.Identifier,
parent: t.Node,
printStack: Array<t.Node>,
): boolean {
// Non-strict code allows the identifier `let`, but it cannot occur as-is in
// certain contexts to avoid ambiguity with contextual keyword `let`.
if (node.name === "let") {
// Some contexts only forbid `let [`, so check if the next token would
// be the left bracket of a computed member expression.
const isFollowedByBracket = t.isMemberExpression(parent, {
object: node,
computed: true,
});
return isFirstInContext(printStack, {
expressionStatement: isFollowedByBracket,
forHead: isFollowedByBracket,
forInHead: isFollowedByBracket,
forOfHead: true,
});
}

// 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.
Expand All @@ -310,7 +331,14 @@ export function Identifier(node: t.Identifier, parent: t.Node): boolean {
// in a particular context.
function isFirstInContext(
printStack: Array<t.Node>,
{ expressionStatement = false, arrowBody = false, exportDefault = false },
{
expressionStatement = false,
arrowBody = false,
exportDefault = false,
forHead = false,
forInHead = false,
forOfHead = false,
},
): boolean {
let i = printStack.length - 1;
let node = printStack[i];
Expand All @@ -322,7 +350,10 @@ function isFirstInContext(
t.isExpressionStatement(parent, { expression: node })) ||
(exportDefault &&
t.isExportDefaultDeclaration(parent, { declaration: node })) ||
(arrowBody && t.isArrowFunctionExpression(parent, { body: node }))
(arrowBody && t.isArrowFunctionExpression(parent, { body: node })) ||
(forHead && t.isForStatement(parent, { init: node })) ||
(forInHead && t.isForInStatement(parent, { left: node })) ||
(forOfHead && t.isForOfStatement(parent, { left: node }))
) {
return true;
}
Expand Down
@@ -0,0 +1,31 @@
/* ExpressionStatement */
let;
\u006cet[x];
(let)[x];
a[let[x]];

/* ForStatement */
for (let;;);
for (\u006cet[x];;);
for ((let)[x];;);
for (a[let[x]];;);

/* ForInStatement */
for (let in {});
for (\u006cet[x] in {});
for ((let)[x] in {});
for (a[let[x]] in {});

/* ForOfStatement { await: false } */
for ((let) of []);
for (\u006cet of []);
for ((let)[x] of []);
for (a[let] of []);

/* ForOfStatement { await: true } */
async () => {
for await ((let) of []);
for await (\u006cet of []);
for await ((let)[x] of []);
for await (a[let] of []);
}
@@ -0,0 +1,4 @@
{
"sourceType": "script",
"strictMode": false
}
@@ -0,0 +1,46 @@
/* ExpressionStatement */
let;
(let)[x];
(let)[x];
a[let[x]];
/* ForStatement */

for (let;;);

for ((let)[x];;);

for ((let)[x];;);

for (a[let[x]];;);
/* ForInStatement */


for (let in {});

for ((let)[x] in {});

for ((let)[x] in {});

for (a[let[x]] in {});
/* ForOfStatement { await: false } */


for ((let) of []);

for ((let) of []);

for ((let)[x] of []);

for (a[let] of []);
/* ForOfStatement { await: true } */


async () => {
for await ((let) of []);

for await ((let) of []);

for await ((let)[x] of []);

for await (a[let] of []);
};

0 comments on commit fd19bad

Please sign in to comment.