Skip to content

Fix panic when hovering over functions with JSDoc links #1339

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ func (n *Node) Text() string {
return n.AsRegularExpressionLiteral().Text
case KindJSDocText:
return strings.Join(n.AsJSDocText().text, "")
case KindJSDocLink:
return strings.Join(n.AsJSDocLink().text, "")
case KindJSDocLinkCode:
return strings.Join(n.AsJSDocLinkCode().text, "")
case KindJSDocLinkPlain:
return strings.Join(n.AsJSDocLinkPlain().text, "")
}
panic(fmt.Sprintf("Unhandled case in Node.Text: %T", n.data))
}
Expand Down
24 changes: 24 additions & 0 deletions internal/ls/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ func writeComments(b *strings.Builder, comments []*ast.Node) {
}
}
b.WriteString(text)
case ast.KindJSDocLinkCode:
// !!! TODO: This is a temporary placeholder implementation that needs to be updated later
name := comment.Name()
text := comment.AsJSDocLinkCode().Text()
if name != nil {
if text == "" {
writeEntityName(b, name)
} else {
writeEntityNameParts(b, name)
}
}
b.WriteString(text)
case ast.KindJSDocLinkPlain:
// !!! TODO: This is a temporary placeholder implementation that needs to be updated later
name := comment.Name()
text := comment.AsJSDocLinkPlain().Text()
if name != nil {
if text == "" {
writeEntityName(b, name)
} else {
writeEntityNameParts(b, name)
}
}
b.WriteString(text)
}
}
}
Expand Down
97 changes: 97 additions & 0 deletions internal/ls/hover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ls_test

import (
"context"
"testing"

"github.com/microsoft/typescript-go/internal/bundled"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
"github.com/microsoft/typescript-go/internal/testutil/projecttestutil"
"gotest.tools/v3/assert"
)

func TestHover(t *testing.T) {
t.Parallel()
if !bundled.Embedded {
// Without embedding, we'd need to read all of the lib files out from disk into the MapFS.
// Just skip this for now.
t.Skip("bundled files are not embedded")
}

testCases := []struct {
title string
input string
expected map[string]*lsproto.Hover
}{
{
title: "JSDocLinksPanic",
input: `
// @filename: index.ts
/**
* A function with JSDoc links that previously caused panic
* {@link console.log} and {@linkcode Array.from} and {@linkplain Object.keys}
*/
function myFunction() {
return "test";
}

/*marker*/myFunction();`,
expected: map[string]*lsproto.Hover{
"marker": {
Contents: lsproto.MarkupContentOrMarkedStringOrMarkedStrings{
MarkupContent: &lsproto.MarkupContent{
Kind: lsproto.MarkupKindMarkdown,
Value: "```tsx\nfunction myFunction(): string\n```\nA function with JSDoc links that previously caused panic\n`console.log` and `Array.from` and `Object.keys`\n",
},
},
},
},
},
}

for _, testCase := range testCases {
t.Run(testCase.title, func(t *testing.T) {
t.Parallel()
runHoverTest(t, testCase.input, testCase.expected)
})
}
}

func runHoverTest(t *testing.T, input string, expected map[string]*lsproto.Hover) {
testData := fourslash.ParseTestData(t, input, "/mainFile.ts")
file := testData.Files[0].FileName()
markerPositions := testData.MarkerPositions
ctx := projecttestutil.WithRequestID(t.Context())
languageService, done := createLanguageServiceForHover(ctx, file, map[string]any{
file: testData.Files[0].Content,
})
defer done()

for markerName, expectedResult := range expected {
marker, ok := markerPositions[markerName]
if !ok {
t.Fatalf("No marker found for '%s'", markerName)
}
result, err := languageService.ProvideHover(
ctx,
ls.FileNameToDocumentURI(file),
marker.LSPosition)
assert.NilError(t, err)
if expectedResult == nil {
assert.Assert(t, result == nil)
} else {
assert.Assert(t, result != nil)
assert.DeepEqual(t, *result, *expectedResult)
}
}
}

func createLanguageServiceForHover(ctx context.Context, fileName string, files map[string]any) (*ls.LanguageService, func()) {
projectService, _ := projecttestutil.Setup(files, nil)
projectService.OpenFile(fileName, files[fileName].(string), core.GetScriptKindFromFileName(fileName), "")
project := projectService.Projects()[0]
return project.GetLanguageServiceForRequest(ctx)
}