better formatting for AwaitExpression in CallExpression/MemberExpression (#6856)

* better formatting for AwaitExpression nested in CallExpression or MemberExpression

* update changelog
This commit is contained in:
Georgii Dolzhykov authored and Simon Lydell committed 2019-11-06 20:53:16 +01:00
1 parent 5458fb5f17
commit 54cbdb89b1
4 files changed
+75 -2

No files matched your search

+31
View File
@@ -1373,6 +1373,36 @@ When using the Azure Cosmos DB extension for VS Code, you can create .mongo file
db.users.find({ someField: { $exists: true } });
```
#### JavaScript: Better formatting for inline `await` expression nested in calls ([#6856] by [@thorn0])
<!-- prettier-ignore -->
```js
// Input
async function f() {
const admins = (await(db.select('*').from('admins').leftJoin('bla').where('id', 'in', [1,2,3,4]))).map(({id, name})=>({id, name}))
}
// Output (Prettier stable)
async function f() {
const admins = (await db
.select("*")
.from("admins")
.leftJoin("bla")
.where("id", "in", [1, 2, 3, 4])).map(({ id, name }) => ({ id, name }));
}
// Output (Prettier master)
async function f() {
const admins = (
await db
.select("*")
.from("admins")
.leftJoin("bla")
.where("id", "in", [1, 2, 3, 4])
).map(({ id, name }) => ({ id, name }));
}
```
[#5682]: https://github.com/prettier/prettier/pull/5682
[#6657]: https://github.com/prettier/prettier/pull/6657
[#5910]: https://github.com/prettier/prettier/pull/5910
@@ -1422,6 +1452,7 @@ db.users.find({ someField: { $exists: true } });
[#6687]: https://github.com/prettier/prettier/pull/6687
[#6796]: https://github.com/prettier/prettier/pull/6796
[#6848]: https://github.com/prettier/prettier/pull/6848
[#6856]: https://github.com/prettier/prettier/pull/6856
[@brainkim]: https://github.com/brainkim
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce
+17 -2
View File
@@ -908,8 +908,23 @@ function printPathNoParens(path, options, print, args) {
}
return concat(parts);
case "AwaitExpression":
return concat(["await ", path.call(print, "argument")]);
case "AwaitExpression": {
parts.push("await ", path.call(print, "argument"));
const parent = path.getParentNode();
if (
((parent.type === "CallExpression" ||
parent.type === "OptionalCallExpression") &&
parent.callee === n) ||
((parent.type === "MemberExpression" ||
parent.type === "OptionalMemberExpression") &&
parent.object === n)
) {
return group(
concat([indent(concat([softline, concat(parts)])), softline])
);
}
return concat(parts);
}
case "ImportSpecifier":
if (n.importKind) {
parts.push(path.call(print, "importKind"), " ");
@@ -113,6 +113,30 @@ async function f2() {
================================================================================
`;
exports[`inline_await.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
const admins = (await(db.select('*').from('admins').leftJoin('bla').where('id', 'in', [1,2,3,4]))).map(({id, name})=>({id, name}))
}
=====================================output=====================================
async function f() {
const admins = (
await db
.select("*")
.from("admins")
.leftJoin("bla")
.where("id", "in", [1, 2, 3, 4])
).map(({ id, name }) => ({ id, name }));
}
================================================================================
`;
exports[`parens.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
+3
View File
@@ -0,0 +1,3 @@
async function f() {
const admins = (await(db.select('*').from('admins').leftJoin('bla').where('id', 'in', [1,2,3,4]))).map(({id, name})=>({id, name}))
}