Skip to content

Commit

Permalink
Fixed an out-of-order quick info issue with contextual rest parameter (
Browse files Browse the repository at this point in the history
…#57580)

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
  • Loading branch information
Andarist and sandersn authored Mar 12, 2024
1 parent 9bdfa44 commit 3e91592
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
19 changes: 12 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15122,11 +15122,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return symbol && withAugmentations ? getMergedSymbol(symbol) : symbol;
}

function hasEffectiveQuestionToken(node: ParameterDeclaration | JSDocParameterTag | JSDocPropertyTag) {
return hasQuestionToken(node) || isOptionalJSDocPropertyLikeTag(node) || isParameter(node) && isJSDocOptionalParameter(node);
}

function isOptionalParameter(node: ParameterDeclaration | JSDocParameterTag | JSDocPropertyTag) {
if (hasQuestionToken(node) || isOptionalJSDocPropertyLikeTag(node) || isJSDocOptionalParameter(node)) {
if (hasEffectiveQuestionToken(node)) {
return true;
}

if (!isParameter(node)) {
return false;
}
if (node.initializer) {
const signature = getSignatureFromDeclaration(node.parent);
const parameterIndex = node.parent.parameters.indexOf(node);
Expand Down Expand Up @@ -15256,10 +15262,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

// Record a new minimum argument count if this is not an optional parameter
const isOptionalParameter = isOptionalJSDocPropertyLikeTag(param) ||
param.initializer || param.questionToken || isRestParameter(param) ||
iife && parameters.length > iife.arguments.length && !type ||
isJSDocOptionalParameter(param);
const isOptionalParameter = hasEffectiveQuestionToken(param) ||
isParameter(param) && param.initializer || isRestParameter(param) ||
iife && parameters.length > iife.arguments.length && !type;
if (!isOptionalParameter) {
minArgumentCount = parameters.length;
}
Expand Down Expand Up @@ -49601,7 +49606,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_parameter_cannot_have_an_initializer);
}
}
else if (isOptionalParameter(parameter)) {
else if (hasEffectiveQuestionToken(parameter)) {
seenOptionalParameter = true;
if (parameter.questionToken && parameter.initializer) {
return grammarErrorOnNode(parameter.name, Diagnostics.Parameter_cannot_have_question_mark_and_initializer);
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ import {
JSDocMemberName,
JSDocOverloadTag,
JSDocParameterTag,
JSDocPropertyLikeTag,
JSDocSatisfiesExpression,
JSDocSatisfiesTag,
JSDocSignature,
Expand Down Expand Up @@ -10485,7 +10484,7 @@ export function canHaveExportModifier(node: Node): node is Extract<HasModifiers,
}

/** @internal */
export function isOptionalJSDocPropertyLikeTag(node: Node): node is JSDocPropertyLikeTag {
export function isOptionalJSDocPropertyLikeTag(node: Node): boolean {
if (!isJSDocPropertyLikeTag(node)) {
return false;
}
Expand Down Expand Up @@ -10514,7 +10513,7 @@ export function isJSDocOptionalParameter(node: ParameterDeclaration) {
return isInJSFile(node) && (
// node.type should only be a JSDocOptionalType when node is a parameter of a JSDocFunctionType
node.type && node.type.kind === SyntaxKind.JSDocOptionalType
|| getJSDocParameterTags(node).some(({ isBracketed, typeExpression }) => isBracketed || !!typeExpression && typeExpression.type.kind === SyntaxKind.JSDocOptionalType)
|| getJSDocParameterTags(node).some(isOptionalJSDocPropertyLikeTag)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
// return curry(getStylingByKeys, 2)(mergedStyling, ...args);
// ^^^^
// | ----------------------------------------------------------------------
// | (parameter) args: any
// | (parameter) args: []
// | ----------------------------------------------------------------------
// },
// 3
Expand Down Expand Up @@ -135,8 +135,12 @@
"kind": "space"
},
{
"text": "any",
"kind": "keyword"
"text": "[",
"kind": "punctuation"
},
{
"text": "]",
"kind": "punctuation"
}
],
"documentation": []
Expand Down

0 comments on commit 3e91592

Please sign in to comment.