Skip to content

Commit 27b3371

Browse files
Fix crash with nil contextToken and add test
- Fixed variable shadowing in getRelevantTokens (contextToken := to contextToken =) - Added nil check in tryGetObjectTypeDeclarationCompletionContainer for Identifier case - Added test to verify no crash when completing at property start after JSDoc - Merged from main and ran hereby generate Co-authored-by: DanielRosenwasser <[email protected]>
1 parent d55224e commit 27b3371

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
// Test for: Crash completing beginning of property name when preceded by JSDoc
11+
// This test verifies that requesting completions at the beginning of a property name
12+
// preceded by JSDoc does not cause a crash. The crash was caused by a nil pointer
13+
// dereference when contextToken was nil.
14+
func TestCompletionJSDocBeforePropertyNoCrash(t *testing.T) {
15+
t.Parallel()
16+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
17+
const content = `export class SomeInterface {
18+
/** ruh-roh! */
19+
/*a*/property: string;
20+
}
21+
22+
export class SomeClass {
23+
/** ruh-roh! */
24+
/*b*/property = "value";
25+
}`
26+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
27+
defer done()
28+
// The primary goal of this test is to ensure no panic occurs when requesting completions.
29+
// The testutil.RecoverAndFail defer will catch any panic and fail the test.
30+
// We verify completions can be requested successfully without checking specific completion items.
31+
f.VerifyCompletions(t, "a", &fourslash.CompletionsExpectedList{
32+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
33+
CommitCharacters: &[]string{".", ",", ";"},
34+
EditRange: fourslash.Ignored{},
35+
},
36+
Items: &fourslash.CompletionsExpectedItems{
37+
Includes: []fourslash.CompletionsExpectedItem{},
38+
},
39+
})
40+
f.VerifyCompletions(t, "b", &fourslash.CompletionsExpectedList{
41+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
42+
CommitCharacters: &[]string{".", ",", ";"},
43+
EditRange: fourslash.Ignored{},
44+
},
45+
Items: &fourslash.CompletionsExpectedItems{
46+
Includes: []fourslash.CompletionsExpectedItem{},
47+
},
48+
})
49+
}

internal/ls/completions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4163,7 +4163,7 @@ func tryGetObjectTypeDeclarationCompletionContainer(
41634163
return nil
41644164
}
41654165
// class c extends React.Component { a: () => 1\n compon| }
4166-
if isFromObjectTypeDeclaration(location) {
4166+
if contextToken != nil && isFromObjectTypeDeclaration(location) {
41674167
return ast.FindAncestor(location, ast.IsObjectTypeDeclaration)
41684168
}
41694169
}

0 commit comments

Comments
 (0)