Avoid adding unnecessary indent to nested await (#14192)
This commit is contained in:
1 parent
0550b41082
commit
cfa5f27afe
4 files changed
+62
-1
No files matched your search
@@ -0,0 +1,22 @@
|
||||
#### Avoid unnecessarily indenting nested `await` expressions (#14192 by @thorn0)
|
||||
|
||||
A refinement of [this change in v2.3](https://prettier.io/blog/2021/05/09/2.3.0.html#improve-formatting-for-nested-await-expressions-in-heads-of-member-and-call-expressions-10342httpsgithubcomprettierprettierpull10342-by-thorn0httpsgithubcomthorn0). Sometimes there is no need to force indentation of nested `await` expressions.
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```jsx
|
||||
// Prettier stable
|
||||
await Promise.all(
|
||||
(
|
||||
await readdir("src")
|
||||
).map((path) => {
|
||||
import(`./${path}`);
|
||||
})
|
||||
);
|
||||
|
||||
// Prettier main
|
||||
await Promise.all(
|
||||
(await readdir("src")).map((path) => {
|
||||
import(`./${path}`);
|
||||
}),
|
||||
);
|
||||
```
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
markerForIfWithoutBlockAndSameLineComment,
|
||||
isArrayOrTupleExpression,
|
||||
isObjectOrRecordExpression,
|
||||
startsWithNoLookaheadToken,
|
||||
} from "./utils/index.js";
|
||||
import { locStart, locEnd } from "./loc.js";
|
||||
import isBlockComment from "./utils/is-block-comment.js";
|
||||
@@ -325,11 +326,18 @@ function printPathNoParens(path, options, print, args) {
|
||||
(isMemberExpression(parent) && parent.object === node)
|
||||
) {
|
||||
parts = [indent([softline, ...parts]), softline];
|
||||
// avoid printing `await (await` on one line
|
||||
const parentAwaitOrBlock = path.findAncestor(
|
||||
(node) =>
|
||||
node.type === "AwaitExpression" || node.type === "BlockStatement"
|
||||
);
|
||||
if (parentAwaitOrBlock?.type !== "AwaitExpression") {
|
||||
if (
|
||||
parentAwaitOrBlock?.type !== "AwaitExpression" ||
|
||||
!startsWithNoLookaheadToken(
|
||||
parentAwaitOrBlock.argument,
|
||||
(leftmostNode) => leftmostNode === node
|
||||
)
|
||||
) {
|
||||
return group(parts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,6 +198,31 @@ const getAccountCount = async () =>
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`nested2.js format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["babel", "flow", "typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
async function f() {
|
||||
await Promise.all(
|
||||
(await readdir("src")).map(async (path) => {
|
||||
import(\`./\${path}\`);
|
||||
})
|
||||
);}
|
||||
|
||||
=====================================output=====================================
|
||||
async function f() {
|
||||
await Promise.all(
|
||||
(await readdir("src")).map(async (path) => {
|
||||
import(\`./\${path}\`);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`parens.js format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["babel", "flow", "typescript"]
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
async function f() {
|
||||
await Promise.all(
|
||||
(await readdir("src")).map(async (path) => {
|
||||
import(`./${path}`);
|
||||
})
|
||||
);}
|
||||
Reference in new issue
Block a user