Skip to content

Commit 07ea363

Browse files
Copilotjakebailey
andauthored
Port TS#56182: Include source node inferences in string literal completions deeper in arguments (#2747)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 985bb98 commit 07ea363

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

internal/fourslash/_scripts/failingTests.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ TestSignatureHelpCallExpressionJs
556556
TestSpaceAfterReturn
557557
TestStringCompletionsImportOrExportSpecifier
558558
TestStringCompletionsVsEscaping
559-
TestStringLiteralCompletionsWithinInferredObjectWhenItsKeysAreUsedOutsideOfIt
560559
TestSuggestionOfUnusedVariableWithExternalModule
561560
TestSymbolCompletionLowerPriority
562561
TestSyntheticImportFromBabelGeneratedFile1
@@ -570,7 +569,6 @@ TestTsxQuickInfo5
570569
TestTsxQuickInfo6
571570
TestTsxQuickInfo7
572571
TestTypeCheckAfterResolve
573-
TestTypeErrorAfterStringCompletionsInNestedCall
574572
TestTypeOperatorNodeBuilding
575573
TestUnclosedStringLiteralAutoformating
576574
TestWhiteSpaceTrimming

internal/fourslash/_scripts/manualTests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ stringLiteralCompletionsInPositionTypedUsingRest
124124
tripleSlashRefPathCompletionAbsolutePaths
125125
tripleSlashRefPathCompletionHiddenFile
126126
tsxCompletion12
127+
typeErrorAfterStringCompletionsInNestedCall

internal/fourslash/tests/gen/typeErrorAfterStringCompletionsInNestedCall_test.go renamed to internal/fourslash/tests/manual/typeErrorAfterStringCompletionsInNestedCall_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
)
1111

1212
func TestTypeErrorAfterStringCompletionsInNestedCall(t *testing.T) {
13-
fourslash.SkipIfFailing(t)
1413
t.Parallel()
1514
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
1615
const content = `// @stableTypeOrdering: true
@@ -49,8 +48,8 @@ createMachine<GreetingEvent>({
4948
},
5049
Items: &fourslash.CompletionsExpectedItems{
5150
Exact: []fourslash.CompletionsExpectedItem{
52-
"ALOHAx",
5351
"ALOHA",
52+
"ALOHAx",
5453
"LUNCH_TIME",
5554
"MORNING",
5655
},

internal/ls/string_completions.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,13 @@ func (l *LanguageService) getStringLiteralCompletionEntries(
281281
fromProperties: stringLiteralCompletionsForObjectLiteral(typeChecker, parent.Parent),
282282
}
283283
}
284-
result := fromContextualType(checker.ContextFlagsCompletions, node, typeChecker)
285-
if result != nil {
286-
return &stringLiteralCompletions{
287-
fromTypes: result,
288-
}
284+
if ast.FindAncestor(parent.Parent, ast.IsCallLikeExpression) != nil {
285+
uniques := &collections.Set[string]{}
286+
stringLiteralTypes := append(
287+
getStringLiteralTypes(typeChecker.GetContextualType(node, checker.ContextFlagsNone), uniques, typeChecker),
288+
getStringLiteralTypes(typeChecker.GetContextualType(node, checker.ContextFlagsCompletions), uniques, typeChecker)...,
289+
)
290+
return toStringLiteralCompletionsFromTypes(stringLiteralTypes)
289291
}
290292
return &stringLiteralCompletions{
291293
fromTypes: fromContextualType(checker.ContextFlagsNone, node, typeChecker),
@@ -421,7 +423,10 @@ func (l *LanguageService) getStringLiteralCompletionEntries(
421423
func fromContextualType(contextFlags checker.ContextFlags, node *ast.Node, typeChecker *checker.Checker) *completionsFromTypes {
422424
// Get completion for string literal from string literal type
423425
// i.e. var x: "hi" | "hello" = "/*completion position*/"
424-
types := getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker, contextFlags), nil, typeChecker)
426+
return toCompletionsFromTypes(getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker, contextFlags), nil, typeChecker))
427+
}
428+
429+
func toCompletionsFromTypes(types []*checker.StringLiteralType) *completionsFromTypes {
425430
if len(types) == 0 {
426431
return nil
427432
}
@@ -431,6 +436,16 @@ func fromContextualType(contextFlags checker.ContextFlags, node *ast.Node, typeC
431436
}
432437
}
433438

439+
func toStringLiteralCompletionsFromTypes(types []*checker.StringLiteralType) *stringLiteralCompletions {
440+
result := toCompletionsFromTypes(types)
441+
if result == nil {
442+
return nil
443+
}
444+
return &stringLiteralCompletions{
445+
fromTypes: result,
446+
}
447+
}
448+
434449
func fromUnionableLiteralType(
435450
grandparent *ast.Node,
436451
parent *ast.Node,

0 commit comments

Comments
 (0)