From 54cbdb89b134d2238dfd642fd6c27aabc82e7f3f Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Wed, 6 Nov 2019 21:53:16 +0200 Subject: [PATCH] better formatting for AwaitExpression in CallExpression/MemberExpression (#6856) * better formatting for AwaitExpression nested in CallExpression or MemberExpression * update changelog --- CHANGELOG.unreleased.md | 31 ++++++++++++++++++++ src/language-js/printer-estree.js | 19 ++++++++++-- tests/async/__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++ tests/async/inline_await.js | 3 ++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/async/inline_await.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 280485ba3..35e6934c1 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -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]) + + +```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 diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 92ba97f13..d3dba1d68 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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"), " "); diff --git a/tests/async/__snapshots__/jsfmt.spec.js.snap b/tests/async/__snapshots__/jsfmt.spec.js.snap index 624db1b45..8d17a0847 100644 --- a/tests/async/__snapshots__/jsfmt.spec.js.snap +++ b/tests/async/__snapshots__/jsfmt.spec.js.snap @@ -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"] diff --git a/tests/async/inline_await.js b/tests/async/inline_await.js new file mode 100644 index 000000000..3ee8968be --- /dev/null +++ b/tests/async/inline_await.js @@ -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})) +}