Skip to content
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
49 changes: 49 additions & 0 deletions internal/fourslash/tests/completionJSDocBeforeProperty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/testutil"
)

// Test for: Crash completing beginning of property name when preceded by JSDoc
// This test verifies that requesting completions at the beginning of a property name
// preceded by JSDoc does not cause a crash. The crash was caused by a nil pointer
// dereference when contextToken was nil.
func TestCompletionJSDocBeforePropertyNoCrash(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `export class SomeInterface {
/** ruh-roh! */
/*a*/property: string;
}

export class SomeClass {
/** ruh-roh! */
/*b*/property = "value";
}`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
// The primary goal of this test is to ensure no panic occurs when requesting completions.
// The testutil.RecoverAndFail defer will catch any panic and fail the test.
// We verify completions can be requested successfully without checking specific completion items.
f.VerifyCompletions(t, "a", &fourslash.CompletionsExpectedList{
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &[]string{".", ",", ";"},
EditRange: fourslash.Ignored{},
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{},
},
})
f.VerifyCompletions(t, "b", &fourslash.CompletionsExpectedList{
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &[]string{".", ",", ";"},
EditRange: fourslash.Ignored{},
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{},
},
})
}
4 changes: 2 additions & 2 deletions internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ func getSourceFromOrigin(origin *symbolOriginInfo) string {
func getRelevantTokens(position int, file *ast.SourceFile) (contextToken *ast.Node, previousToken *ast.Node) {
previousToken = astnav.FindPrecedingToken(file, position)
if previousToken != nil && position <= previousToken.End() && (ast.IsMemberName(previousToken) || ast.IsKeywordKind(previousToken.Kind)) {
contextToken := astnav.FindPrecedingToken(file, previousToken.Pos())
contextToken = astnav.FindPrecedingToken(file, previousToken.Pos())
Copy link
Member

Choose a reason for hiding this comment

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

this can't possibly do anything anymore...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right - the variable shadowing fix alone wasn't sufficient. The issue was that tryGetObjectTypeDeclarationCompletionContainer could return a non-nil declaration even when contextToken was nil (specifically in the Identifier case at line 4166). I've added a nil check there: if contextToken != nil && isFromObjectTypeDeclaration(location) (commit 27b3371).

return contextToken, previousToken
}
return previousToken, previousToken
Expand Down Expand Up @@ -4163,7 +4163,7 @@ func tryGetObjectTypeDeclarationCompletionContainer(
return nil
}
// class c extends React.Component { a: () => 1\n compon| }
if isFromObjectTypeDeclaration(location) {
if contextToken != nil && isFromObjectTypeDeclaration(location) {
return ast.FindAncestor(location, ast.IsObjectTypeDeclaration)
}
}
Expand Down