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

import (
"testing"

"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestCompletionInTernaryConditional(t *testing.T) {
t.Parallel()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `export enum Bar { }
export enum Foo { }


function foo(x: Foo) { return x; }
function bar(z: string, x: Foo) { return x; }

const a = '';

foo(/*1*/);
bar(a, a == '' ? /*2*/);
bar(a, a == '' ? /*3*/ : /*4*/);`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()

// Test marker 1 - should have Foo preselected in simple call
f.VerifyCompletions(t, "1", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &DefaultCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "Foo",
Kind: PtrTo(lsproto.CompletionItemKindEnum),
Preselect: PtrTo(true),
},
},
},
})

// Test marker 2 - should have Foo preselected after ? in incomplete ternary
f.VerifyCompletions(t, "2", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &DefaultCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "Foo",
Kind: PtrTo(lsproto.CompletionItemKindEnum),
Preselect: PtrTo(true),
},
},
},
})

// Test marker 3 - should have Foo preselected after ? in ternary with colon
f.VerifyCompletions(t, "3", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &DefaultCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "Foo",
Kind: PtrTo(lsproto.CompletionItemKindEnum),
Preselect: PtrTo(true),
},
},
},
})

// Test marker 4 - should have Foo preselected after : in ternary
f.VerifyCompletions(t, "4", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &DefaultCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "Foo",
Kind: PtrTo(lsproto.CompletionItemKindEnum),
Preselect: PtrTo(true),
},
},
},
})
}
32 changes: 32 additions & 0 deletions internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2923,6 +2923,21 @@ func isStaticProperty(symbol *ast.Symbol) bool {
ast.IsClassLike(symbol.ValueDeclaration.Parent)
}

// getContextualTypeForConditionalExpression handles completion within a conditional expression
// (ternary operator) by using the parent expression to find the contextual type.
func getContextualTypeForConditionalExpression(conditionalExpr *ast.Node, position int, file *ast.SourceFile, typeChecker *checker.Checker) *checker.Type {
argInfo := getArgumentInfoForCompletions(conditionalExpr, position, file, typeChecker)
if argInfo != nil {
return typeChecker.GetContextualTypeForArgumentAtIndex(argInfo.invocation, argInfo.argumentIndex)
}
// Fall through to regular contextual type logic if not in an argument
contextualType := typeChecker.GetContextualType(conditionalExpr, checker.ContextFlagsCompletions)
if contextualType != nil {
return contextualType
}
return typeChecker.GetContextualType(conditionalExpr, checker.ContextFlagsNone)
}

func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFile, typeChecker *checker.Checker) *checker.Type {
parent := previousToken.Parent
switch previousToken.Kind {
Expand Down Expand Up @@ -2952,6 +2967,23 @@ func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFi
return typeChecker.GetContextualTypeForJsxAttribute(parent.Parent)
}
return nil
case ast.KindQuestionToken:
// When completing after `?` in a ternary conditional (e.g., `foo(a ? /*here*/)`),
// we need to look at the parent conditional expression to find the contextual type.
if ast.IsConditionalExpression(parent) {
return getContextualTypeForConditionalExpression(parent, position, file, typeChecker)
}
return nil
case ast.KindColonToken:
// When completing after `:` in a ternary conditional (e.g., `foo(a ? b : /*here*/)`),
// we need to look at the parent conditional expression to find the contextual type.
// Only handle this if parent is ConditionalExpression, otherwise fall through to default
// (colons are used in other contexts like object literals, type annotations, etc.)
if ast.IsConditionalExpression(parent) {
return getContextualTypeForConditionalExpression(parent, position, file, typeChecker)
}
// Fall through to default for other colon contexts (object literals, etc.)
fallthrough
default:
argInfo := getArgumentInfoForCompletions(previousToken, position, file, typeChecker)
if argInfo != nil {
Expand Down