Skip to content

Commit ddf52a6

Browse files
Fix crash when completing at beginning of property name preceded by JSDoc
Fixed two issues: 1. Variable shadowing bug in getRelevantTokens where contextToken was declared with := instead of assigned with =, causing it to remain nil 2. Added nil checks in tryGetClassLikeCompletionSymbols to handle cases where contextToken is nil These changes prevent the nil pointer dereference crash reported in the issue. Co-authored-by: DanielRosenwasser <[email protected]>
1 parent 9654302 commit ddf52a6

File tree

2 files changed

+5
-36
lines changed

2 files changed

+5
-36
lines changed

internal/fourslash/tests/completionJSDocBeforeProperty_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

internal/ls/completions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ func (l *LanguageService) getCompletionData(
14441444
completionKind = CompletionKindMemberLike
14451445
// Declaring new property/method/accessor
14461446
isNewIdentifierLocation = true
1447-
if contextToken.Kind == ast.KindAsteriskToken {
1447+
if contextToken != nil && contextToken.Kind == ast.KindAsteriskToken {
14481448
keywordFilters = KeywordCompletionFiltersNone
14491449
} else if ast.IsClassLike(decl) {
14501450
keywordFilters = KeywordCompletionFiltersClassElementKeywords
@@ -1458,17 +1458,17 @@ func (l *LanguageService) getCompletionData(
14581458
}
14591459

14601460
var classElement *ast.Node
1461-
if contextToken.Kind == ast.KindSemicolonToken {
1461+
if contextToken != nil && contextToken.Kind == ast.KindSemicolonToken {
14621462
classElement = contextToken.Parent.Parent
1463-
} else {
1463+
} else if contextToken != nil {
14641464
classElement = contextToken.Parent
14651465
}
14661466
var classElementModifierFlags ast.ModifierFlags
14671467
if ast.IsClassElement(classElement) {
14681468
classElementModifierFlags = classElement.ModifierFlags()
14691469
}
14701470
// If this is context token is not something we are editing now, consider if this would lead to be modifier.
1471-
if contextToken.Kind == ast.KindIdentifier && !isCurrentlyEditingNode(contextToken, file, position) {
1471+
if contextToken != nil && contextToken.Kind == ast.KindIdentifier && !isCurrentlyEditingNode(contextToken, file, position) {
14721472
switch contextToken.Text() {
14731473
case "private":
14741474
classElementModifierFlags |= ast.ModifierFlagsPrivate
@@ -2750,7 +2750,7 @@ func getSourceFromOrigin(origin *symbolOriginInfo) string {
27502750
func getRelevantTokens(position int, file *ast.SourceFile) (contextToken *ast.Node, previousToken *ast.Node) {
27512751
previousToken = astnav.FindPrecedingToken(file, position)
27522752
if previousToken != nil && position <= previousToken.End() && (ast.IsMemberName(previousToken) || ast.IsKeywordKind(previousToken.Kind)) {
2753-
contextToken := astnav.FindPrecedingToken(file, previousToken.Pos())
2753+
contextToken = astnav.FindPrecedingToken(file, previousToken.Pos())
27542754
return contextToken, previousToken
27552755
}
27562756
return previousToken, previousToken

0 commit comments

Comments
 (0)