Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leading comments in mapped types with readonly #13427

Merged
merged 3 commits into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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";
Expand Down Expand Up @@ -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"));
Expand Down
7 changes: 0 additions & 7 deletions src/language-js/print/typescript.js
Expand Up @@ -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, "?")
Expand Down
Expand Up @@ -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"]
Expand Down
40 changes: 40 additions & 0 deletions 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];
};