From 70a8767420a402595209b778c5bb189b845a03d3 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Tue, 30 Aug 2022 15:19:47 +0300 Subject: [PATCH 1/3] Print readonly modifier in printTypeParameter --- src/language-js/print/type-parameters.js | 7 ++ src/language-js/print/typescript.js | 7 -- .../__snapshots__/jsfmt.spec.js.snap | 92 +++++++++++++++++++ .../typescript/mapped-type/issue-11098.ts | 40 ++++++++ 4 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 tests/format/typescript/mapped-type/issue-11098.ts diff --git a/src/language-js/print/type-parameters.js b/src/language-js/print/type-parameters.js index efad3bc25a89..96dedef44c1b 100644 --- a/src/language-js/print/type-parameters.js +++ b/src/language-js/print/type-parameters.js @@ -16,6 +16,7 @@ import { shouldPrintComma, getFunctionParameters, isObjectType, + getTypeScriptMappedTypeModifier, } from "../utils/index.js"; import { createGroupIdMapper } from "../../common/util.js"; import { shouldHugType } from "./type-annotation.js"; @@ -116,6 +117,12 @@ function printTypeParameter(path, options, print) { const name = node.type === "TSTypeParameter" ? print("name") : node.name; if (parent.type === "TSMappedType") { + if (parent.readonly) { + parts.push( + getTypeScriptMappedTypeModifier(parent.readonly, "readonly"), + " " + ); + } parts.push("[", name); if (node.constraint) { parts.push(" in ", print("constraint")); diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index e3c73be2b46c..eda46c63b6ee 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -298,13 +298,6 @@ function printTypescript(path, options, print) { "{", indent([ options.bracketSpacing ? line : softline, - node.readonly - ? [ - getTypeScriptMappedTypeModifier(node.readonly, "readonly"), - " ", - ] - : "", - printTypeScriptModifiers(path, options, print), print("typeParameter"), node.optional ? getTypeScriptMappedTypeModifier(node.optional, "?") diff --git a/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap index 18ffc4528d72..a03234af4f40 100644 --- a/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap @@ -22,6 +22,98 @@ type Example = { ================================================================================ `; +exports[`issue-11098.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // comment1 + // comment2 + readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + -readonly [T in number]; +}; + +type Type = { + // comment + + readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // comment + [T in number]; +}; + +=====================================output===================================== +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // comment1 + // comment2 + readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + -readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // comment + [T in number]; +}; + +================================================================================ +`; + exports[`mapped-type.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/mapped-type/issue-11098.ts b/tests/format/typescript/mapped-type/issue-11098.ts new file mode 100644 index 000000000000..69570f3c3034 --- /dev/null +++ b/tests/format/typescript/mapped-type/issue-11098.ts @@ -0,0 +1,40 @@ +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // comment1 + // comment2 + readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + -readonly [T in number]; +}; + +type Type = { + // comment + + readonly [T in number]; +}; + +type Type = { + // comment + +readonly [T in number]; +}; + +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // comment + [T in number]; +}; From 436b066c4f663fbeb28776e0e588510ec245a02f Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Sun, 4 Sep 2022 15:20:41 +0300 Subject: [PATCH 2/3] Add changelog --- changelog_unreleased/typescript/13427.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 changelog_unreleased/typescript/13427.md diff --git a/changelog_unreleased/typescript/13427.md b/changelog_unreleased/typescript/13427.md new file mode 100644 index 000000000000..96f9b91da401 --- /dev/null +++ b/changelog_unreleased/typescript/13427.md @@ -0,0 +1,22 @@ +#### Fix leading comments in mapped types with `readonly` (#13427 by @thorn0, @sosukesuzuki) + + +```tsx +// Input +type Type = { + // comment + readonly [key in Foo]; +}; + +// Prettier stable +type Type = { + readonly // comment + [key in Foo]; +}; + +// Prettier main +type Type = { + // comment + readonly [key in Foo]; +}; +``` From b3fc46355062ead5bc982c3d3102b77cd3a5e549 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 5 Sep 2022 00:45:58 +0300 Subject: [PATCH 3/3] Add tests --- .../__snapshots__/jsfmt.spec.js.snap | 21 +++++++++++++++++++ .../typescript/mapped-type/issue-11098.ts | 11 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap index a03234af4f40..fe565652cc5f 100644 --- a/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap @@ -69,6 +69,17 @@ type Type = { [T in number]; }; +type Type = { + readonly + // comment + [T in number]; +}; + +type Type = { + readonly // foo + /* bar */ [T in number]; +}; + =====================================output===================================== type Type = { // comment @@ -111,6 +122,16 @@ type Type = { [T in number]; }; +type Type = { + // comment + readonly [T in number]; +}; + +type Type = { + // foo + /* bar */ readonly [T in number]; +}; + ================================================================================ `; diff --git a/tests/format/typescript/mapped-type/issue-11098.ts b/tests/format/typescript/mapped-type/issue-11098.ts index 69570f3c3034..2024b8eaf49f 100644 --- a/tests/format/typescript/mapped-type/issue-11098.ts +++ b/tests/format/typescript/mapped-type/issue-11098.ts @@ -38,3 +38,14 @@ type Type = { // comment [T in number]; }; + +type Type = { + readonly + // comment + [T in number]; +}; + +type Type = { + readonly // foo + /* bar */ [T in number]; +};