diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5c85e5872e531..4e98e84e92f54 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4167,16 +4167,9 @@ namespace ts { return getTypeForBindingElement(declaration); } - let isOptional = false; - if (includeOptionality) { - if (isInJavaScriptFile(declaration) && isParameter(declaration)) { - const parameterTags = getJSDocParameterTags(declaration); - isOptional = !!(parameterTags && parameterTags.length > 0 && find(parameterTags, tag => tag.isBracketed)); - } - if (!isBindingElement(declaration) && !isVariableDeclaration(declaration) && !!declaration.questionToken) { - isOptional = true; - } - } + const isOptional = includeOptionality && ( + isInJavaScriptFile(declaration) && isParameter(declaration) && getJSDocParameterTags(declaration).some(tag => tag.isBracketed) + || !isBindingElement(declaration) && !isVariableDeclaration(declaration) && !!declaration.questionToken); // Use type from type annotation if one is present const declaredType = tryGetTypeFromEffectiveTypeNode(declaration); @@ -6581,23 +6574,10 @@ namespace ts { } function isJSDocOptionalParameter(node: ParameterDeclaration) { - if (isInJavaScriptFile(node)) { - if (node.type && node.type.kind === SyntaxKind.JSDocOptionalType) { - return true; - } - const paramTags = getJSDocParameterTags(node); - if (paramTags) { - for (const paramTag of paramTags) { - if (paramTag.isBracketed) { - return true; - } - - if (paramTag.typeExpression) { - return paramTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType; - } - } - } - } + return isInJavaScriptFile(node) && ( + node.type && node.type.kind === SyntaxKind.JSDocOptionalType + || getJSDocParameterTags(node).some(({ isBracketed, typeExpression }) => + isBracketed || !!typeExpression && typeExpression.type.kind === SyntaxKind.JSDocOptionalType)); } function tryFindAmbientModule(moduleName: string, withAugmentations: boolean) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index fa0aa2d4e02f6..8f076d9016b04 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4419,13 +4419,13 @@ namespace ts { * Does not return tags for binding patterns, because JSDoc matches * parameters by name and binding patterns do not have a name. */ - export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray | undefined { + export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray { if (param.name && isIdentifier(param.name)) { const name = param.name.escapedText; return getJSDocTags(param.parent).filter((tag): tag is JSDocParameterTag => isJSDocParameterTag(tag) && isIdentifier(tag.name) && tag.name.escapedText === name); } // a binding pattern doesn't have a name, so it's not possible to match it a JSDoc parameter, which is identified by name - return undefined; + return emptyArray; } /** @@ -4481,11 +4481,8 @@ namespace ts { */ export function getJSDocType(node: Node): TypeNode | undefined { let tag: JSDocTypeTag | JSDocParameterTag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag; - if (!tag && node.kind === SyntaxKind.Parameter) { - const paramTags = getJSDocParameterTags(node as ParameterDeclaration); - if (paramTags) { - tag = find(paramTags, tag => !!tag.typeExpression); - } + if (!tag && isParameter(node)) { + tag = find(getJSDocParameterTags(node), tag => !!tag.typeExpression); } return tag && tag.typeExpression && tag.typeExpression.type; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 7034909f6dbaa..4e3e01692f20e 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2996,7 +2996,7 @@ declare namespace ts { * Does not return tags for binding patterns, because JSDoc matches * parameters by name and binding patterns do not have a name. */ - function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray | undefined; + function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray; /** * Return true if the node has JSDoc parameter tags. * diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 1908eb1114a27..1031837234398 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -3051,7 +3051,7 @@ declare namespace ts { * Does not return tags for binding patterns, because JSDoc matches * parameters by name and binding patterns do not have a name. */ - function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray | undefined; + function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray; /** * Return true if the node has JSDoc parameter tags. *