From 42f8271a1e8aa8cd2a433fd317a80652cb3bb52a Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Sat, 20 Feb 2021 14:37:08 +0200 Subject: [PATCH] Indent type assertions in heads of call and member expressions (#10341) --- changelog_unreleased/typescript/10341.md | 31 +++++++++++++++++++ src/language-js/print/typescript.js | 19 +++++++++--- .../as/__snapshots__/jsfmt.spec.js.snap | 27 ++++++++++++++++ tests/typescript/as/nested-await-and-as.ts | 7 +++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 changelog_unreleased/typescript/10341.md create mode 100644 tests/typescript/as/nested-await-and-as.ts diff --git a/changelog_unreleased/typescript/10341.md b/changelog_unreleased/typescript/10341.md new file mode 100644 index 000000000..91028c186 --- /dev/null +++ b/changelog_unreleased/typescript/10341.md @@ -0,0 +1,31 @@ +#### Indent type assertions in heads of member and call expressions (#10341 by @thorn0) + + +```ts +// Input +const getAccountCount = async () => + (await + ((await ( + await focusOnSection(BOOKMARKED_PROJECTS_SECTION_NAME) + ).findItem("My bookmarks")) as TreeItem + ).getChildren() + ).length + +// Prettier stable +const getAccountCount = async () => + ( + await ((await ( + await focusOnSection(BOOKMARKED_PROJECTS_SECTION_NAME) + ).findItem("My bookmarks")) as TreeItem).getChildren() + ).length; + +// Prettier main +const getAccountCount = async () => + ( + await ( + (await ( + await focusOnSection(BOOKMARKED_PROJECTS_SECTION_NAME) + ).findItem("My bookmarks")) as TreeItem + ).getChildren() + ).length; +``` diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index 9fe3c7fdc..adc93eb68 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -18,6 +18,8 @@ const { isLiteral, getTypeScriptMappedTypeModifier, shouldPrintComma, + isCallExpression, + isMemberExpression, } = require("../utils"); const { locStart, locEnd } = require("../loc"); @@ -160,12 +162,21 @@ function printTypescript(path, options, print) { return "undefined"; case "TSUnknownKeyword": return "unknown"; - case "TSAsExpression": - return [ + case "TSAsExpression": { + parts.push( path.call(print, "expression"), " as ", - path.call(print, "typeAnnotation"), - ]; + path.call(print, "typeAnnotation") + ); + const parent = path.getParentNode(); + if ( + (isCallExpression(parent) && parent.callee === n) || + (isMemberExpression(parent) && parent.object === n) + ) { + return group([indent([softline, ...parts]), softline]); + } + return parts; + } case "TSArrayType": return [path.call(print, "elementType"), "[]"]; case "TSPropertySignature": { diff --git a/tests/typescript/as/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/as/__snapshots__/jsfmt.spec.js.snap index 5291b99dc..11c322eef 100644 --- a/tests/typescript/as/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/as/__snapshots__/jsfmt.spec.js.snap @@ -320,6 +320,33 @@ averredBathersBoxroomBuggyNurl( ================================================================================ `; +exports[`nested-await-and-as.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const getAccountCount = async () => + (await + ((await ( + await focusOnSection(BOOKMARKED_PROJECTS_SECTION_NAME) + ).findItem("My bookmarks")) as TreeItem + ).getChildren() + ).length + +=====================================output===================================== +const getAccountCount = async () => + ( + await ( + (await ( + await focusOnSection(BOOKMARKED_PROJECTS_SECTION_NAME) + ).findItem("My bookmarks")) as TreeItem + ).getChildren() + ).length; + +================================================================================ +`; + exports[`return.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript/as/nested-await-and-as.ts b/tests/typescript/as/nested-await-and-as.ts new file mode 100644 index 000000000..e43a727f7 --- /dev/null +++ b/tests/typescript/as/nested-await-and-as.ts @@ -0,0 +1,7 @@ +const getAccountCount = async () => + (await + ((await ( + await focusOnSection(BOOKMARKED_PROJECTS_SECTION_NAME) + ).findItem("My bookmarks")) as TreeItem + ).getChildren() + ).length