TypeScript: Keep line breaks within mapped types. (#6146)

master
Sosuke Suzuki 2019-05-25 07:00:32 +09:00 committed by Lucas Duailibe
parent 28727c3bb6
commit 3654108ebe
8 changed files with 104 additions and 17 deletions

View File

@ -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.
<!-- prettier-ignore -->
```ts
// Input
type A<T> = {
readonly [P in keyof T]: T[P];
};
// Output (Prettier stable)
type A<T> = { readonly [P in keyof T]: T[P] };
// Output (Prettier master)
type A<T> = {
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

View File

@ -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, " "]) : "",

View File

@ -32,7 +32,9 @@ printWidth: 80
<html lang="en">
<head>
<script lang="ts">
type X = { [K in keyof Y]: Partial<K> };
type X = {
[K in keyof Y]: Partial<K>;
};
class Foo<T> {
constructor(private foo: keyof Apple) {}

View File

@ -617,7 +617,7 @@ type Meta<T, A> = {
value: T[P];
also: A;
readonly children: Meta<T[P], A>;
}
};
};
interface Input {

View File

@ -15,9 +15,13 @@ type ReadonlyPartial<T> = {
}; // Add readonly and ?
=====================================output=====================================
type MutableRequired<T> = { -readonly [P in keyof T]-?: T[P] }; // Remove readonly and ?
type MutableRequired<T> = {
-readonly [P in keyof T]-?: T[P];
}; // Remove readonly and ?
type ReadonlyPartial<T> = { +readonly [P in keyof T]+?: T[P] }; // Add readonly and ?
type ReadonlyPartial<T> = {
+readonly [P in keyof T]+?: T[P];
}; // Add readonly and ?
================================================================================
`;
@ -33,7 +37,9 @@ var x: {
};
=====================================output=====================================
var x: { [A in keyof B]?: any };
var x: {
[A in keyof B]?: any;
};
================================================================================
`;
@ -49,7 +55,9 @@ var x: {
};
=====================================output=====================================
var x: { readonly [A in keyof B]: any };
var x: {
readonly [A in keyof B]: any;
};
================================================================================
`;

View File

@ -172,23 +172,59 @@ type G = {
[g in G] /* commentG */: string
}
type H = { /* commentH */ [h in H]: string }
type I = { [/* commentI */ i in I]: string }
type J = { [j /* commentJ */ in J]: string }
type K = { [k in /* commentK */ K]: string }
type L = { [l in L /* commentL */]: string }
type M = { [m in M] /* commentG */: string }
=====================================output=====================================
type A = {
// commentA
[a in A]: string
[a in A]: string;
};
type B = { /* commentB */ [b in B]: string };
type B = {
/* commentB */ [b in B]: string;
};
type C = { [/* commentC */ c in C]: string };
type C = {
[/* commentC */ c in C]: string;
};
type D = { [d /* commentD */ in D]: string };
type D = {
[d /* commentD */ in D]: string;
};
type E = { [e in /* commentE */ E]: string };
type E = {
[e in /* commentE */ E]: string;
};
type F = { [f in F /* commentF */]: string };
type F = {
[f in F /* commentF */]: string;
};
type G = { [g in G /* commentG */]: string };
type G = {
[g in G /* commentG */]: string;
};
type H = { [/* commentH */ h in H]: string };
type I = { [/* commentI */ i in I]: string };
type J = { [j /* commentJ */ in J]: string };
type K = { [k in /* commentK */ K]: string };
type L = { [l in L /* commentL */]: string };
type M = { [m in M /* commentG */]: string };
================================================================================
`;

View File

@ -26,3 +26,15 @@ type F = {
type G = {
[g in G] /* commentG */: string
}
type H = { /* commentH */ [h in H]: string }
type I = { [/* commentI */ i in I]: string }
type J = { [j /* commentJ */ in J]: string }
type K = { [k in /* commentK */ K]: string }
type L = { [l in L /* commentL */]: string }
type M = { [m in M] /* commentG */: string }

View File

@ -38,7 +38,7 @@ type NonFunctionPropertyNames<T> = {
interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}
type DeepReadonlyObject<T> = {
readonly [P in NonFunctionPropertyNames<T>]: DeepReadonly<T[P]>
readonly [P in NonFunctionPropertyNames<T>]: DeepReadonly<T[P]>;
};
type TypeName<T> = T extends string