@@ -1405,29 +1405,30 @@ module ts {
1405
1405
}
1406
1406
}
1407
1407
1408
- export function typeToDisplayParts ( typechecker : TypeChecker , type : Type , enclosingDeclaration ?: Node , flags ?: TypeFormatFlags ) : SymbolDisplayPart [ ] {
1408
+ function mapToDisplayParts ( writeDisplayParts : ( writer : DisplayPartsSymbolWriter ) => void ) : SymbolDisplayPart [ ] {
1409
1409
var displayPartWriter = getDisplayPartWriter ( ) ;
1410
- typechecker . writeType ( type , displayPartWriter , enclosingDeclaration , flags ) ;
1410
+ writeDisplayParts ( displayPartWriter ) ;
1411
1411
var result = displayPartWriter . displayParts ( ) ;
1412
1412
releaseDisplayPartWriter ( displayPartWriter ) ;
1413
1413
return result ;
1414
1414
}
1415
1415
1416
- export function symbolToDisplayParts ( typeChecker : TypeChecker , symbol : Symbol , enclosingDeclaration ?: Node , meaning ?: SymbolFlags ) : SymbolDisplayPart [ ] {
1417
- var displayPartWriter = getDisplayPartWriter ( ) ;
1418
- typeChecker . writeSymbol ( symbol , displayPartWriter , enclosingDeclaration , meaning ) ;
1419
- var result = displayPartWriter . displayParts ( ) ;
1420
- releaseDisplayPartWriter ( displayPartWriter ) ;
1416
+ export function typeToDisplayParts ( typechecker : TypeChecker , type : Type , enclosingDeclaration ?: Node , flags ?: TypeFormatFlags ) : SymbolDisplayPart [ ] {
1417
+ return mapToDisplayParts ( writer => {
1418
+ typechecker . writeType ( type , writer , enclosingDeclaration , flags ) ;
1419
+ } ) ;
1420
+ }
1421
1421
1422
- return result ;
1422
+ export function symbolToDisplayParts ( typeChecker : TypeChecker , symbol : Symbol , enclosingDeclaration ?: Node , meaning ?: SymbolFlags , flags ?: SymbolFormatFlags ) : SymbolDisplayPart [ ] {
1423
+ return mapToDisplayParts ( writer => {
1424
+ typeChecker . writeSymbol ( symbol , writer , enclosingDeclaration , meaning , flags ) ;
1425
+ } ) ;
1423
1426
}
1424
1427
1425
- function signatureToDisplayParts ( typechecker : TypeChecker , signature : Signature , enclosingDeclaration ?: Node , flags ?: TypeFormatFlags ) : SymbolDisplayPart [ ] {
1426
- var displayPartWriter = getDisplayPartWriter ( ) ;
1427
- typechecker . writeSignature ( signature , displayPartWriter , enclosingDeclaration , flags ) ;
1428
- var result = displayPartWriter . displayParts ( ) ;
1429
- releaseDisplayPartWriter ( displayPartWriter ) ;
1430
- return result ;
1428
+ function signatureToDisplayParts ( typechecker : TypeChecker , signature : Signature , enclosingDeclaration ?: Node , flags ?: TypeFormatFlags ) : SymbolDisplayPart [ ] {
1429
+ return mapToDisplayParts ( writer => {
1430
+ typechecker . writeSignature ( signature , writer , enclosingDeclaration , flags ) ;
1431
+ } ) ;
1431
1432
}
1432
1433
1433
1434
export function getDefaultCompilerOptions ( ) : CompilerOptions {
@@ -2792,13 +2793,13 @@ module ts {
2792
2793
if ( symbolFlags & SymbolFlags . Class && ! hasAddedSymbolInfo ) {
2793
2794
displayParts . push ( keywordPart ( SyntaxKind . ClassKeyword ) ) ;
2794
2795
displayParts . push ( spacePart ( ) ) ;
2795
- displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , sourceFile ) ) ;
2796
+ displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , sourceFile , /*meaning*/ undefined , SymbolFormatFlags . WriteTypeParametersOfClassOrInterface ) ) ;
2796
2797
}
2797
2798
if ( symbolFlags & SymbolFlags . Interface ) {
2798
2799
addNewLineIfDisplayPartsExist ( ) ;
2799
2800
displayParts . push ( keywordPart ( SyntaxKind . InterfaceKeyword ) ) ;
2800
2801
displayParts . push ( spacePart ( ) ) ;
2801
- displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , sourceFile ) ) ;
2802
+ displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , sourceFile , /*meaning*/ undefined , SymbolFormatFlags . WriteTypeParametersOfClassOrInterface ) ) ;
2802
2803
}
2803
2804
if ( symbolFlags & SymbolFlags . Enum ) {
2804
2805
addNewLineIfDisplayPartsExist ( ) ;
@@ -2819,6 +2820,25 @@ module ts {
2819
2820
displayParts . push ( punctuationPart ( SyntaxKind . CloseParenToken ) ) ;
2820
2821
displayParts . push ( spacePart ( ) ) ;
2821
2822
displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , enclosingDeclaration ) ) ;
2823
+ displayParts . push ( spacePart ( ) ) ;
2824
+ displayParts . push ( keywordPart ( SyntaxKind . InKeyword ) ) ;
2825
+ displayParts . push ( spacePart ( ) ) ;
2826
+ if ( symbol . parent ) {
2827
+ // Class/Interface type parameter
2828
+ displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol . parent , enclosingDeclaration , /*meaning*/ undefined , SymbolFormatFlags . WriteTypeParametersOfClassOrInterface ) )
2829
+ }
2830
+ else {
2831
+ // Method/function type parameter
2832
+ var signatureDeclaration = < SignatureDeclaration > getDeclarationOfKind ( symbol , SyntaxKind . TypeParameter ) . parent ;
2833
+ var signature = typeResolver . getSignatureFromDeclaration ( signatureDeclaration ) ;
2834
+ if ( signatureDeclaration . kind === SyntaxKind . ConstructSignature ) {
2835
+ displayParts . push ( keywordPart ( SyntaxKind . NewKeyword ) ) ;
2836
+ displayParts . push ( spacePart ( ) ) ;
2837
+ } else if ( signatureDeclaration . kind !== SyntaxKind . CallSignature ) {
2838
+ displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , signatureDeclaration . symbol , sourceFile , /*meaning*/ undefined , SymbolFormatFlags . WriteTypeParametersOfClassOrInterface ) )
2839
+ }
2840
+ displayParts . push . apply ( displayParts , signatureToDisplayParts ( typeResolver , signature , sourceFile , TypeFormatFlags . NoTruncation ) ) ;
2841
+ }
2822
2842
}
2823
2843
if ( symbolFlags & SymbolFlags . EnumMember ) {
2824
2844
addPrefixForAnyFunctionOrVar ( symbol , "enum member" ) ;
@@ -2848,7 +2868,16 @@ module ts {
2848
2868
symbolFlags & SymbolFlags . Variable ) {
2849
2869
displayParts . push ( punctuationPart ( SyntaxKind . ColonToken ) ) ;
2850
2870
displayParts . push ( spacePart ( ) ) ;
2851
- displayParts . push . apply ( displayParts , typeToDisplayParts ( typeResolver , type , enclosingDeclaration , TypeFormatFlags . NoTruncation ) ) ;
2871
+ // If the type is type parameter, format it specially
2872
+ if ( type . symbol && type . symbol . flags & SymbolFlags . TypeParameter ) {
2873
+ var typeParameterParts = mapToDisplayParts ( writer => {
2874
+ typeResolver . writeTypeParameter ( < TypeParameter > type , writer , enclosingDeclaration , TypeFormatFlags . NoTruncation ) ;
2875
+ } ) ;
2876
+ displayParts . push . apply ( displayParts , typeParameterParts ) ;
2877
+ }
2878
+ else {
2879
+ displayParts . push . apply ( displayParts , typeToDisplayParts ( typeResolver , type , enclosingDeclaration , TypeFormatFlags . NoTruncation ) ) ;
2880
+ }
2852
2881
}
2853
2882
else if ( symbolFlags & SymbolFlags . Function ||
2854
2883
symbolFlags & SymbolFlags . Method ||
@@ -2880,9 +2909,8 @@ module ts {
2880
2909
displayParts . push ( textPart ( symbolKind ) ) ;
2881
2910
displayParts . push ( punctuationPart ( SyntaxKind . CloseParenToken ) ) ;
2882
2911
displayParts . push ( spacePart ( ) ) ;
2883
- //if (symbol.declarations && symbol.declarations.length) {
2884
- displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , sourceFile ) ) ;
2885
- //}
2912
+ // Write type parameters of class/Interface if it is property/method of the generic class/interface
2913
+ displayParts . push . apply ( displayParts , symbolToDisplayParts ( typeResolver , symbol , sourceFile , /*meaning*/ undefined , SymbolFormatFlags . WriteTypeParametersOfClassOrInterface ) ) ;
2886
2914
}
2887
2915
}
2888
2916
0 commit comments