Skip to content

Commit

Permalink
Fix leading comments in mapped types with readonly (Cherry-pick pre…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Jan 4, 2024
1 parent c81ec23 commit cf005ae
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 7 deletions.
22 changes: 22 additions & 0 deletions changelog_unreleased/typescript/13427.md
@@ -0,0 +1,22 @@
#### Fix leading comments in mapped types with `readonly` (#13427 by @thorn0, @sosukesuzuki)

<!-- prettier-ignore -->
```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];
};
```
7 changes: 7 additions & 0 deletions src/language-js/print/type-parameters.js
Expand Up @@ -12,6 +12,7 @@ const {
shouldPrintComma,
getFunctionParameters,
isObjectType,
getTypeScriptMappedTypeModifier,
} = require("../utils/index.js");
const { createGroupIdMapper } = require("../../common/util.js");
const { shouldHugType } = require("./type-annotation.js");
Expand Down Expand Up @@ -109,6 +110,12 @@ function printTypeParameter(path, options, print) {
const parts = [];
const parent = path.getParentNode();
if (parent.type === "TSMappedType") {
if (parent.readonly) {
parts.push(
getTypeScriptMappedTypeModifier(parent.readonly, "readonly"),
" "
);
}
parts.push("[", print("name"));
if (node.constraint) {
parts.push(" in ", print("constraint"));
Expand Down
7 changes: 0 additions & 7 deletions src/language-js/print/typescript.js
Expand Up @@ -307,13 +307,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, "?")
Expand Down
113 changes: 113 additions & 0 deletions tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -22,6 +22,119 @@ 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];
};
type Type = {
readonly
// comment
[T in number];
};
type Type = {
readonly // foo
/* bar */ [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];
};
type Type = {
// comment
readonly [T in number];
};
type Type = {
// foo
/* bar */ readonly [T in number];
};
================================================================================
`;

exports[`mapped-type.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
51 changes: 51 additions & 0 deletions tests/format/typescript/mapped-type/issue-11098.ts
@@ -0,0 +1,51 @@
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];
};

type Type = {
readonly
// comment
[T in number];
};

type Type = {
readonly // foo
/* bar */ [T in number];
};

0 comments on commit cf005ae

Please sign in to comment.