Preserve arity for preserving js optional parameters (#37173)

This commit is contained in:
Wesley Wigham 2020-03-04 00:48:53 -08:00 committed by GitHub
parent 7a82b74983
commit dfc0b58fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 3 deletions

View File

@ -10510,7 +10510,7 @@ namespace ts {
const signature = getSignatureFromDeclaration(node.parent);
const parameterIndex = node.parent.parameters.indexOf(node);
Debug.assert(parameterIndex >= 0);
return parameterIndex >= getMinArgumentCount(signature);
return parameterIndex >= getMinArgumentCount(signature, /*strongArityForUntypedJS*/ true);
}
const iife = getImmediatelyInvokedFunctionExpression(node.parent);
if (iife) {
@ -10601,6 +10601,9 @@ namespace ts {
isValueSignatureDeclaration(declaration) &&
!hasJSDocParameterTags(declaration) &&
!getJSDocType(declaration);
if (isUntypedSignatureInJSFile) {
flags |= SignatureFlags.IsUntypedSignatureInJSFile;
}
// If this is a JSDoc construct signature, then skip the first parameter in the
// parameter list. The first parameter represents the return type of the construct
@ -10631,7 +10634,6 @@ namespace ts {
const isOptionalParameter = isOptionalJSDocParameterTag(param) ||
param.initializer || param.questionToken || param.dotDotDotToken ||
iife && parameters.length > iife.arguments.length && !type ||
isUntypedSignatureInJSFile ||
isJSDocOptionalParameter(param);
if (!isOptionalParameter) {
minArgumentCount = parameters.length;
@ -26375,7 +26377,7 @@ namespace ts {
return length;
}
function getMinArgumentCount(signature: Signature) {
function getMinArgumentCount(signature: Signature, strongArityForUntypedJS?: boolean) {
if (signatureHasRestParameter(signature)) {
const restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
if (isTupleType(restType)) {
@ -26385,6 +26387,9 @@ namespace ts {
}
}
}
if (!strongArityForUntypedJS && signature.flags & SignatureFlags.IsUntypedSignatureInJSFile) {
return 0;
}
return signature.minArgumentCount;
}

View File

@ -4817,6 +4817,7 @@ namespace ts {
HasLiteralTypes = 1 << 1, // Indicates signature is specialized
IsInnerCallChain = 1 << 2, // Indicates signature comes from a CallChain nested in an outer OptionalChain
IsOuterCallChain = 1 << 3, // Indicates signature comes from a CallChain that is the outermost chain of an optional expression
IsUntypedSignatureInJSFile = 1 << 4, // Indicates signature is from a js file and has no types
// We do not propagate `IsInnerCallChain` to instantiated signatures, as that would result in us
// attempting to add `| undefined` on each recursive call to `getReturnTypeOfSignature` when

View File

@ -0,0 +1,27 @@
//// [bar.js]
export class Z {
f(x = 1, y) {
return [x, y];
}
}
//// [bar.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Z = void 0;
var Z = /** @class */ (function () {
function Z() {
}
Z.prototype.f = function (x, y) {
if (x === void 0) { x = 1; }
return [x, y];
};
return Z;
}());
exports.Z = Z;
//// [bar.d.ts]
export class Z {
f(x: number, y: any): any[];
}

View File

@ -0,0 +1,14 @@
=== tests/cases/conformance/jsdoc/declarations/bar.js ===
export class Z {
>Z : Symbol(Z, Decl(bar.js, 0, 0))
f(x = 1, y) {
>f : Symbol(Z.f, Decl(bar.js, 0, 16))
>x : Symbol(x, Decl(bar.js, 1, 6))
>y : Symbol(y, Decl(bar.js, 1, 12))
return [x, y];
>x : Symbol(x, Decl(bar.js, 1, 6))
>y : Symbol(y, Decl(bar.js, 1, 12))
}
}

View File

@ -0,0 +1,16 @@
=== tests/cases/conformance/jsdoc/declarations/bar.js ===
export class Z {
>Z : Z
f(x = 1, y) {
>f : (x: number, y: any) => any[]
>x : number
>1 : 1
>y : any
return [x, y];
>[x, y] : any[]
>x : number
>y : any
}
}

View File

@ -0,0 +1,11 @@
// @allowJs: true
// @checkJs: true
// @target: es5
// @outDir: ./out
// @declaration: true
// @filename: bar.js
export class Z {
f(x = 1, y) {
return [x, y];
}
}