-
Notifications
You must be signed in to change notification settings - Fork 30
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
Traverse JSDoc nodes if present #19
base: master
Are you sure you want to change the base?
Traverse JSDoc nodes if present #19
Conversation
This change has two open questions that manifest as ugly code: 1. The jsDoc property is not exposed in the Typescript API because the intended interface is `getJSDocTags`, which does a lot more work to provide a list of tags that are collected directly from a node, or any one of several parent nodes. I use type assertions to any to access the property without checking. 2. The jsDoc property is of type JSDoc[], not Node, so it can't be traversed directly. I decided to add the discovered JSDoc nodes as children before the actual children, since that's where JSDoc appears syntactically. But it's odd that that each tag appears as an individual child instead of appearing under some synthetic jsdoc node. Unfortunately, I don't know tsquery well enough to know if such a synthetic node is possible, or a good idea. Both problems boil down to "JSDoc is stored on a side-band to the AST", which leads to problems parsing and representing it as part of the AST.
Thanks for this, I'll check it out this weekend properly when I get a sec. My first thoughts are to dig into the "side-band to the AST" thing - is that a unique property of the JSDoc nodes? Or is that also similar to how the type nodes are stored? I did a little experiment of swapping out |
I looked at the source for getChildren and it does quite a bit more work than forEachChild. It does adds jsdocs, which is nice since tsquery wouldn't have to add handling for it. Basically, After writing that, I think that
|
Yeah I've done a bit of thinking about this too, and I think I agree with you. I'd be happy to make the breaking change if it makes this whole thing more useful. Interested in what @urish thinks too though, as he is the main downstream consumer right now. My one remaining reservation is with the new I'm wondering if there is any way you can query that side-band information? That way it could be enabled by some sort of flag, and everyone wouldn't have to take the perf hit. |
Well there's the private way that I use in this PR, which accesses the /**
* @param {string} x
* @return {number}
*/
function f(/** @type {number} */x) {
}
In contrast, I personally would prefer the less abstract behaviour when querying a syntax tree, since I feel confident that I could identify interesting nodes to later call I have mixed feelings about making the jsDoc property public, though, because it would add an obvious, and wrong, way to check whether a node has jsDoc type tags. I think it would make it harder for new users to pick up the compiler API and use it for type checking. But I don't know if type checking usage is more common than syntax usage. |
Fixes #18
This change has two open questions that manifest as ugly code:
getJSDocTags
, which does a lot more work to provide a list of tags that are collected directly from a node, or any one of several parent nodes. I use type assertions to any to access the property without checking.Both problems boil down to "JSDoc is stored on a side-band to the AST", which leads to problems parsing and representing it as part of the AST. Given (1) my inexperience with tsquery and (2) my hacky solutions to the two open questions, you should probably think of this PR as a starting point for discussion rather than something to merge.