Skip to content

Commit d1d6960

Browse files
author
Andy
authored
getJSDocParameterTags: Always return defined result (#22523)
* getJSDocParameterTags: Always return defined result * Make line shorter * Simplify remaining use
1 parent 60501c8 commit d1d6960

File tree

4 files changed

+13
-36
lines changed

4 files changed

+13
-36
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,16 +4167,9 @@ namespace ts {
41674167
return getTypeForBindingElement(<BindingElement>declaration);
41684168
}
41694169

4170-
let isOptional = false;
4171-
if (includeOptionality) {
4172-
if (isInJavaScriptFile(declaration) && isParameter(declaration)) {
4173-
const parameterTags = getJSDocParameterTags(declaration);
4174-
isOptional = !!(parameterTags && parameterTags.length > 0 && find(parameterTags, tag => tag.isBracketed));
4175-
}
4176-
if (!isBindingElement(declaration) && !isVariableDeclaration(declaration) && !!declaration.questionToken) {
4177-
isOptional = true;
4178-
}
4179-
}
4170+
const isOptional = includeOptionality && (
4171+
isInJavaScriptFile(declaration) && isParameter(declaration) && getJSDocParameterTags(declaration).some(tag => tag.isBracketed)
4172+
|| !isBindingElement(declaration) && !isVariableDeclaration(declaration) && !!declaration.questionToken);
41804173

41814174
// Use type from type annotation if one is present
41824175
const declaredType = tryGetTypeFromEffectiveTypeNode(declaration);
@@ -6581,23 +6574,10 @@ namespace ts {
65816574
}
65826575

65836576
function isJSDocOptionalParameter(node: ParameterDeclaration) {
6584-
if (isInJavaScriptFile(node)) {
6585-
if (node.type && node.type.kind === SyntaxKind.JSDocOptionalType) {
6586-
return true;
6587-
}
6588-
const paramTags = getJSDocParameterTags(node);
6589-
if (paramTags) {
6590-
for (const paramTag of paramTags) {
6591-
if (paramTag.isBracketed) {
6592-
return true;
6593-
}
6594-
6595-
if (paramTag.typeExpression) {
6596-
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
6597-
}
6598-
}
6599-
}
6600-
}
6577+
return isInJavaScriptFile(node) && (
6578+
node.type && node.type.kind === SyntaxKind.JSDocOptionalType
6579+
|| getJSDocParameterTags(node).some(({ isBracketed, typeExpression }) =>
6580+
isBracketed || !!typeExpression && typeExpression.type.kind === SyntaxKind.JSDocOptionalType));
66016581
}
66026582

66036583
function tryFindAmbientModule(moduleName: string, withAugmentations: boolean) {

src/compiler/utilities.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4419,13 +4419,13 @@ namespace ts {
44194419
* Does not return tags for binding patterns, because JSDoc matches
44204420
* parameters by name and binding patterns do not have a name.
44214421
*/
4422-
export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> | undefined {
4422+
export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> {
44234423
if (param.name && isIdentifier(param.name)) {
44244424
const name = param.name.escapedText;
44254425
return getJSDocTags(param.parent).filter((tag): tag is JSDocParameterTag => isJSDocParameterTag(tag) && isIdentifier(tag.name) && tag.name.escapedText === name);
44264426
}
44274427
// a binding pattern doesn't have a name, so it's not possible to match it a JSDoc parameter, which is identified by name
4428-
return undefined;
4428+
return emptyArray;
44294429
}
44304430

44314431
/**
@@ -4481,11 +4481,8 @@ namespace ts {
44814481
*/
44824482
export function getJSDocType(node: Node): TypeNode | undefined {
44834483
let tag: JSDocTypeTag | JSDocParameterTag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag;
4484-
if (!tag && node.kind === SyntaxKind.Parameter) {
4485-
const paramTags = getJSDocParameterTags(node as ParameterDeclaration);
4486-
if (paramTags) {
4487-
tag = find(paramTags, tag => !!tag.typeExpression);
4488-
}
4484+
if (!tag && isParameter(node)) {
4485+
tag = find(getJSDocParameterTags(node), tag => !!tag.typeExpression);
44894486
}
44904487

44914488
return tag && tag.typeExpression && tag.typeExpression.type;

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2996,7 +2996,7 @@ declare namespace ts {
29962996
* Does not return tags for binding patterns, because JSDoc matches
29972997
* parameters by name and binding patterns do not have a name.
29982998
*/
2999-
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> | undefined;
2999+
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
30003000
/**
30013001
* Return true if the node has JSDoc parameter tags.
30023002
*

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,7 @@ declare namespace ts {
30513051
* Does not return tags for binding patterns, because JSDoc matches
30523052
* parameters by name and binding patterns do not have a name.
30533053
*/
3054-
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> | undefined;
3054+
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
30553055
/**
30563056
* Return true if the node has JSDoc parameter tags.
30573057
*

0 commit comments

Comments
 (0)