Skip to content

Add support for JSDoc @implements tag in reparser #1104

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
65 changes: 65 additions & 0 deletions internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,71 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) {
fun.FunctionLikeData().Type = p.makeNewType(tag.AsJSDocReturnTag().TypeExpression, fun)
}
}
case ast.KindJSDocImplementsTag:
implementsTag := tag.AsJSDocImplementsTag()
if parent.Kind == ast.KindClassDeclaration {
class := parent.AsClassDeclaration()

// Create ExpressionWithTypeArguments from the class name
expression := implementsTag.ClassName
// For JSDoc @implements, we use the className as-is without extracting type arguments
// since it's already parsed appropriately in parseExpressionWithTypeArgumentsForAugments
var typeArguments *ast.NodeList
if expression.Kind == ast.KindExpressionWithTypeArguments {
exprWithArgs := expression.AsExpressionWithTypeArguments()
expression = exprWithArgs.Expression
typeArguments = exprWithArgs.TypeArguments
}

// Create a new ExpressionWithTypeArguments
implementsExpression := p.factory.NewExpressionWithTypeArguments(expression, typeArguments)
implementsExpression.Loc = tag.Loc
implementsExpression.Flags = p.contextFlags | ast.NodeFlagsReparsed

// Create implements heritage clause
implementsClause := p.factory.NewHeritageClause(ast.KindImplementsKeyword, p.factory.NewNodeList([]*ast.Node{implementsExpression}))
implementsClause.Loc = tag.Loc
implementsClause.Flags = p.contextFlags | ast.NodeFlagsReparsed

// Add to existing heritage clauses or create new list
var heritageList []*ast.Node
if class.HeritageClauses != nil {
heritageList = append(heritageList, class.HeritageClauses.Nodes...)
}
heritageList = append(heritageList, implementsClause)

class.HeritageClauses = p.factory.NewNodeList(heritageList)
} else if parent.Kind == ast.KindClassExpression {
class := parent.AsClassExpression()

// Create ExpressionWithTypeArguments from the class name
expression := implementsTag.ClassName
var typeArguments *ast.NodeList
if expression.Kind == ast.KindExpressionWithTypeArguments {
exprWithArgs := expression.AsExpressionWithTypeArguments()
expression = exprWithArgs.Expression
typeArguments = exprWithArgs.TypeArguments
}

// Create a new ExpressionWithTypeArguments
implementsExpression := p.factory.NewExpressionWithTypeArguments(expression, typeArguments)
implementsExpression.Loc = tag.Loc
implementsExpression.Flags = p.contextFlags | ast.NodeFlagsReparsed

// Create implements heritage clause
implementsClause := p.factory.NewHeritageClause(ast.KindImplementsKeyword, p.factory.NewNodeList([]*ast.Node{implementsExpression}))
implementsClause.Loc = tag.Loc
implementsClause.Flags = p.contextFlags | ast.NodeFlagsReparsed

// Add to existing heritage clauses or create new list
var heritageList []*ast.Node
if class.HeritageClauses != nil {
heritageList = append(heritageList, class.HeritageClauses.Nodes...)
}
heritageList = append(heritageList, implementsClause)

class.HeritageClauses = p.factory.NewNodeList(heritageList)
}
}
}
}
Expand Down
Loading