Skip to content

Commit 54cbdb8

Browse files
thorn0lydell
authored andcommitted
better formatting for AwaitExpression in CallExpression/MemberExpression (prettier#6856)
* better formatting for AwaitExpression nested in CallExpression or MemberExpression * update changelog
1 parent 5458fb5 commit 54cbdb8

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.unreleased.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,36 @@ When using the Azure Cosmos DB extension for VS Code, you can create .mongo file
13731373
db.users.find({ someField: { $exists: true } });
13741374
```
13751375

1376+
#### JavaScript: Better formatting for inline `await` expression nested in calls ([#6856] by [@thorn0])
1377+
1378+
<!-- prettier-ignore -->
1379+
```js
1380+
// Input
1381+
async function f() {
1382+
const admins = (await(db.select('*').from('admins').leftJoin('bla').where('id', 'in', [1,2,3,4]))).map(({id, name})=>({id, name}))
1383+
}
1384+
1385+
// Output (Prettier stable)
1386+
async function f() {
1387+
const admins = (await db
1388+
.select("*")
1389+
.from("admins")
1390+
.leftJoin("bla")
1391+
.where("id", "in", [1, 2, 3, 4])).map(({ id, name }) => ({ id, name }));
1392+
}
1393+
1394+
// Output (Prettier master)
1395+
async function f() {
1396+
const admins = (
1397+
await db
1398+
.select("*")
1399+
.from("admins")
1400+
.leftJoin("bla")
1401+
.where("id", "in", [1, 2, 3, 4])
1402+
).map(({ id, name }) => ({ id, name }));
1403+
}
1404+
```
1405+
13761406
[#5682]: https://github.com/prettier/prettier/pull/5682
13771407
[#6657]: https://github.com/prettier/prettier/pull/6657
13781408
[#5910]: https://github.com/prettier/prettier/pull/5910
@@ -1422,6 +1452,7 @@ db.users.find({ someField: { $exists: true } });
14221452
[#6687]: https://github.com/prettier/prettier/pull/6687
14231453
[#6796]: https://github.com/prettier/prettier/pull/6796
14241454
[#6848]: https://github.com/prettier/prettier/pull/6848
1455+
[#6856]: https://github.com/prettier/prettier/pull/6856
14251456
[@brainkim]: https://github.com/brainkim
14261457
[@duailibe]: https://github.com/duailibe
14271458
[@gavinjoyce]: https://github.com/gavinjoyce

src/language-js/printer-estree.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,23 @@ function printPathNoParens(path, options, print, args) {
908908
}
909909

910910
return concat(parts);
911-
case "AwaitExpression":
912-
return concat(["await ", path.call(print, "argument")]);
911+
case "AwaitExpression": {
912+
parts.push("await ", path.call(print, "argument"));
913+
const parent = path.getParentNode();
914+
if (
915+
((parent.type === "CallExpression" ||
916+
parent.type === "OptionalCallExpression") &&
917+
parent.callee === n) ||
918+
((parent.type === "MemberExpression" ||
919+
parent.type === "OptionalMemberExpression") &&
920+
parent.object === n)
921+
) {
922+
return group(
923+
concat([indent(concat([softline, concat(parts)])), softline])
924+
);
925+
}
926+
return concat(parts);
927+
}
913928
case "ImportSpecifier":
914929
if (n.importKind) {
915930
parts.push(path.call(print, "importKind"), " ");

tests/async/__snapshots__/jsfmt.spec.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ async function f2() {
113113
================================================================================
114114
`;
115115

116+
exports[`inline_await.js 1`] = `
117+
====================================options=====================================
118+
parsers: ["flow", "typescript"]
119+
printWidth: 80
120+
| printWidth
121+
=====================================input======================================
122+
async function f() {
123+
const admins = (await(db.select('*').from('admins').leftJoin('bla').where('id', 'in', [1,2,3,4]))).map(({id, name})=>({id, name}))
124+
}
125+
126+
=====================================output=====================================
127+
async function f() {
128+
const admins = (
129+
await db
130+
.select("*")
131+
.from("admins")
132+
.leftJoin("bla")
133+
.where("id", "in", [1, 2, 3, 4])
134+
).map(({ id, name }) => ({ id, name }));
135+
}
136+
137+
================================================================================
138+
`;
139+
116140
exports[`parens.js 1`] = `
117141
====================================options=====================================
118142
parsers: ["flow", "typescript"]

tests/async/inline_await.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
async function f() {
2+
const admins = (await(db.select('*').from('admins').leftJoin('bla').where('id', 'in', [1,2,3,4]))).map(({id, name})=>({id, name}))
3+
}

0 commit comments

Comments
 (0)