getJSDocParameterTags: Always return defined result (#22523)

* getJSDocParameterTags: Always return defined result

* Make line shorter

* Simplify remaining use
This commit is contained in:
Andy
2018-03-13 17:56:00 -07:00
committed by GitHub
parent 60501c8b49
commit d1d69602ae
4 changed files with 13 additions and 36 deletions
+7 -27
View File
@@ -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) {
+4 -7
View File
@@ -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
View File
@@ -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
View File
@@ -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.
*