Skip to content

Commit b0b5d75

Browse files
Fix completion crash after closing bracket in contextually-typed array literals (#2297)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: DanielRosenwasser <[email protected]>
1 parent dab8a33 commit b0b5d75

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

internal/fourslash/tests/manual/completionsInArrayLiteralWithContextualType_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,19 @@ func TestCompletionsInArrayLiteralWithContextualType(t *testing.T) {
6666
},
6767
},
6868
})
69+
70+
// Test 4: Completions after `]` in a tuple should not crash (issue #2296)
71+
// When completing after the closing bracket, we're outside the array literal
72+
// so we shouldn't be getting contextual types for array elements
73+
const content4 = `let x: [number] = [123]/*d*/;`
74+
f4, done4 := fourslash.NewFourslash(t, nil /*capabilities*/, content4)
75+
defer done4()
76+
// Just verify that completions don't crash - accept any completion list
77+
f4.VerifyCompletions(t, "d", &fourslash.CompletionsExpectedList{
78+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
79+
CommitCharacters: &DefaultCommitCharacters,
80+
EditRange: Ignored,
81+
},
82+
Items: &fourslash.CompletionsExpectedItems{},
83+
})
6984
}

internal/ls/completions.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,6 +2998,13 @@ func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFi
29982998
}
29992999
}
30003000
return nil
3001+
case ast.KindCloseBracketToken:
3002+
// When completing after `]` (e.g., `[x]/*here*/`), we should not provide a contextual type
3003+
// for the closing bracket token itself. Without this case, CloseBracketToken would fall through
3004+
// to the default case, and if the parent is an array literal, GetContextualType would try to
3005+
// find the token's index in the array elements (returning -1), leading to an out-of-bounds panic
3006+
// in getContextualTypeForElementExpression.
3007+
return nil
30013008
case ast.KindQuestionToken:
30023009
// When completing after `?` in a ternary conditional (e.g., `foo(a ? /*here*/)`),
30033010
// we need to look at the parent conditional expression to find the contextual type.

0 commit comments

Comments
 (0)