Skip to content

fix(49151): formatter wrongly indent '>' after type parameters/arguments #49165

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

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@ namespace ts.formatting {
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxClosingElement:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.ExpressionWithTypeArguments:
return false;
}
break;
Expand Down Expand Up @@ -835,7 +834,7 @@ namespace ts.formatting {
const listEndToken = getCloseTokenForOpenToken(listStartToken);
if (listEndToken !== SyntaxKind.Unknown && formattingScanner.isOnToken() && formattingScanner.getStartPos() < originalRange.end) {
let tokenInfo: TokenInfo | undefined = formattingScanner.readTokenInfo(parent);
if (tokenInfo.token.kind === SyntaxKind.CommaToken && isCallLikeExpression(parent)) {
if (tokenInfo.token.kind === SyntaxKind.CommaToken) {
// consume the comma
consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent);
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
Expand Down Expand Up @@ -1311,6 +1310,12 @@ namespace ts.formatting {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.ArrowFunction:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructorType:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
if ((node as FunctionDeclaration).typeParameters === list) {
return SyntaxKind.LessThanToken;
}
Expand All @@ -1327,7 +1332,19 @@ namespace ts.formatting {
return SyntaxKind.OpenParenToken;
}
break;
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.TypeAliasDeclaration:
if ((node as ClassDeclaration).typeParameters === list) {
return SyntaxKind.LessThanToken;
}
break;
case SyntaxKind.TypeReference:
case SyntaxKind.TaggedTemplateExpression:
case SyntaxKind.TypeQuery:
case SyntaxKind.ExpressionWithTypeArguments:
case SyntaxKind.ImportType:
if ((node as TypeReferenceNode).typeArguments === list) {
return SyntaxKind.LessThanToken;
}
Expand Down
140 changes: 140 additions & 0 deletions tests/cases/fourslash/genericsFormattingMultiline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/// <reference path='fourslash.ts' />

////
//// class Foo <
//// T1 extends unknown,
//// T2
//// > {
//// public method <
//// T3,
//// > (a: T1, b: Array <
//// string
//// > ): Map <
//// T1 ,
//// Array < T3 >
//// > { throw new Error(); }
//// }
////
//// interface IFoo<
//// T,
//// > {
//// new < T
//// > ( a: T);
//// op?<
//// T,
//// M
//// > (a: T, b : M );
//// <
//// T,
//// >(x: T): T;
//// }
////
//// type foo<
//// T
//// > = Foo <
//// number, Array < number > > ;
////
//// function bar <
//// T, U extends T
//// > () {
//// return class <
//// T2,
//// > {
//// }
//// }
////
//// bar<
//// string,
//// "s"
//// > ();
////
//// declare const func: <
//// T extends number[],
//// > (x: T) => new <
//// U
//// > () => U;
////
//// class A < T > extends bar <
//// T,number
//// >( ) < T
//// > {
//// }
////
//// function s<T, U>(x: TemplateStringsArray, ...args: any[]) { return x.join(); }
////
//// const t = s<
//// number ,
//// string[] & ArrayLike<any>
//// >`abc${1}def` ;
////


format.document();

verify.currentFileContentIs(`
class Foo<
T1 extends unknown,
T2
> {
public method<
T3,
>(a: T1, b: Array<
string
>): Map<
T1,
Array<T3>
> { throw new Error(); }
}

interface IFoo<
T,
> {
new <T
>(a: T);
op?<
T,
M
>(a: T, b: M);
<
T,
>(x: T): T;
}

type foo<
T
> = Foo<
number, Array<number>>;

function bar<
T, U extends T
>() {
return class <
T2,
> {
}
}

bar<
string,
"s"
>();

declare const func: <
T extends number[],
> (x: T) => new <
U
> () => U;

class A<T> extends bar<
T, number
>()<T
> {
}

function s<T, U>(x: TemplateStringsArray, ...args: any[]) { return x.join(); }

const t = s<
number,
string[] & ArrayLike<any>
>\`abc\${1}def\`;
`);