-
Notifications
You must be signed in to change notification settings - Fork 552
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
base: main
Are you sure you want to change the base?
Conversation
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!
@@ -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.
- 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.
internal/checker/checker.go
Outdated
@@ -6413,7 +6418,7 @@ func (c *Checker) checkUnusedIdentifiers(potentiallyUnusedIdentifiers []*ast.Nod | |||
} | |||
c.checkUnusedTypeParameters(node) | |||
case ast.KindMethodSignature, ast.KindCallSignature, ast.KindConstructSignature, ast.KindFunctionType, ast.KindConstructorType, | |||
ast.KindTypeAliasDeclaration, ast.KindInterfaceDeclaration: | |||
ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration, ast.KindInterfaceDeclaration: |
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.
Every time we have a KindTypeAliasDeclaration, we should have a KindJSTypeAliasDeclaration? How does that scale for getting things right long-term? Hopefully it's not too much worse than the old code...
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.
Yes, except for the places where we shouldn't. I looked at the current codebase and I'm confident that it's 95% right and that the tests catch the rest.
The real value of having two kinds is in commonjs, where module.exports=
is going to need to allow a bunch of things that export=
doesn't.
Still, it's ultimately a matter of whether jstype should work the same as type by default or not, not expressiveness, because you can always have isJS := node.Flags & FlagsReparsed != 0
. That way jstype works like type by default.
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.
In other words, as an exercise for me, when developing, having two kinds is GREAT, because it forces me to go port every single TS construct to JS and consider the semantics. And if I forget something, the tests won't work.
However, long-term it might make more sense to have everything work the same by default.
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.
After discussing this in the design meeting: Anders prefers having two separate kinds and nobody else spoke up.
@@ -2044,7 +2043,7 @@ var getFeatureMap = sync.OnceValue(func() map[string][]FeatureMapEntry { | |||
}) | |||
|
|||
func rangeOfTypeParameters(sourceFile *ast.SourceFile, typeParameters *ast.NodeList) core.TextRange { | |||
return core.NewTextRange(typeParameters.Pos()-1, min(len(sourceFile.Text), scanner.SkipTrivia(sourceFile.Text, typeParameters.End())+1)) | |||
return core.NewTextRange(max(0, typeParameters.Pos()-1), min(len(sourceFile.Text), scanner.SkipTrivia(sourceFile.Text, typeParameters.End())+1)) |
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.
How likely are we to hit this bug elsehwere when working with positions on synthetic nodes? IIRC we would never have to do this previously, but now there's a reason to check for each error.
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.
The actual problem is that typeParameters could never be parsed at the very beginning of the file, but now they can:
/** @template T */
function f() { }
opposed to TS, which has no way to have a type parameter list that starts at position 0.
function f<T>() { }
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.
This might indicate that the synthetic type parameter list Pos should not be 0, though.
Keep the constructors and some predicates separate as well.
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