Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.7k
Fix completions of optional properties in generic positions#33937
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
Andrew Branch (andrewbranch)
merged 18 commits into
microsoft:master
from
andrewbranch:bug/30507Oct 17, 2019
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8e37e0a
fixes #30507
timsuchanek 5c561ea
Add test case for generic Partial type
timsuchanek d674ee0
Fixes #28470
timsuchanek ad166f7
Simplify contextFlags binary check
timsuchanek 50286b9
Add string literal completion test
timsuchanek ba1e478
Fix ContextFlags typings
timsuchanek adf8a54
Speed up inference expression for completion
timsuchanek 25fa027
Fix baseline merge
andrewbranch 7883ec6
Make contextFlags internal
timsuchanek 7e7dc08
Reapply readonly array changes
timsuchanek c1a47a5
accept baselines
timsuchanek 96a782e
Fix generic completion tests
timsuchanek 5ae31ff
Merge master
orta b863e01
Merge branch 'master' into genericObjectExpressionCompletion
andrewbranch d1cb5cb
Re-merge ContextFlags
andrewbranch 0b46fe3
Don’t change type during inference
andrewbranch 66d8c27
Fix typos and superfluous undefined arguments
andrewbranch c95f6ab
Add test for completions in unconstrained generic object literal
andrewbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -174,12 +174,6 @@ namespace ts { | ||
| IsForSignatureHelp = 1 << 4, // Call resolution for purposes of signature help | ||
| } | ||
| const enum ContextFlags { | ||
| None = 0, | ||
| Signature = 1 << 0, // Obtaining contextual signature | ||
| NoConstraints = 1 << 1, // Don't obtain type variable constraints | ||
| } | ||
| const enum AccessFlags { | ||
| None = 0, | ||
| NoIndexSignatures = 1 << 0, | ||
| @@ -454,9 +448,9 @@ namespace ts { | ||
| }, | ||
| getAugmentedPropertiesOfType, | ||
| getRootSymbols, | ||
| getContextualType: nodeIn => { | ||
| getContextualType: (nodeIn: Expression, contextFlags?: ContextFlags) => { | ||
| const node = getParseTreeNode(nodeIn, isExpression); | ||
| return node ? getContextualType(node) : undefined; | ||
| return node ? getContextualType(node, contextFlags) : undefined; | ||
| }, | ||
| getContextualTypeForObjectLiteralElement: nodeIn => { | ||
| const node = getParseTreeNode(nodeIn, isObjectLiteralElementLike); | ||
| @@ -20887,19 +20881,23 @@ namespace ts { | ||
| } | ||
| // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. | ||
| function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression): Type | undefined { | ||
| function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression, contextFlags?: ContextFlags): Type | undefined { | ||
| const args = getEffectiveCallArguments(callTarget); | ||
| const argIndex = args.indexOf(arg); // -1 for e.g. the expression of a CallExpression, or the tag of a TaggedTemplateExpression | ||
| return argIndex === -1 ? undefined : getContextualTypeForArgumentAtIndex(callTarget, argIndex); | ||
| return argIndex === -1 ? undefined : getContextualTypeForArgumentAtIndex(callTarget, argIndex, contextFlags); | ||
| } | ||
| function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number): Type { | ||
| function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number, contextFlags?: ContextFlags): Type { | ||
| // If we're already in the process of resolving the given signature, don't resolve again as | ||
| // that could cause infinite recursion. Instead, return anySignature. | ||
| const signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget); | ||
| if (isJsxOpeningLikeElement(callTarget) && argIndex === 0) { | ||
| return getEffectiveFirstArgumentForJsxSignature(signature, callTarget); | ||
| } | ||
| if (contextFlags && contextFlags & ContextFlags.Completion && signature.target) { | ||
| const baseSignature = getBaseSignature(signature.target); | ||
andrewbranch marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| return intersectTypes(getTypeAtPosition(signature, argIndex), getTypeAtPosition(baseSignature, argIndex)); | ||
andrewbranch marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return getTypeAtPosition(signature, argIndex); | ||
| } | ||
| @@ -21291,7 +21289,7 @@ namespace ts { | ||
| } | ||
| /* falls through */ | ||
| case SyntaxKind.NewExpression: | ||
| return getContextualTypeForArgument(<CallExpression | NewExpression>parent, node); | ||
| return getContextualTypeForArgument(<CallExpression | NewExpression>parent, node, contextFlags); | ||
| case SyntaxKind.TypeAssertionExpression: | ||
| case SyntaxKind.AsExpression: | ||
| return isConstTypeReference((<AssertionExpression>parent).type) ? undefined : getTypeFromTypeNode((<AssertionExpression>parent).type); | ||
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
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,14 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| ////function f<T>(x: T) { | ||
| //// return x; | ||
| ////} | ||
| //// | ||
| ////f({ /**/ }); | ||
| verify.completions({ | ||
| marker: "", | ||
| exact: [] | ||
| }); |
11 changes: 11 additions & 0 deletions
11 tests/cases/fourslash/completionsWithGenericStringLiteral.ts
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,11 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| //// declare function get<T, K extends keyof T>(obj: T, key: K): T[K]; | ||
| //// get({ hello: 123, world: 456 }, "/**/"); | ||
| verify.completions({ | ||
| marker: "", | ||
| includes: ['hello', 'world'] | ||
| }); | ||
19 changes: 19 additions & 0 deletions
19 tests/cases/fourslash/completionsWithOptionalPropertiesGeneric.ts
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,19 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| //// interface MyOptions { | ||
| //// hello?: boolean; | ||
| //// world?: boolean; | ||
| //// } | ||
| //// declare function bar<T extends MyOptions>(options?: Partial<T>): void; | ||
andrewbranch marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| //// bar({ hello, /*1*/ }); | ||
| verify.completions({ | ||
| marker: '1', | ||
| includes: [ | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'world' | ||
| }, | ||
| ] | ||
| }) | ||
27 changes: 27 additions & 0 deletions
27 tests/cases/fourslash/completionsWithOptionalPropertiesGenericConstructor.ts
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,27 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| //// interface Options { | ||
| //// someFunction?: () => string | ||
| //// anotherFunction?: () => string | ||
| //// } | ||
| //// | ||
| //// export class Clazz<T extends Options> { | ||
| //// constructor(public a: T) {} | ||
| //// } | ||
| //// | ||
| //// new Clazz({ /*1*/ }) | ||
| verify.completions({ | ||
| marker: '1', | ||
| includes: [ | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'someFunction' | ||
| }, | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'anotherFunction' | ||
| }, | ||
| ] | ||
| }) |
23 changes: 23 additions & 0 deletions
23 tests/cases/fourslash/completionsWithOptionalPropertiesGenericDeep.ts
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,23 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| //// interface DeepOptions { | ||
| //// another?: boolean; | ||
| //// } | ||
| //// interface MyOptions { | ||
| //// hello?: boolean; | ||
| //// world?: boolean; | ||
| //// deep?: DeepOptions | ||
| //// } | ||
| //// declare function bar<T extends MyOptions>(options?: Partial<T>): void; | ||
| //// bar({ deep: {/*1*/} }); | ||
| verify.completions({ | ||
| marker: '1', | ||
| includes: [ | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'another' | ||
| }, | ||
| ] | ||
| }) |
33 changes: 33 additions & 0 deletions
33 tests/cases/fourslash/completionsWithOptionalPropertiesGenericPartial.ts
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,33 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| //// interface Foo { | ||
| //// a_a: boolean; | ||
| //// a_b: boolean; | ||
| //// a_c: boolean; | ||
| //// b_a: boolean; | ||
| //// } | ||
| //// function partialFoo<T extends Partial<Foo>>(t: T) {return t} | ||
| //// partialFoo({ /*1*/ }); | ||
| verify.completions({ | ||
| marker: '1', | ||
| includes: [ | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'a_a' | ||
| }, | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'a_b' | ||
| }, | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'a_c' | ||
| }, | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'b_a' | ||
| }, | ||
| ] | ||
| }) |
22 changes: 22 additions & 0 deletions
22 tests/cases/fourslash/completionsWithOptionalPropertiesGenericPartial2.ts
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,22 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| //// interface Foo { | ||
| //// a: boolean; | ||
| //// } | ||
| //// function partialFoo<T extends Partial<Foo>>(x: T, y: T) {return t} | ||
| //// partialFoo({ a: true, b: true }, { /*1*/ }); | ||
| verify.completions({ | ||
| marker: '1', | ||
| includes: [ | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'a' | ||
| }, | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'b' | ||
| } | ||
| ] | ||
| }) |
29 changes: 29 additions & 0 deletions
29 tests/cases/fourslash/completionsWithOptionalPropertiesGenericPartial3.ts
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,29 @@ | ||
| /// <reference path="fourslash.ts" /> | ||
| // @strict: true | ||
| ////interface Foo { | ||
| //// a: boolean; | ||
| ////} | ||
| ////function partialFoo<T extends Partial<Foo>>(x: T, y: T extends { b?: boolean } ? T & { c: true } : T) { | ||
| //// return x; | ||
| ////} | ||
| //// | ||
| ////partialFoo({ a: true, b: true }, { /*1*/ }); | ||
| verify.completions({ | ||
| marker: '1', | ||
| includes: [ | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'a' | ||
| }, | ||
| { | ||
| sortText: completion.SortText.OptionalMember, | ||
| name: 'b' | ||
| }, | ||
| { | ||
| name: 'c' | ||
| } | ||
| ] | ||
| }) |
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.