Skip to content

Fix getTokensFromNode template scanning issue #1560

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
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
31 changes: 31 additions & 0 deletions internal/ls/signaturehelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,12 @@ func getTokensFromNode(node *ast.Node, sourceFile *ast.SourceFile) []*ast.Node {
if node == nil {
return nil
}

// Special handling for template expressions
if node.Kind == ast.KindTemplateExpression {
return getTokensFromTemplateExpression(node, sourceFile)
}

var children []*ast.Node
current := node
left := node.Pos()
Expand All @@ -1098,6 +1104,31 @@ func getTokensFromNode(node *ast.Node, sourceFile *ast.SourceFile) []*ast.Node {
return children
}

func getTokensFromTemplateExpression(node *ast.Node, sourceFile *ast.SourceFile) []*ast.Node {
templateExpr := node.AsTemplateExpression()
var children []*ast.Node

// Add the template head
head := templateExpr.Head
children = append(children, head.AsNode())

// Add tokens for each template span
if templateExpr.TemplateSpans != nil {
for _, spanNode := range templateExpr.TemplateSpans.Nodes {
span := spanNode.AsTemplateSpan()

// Add tokens from the expression part
exprTokens := getTokensFromNode(span.Expression.AsNode(), sourceFile)
children = append(children, exprTokens...)

// Add the template literal part (middle or tail)
children = append(children, span.Literal.AsNode())
}
}

return children
}

func getTokenFromNodeList(nodeList *ast.NodeList, nodeListParent *ast.Node, sourceFile *ast.SourceFile) []*ast.Node {
if nodeList == nil || nodeListParent == nil {
return nil
Expand Down
9 changes: 9 additions & 0 deletions internal/ls/signaturehelp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,15 @@ f("x", /**/);`,
"": {text: `f(a: "x", b: "x", c: "x"): void`, parameterCount: 3, parameterSpan: `b: "x"`, activeParameter: &lsproto.UintegerOrNull{Uinteger: ptrTo(uint32(1))}},
},
},
{
title: "signatureHelpTaggedTemplate",
input: `function tag(strings: TemplateStringsArray, ...values: any[]) { return ""; }
const value = 42;
const result = tag` + "`Hello ${value} /*1*/ world ${value}!`" + `;`,
expected: map[string]verifySignatureHelpOptions{
"1": {text: "tag(strings: TemplateStringsArray, ...values: any[]): string", parameterCount: 2, parameterSpan: "...values: any[]", activeParameter: &lsproto.UintegerOrNull{Uinteger: ptrTo(uint32(1))}},
},
},
{
title: "signatureHelpInParenthetical",
input: `class base { constructor (public n: number, public y: string) { } }
Expand Down
Loading