-
Notifications
You must be signed in to change notification settings - Fork 566
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
Add basic jsdoc tag types #610
Open
sandersn
wants to merge
37
commits into
microsoft:main
Choose a base branch
from
sandersn:add-basic-jsdoc-tag-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I chose to create a wrapper type node for synthetic JS types rather than cloning the type node. This is how many other tags are going to work, like `@typedef` and `@overload`, so I think it makes sense. However, running tests points out that `@type` assertions, `@satisfies`, and the modifier-like tags like `@private` and `@readonly` will need to emit non-wrapped synthetic nodes, much like in microsoft#412. Much more work to be done here on replicating the host<->tag matching rules from Strada.
For example, recursive uses aren't done yet. Test diffs look generally right but are hard to evaluate without typedef and template tags, specifically.
Written mostly by AI, without reference to the original source. So it's going to need some cleanup.
It builds! But panics on tests!
sandersn
commented
Mar 14, 2025
@@ -3032,6 +3046,7 @@ type ClassLikeBase struct { | |||
DeclarationBase | |||
ExportableBase | |||
ModifiersBase | |||
LocalsContainerBase |
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.
Unlike type
, @typedef
is allowed as a local inside classes.
I haven't checked to see how common this is so I added support for now as a way of fixing a nil panic. But I will remove it if it's not common.
sandersn
commented
Mar 14, 2025
- Transform `@typedef`+`@property` in a type object literal. - type check JSDoc types - add `@param` optionality when bracketed.
- refactor `@template` parsing to share constraints in multi-parameter tags - type `@template` tags - type assertions on parenthesised expressions - miscellaneous fixes - I looked at the diffs for compiler/ tests and skimmed the conformance/ ones. There are still some failures I want to fix.
nameresolver still needs to check for JS nodes because it will often start a walk upward looking at the *original* type nodes deeply nested in JSDoc. When it hits the JSDocTypeExpression, it should move over to the type expression's hosted position in the tree, such as a real parameter. The JS code is much less intrusive than before though.
jakebailey
reviewed
Mar 24, 2025
Keep the constructors and some predicates separate as well.
…cript-go into add-basic-jsdoc-tag-types
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds basic support for the JSDoc tags
@type
,@param
,@returns
,@typedef
and@template
. The implementation is quite different from Strada, but Corsa will now have examples of all 3 major approaches we've planned for JS inference we've planned for JS inference in Corsa, meaning that the rest is just filling out support.Functionality
@type
applies to variable declarations and property declarations:It also works as a cast on parenthesised expressions:
And it behaves as a cast on property assignments, export assignments, and return statements:
I ported this support from Strada, and it's useful for
@param/@returns
(see next), but it doesn't really make sense for@type
. I might cut it, or perhaps add a realType
field to property assignment/export assignment/return statement; the checker currently acts as if they do because of porting from Strada.@param
and@returns
apply to parameters and return types of functions. They work on anything function-like, including in lower positions:@typedef
supports both single-line types and multi-line object types with@param
. Nested object types are not yet supported. As in Strada,@typedef
s are implicitly exported, but this PR is missing the fanciness to make that work right with global scripts or CommonJS modules. Currently,@typedef
always turns a file into a module.@template
tags work with@typedef
, function-likes and classes.Architecture
Corsa now uses 3 different approaches to support JS inference:
f.p = expandable
), including to class properties, are (or will be) handled in the binder, just like Strada. Unfortunately, this means that everyBinaryExpression
includesDeclarationBase
.@type @template @private
) produce synthetic nodes during parsing. After a node is parsed and its JSDoc is parsed, missing fields on the node can be set to nodes from the tag instead.@typedef
) produces synthetic statements, which the parser inserts right before the just-parsed node. These statements have their own node kinds and type to allow for JS-specific semantics, although this PR doesn't have any@typedef
-specific semantics vstype
aliases.Worth noting: I have no ability yet to support API users like TSDoc, who in Strada could request all tags for a variable declaration and get all tags on the variable declaration statement, for example. I expect eventually we will have to port the old tag lookup code just for uses like this.
Expando representation
For an expando assignment
f.p = expandable
, I could emit a syntheticjsnamespace f { jsexport var p = expandable }
. This might be more efficient than treating BinaryExpression as a declaration in the binder. Class properties would need an equivalentjsprop p = expandable
, and this declaration would either need to be lifted to the class body or retain special support in the binder/checker to allow (JS) property declarations to occur anywhere inside a class.More information on the synthetic nodes:
Hosts
Corsa searches for a host similar to the way Strada does, but the search is (1) top-down (2) at parse-time instead of repeated each time on-demand. In other words, in Strada, hosts would search up the tree for applicable tags. In Corsa, tags search down the tree for applicable hosts. However, this makes it harder to track multiple hosts, so for this PR, the search stops at the first host, which is by far the most common case.
The bottom-up on-demand nature of Strada's tag-to-host attachment meant that if multiple hosts find a tag, the tag applies to multiple hosts. In Corsa, tags look for hosts, and they apply to the first host only.
This should be much more efficient. This means, at least currently, that each tag can only have one host.
Also note that 'host' generally means the entire signature in Strada, but is a specific parameter type annotation, for example, in Corsa.
Clones
Currently, only the top node of a hosted type is cloned into its new position. It's not a deep clone, so children of the type still retain their original positions and parents. That means when walking the tree top-down, the checker may suddenly be looking at JSDoc nodes that originated from a different place in the tree.
This is important because if the checker needs to walk back up the tree, the parent pointers have not been changed. To fix this, I had to track a type's host on JSDocTypeExpression:
JSDocTypeExpression
JSDocTypeExpression is the JSDoc node kind just above all the types that are cloned. Because of this, when there is a bottom-up lookup, JSDocTypeExpression is the signal that a type was cloned from JSDoc. JSDocTypeExpressions store a pointer to the host node of its type, and name resolution, for example, uses it to jump from a JSDoc type to its host.
Overall, a deep clone with parent pointer fixup might be better. But I looked at DeepClone and it doesn't change the parent or provide hooks to change it.
Implementation
@typedef
can be nested in a class body. Questionable.parseListIndex
, notparseDelimitedList
, so I only needed to change the former. After parsing, I check whether a new parser propertyreparseList
contains elements and add them before the just-returned element if so. Then I emptyreparseList
.jstype T = U
. I have no idea if that's right, but normal JSDoc emit should already emit the@typedef
(and JSTypeAliasDeclaration should be erased by the type eraser.)Limits and Future Work