Improved parameter names for call signatures resulting from unions when only one parameter name is available. (#32056)

This commit is contained in:
Titian Cernicova-Dragomir 2019-07-11 20:06:49 +03:00 committed by Wesley Wigham
parent fbdbb141a2
commit d2c9d6cc1b
2 changed files with 24 additions and 5 deletions

View File

@ -7247,9 +7247,11 @@ namespace ts {
}
function combineUnionParameters(left: Signature, right: Signature) {
const longest = getParameterCount(left) >= getParameterCount(right) ? left : right;
const leftCount = getParameterCount(left);
const rightCount = getParameterCount(right);
const longest = leftCount >= rightCount ? left : right;
const shorter = longest === left ? right : left;
const longestCount = getParameterCount(longest);
const longestCount = longest === left ? leftCount : rightCount;
const eitherHasEffectiveRest = (hasEffectiveRestParameter(left) || hasEffectiveRestParameter(right));
const needsExtraRestElement = eitherHasEffectiveRest && !hasEffectiveRestParameter(longest);
const params = new Array<Symbol>(longestCount + (needsExtraRestElement ? 1 : 0));
@ -7259,11 +7261,16 @@ namespace ts {
const unionParamType = getIntersectionType([longestParamType, shorterParamType]);
const isRestParam = eitherHasEffectiveRest && !needsExtraRestElement && i === (longestCount - 1);
const isOptional = i >= getMinArgumentCount(longest) && i >= getMinArgumentCount(shorter);
const leftName = getParameterNameAtPosition(left, i);
const rightName = getParameterNameAtPosition(right, i);
const leftName = i >= leftCount ? undefined : getParameterNameAtPosition(left, i);
const rightName = i >= rightCount ? undefined : getParameterNameAtPosition(right, i);
const paramName = leftName === rightName ? leftName :
!leftName ? rightName :
!rightName ? leftName :
undefined;
const paramSymbol = createSymbol(
SymbolFlags.FunctionScopedVariable | (isOptional && !isRestParam ? SymbolFlags.Optional : 0),
leftName === rightName ? leftName : `arg${i}` as __String
paramName || `arg${i}` as __String
);
paramSymbol.type = isRestParam ? createArrayType(unionParamType) : unionParamType;
params[i] = paramSymbol;

View File

@ -34,6 +34,14 @@
//// ;
////
////callableThing4(/*4*/);
////
////declare const callableThing5:
//// | (<U>(a1: U) => void)
//// | (() => void)
//// ;
////
////callableThing5(/*5*/1)
////
verify.signatureHelp({
marker: "1",
@ -50,4 +58,8 @@ verify.signatureHelp({
{
marker: "4",
text: "callableThing4(arg0: { x: number; } & { y: number; } & { z: number; } & { u: number; } & { v: number; } & { w: number; }): void"
},
{
marker: "5",
text: "callableThing5(a1: number): void"
});