Skip to content

Commit

Permalink
Add missing parentheses to let in for..of loop (#14044)
Browse files Browse the repository at this point in the history
Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
  • Loading branch information
fisker and thorn0 committed Dec 23, 2022
1 parent 895adf7 commit 57a109a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
2 changes: 1 addition & 1 deletion changelog_unreleased/javascript/14000.md
@@ -1,4 +1,4 @@
#### Fix missing parentheses when an expression statement starts with `let[` (#14000 by @fisker, @thorn0)
#### Fix missing parentheses when an expression statement starts with `let[` (#14000, #14044 by @fisker, @thorn0)

<!-- prettier-ignore -->
```jsx
Expand Down
23 changes: 19 additions & 4 deletions src/language-js/needs-parens.js
Expand Up @@ -67,16 +67,32 @@ function needsParens(path, options) {
return true;
}

// `for (async of []);` is invalid
// `for ((async) of []);` and `for ((let) of []);`
if (
name === "left" &&
node.name === "async" &&
(node.name === "async" || node.name === "let") &&
parent.type === "ForOfStatement" &&
!parent.await
) {
return true;
}

// `for ((let.a) of []);`
if (node.name === "let") {
const expression = path.findAncestor(
(node) => node.type === "ForOfStatement"
)?.left;
if (
expression &&
startsWithNoLookaheadToken(
expression,
(leftmostNode) => leftmostNode === node
)
) {
return true;
}
}

// `(let)[a] = 1`
if (
name === "object" &&
Expand All @@ -89,8 +105,7 @@ function needsParens(path, options) {
(node) =>
node.type === "ExpressionStatement" ||
node.type === "ForStatement" ||
node.type === "ForInStatement" ||
node.type === "ForOfStatement"
node.type === "ForInStatement"
);
const expression = !statement
? undefined
Expand Down
57 changes: 57 additions & 0 deletions tests/format/js/identifier/for-of/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`await.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function a() {
for await((let).a of foo);
for await((let)[a] of foo);
for await((let)()[a] of foo);
}
=====================================output=====================================
async function a() {
for await ((let).a of foo);
for await ((let)[a] of foo);
for await ((let)()[a] of foo);
}
================================================================================
`;

exports[`let.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
for ((let) of foo);
for (foo of let);
for (foo of let.a);
for (foo of let[a]);
for ((let.a) of foo);
for ((let[a]) of foo);
for ((let)().a of foo);
for (letFoo of foo);
for ((let.a) in foo);
for ((let[a]) in foo);
=====================================output=====================================
for ((let) of foo);
for (foo of let);
for (foo of let.a);
for (foo of let[a]);
for ((let).a of foo);
for ((let)[a] of foo);
for ((let)().a of foo);
for (letFoo of foo);
for (let.a in foo);
for ((let)[a] in foo);
================================================================================
`;
5 changes: 5 additions & 0 deletions tests/format/js/identifier/for-of/await.js
@@ -0,0 +1,5 @@
async function a() {
for await((let).a of foo);
for await((let)[a] of foo);
for await((let)()[a] of foo);
}
5 changes: 5 additions & 0 deletions tests/format/js/identifier/for-of/jsfmt.spec.js
@@ -0,0 +1,5 @@
run_spec(__dirname, [
"babel",
// "flow",
"typescript",
]);
11 changes: 11 additions & 0 deletions tests/format/js/identifier/for-of/let.js
@@ -0,0 +1,11 @@
for ((let) of foo);
for (foo of let);
for (foo of let.a);
for (foo of let[a]);
for ((let.a) of foo);
for ((let[a]) of foo);
for ((let)().a of foo);
for (letFoo of foo);

for ((let.a) in foo);
for ((let[a]) in foo);

0 comments on commit 57a109a

Please sign in to comment.