Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing parentheses to let in for..of loop #14044

Merged
merged 10 commits into from Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);