-
Notifications
You must be signed in to change notification settings - Fork 764
Fix contextual type for array completions #2309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| 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/testutil" | ||
| ) | ||
|
|
||
| func TestArgumentCompletions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = ` | ||
| function foo(a: "a", b: "b") {} | ||
| foo("a", /*1*/); | ||
|
|
||
|
|
||
| const t3 = ['x', 'y', 'z'] as const; | ||
| const x: [string, string, string, 'a' | 'b'] = [...t3, /*2*/]; | ||
| ` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyCompletions(t, "1", &fourslash.CompletionsExpectedList{ | ||
| ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ | ||
| CommitCharacters: &DefaultCommitCharacters, | ||
| }, | ||
| Items: &fourslash.CompletionsExpectedItems{ | ||
| Includes: []fourslash.CompletionsExpectedItem{`"b"`}, | ||
| }, | ||
| }) | ||
| f.VerifyCompletions(t, "2", &fourslash.CompletionsExpectedList{ | ||
| ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ | ||
| CommitCharacters: &DefaultCommitCharacters, | ||
| }, | ||
| Items: &fourslash.CompletionsExpectedItems{ | ||
| Includes: []fourslash.CompletionsExpectedItem{`"b"`}, | ||
| }, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2974,27 +2974,7 @@ func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFi | |
| contextualArrayType := typeChecker.GetContextualType(parent, checker.ContextFlagsNone) | ||
| if contextualArrayType != nil { | ||
| // Get the type for the first element (index 0) | ||
| return typeChecker.GetContextualTypeForArrayElement(contextualArrayType, 0) | ||
| } | ||
| } | ||
| return nil | ||
| case ast.KindCommaToken: | ||
| // When completing after `,` in an array literal (e.g., `[x, /*here*/]`), | ||
| // we should provide contextual type for the element after the comma | ||
| if ast.IsArrayLiteralExpression(parent) { | ||
| contextualArrayType := typeChecker.GetContextualType(parent, checker.ContextFlagsNone) | ||
| if contextualArrayType != nil { | ||
| // Count how many elements come before the cursor position | ||
| arrayLiteral := parent.AsArrayLiteralExpression() | ||
| elementIndex := 0 | ||
| for _, elem := range arrayLiteral.Elements.Nodes { | ||
| if elem.Pos() < position { | ||
| elementIndex++ | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| return typeChecker.GetContextualTypeForArrayElement(contextualArrayType, elementIndex) | ||
| return typeChecker.GetContextualTypeForArrayLiteralAtPosition(contextualArrayType, parent, position) | ||
| } | ||
| } | ||
| return nil | ||
|
|
@@ -3020,22 +3000,30 @@ func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFi | |
| 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 { | ||
| return typeChecker.GetContextualTypeForArgumentAtIndex(argInfo.invocation, argInfo.argumentIndex) | ||
| } else if isEqualityOperatorKind(previousToken.Kind) && ast.IsBinaryExpression(parent) && isEqualityOperatorKind(parent.AsBinaryExpression().OperatorToken.Kind) { | ||
| // completion at `x ===/**/` | ||
| return typeChecker.GetTypeAtLocation(parent.AsBinaryExpression().Left) | ||
| } else { | ||
| contextualType := typeChecker.GetContextualType(previousToken, checker.ContextFlagsCompletions) | ||
| if contextualType != nil { | ||
| return contextualType | ||
| case ast.KindCommaToken: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might still co-locate the I was thinking about whether there are cases where we should also guard on |
||
| // When completing after `,` in an array literal (e.g., `[x, /*here*/]`), | ||
| // we should provide contextual type for the element after the comma. | ||
| if ast.IsArrayLiteralExpression(parent) { | ||
| contextualArrayType := typeChecker.GetContextualType(parent, checker.ContextFlagsNone) | ||
| if contextualArrayType != nil { | ||
| return typeChecker.GetContextualTypeForArrayLiteralAtPosition(contextualArrayType, parent, position) | ||
| } | ||
| return typeChecker.GetContextualType(previousToken, checker.ContextFlagsNone) | ||
| return nil | ||
| } | ||
| } | ||
| // Default case: see if we're in an argument position. | ||
| argInfo := getArgumentInfoForCompletions(previousToken, position, file, typeChecker) | ||
| if argInfo != nil { | ||
| return typeChecker.GetContextualTypeForArgumentAtIndex(argInfo.invocation, argInfo.argumentIndex) | ||
| } else if isEqualityOperatorKind(previousToken.Kind) && ast.IsBinaryExpression(parent) && isEqualityOperatorKind(parent.AsBinaryExpression().OperatorToken.Kind) { | ||
| // completion at `x ===/**/` | ||
| return typeChecker.GetTypeAtLocation(parent.AsBinaryExpression().Left) | ||
| } else { | ||
| contextualType := typeChecker.GetContextualType(previousToken, checker.ContextFlagsCompletions) | ||
| if contextualType != nil { | ||
| return contextualType | ||
| } | ||
| return typeChecker.GetContextualType(previousToken, checker.ContextFlagsNone) | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.