Skip to content
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

Merged
merged 42 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
10c944c
First example of JS tag emitting types
sandersn Mar 6, 2025
d601063
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 10, 2025
12dc74c
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 10, 2025
eac0860
couple of small bug fixes/refactors
sandersn Mar 10, 2025
fce99be
Add basic uses of type and return tags
sandersn Mar 10, 2025
34666c3
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 12, 2025
b15e970
Basic param support
sandersn Mar 12, 2025
551f4a4
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 13, 2025
b6e920b
Completely unfinished typedef, need to push to sync to laptop
sandersn Mar 13, 2025
24efd00
finish adding @typedef
sandersn Mar 14, 2025
6e1b734
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 14, 2025
d8d091f
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 14, 2025
355a339
Remove JSTypeExpression wrapper, use Clone instead
sandersn Mar 14, 2025
5184fda
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 17, 2025
4d420bc
Various fixes+ features
sandersn Mar 19, 2025
449315c
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 19, 2025
20a6f6a
Add some tags, accept baselines
sandersn Mar 20, 2025
85a68a1
Add Host pointer to JSDocTypeExpression,fix name resolution
sandersn Mar 21, 2025
a74d9a8
support variabledeclaration as well
sandersn Mar 21, 2025
c0056b3
undo stray merge change
sandersn Mar 24, 2025
4d51c72
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 24, 2025
f13059f
cosmetic cleanup
sandersn Mar 24, 2025
49fb006
undo bad typedef parsing simplification
sandersn Mar 24, 2025
f2852a8
remove unused JS checks in checker
sandersn Mar 24, 2025
e2c807e
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 24, 2025
0fb08ce
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Mar 26, 2025
d448d64
remove JSTypeAliasDeclaration type, keep kind
sandersn Mar 26, 2025
fc6199b
remove another jsdoc todo
sandersn Mar 26, 2025
6146316
fix formatting
sandersn Mar 26, 2025
00e3d76
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Apr 1, 2025
9225da2
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Apr 1, 2025
f39861f
delete unused baselines
sandersn Apr 1, 2025
8c3e3ea
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Apr 3, 2025
bac9152
Merge branch 'add-basic-jsdoc-tag-types' of github.com:sandersn/types…
sandersn Apr 3, 2025
13ec1c3
Improve locations of reparsed nodes
sandersn Apr 3, 2025
8e71486
remove now-unneeded check in rangeOfTypeParameters
sandersn Apr 3, 2025
c7ae384
hereby format
sandersn Apr 3, 2025
6ba9c18
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Apr 7, 2025
7af2df5
update tests after merge
sandersn Apr 7, 2025
57badae
re-delete unused baselines
sandersn Apr 7, 2025
807d725
skip baselining all reparsed nodes except `as`
sandersn Apr 8, 2025
983f3b7
Merge branch 'main' into add-basic-jsdoc-tag-types
sandersn Apr 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
66 changes: 49 additions & 17 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func (n *Node) TypeParameterList() *NodeList {
return n.AsClassExpression().TypeParameters
case KindInterfaceDeclaration:
return n.AsInterfaceDeclaration().TypeParameters
case KindTypeAliasDeclaration:
case KindTypeAliasDeclaration, KindJSTypeAliasDeclaration:
return n.AsTypeAliasDeclaration().TypeParameters
default:
funcLike := n.FunctionLikeData()
Expand Down Expand Up @@ -436,7 +436,7 @@ func (n *Node) MemberList() *NodeList {
case KindMappedType:
return n.AsMappedTypeNode().Members
}
panic("Unhandled case in Node.MemberList")
panic("Unhandled case in Node.MemberList: " + n.Kind.String())
}

func (n *Node) Members() []*Node {
Expand Down Expand Up @@ -487,7 +487,7 @@ func (n *Node) Type() *Node {
return n.AsAsExpression().Type
case KindSatisfiesExpression:
return n.AsSatisfiesExpression().Type
case KindTypeAliasDeclaration:
case KindTypeAliasDeclaration, KindJSTypeAliasDeclaration:
return n.AsTypeAliasDeclaration().Type
case KindNamedTupleMember:
return n.AsNamedTupleMember().Type
Expand All @@ -499,6 +499,8 @@ func (n *Node) Type() *Node {
return n.AsTemplateLiteralTypeSpan().Type
case KindJSDocTypeExpression:
return n.AsJSDocTypeExpression().Type
case KindJSDocPropertyTag:
return n.AsJSDocPropertyTag().TypeExpression
case KindJSDocNullableType:
return n.AsJSDocNullableType().Type
case KindJSDocNonNullableType:
Expand All @@ -513,7 +515,7 @@ func (n *Node) Type() *Node {
return funcLike.Type
}
}
panic("Unhandled case in Node.Type")
panic("Unhandled case in Node.Type: " + n.Kind.String())
}

func (n *Node) Initializer() *Node {
Expand Down Expand Up @@ -3380,6 +3382,7 @@ type ClassLikeBase struct {
DeclarationBase
ExportableBase
ModifiersBase
LocalsContainerBase
Copy link
Member Author

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.

compositeNodeBase
name *IdentifierNode // IdentifierNode
TypeParameters *NodeList // NodeList[*TypeParameterDeclarationNode]. Optional
Expand Down Expand Up @@ -3617,13 +3620,17 @@ type TypeAliasDeclaration struct {
Type *TypeNode // TypeNode
}

func (f *NodeFactory) NewTypeAliasDeclaration(modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
func (f *NodeFactory) newTypeOrJSTypeAliasDeclaration(kind Kind, modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
data := &TypeAliasDeclaration{}
data.modifiers = modifiers
data.name = name
data.TypeParameters = typeParameters
data.Type = typeNode
return newNode(KindTypeAliasDeclaration, data, f.hooks)
return newNode(kind, data, f.hooks)
}

func (f *NodeFactory) NewTypeAliasDeclaration(modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
return f.newTypeOrJSTypeAliasDeclaration(KindTypeAliasDeclaration, modifiers, name, typeParameters, typeNode)
}

func (f *NodeFactory) UpdateTypeAliasDeclaration(node *TypeAliasDeclaration, modifiers *ModifierList, name *IdentifierNode, typeParameters *TypeParameterList, typeNode *TypeNode) *Node {
Expand All @@ -3638,6 +3645,9 @@ func (node *TypeAliasDeclaration) ForEachChild(v Visitor) bool {
}

func (node *TypeAliasDeclaration) VisitEachChild(v *NodeVisitor) *Node {
if node.Kind == KindJSTypeAliasDeclaration {
return v.Factory.UpdateJSTypeAliasDeclaration(node, v.visitModifiers(node.modifiers), v.visitNode(node.name), v.visitNodes(node.TypeParameters), v.visitNode(node.Type))
}
return v.Factory.UpdateTypeAliasDeclaration(node, v.visitModifiers(node.modifiers), v.visitNode(node.name), v.visitNodes(node.TypeParameters), v.visitNode(node.Type))
}

Expand All @@ -3651,6 +3661,25 @@ func IsTypeAliasDeclaration(node *Node) bool {
return node.Kind == KindTypeAliasDeclaration
}

func IsTypeOrJSTypeAliasDeclaration(node *Node) bool {
return node.Kind == KindTypeAliasDeclaration || node.Kind == KindJSTypeAliasDeclaration
}

func (f *NodeFactory) NewJSTypeAliasDeclaration(modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
return f.newTypeOrJSTypeAliasDeclaration(KindJSTypeAliasDeclaration, modifiers, name, typeParameters, typeNode)
}

func (f *NodeFactory) UpdateJSTypeAliasDeclaration(node *TypeAliasDeclaration, modifiers *ModifierList, name *IdentifierNode, typeParameters *TypeParameterList, typeNode *TypeNode) *Node {
if modifiers != node.modifiers || name != node.name || typeParameters != node.TypeParameters || typeNode != node.Type {
return updateNode(f.NewJSTypeAliasDeclaration(modifiers, name, typeParameters, typeNode), node.AsNode(), f.hooks)
}
return node.AsNode()
}

func IsJSTypeAliasDeclaration(node *Node) bool {
return node.Kind == KindJSTypeAliasDeclaration
}

// EnumMember

type EnumMember struct {
Expand Down Expand Up @@ -8584,6 +8613,7 @@ func (node *JSDocLinkCode) Name() *DeclarationName {

type JSDocTypeExpression struct {
TypeNodeBase
Host *Node
Type *TypeNode
}

Expand Down Expand Up @@ -8865,8 +8895,7 @@ func (node *JSDocTemplateTag) Clone(f *NodeFactory) *Node {

func (node *JSDocTemplateTag) TypeParameters() *TypeParameterList { return node.typeParameters }

// JSDocParameterTag

// JSDocPropertyTag
type JSDocPropertyTag struct {
JSDocTagBase
name *EntityName
Expand Down Expand Up @@ -8918,6 +8947,7 @@ func (node *JSDocPropertyTag) Clone(f *NodeFactory) *Node {

func (node *JSDocPropertyTag) Name() *EntityName { return node.name }

// JSDocParameterTag
type JSDocParameterTag struct {
JSDocTagBase
name *EntityName
Expand Down Expand Up @@ -9473,40 +9503,42 @@ func (node *JSDocOverloadTag) Clone(f *NodeFactory) *Node {
type JSDocTypedefTag struct {
JSDocTagBase
TypeExpression *Node
FullName *Node
name *IdentifierNode
}

func (f *NodeFactory) NewJSDocTypedefTag(tagName *IdentifierNode, typeExpression *Node, fullName *Node, comment *NodeList) *Node {
func (f *NodeFactory) NewJSDocTypedefTag(tagName *IdentifierNode, typeExpression *Node, name *IdentifierNode, comment *NodeList) *Node {
data := &JSDocTypedefTag{}
data.TagName = tagName
data.TypeExpression = typeExpression
data.FullName = fullName
data.name = name
data.Comment = comment
return newNode(KindJSDocTypedefTag, data, f.hooks)
}

func (f *NodeFactory) UpdateJSDocTypedefTag(node *JSDocTypedefTag, tagName *IdentifierNode, typeExpression *Node, fullName *Node, comment *NodeList) *Node {
if tagName != node.TagName || typeExpression != node.TypeExpression || fullName != node.FullName || comment != node.Comment {
func (f *NodeFactory) UpdateJSDocTypedefTag(node *JSDocTypedefTag, tagName *IdentifierNode, typeExpression *Node, fullName *IdentifierNode, comment *NodeList) *Node {
if tagName != node.TagName || typeExpression != node.TypeExpression || fullName != node.name || comment != node.Comment {
return updateNode(f.NewJSDocTypedefTag(tagName, typeExpression, fullName, comment), node.AsNode(), f.hooks)
}
return node.AsNode()
}

func (node *JSDocTypedefTag) ForEachChild(v Visitor) bool {
if node.TypeExpression != nil && node.TypeExpression.Kind == KindJSDocTypeLiteral {
return visit(v, node.TagName) || visit(v, node.FullName) || visit(v, node.TypeExpression) || visitNodeList(v, node.Comment)
return visit(v, node.TagName) || visit(v, node.name) || visit(v, node.TypeExpression) || visitNodeList(v, node.Comment)
}
return visit(v, node.TagName) || visit(v, node.TypeExpression) || visit(v, node.FullName) || visitNodeList(v, node.Comment)
return visit(v, node.TagName) || visit(v, node.TypeExpression) || visit(v, node.name) || visitNodeList(v, node.Comment)
}

func (node *JSDocTypedefTag) VisitEachChild(v *NodeVisitor) *Node {
return v.Factory.UpdateJSDocTypedefTag(node, v.visitNode(node.TagName), v.visitNode(node.TypeExpression), v.visitNode(node.FullName), v.visitNodes(node.Comment))
return v.Factory.UpdateJSDocTypedefTag(node, v.visitNode(node.TagName), v.visitNode(node.TypeExpression), v.visitNode(node.name), v.visitNodes(node.Comment))
}

func (node *JSDocTypedefTag) Clone(f *NodeFactory) *Node {
return cloneNode(f.NewJSDocTypedefTag(node.TagName, node.TypeExpression, node.FullName, node.Comment), node.AsNode(), f.hooks)
return cloneNode(f.NewJSDocTypedefTag(node.TagName, node.TypeExpression, node.name, node.Comment), node.AsNode(), f.hooks)
}

func (node *JSDocTypedefTag) Name() *DeclarationName { return node.name }

// JSDocTypeLiteral
type JSDocTypeLiteral struct {
TypeNodeBase
Expand Down
2 changes: 2 additions & 0 deletions internal/ast/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ const (
KindJSDocImportTag
// Synthesized list
KindSyntaxList
// Synthesized JS nodes
KindJSTypeAliasDeclaration
// Transformation nodes
KindNotEmittedStatement
KindPartiallyEmittedExpression
Expand Down
Loading