Skip to content

getJSDocs cleanup #12335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 21, 2016
Merged

getJSDocs cleanup #12335

merged 10 commits into from
Nov 21, 2016

Conversation

sandersn
Copy link
Member

  1. Got rid of the confusingly named getJsDocComments, which does something quite different from getJSDocComments.
  2. For the most part, the checker now just calls getJSDocType to get a type node and doesn't traverse jsdoc anymore.
  3. getJSDocs, the core of the JSDoc retrieval, now caches retrieved JSDoc on nodes.
  4. All the JSDoc functions in utilities are streamlined to use much less code, including getting rid of near-duplicate code like getCorrespondingJSDocParameterTag.

Fix functions that were unused (getJsDocComments), redundant (append) or badly named
(getJSDocCommentsFromText)
1. Get rid of parent check.
2. Use a couple of nested functions, one of them a recursive worker.
And delete its near-duplicate which was much less correct. The callers
that had to switch are slightly more complex and more correct now.
Cache only uses one property now. getJSDocs isn't generic and doesn't
take a function parameter any more. The code is overall easier to read.
@sandersn
Copy link
Member Author

@Andy-MS I know you were looking forward to seeing this happen.
@zhengbli I think you have worked with the JSDoc infrastructure most on the team.

@sandersn sandersn assigned zhengbli and ghost Nov 17, 2016
return tag;
}
}
export function getJSDocComments(node: Node): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would getCommentsOfJSDocs be clearer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -498,6 +498,7 @@ namespace ts {
/* @internal */ original?: Node; // The original node if this is an updated node.
/* @internal */ startsOnNewLine?: boolean; // Whether a synthesized node should start on a new line (used by transforms).
/* @internal */ jsDocComments?: JSDoc[]; // JSDoc for the node, if it has any.
/* @internal */ jsDocCache?: (JSDoc | JSDocParameterTag)[]; // JSDoc for the node, plus JSDoc retrieved from its parents
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment explaining that JSDocParameterTag is treated differently because we want to avoid redoing the mapping from each tag to each parameter declaration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I improved the comment to explain that jsDoc[Comments] is the JSDoc syntactically on the node whereas jsDocCache is all applicable docs and @param tags.

return node && firstOrUndefined(getJSDocTags(node, kind));
}

function getJSDocs(node: Node): (JSDoc | JSDocParameterTag)[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is kind of strange that this function is called getJSDocs, but it sometime return a tag. Can we seperate these two things? for example, give parameter declaration nodes a seperate cache just for JSDocParameterTag

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this but didn't want two caches, especially when they are almost the same (both tags and docs have comments). Would a different name help, something like getRelatedJSDoc or getApplicableJSDoc?


return result;
cache = concatenate(cache, node.jsDocComments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have renamed all jsDocComments to just jsDoc, can we do that for Node.jsDocComments as well for consistence? I feel because we call this thing JSDocComment before, we might want to avoid reuse the same name with a different meaning in the new world, per my other comment of using CommentOfJSDoc instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. It was too large a change for the main jsdoc PR but it should fit in this one just fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


function getJSDocs(node: Node): (JSDoc | JSDocParameterTag)[] {
let cache: (JSDoc | JSDocParameterTag)[] = node.jsDocCache;
if (!cache) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does the cache get cleared?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never. Once it's created, it should not be changed. But it's only created if it's requested.

@sandersn sandersn merged commit 4092531 into master Nov 21, 2016
@sandersn sandersn deleted the getJSDoc-cleanup branch November 21, 2016 19:30
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants