Skip to content

Commit df42336

Browse files
committed
Overload formatting in the symbol displaying
1 parent c450d8d commit df42336

File tree

7 files changed

+637
-490
lines changed

7 files changed

+637
-490
lines changed

src/compiler/checker.ts

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ module ts {
9999
getFullyQualifiedName: getFullyQualifiedName,
100100
getResolvedSignature: getResolvedSignature,
101101
getEnumMemberValue: getEnumMemberValue,
102-
isValidPropertyAccess: isValidPropertyAccess
102+
isValidPropertyAccess: isValidPropertyAccess,
103+
getSignatureFromDeclaration: getSignatureFromDeclaration,
104+
writeSignature: writeSignature,
105+
isImplementationOfOverload: isImplementationOfOverload
103106
};
104107

105108
var undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
@@ -1055,11 +1058,10 @@ module ts {
10551058
return result;
10561059
}
10571060

1058-
function writeType(type: Type, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags) {
1059-
var typeStack: Type[];
1060-
return writeType(type, /*allowFunctionOrConstructorTypeLiteral*/ true);
1061+
function writeType(type: Type, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, typeStack?: Type[]) {
1062+
return writeType(type, flags | TypeFormatFlags.WriteArrowStyleSignature);
10611063

1062-
function writeType(type: Type, allowFunctionOrConstructorTypeLiteral: boolean) {
1064+
function writeType(type: Type, flags: TypeFormatFlags) {
10631065
if (type.flags & TypeFlags.Intrinsic) {
10641066
writer.writeKind((<IntrinsicType>type).intrinsicName, SymbolDisplayPartKind.keyword);
10651067
}
@@ -1073,7 +1075,7 @@ module ts {
10731075
writeTupleType(<TupleType>type);
10741076
}
10751077
else if (type.flags & TypeFlags.Anonymous) {
1076-
writeAnonymousType(<ObjectType>type, allowFunctionOrConstructorTypeLiteral);
1078+
writeAnonymousType(<ObjectType>type, flags);
10771079
}
10781080
else if (type.flags & TypeFlags.StringLiteral) {
10791081
writer.writeKind((<StringLiteralType>type).text, SymbolDisplayPartKind.stringLiteral);
@@ -1095,15 +1097,15 @@ module ts {
10951097
writePunctuation(writer, SyntaxKind.CommaToken);
10961098
writeSpace(writer);
10971099
}
1098-
writeType(types[i], /*allowFunctionOrConstructorTypeLiteral*/ true);
1100+
writeType(types[i], flags | TypeFormatFlags.WriteArrowStyleSignature);
10991101
}
11001102
}
11011103

11021104
function writeTypeReference(type: TypeReference) {
11031105
if (type.target === globalArrayType && !(flags & TypeFormatFlags.WriteArrayAsGenericType)) {
11041106
// If we are writing array element type the arrow style signatures are not allowed as
11051107
// we need to surround it by curlies, e.g. { (): T; }[]; as () => T[] would mean something different
1106-
writeType(type.typeArguments[0], /*allowFunctionOrConstructorTypeLiteral*/ false);
1108+
writeType(type.typeArguments[0], flags & ~TypeFormatFlags.WriteArrowStyleSignature);
11071109
writePunctuation(writer, SyntaxKind.OpenBracketToken);
11081110
writePunctuation(writer, SyntaxKind.CloseBracketToken);
11091111
}
@@ -1121,7 +1123,7 @@ module ts {
11211123
writePunctuation(writer, SyntaxKind.CloseBracketToken);
11221124
}
11231125

1124-
function writeAnonymousType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) {
1126+
function writeAnonymousType(type: ObjectType, flags: TypeFormatFlags) {
11251127
// Always use 'typeof T' for type of class, enum, and module objects
11261128
if (type.symbol && type.symbol.flags & (SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
11271129
writeTypeofSymbol(type);
@@ -1139,7 +1141,7 @@ module ts {
11391141
typeStack = [];
11401142
}
11411143
typeStack.push(type);
1142-
writeLiteralType(type, allowFunctionOrConstructorTypeLiteral);
1144+
writeLiteralType(type, flags);
11431145
typeStack.pop();
11441146
}
11451147

@@ -1167,7 +1169,7 @@ module ts {
11671169
writeSymbol(type.symbol, writer, enclosingDeclaration, SymbolFlags.Value);
11681170
}
11691171

1170-
function writeLiteralType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) {
1172+
function writeLiteralType(type: ObjectType, flags: TypeFormatFlags) {
11711173
var resolved = resolveObjectTypeMembers(type);
11721174
if (!resolved.properties.length && !resolved.stringIndexType && !resolved.numberIndexType) {
11731175
if (!resolved.callSignatures.length && !resolved.constructSignatures.length) {
@@ -1176,15 +1178,15 @@ module ts {
11761178
return;
11771179
}
11781180

1179-
if (allowFunctionOrConstructorTypeLiteral) {
1181+
if (flags & TypeFormatFlags.WriteArrowStyleSignature) {
11801182
if (resolved.callSignatures.length === 1 && !resolved.constructSignatures.length) {
1181-
writeSignature(resolved.callSignatures[0], shouldTypeBeAllowStyleTypeLiteral());
1183+
writeSignature(resolved.callSignatures[0], writer, enclosingDeclaration, flags, typeStack);
11821184
return;
11831185
}
11841186
if (resolved.constructSignatures.length === 1 && !resolved.callSignatures.length) {
11851187
writeKeyword(writer, SyntaxKind.NewKeyword);
11861188
writeSpace(writer);
1187-
writeSignature(resolved.constructSignatures[0], shouldTypeBeAllowStyleTypeLiteral());
1189+
writeSignature(resolved.constructSignatures[0], writer, enclosingDeclaration, flags, typeStack);
11881190
return;
11891191
}
11901192
}
@@ -1194,15 +1196,15 @@ module ts {
11941196
writer.writeLine();
11951197
writer.increaseIndent();
11961198
for (var i = 0; i < resolved.callSignatures.length; i++) {
1197-
writeSignature(resolved.callSignatures[i]);
1199+
writeSignature(resolved.callSignatures[i], writer, enclosingDeclaration, flags & ~TypeFormatFlags.WriteArrowStyleSignature, typeStack);
11981200
writePunctuation(writer, SyntaxKind.SemicolonToken);
11991201
writer.writeLine();
12001202
}
12011203
for (var i = 0; i < resolved.constructSignatures.length; i++) {
12021204
writeKeyword(writer, SyntaxKind.NewKeyword);
12031205
writeSpace(writer);
12041206

1205-
writeSignature(resolved.constructSignatures[i]);
1207+
writeSignature(resolved.constructSignatures[i], writer, enclosingDeclaration, flags & ~TypeFormatFlags.WriteArrowStyleSignature, typeStack);
12061208
writePunctuation(writer, SyntaxKind.SemicolonToken);
12071209
writer.writeLine();
12081210
}
@@ -1216,7 +1218,7 @@ module ts {
12161218
writePunctuation(writer, SyntaxKind.CloseBracketToken);
12171219
writePunctuation(writer, SyntaxKind.ColonToken);
12181220
writeSpace(writer);
1219-
writeType(resolved.stringIndexType, /*allowFunctionOrConstructorTypeLiteral*/ true);
1221+
writeType(resolved.stringIndexType, flags | TypeFormatFlags.WriteArrowStyleSignature);
12201222
writePunctuation(writer, SyntaxKind.SemicolonToken);
12211223
writer.writeLine();
12221224
}
@@ -1230,7 +1232,7 @@ module ts {
12301232
writePunctuation(writer, SyntaxKind.CloseBracketToken);
12311233
writePunctuation(writer, SyntaxKind.ColonToken);
12321234
writeSpace(writer);
1233-
writeType(resolved.numberIndexType, /*allowFunctionOrConstructorTypeLiteral*/ true);
1235+
writeType(resolved.numberIndexType, flags | TypeFormatFlags.WriteArrowStyleSignature);
12341236
writePunctuation(writer, SyntaxKind.SemicolonToken);
12351237
writer.writeLine();
12361238
}
@@ -1244,7 +1246,7 @@ module ts {
12441246
if (isOptionalProperty(p)) {
12451247
writePunctuation(writer, SyntaxKind.QuestionToken);
12461248
}
1247-
writeSignature(signatures[j]);
1249+
writeSignature(signatures[j], writer, enclosingDeclaration, flags & ~TypeFormatFlags.WriteArrowStyleSignature, typeStack);
12481250
writePunctuation(writer, SyntaxKind.SemicolonToken);
12491251
writer.writeLine();
12501252
}
@@ -1256,72 +1258,67 @@ module ts {
12561258
}
12571259
writePunctuation(writer, SyntaxKind.ColonToken);
12581260
writeSpace(writer);
1259-
writeType(t, /*allowFunctionOrConstructorTypeLiteral*/ true);
1261+
writeType(t, flags | TypeFormatFlags.WriteArrowStyleSignature);
12601262
writePunctuation(writer, SyntaxKind.SemicolonToken);
12611263
writer.writeLine();
12621264
}
12631265
}
12641266
writer.decreaseIndent();
12651267
writePunctuation(writer, SyntaxKind.CloseBraceToken);
1266-
1267-
function shouldTypeBeAllowStyleTypeLiteral() {
1268-
return !typeStack || typeStack.length !== 1 || typeStack[0] !== type || // If this is not a top level type we are writing, arrowStyle is ok
1269-
!(flags & TypeFormatFlags.NoArrowStyleTopLevelSignature);
1270-
}
12711268
}
1269+
}
12721270

1273-
function writeSignature(signature: Signature, arrowStyle?: boolean) {
1274-
if (signature.typeParameters) {
1275-
writePunctuation(writer, SyntaxKind.LessThanToken);
1276-
for (var i = 0; i < signature.typeParameters.length; i++) {
1277-
if (i > 0) {
1278-
writePunctuation(writer, SyntaxKind.CommaToken);
1279-
writeSpace(writer);
1280-
}
1281-
var tp = signature.typeParameters[i];
1282-
writeSymbol(tp.symbol, writer);
1283-
var constraint = getConstraintOfTypeParameter(tp);
1284-
if (constraint) {
1285-
writeSpace(writer);
1286-
writeKeyword(writer, SyntaxKind.ExtendsKeyword);
1287-
writeSpace(writer);
1288-
writeType(constraint, /*allowFunctionOrConstructorTypeLiteral*/ true);
1289-
}
1290-
}
1291-
writePunctuation(writer, SyntaxKind.GreaterThanToken);
1292-
}
1293-
writePunctuation(writer, SyntaxKind.OpenParenToken);
1294-
for (var i = 0; i < signature.parameters.length; i++) {
1271+
function writeSignature(signature: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, typeStack?: Type[]) {
1272+
if (signature.typeParameters) {
1273+
writePunctuation(writer, SyntaxKind.LessThanToken);
1274+
for (var i = 0; i < signature.typeParameters.length; i++) {
12951275
if (i > 0) {
12961276
writePunctuation(writer, SyntaxKind.CommaToken);
12971277
writeSpace(writer);
12981278
}
1299-
var p = signature.parameters[i];
1300-
if (getDeclarationFlagsFromSymbol(p) & NodeFlags.Rest) {
1301-
writePunctuation(writer, SyntaxKind.DotDotDotToken);
1302-
}
1303-
writeSymbol(p, writer);
1304-
if (p.valueDeclaration.flags & NodeFlags.QuestionMark || (<VariableDeclaration>p.valueDeclaration).initializer) {
1305-
writePunctuation(writer, SyntaxKind.QuestionToken);
1279+
var tp = signature.typeParameters[i];
1280+
writeSymbol(tp.symbol, writer);
1281+
var constraint = getConstraintOfTypeParameter(tp);
1282+
if (constraint) {
1283+
writeSpace(writer);
1284+
writeKeyword(writer, SyntaxKind.ExtendsKeyword);
1285+
writeSpace(writer);
1286+
writeType(constraint, writer, enclosingDeclaration, flags, typeStack);
13061287
}
1307-
writePunctuation(writer, SyntaxKind.ColonToken);
1308-
writeSpace(writer);
1309-
1310-
writeType(getTypeOfSymbol(p), /*allowFunctionOrConstructorTypeLiteral*/ true);
13111288
}
1312-
1313-
writePunctuation(writer, SyntaxKind.CloseParenToken);
1314-
if (arrowStyle) {
1289+
writePunctuation(writer, SyntaxKind.GreaterThanToken);
1290+
}
1291+
writePunctuation(writer, SyntaxKind.OpenParenToken);
1292+
for (var i = 0; i < signature.parameters.length; i++) {
1293+
if (i > 0) {
1294+
writePunctuation(writer, SyntaxKind.CommaToken);
13151295
writeSpace(writer);
1316-
writePunctuation(writer, SyntaxKind.EqualsGreaterThanToken);
13171296
}
1318-
else {
1319-
writePunctuation(writer, SyntaxKind.ColonToken);
1297+
var p = signature.parameters[i];
1298+
if (getDeclarationFlagsFromSymbol(p) & NodeFlags.Rest) {
1299+
writePunctuation(writer, SyntaxKind.DotDotDotToken);
13201300
}
1301+
writeSymbol(p, writer);
1302+
if (p.valueDeclaration.flags & NodeFlags.QuestionMark || (<VariableDeclaration>p.valueDeclaration).initializer) {
1303+
writePunctuation(writer, SyntaxKind.QuestionToken);
1304+
}
1305+
writePunctuation(writer, SyntaxKind.ColonToken);
13211306
writeSpace(writer);
13221307

1323-
writeType(getReturnTypeOfSignature(signature), /*allowFunctionOrConstructorTypeLiteral*/ true);
1308+
writeType(getTypeOfSymbol(p), writer, enclosingDeclaration, flags, typeStack);
1309+
}
1310+
1311+
writePunctuation(writer, SyntaxKind.CloseParenToken);
1312+
if (flags & TypeFormatFlags.WriteArrowStyleSignature) {
1313+
writeSpace(writer);
1314+
writePunctuation(writer, SyntaxKind.EqualsGreaterThanToken);
13241315
}
1316+
else {
1317+
writePunctuation(writer, SyntaxKind.ColonToken);
1318+
}
1319+
writeSpace(writer);
1320+
1321+
writeType(getReturnTypeOfSignature(signature), writer, enclosingDeclaration, flags, typeStack);
13251322
}
13261323

13271324
function isDeclarationVisible(node: Declaration): boolean {

src/compiler/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ module ts {
651651
getRootSymbol(symbol: Symbol): Symbol;
652652
getContextualType(node: Node): Type;
653653
getResolvedSignature(node: CallExpression, candidatesOutArray?: Signature[]): Signature;
654+
getSignatureFromDeclaration(declaration: SignatureDeclaration): Signature;
655+
writeSignature(signatures: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
656+
isImplementationOfOverload(node: FunctionDeclaration): boolean;
654657

655658
// Returns the constant value of this enum member, or 'undefined' if the enum member has a
656659
// computed value.
@@ -678,7 +681,7 @@ module ts {
678681
WriteArrayAsGenericType = 0x00000001, // Write Array<T> instead T[]
679682
UseTypeOfFunction = 0x00000002, // Write typeof instead of function type literal
680683
NoTruncation = 0x00000004, // Don't truncate typeToString result
681-
NoArrowStyleTopLevelSignature = 0x00000008, // Do not write type global top level function or constructor literal
684+
WriteArrowStyleSignature= 0x00000008, // Write arrow style signature
682685
}
683686

684687
export enum SymbolAccessibility {

src/harness/fourslash.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,22 +1970,26 @@ module FourSlash {
19701970
this.taoInvalidReason = 'assertItemInCompletionList only supports the "name" parameter';
19711971
}
19721972

1973+
function assertionMessage(msg: string) {
1974+
return "\nMarker: " + currentTestState.lastKnownMarker + "\nChecking: " + msg + "\n\n";
1975+
}
1976+
19731977
for (var i = 0; i < items.length; i++) {
19741978
var item = items[i];
19751979
if (item.name == name) {
19761980
if (documentation != undefined || text !== undefined) {
19771981
var details = this.getCompletionEntryDetails(item.name);
19781982

19791983
if (documentation != undefined) {
1980-
assert.equal(ts.displayPartsToString(details.documentation), documentation);
1984+
assert.equal(ts.displayPartsToString(details.documentation), documentation, assertionMessage("completion item documentation"));
19811985
}
19821986
if (text !== undefined) {
1983-
assert.equal(ts.displayPartsToString(details.displayParts), text);
1987+
assert.equal(ts.displayPartsToString(details.displayParts), text, assertionMessage("completion item detail text"));
19841988
}
19851989
}
19861990

19871991
if (kind !== undefined) {
1988-
assert.equal(item.kind, kind);
1992+
assert.equal(item.kind, kind, assertionMessage("completion item kind"));
19891993
}
19901994

19911995
return;

0 commit comments

Comments
 (0)