-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Simplify getJSDocCommentAndTags #24997
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
Conversation
Previously, getJSDocCommentAndTags could recur up to four times if any of four predicates matched. However, to avoid duplicates, the predicates have been tuned to be mutually exclusive, which means that the recursion can be turned into a while loop. The while loop is much simpler and safer, since it is guaranteed to only walk up the tree one time. In addition, the extra check that adds jsdoc from initializers only runs once, before the loop, further reducing the opportunity for duplicate jsdocs. I thought about further simplifying the code that gets the next node to check, but to know when to stop the loop, I'd need a predicate that is as complicated as the code in `getNextJSDocCommentLocation`, so I kept the existing code, just reordering it for compactness.
src/parser/utilities.ts
Outdated
} | ||
else if (parent.parent && parent.parent.parent && | ||
(getSingleVariableOfVariableStatement(parent.parent.parent) || | ||
getSingleInitializerOfVariableStatementOrPropertyDeclaration(parent.parent.parent) === node || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we test for both parent.parent
and parent.parent.parent
versions of this? Doesn't this mean we would add jsdoc for a child of an initializer?
The parent.parent.parent check catches
/**
* @callback FooHandler2
* @param {string=} eventName
* @param {string} [eventName2]
*/
/**
* @type {FooHandler2} callback
*/
var t2; The parent.parent check catches
I'll think about which other things might also follow this code path. If I can construct a broken test case, then I'll see about tightening these predicates. |
We won't add jsdoc for the child of an initializer because parent.parent.parent is still only the VariableDeclarationList of VariableStatement. Still, it's not actually correct to treat the type annotation for a declaration as a type annotation for the initializer too; instead, the initializer is supposed to get a contextual type that comes from the variable declaration. Hmm, actually, I uncovered another oddity in I'd like to merge this and deal with any fallout first, and then I'll further simplify+tighten the checking afterward, starting with the odd jsdoc handling in getContextualSignature. |
Previously, getJSDocCommentAndTags could recur up to four times if any of four predicates matched. However, to avoid duplicates, the predicates have been tuned to be mutually exclusive, which means that the recursion can be turned into a while loop. The while loop is much simpler and safer, since it is guaranteed to only walk up the tree one time. In addition, the extra check that adds jsdoc from initializers only runs once, before the loop, further reducing the opportunity for duplicate jsdocs.
I thought about further simplifying the code that gets the next node to check, but to know when to stop the loop, I'd need a predicate that is as complicated as the code in
getNextJSDocCommentLocation
, so I kept the existing code, just reordering it for compactness.Note that this change reverses the order of multiple jsdocs for a single parameter or function expression, since those can have 3 and 2 locations, respectively. I don't think this is a problem because such cases are likely to be (1) rare (2) a mistake. I filed #24996 to make duplicated jsdocs an error.
Fixes #25067