Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ namespace ts {
return undefined;
}

export function forEachAncestor<T>(node: Node, callback: (n: Node) => T | undefined | "quit"): T | undefined {
while (true) {
const res = callback(node);
if (res === "quit") return undefined;
if (res !== undefined) return res;
if (isSourceFile(node)) return undefined;
node = node.parent;
}
}

/**
* Calls `callback` for each entry in the map, returning the first truthy result.
* Use `map.forEach` instead for normal iteration.
Expand Down
12 changes: 8 additions & 4 deletions src/services/jsDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ namespace ts.JsDoc {
readonly parameters?: ReadonlyArray<ParameterDeclaration>;
}
function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined {
for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) {
return forEachAncestor(tokenAtPos, getCommentOwnerInfoWorker);
}
function getCommentOwnerInfoWorker(commentOwner: Node): CommentOwnerInfo | undefined | "quit" {
switch (commentOwner.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
Expand All @@ -348,6 +350,9 @@ namespace ts.JsDoc {
const { parameters } = commentOwner as FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | MethodSignature;
return { commentOwner, parameters };

case SyntaxKind.PropertyAssignment:
return getCommentOwnerInfoWorker((commentOwner as PropertyAssignment).initializer);

case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.PropertySignature:
Expand All @@ -366,7 +371,7 @@ namespace ts.JsDoc {
}

case SyntaxKind.SourceFile:
return undefined;
return "quit";

case SyntaxKind.ModuleDeclaration:
// If in walking up the tree, we hit a a nested namespace declaration,
Expand All @@ -377,14 +382,13 @@ namespace ts.JsDoc {
case SyntaxKind.BinaryExpression: {
const be = commentOwner as BinaryExpression;
if (getSpecialPropertyAssignmentKind(be) === SpecialPropertyAssignmentKind.None) {
return undefined;
return "quit";
}
const parameters = isFunctionLike(be.right) ? be.right.parameters : emptyArray;
return { commentOwner, parameters };
}
}
}
}

/**
* Digs into an an initializer or RHS operand of an assignment operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const multiLineOffset = 12;
//// }
//// /*1*/
//// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { }
//// /*2*/
//// m: function(a) {}
////}

verify.docCommentTemplateAt("0", singleLineOffset, "/** */");
Expand All @@ -21,3 +23,9 @@ verify.docCommentTemplateAt("1", multiLineOffset,
* @param y
* @param z
*/`);

verify.docCommentTemplateAt("2", multiLineOffset,
`/**
*
* @param a
*/`);