From 3654108ebe028fbc3063ceccb3c4ce0d4164510a Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 25 May 2019 07:00:32 +0900 Subject: [PATCH] TypeScript: Keep line breaks within mapped types. (#6146) --- CHANGELOG.unreleased.md | 21 ++++++++ src/language-js/printer-estree.js | 14 ++++-- .../__snapshots__/jsfmt.spec.js.snap | 4 +- .../compiler/__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 16 ++++-- .../__snapshots__/jsfmt.spec.js.snap | 50 ++++++++++++++++--- tests/typescript_comments/mapped_types.ts | 12 +++++ .../__snapshots__/jsfmt.spec.js.snap | 2 +- 8 files changed, 104 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 914cde90..d75da2cc 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -363,6 +363,26 @@ new (x())!.y(); new e[f().x].y(); ``` +### TypeScript: Keep line breaks within mapped types.([#6146] by [@sosukesuzuki]) + +Previously, Prettier has removed line breaks within mapped types.This change keeps it, similar to how it treats other object types. + + +```ts +// Input +type A = { + readonly [P in keyof T]: T[P]; +}; + +// Output (Prettier stable) +type A = { readonly [P in keyof T]: T[P] }; + +// Output (Prettier master) +type A = { + readonly [P in keyof T]: T[P]; +}; +``` + [#5979]: https://github.com/prettier/prettier/pull/5979 [#6086]: https://github.com/prettier/prettier/pull/6086 [#6088]: https://github.com/prettier/prettier/pull/6088 @@ -380,6 +400,7 @@ new e[f().x].y(); [#6136]: https://github.com/prettier/prettier/pull/6136 [#6140]: https://github.com/prettier/prettier/pull/6140 [#6148]: https://github.com/prettier/prettier/pull/6148 +[#6146]: https://github.com/prettier/prettier/pull/6146 [@belochub]: https://github.com/belochub [@brainkim]: https://github.com/brainkim [@duailibe]: https://github.com/duailibe diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 09cc3a33..b47eebe9 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3213,7 +3213,12 @@ function printPathNoParens(path, options, print, args) { } case "TSTypeOperator": return concat([n.operator, " ", path.call(print, "typeAnnotation")]); - case "TSMappedType": + case "TSMappedType": { + const shouldBreak = hasNewlineInRange( + options.originalText, + options.locStart(n), + options.locEnd(n) + ); return group( concat([ "{", @@ -3232,14 +3237,17 @@ function printPathNoParens(path, options, print, args) { ? getTypeScriptMappedTypeModifier(n.optional, "?") : "", ": ", - path.call(print, "typeAnnotation") + path.call(print, "typeAnnotation"), + shouldBreak && options.semi ? ";" : "" ]) ), comments.printDanglingComments(path, options, /* sameIndent */ true), options.bracketSpacing ? line : softline, "}" - ]) + ]), + { shouldBreak } ); + } case "TSMethodSignature": parts.push( n.accessibility ? concat([n.accessibility, " "]) : "", diff --git a/tests/multiparser_html_ts/__snapshots__/jsfmt.spec.js.snap b/tests/multiparser_html_ts/__snapshots__/jsfmt.spec.js.snap index 64ae0003..4b5b82d0 100644 --- a/tests/multiparser_html_ts/__snapshots__/jsfmt.spec.js.snap +++ b/tests/multiparser_html_ts/__snapshots__/jsfmt.spec.js.snap @@ -32,7 +32,9 @@ printWidth: 80