mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
getJSDocParameterTags: Always return defined result (#22523)
* getJSDocParameterTags: Always return defined result * Make line shorter * Simplify remaining use
This commit is contained in:
+7
-27
@@ -4167,16 +4167,9 @@ namespace ts {
|
||||
return getTypeForBindingElement(<BindingElement>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) {
|
||||
|
||||
@@ -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<JSDocParameterTag> | undefined {
|
||||
export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> {
|
||||
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;
|
||||
|
||||
+1
-1
@@ -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<JSDocParameterTag> | undefined;
|
||||
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
|
||||
/**
|
||||
* Return true if the node has JSDoc parameter tags.
|
||||
*
|
||||
|
||||
+1
-1
@@ -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<JSDocParameterTag> | undefined;
|
||||
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
|
||||
/**
|
||||
* Return true if the node has JSDoc parameter tags.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user