diff --git a/tsc/internal/fourslash/tests/signatureHelpUnclosedParen_test.go b/tsc/internal/fourslash/tests/signatureHelpUnclosedParen_test.go new file mode 100644 index 0000000000000..6023219fab420 --- /dev/null +++ b/tsc/internal/fourslash/tests/signatureHelpUnclosedParen_test.go @@ -0,0 +1,19 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestSignatureHelpUnclosedTypeArgumentHang(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `<()(/**/` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.GoToMarker(t, "") + + f.VerifyNoSignatureHelp(t) +} diff --git a/tsc/internal/ls/utilities.go b/tsc/internal/ls/utilities.go index a05994440d530..87401f57f56cf 100644 --- a/tsc/internal/ls/utilities.go +++ b/tsc/internal/ls/utilities.go @@ -1161,12 +1161,13 @@ func findPrecedingMatchingToken(token *ast.Node, matchingTokenKind ast.Kind, sou matchingTokenText := scanner.TokenToString(matchingTokenKind) // Text-scan based fast path - can be bamboozled by comments and other trivia, but often provides // a good, fast approximation without too much extra work in the cases where it fails. - bestGuessIndex := strings.LastIndex(sourceFile.Text(), matchingTokenText) + precedingText := sourceFile.Text()[:token.Pos()] + bestGuessIndex := strings.LastIndex(precedingText, matchingTokenText) if bestGuessIndex == -1 { return nil // if the token text doesn't appear in the file, there can't be a match - super fast bail } // we can only use the textual result directly if we didn't have to count any close tokens within the range - if strings.LastIndex(sourceFile.Text(), closeTokenText) < bestGuessIndex { + if strings.LastIndex(precedingText, closeTokenText) < bestGuessIndex { nodeAtGuess := astnav.FindPrecedingToken(sourceFile, bestGuessIndex+1) if nodeAtGuess != nil && nodeAtGuess.Kind == matchingTokenKind { return nodeAtGuess