Skip to content

Commit 2486aa9

Browse files
committed
Refactor collectCandidates and add type parameters to signature display
1 parent 7367053 commit 2486aa9

File tree

2 files changed

+56
-48
lines changed

2 files changed

+56
-48
lines changed

src/compiler/checker.ts

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,50 +4143,6 @@ module ts {
41434143
return isCorrect;
41444144
}
41454145

4146-
// The candidate list orders groups in reverse, but within a group signatures are kept in declaration order
4147-
// A nit here is that we reorder only signatures that belong to the same symbol,
4148-
// so order how inherited signatures are processed is still preserved.
4149-
// interface A { (x: string): void }
4150-
// interface B extends A { (x: 'foo'): string }
4151-
// var b: B;
4152-
// b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void]
4153-
function collectCandidates(node: CallExpression, signatures: Signature[], candidatesOutArray: Signature[]): Signature[]{
4154-
var result: Signature[] = candidatesOutArray || [];
4155-
var lastParent: Node;
4156-
var lastSymbol: Symbol;
4157-
var cutoffPos: number = 0;
4158-
var pos: number;
4159-
for (var i = 0; i < signatures.length; i++) {
4160-
var signature = signatures[i];
4161-
if (true) {
4162-
var symbol = signature.declaration && getSymbolOfNode(signature.declaration);
4163-
var parent = signature.declaration && signature.declaration.parent;
4164-
if (!lastSymbol || symbol === lastSymbol) {
4165-
if (lastParent && parent === lastParent) {
4166-
pos++;
4167-
}
4168-
else {
4169-
lastParent = parent;
4170-
pos = cutoffPos;
4171-
}
4172-
}
4173-
else {
4174-
// current declaration belongs to a different symbol
4175-
// set cutoffPos so re-orderings in the future won't change result set from 0 to cutoffPos
4176-
pos = cutoffPos = result.length;
4177-
lastParent = parent;
4178-
}
4179-
lastSymbol = symbol;
4180-
4181-
for (var j = result.length; j > pos; j--) {
4182-
result[j] = result[j - 1];
4183-
}
4184-
result[pos] = signature;
4185-
}
4186-
}
4187-
return result;
4188-
}
4189-
41904146
// If type has a single call signature and no other members, return that signature. Otherwise, return undefined.
41914147
function getSingleCallSignature(type: Type): Signature {
41924148
if (type.flags & TypeFlags.ObjectType) {
@@ -4280,7 +4236,9 @@ module ts {
42804236

42814237
function resolveCall(node: CallExpression, signatures: Signature[], candidatesOutArray: Signature[]): Signature {
42824238
forEach(node.typeArguments, checkSourceElement);
4283-
var candidates = collectCandidates(node, signatures, candidatesOutArray);
4239+
var candidates = candidatesOutArray || [];
4240+
// collectCandidates fills up the candidates array directly
4241+
collectCandidates();
42844242
if (!candidates.length) {
42854243
error(node, Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target);
42864244
return resolveErrorCall(node);
@@ -4336,6 +4294,50 @@ module ts {
43364294
return resolveErrorCall(node);
43374295
}
43384296
return resolveErrorCall(node);
4297+
4298+
// The candidate list orders groups in reverse, but within a group signatures are kept in declaration order
4299+
// A nit here is that we reorder only signatures that belong to the same symbol,
4300+
// so order how inherited signatures are processed is still preserved.
4301+
// interface A { (x: string): void }
4302+
// interface B extends A { (x: 'foo'): string }
4303+
// var b: B;
4304+
// b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void]
4305+
function collectCandidates(): void {
4306+
var result = candidates;
4307+
var lastParent: Node;
4308+
var lastSymbol: Symbol;
4309+
var cutoffPos: number = 0;
4310+
var pos: number;
4311+
Debug.assert(!result.length);
4312+
for (var i = 0; i < signatures.length; i++) {
4313+
var signature = signatures[i];
4314+
if (true) {
4315+
var symbol = signature.declaration && getSymbolOfNode(signature.declaration);
4316+
var parent = signature.declaration && signature.declaration.parent;
4317+
if (!lastSymbol || symbol === lastSymbol) {
4318+
if (lastParent && parent === lastParent) {
4319+
pos++;
4320+
}
4321+
else {
4322+
lastParent = parent;
4323+
pos = cutoffPos;
4324+
}
4325+
}
4326+
else {
4327+
// current declaration belongs to a different symbol
4328+
// set cutoffPos so re-orderings in the future won't change result set from 0 to cutoffPos
4329+
pos = cutoffPos = result.length;
4330+
lastParent = parent;
4331+
}
4332+
lastSymbol = symbol;
4333+
4334+
for (var j = result.length; j > pos; j--) {
4335+
result[j] = result[j - 1];
4336+
}
4337+
result[pos] = signature;
4338+
}
4339+
}
4340+
}
43394341
}
43404342

43414343
function resolveCallExpression(node: CallExpression, candidatesOutArray: Signature[]): Signature {

src/services/services.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3564,9 +3564,15 @@ module ts {
35643564
display += ": " + typeInfoResolver.typeToString(typeInfoResolver.getTypeOfSymbol(p), argumentListOrTypeArgumentList);
35653565
return new SignatureHelpParameter(p.name, "", display, isOptional);
35663566
});
3567-
var callTarget = (<CallExpression>argumentListOrTypeArgumentList.parent).func;
3568-
var signatureName = typeInfoResolver.symbolToString(typeInfoResolver.getSymbolInfo(callTarget), /*enclosingDeclaration*/ undefined, /*meaning*/ undefined);
3569-
var prefix = signatureName + "(";
3567+
var callTargetNode = (<CallExpression>argumentListOrTypeArgumentList.parent).func;
3568+
var callTargetSymbol = typeInfoResolver.getSymbolInfo(callTargetNode);
3569+
var signatureName = callTargetSymbol ? typeInfoResolver.symbolToString(callTargetSymbol, /*enclosingDeclaration*/ undefined, /*meaning*/ undefined) : "";
3570+
var prefix = signatureName;
3571+
// TODO(jfreeman): Constraints?
3572+
if (candidateSignature.typeParameters && candidateSignature.typeParameters.length) {
3573+
prefix += "<" + map(candidateSignature.typeParameters, tp => tp.symbol.name).join(", ") + ">";
3574+
}
3575+
prefix += "(";
35703576
var suffix = "): " + typeInfoResolver.typeToString(candidateSignature.getReturnType(), argumentListOrTypeArgumentList);
35713577
return new SignatureHelpItem(candidateSignature.hasRestParameter, prefix, suffix, ", ", parameterHelpItems, "");
35723578
});

0 commit comments

Comments
 (0)