Skip to content

Add callback tag, fix jsdoc type parameter resolution #1187

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

Merged
merged 8 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/api/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func getChildrenPropertyMask(node *ast.Node) uint8 {
return (boolToByte(n.TagName != nil) << 0) | (boolToByte(n.TypeExpression != nil) << 1) | (boolToByte(n.Name() != nil) << 2) | (boolToByte(n.Comment != nil) << 3)
case ast.KindJSDocSignature:
n := node.AsJSDocSignature()
return (boolToByte(n.TypeParameters() != nil) << 0) | (boolToByte(n.Parameters != nil) << 1) | (boolToByte(n.Type != nil) << 2)
return (boolToByte(n.TypeParameters != nil) << 0) | (boolToByte(n.Parameters != nil) << 1) | (boolToByte(n.Type != nil) << 2)
case ast.KindClassStaticBlockDeclaration:
n := node.AsClassStaticBlockDeclaration()
return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.Body != nil) << 1)
Expand Down
21 changes: 9 additions & 12 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9201,6 +9201,7 @@ type JSDocTemplateTag struct {
JSDocTagBase
Constraint *Node
TypeParameters *TypeParameterList
Host *Node
}

func (f *NodeFactory) NewJSDocTemplateTag(tagName *IdentifierNode, constraint *Node, typeParameters *TypeParameterList, comment *NodeList) *Node {
Expand Down Expand Up @@ -9876,40 +9877,36 @@ func (node *JSDocTypeLiteral) Clone(f NodeFactoryCoercible) *Node {
// JSDocSignature
type JSDocSignature struct {
TypeNodeBase
typeParameters *TypeParameterList
Parameters *NodeList
Type *JSDocTag
FunctionLikeBase
}

func (f *NodeFactory) NewJSDocSignature(typeParameters *TypeParameterList, parameters *NodeList, typeNode *JSDocTag) *Node {
func (f *NodeFactory) NewJSDocSignature(typeParameters *NodeList, parameters *NodeList, typeNode *JSDocTag) *Node {
data := &JSDocSignature{}
data.typeParameters = typeParameters
data.TypeParameters = typeParameters
data.Parameters = parameters
data.Type = typeNode
return f.newNode(KindJSDocSignature, data)
}

func (f *NodeFactory) UpdateJSDocSignature(node *JSDocSignature, typeParameters *TypeParameterList, parameters *NodeList, typeNode *JSDocTag) *Node {
if typeParameters != node.typeParameters || parameters != node.Parameters || typeNode != node.Type {
func (f *NodeFactory) UpdateJSDocSignature(node *JSDocSignature, typeParameters *NodeList, parameters *NodeList, typeNode *JSDocTag) *Node {
if typeParameters != node.TypeParameters || parameters != node.Parameters || typeNode != node.Type {
return updateNode(f.NewJSDocSignature(typeParameters, parameters, typeNode), node.AsNode(), f.hooks)
}
return node.AsNode()
}

func (node *JSDocSignature) ForEachChild(v Visitor) bool {
return visitNodeList(v, node.typeParameters) || visitNodeList(v, node.Parameters) || visit(v, node.Type)
return visitNodeList(v, node.TypeParameters) || visitNodeList(v, node.Parameters) || visit(v, node.Type)
}

func (node *JSDocSignature) VisitEachChild(v *NodeVisitor) *Node {
return v.Factory.UpdateJSDocSignature(node, v.visitNodes(node.typeParameters), v.visitNodes(node.Parameters), v.visitNode(node.Type))
return v.Factory.UpdateJSDocSignature(node, v.visitNodes(node.TypeParameters), v.visitNodes(node.Parameters), v.visitNode(node.Type))
}

func (node *JSDocSignature) Clone(f NodeFactoryCoercible) *Node {
return cloneNode(f.AsNodeFactory().NewJSDocSignature(node.TypeParameters(), node.Parameters, node.Type), node.AsNode(), f.AsNodeFactory().hooks)
return cloneNode(f.AsNodeFactory().NewJSDocSignature(node.TypeParameters, node.Parameters, node.Type), node.AsNode(), f.AsNodeFactory().hooks)
}

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

// JSDocNameReference
type JSDocNameReference struct {
TypeNodeBase
Expand Down
8 changes: 5 additions & 3 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,11 @@ func WalkUpParenthesizedTypes(node *TypeNode) *Node {
}

func GetEffectiveTypeParent(parent *Node) *Node {
if IsInJSFile(parent) && parent.Kind == KindJSDocTypeExpression {
if host := parent.AsJSDocTypeExpression().Host; host != nil {
parent = host
if parent != nil && IsInJSFile(parent) {
if parent.Kind == KindJSDocTypeExpression && parent.AsJSDocTypeExpression().Host != nil {
parent = parent.AsJSDocTypeExpression().Host
} else if parent.Kind == KindJSDocTemplateTag && parent.AsJSDocTemplateTag().Host != nil {
parent = parent.AsJSDocTemplateTag().Host
}
}
return parent
Expand Down
12 changes: 5 additions & 7 deletions internal/binder/nameresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,8 @@ loop:
if isSelfReferenceLocation(location, lastLocation) {
lastSelfReferenceLocation = location
}
if location.Kind == ast.KindJSDocTypeExpression && location.AsJSDocTypeExpression().Host != nil {
lastLocation = location.AsJSDocTypeExpression().Host.Type()
location = location.AsJSDocTypeExpression().Host
} else {
lastLocation = location
location = location.Parent
}
lastLocation = location
location = ast.GetEffectiveTypeParent(location.Parent)
Comment on lines +291 to +292
Copy link
Preview

Copilot AI Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning lastLocation before normalizing location means lastLocation may reference raw JSDoc nodes instead of their hosts, which could break self-reference detection. Consider updating lastLocation after computing the effective parent.

Copilot uses AI. Check for mistakes.

}
// We just climbed up parents looking for the name, meaning that we started in a descendant node of `lastLocation`.
// If `result === lastSelfReferenceLocation.symbol`, that means that we are somewhere inside `lastSelfReferenceLocation` looking up a name, and resolving to `lastLocation` itself.
Expand Down Expand Up @@ -487,6 +482,9 @@ func isTypeParameterSymbolDeclaredInContainer(symbol *ast.Symbol, container *ast
for _, decl := range symbol.Declarations {
if decl.Kind == ast.KindTypeParameter {
parent := decl.Parent
if parent.Kind == ast.KindJSDocTemplateTag {
parent = parent.AsJSDocTemplateTag().Host
}
if parent == container {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22602,7 +22602,7 @@ func (c *Checker) getOuterTypeParametersOfClassOrInterface(symbol *ast.Symbol) [
// Return the outer type parameters of a node or undefined if the node has no outer type parameters.
func (c *Checker) getOuterTypeParameters(node *ast.Node, includeThisTypes bool) []*Type {
for {
node = node.Parent
node = ast.GetEffectiveTypeParent(node.Parent)
if node == nil {
return nil
}
Expand Down
Loading