-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Support completions contextual types in more places #20768
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 all commits
Commits
Show all changes
2 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 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 |
---|---|---|
|
@@ -13943,7 +13943,7 @@ namespace ts { | |
// the contextual type of an initializer expression is the type annotation of the containing declaration, if present. | ||
function getContextualTypeForInitializerExpression(node: Expression): Type { | ||
const declaration = <VariableLikeDeclaration>node.parent; | ||
if (hasInitializer(declaration) && node === declaration.initializer || node.kind === SyntaxKind.EqualsToken) { | ||
if (hasInitializer(declaration) && node === declaration.initializer) { | ||
const typeNode = getEffectiveTypeAnnotationNode(declaration); | ||
if (typeNode) { | ||
return getTypeFromTypeNode(typeNode); | ||
|
@@ -14075,12 +14075,6 @@ namespace ts { | |
case SyntaxKind.AmpersandAmpersandToken: | ||
case SyntaxKind.CommaToken: | ||
return node === right ? getContextualType(binaryExpression) : undefined; | ||
case SyntaxKind.EqualsEqualsEqualsToken: | ||
case SyntaxKind.EqualsEqualsToken: | ||
case SyntaxKind.ExclamationEqualsEqualsToken: | ||
case SyntaxKind.ExclamationEqualsToken: | ||
// For completions after `x === ` | ||
return node === operatorToken ? getTypeOfExpression(binaryExpression.left) : undefined; | ||
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. is this really not used anywhere else besides services? 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. Yes, this was also added by #20020. |
||
default: | ||
return undefined; | ||
} | ||
|
@@ -14296,12 +14290,8 @@ namespace ts { | |
return getContextualTypeForReturnExpression(node); | ||
case SyntaxKind.YieldExpression: | ||
return getContextualTypeForYieldOperand(<YieldExpression>parent); | ||
case SyntaxKind.CallExpression: | ||
case SyntaxKind.NewExpression: | ||
if (node.kind === SyntaxKind.NewKeyword) { // for completions after `new ` | ||
return getContextualType(parent as NewExpression); | ||
} | ||
// falls through | ||
case SyntaxKind.CallExpression: | ||
return getContextualTypeForArgument(<CallExpression | NewExpression>parent, node); | ||
case SyntaxKind.TypeAssertionExpression: | ||
case SyntaxKind.AsExpression: | ||
|
@@ -14336,12 +14326,6 @@ namespace ts { | |
case SyntaxKind.JsxOpeningElement: | ||
case SyntaxKind.JsxSelfClosingElement: | ||
return getAttributesTypeFromJsxOpeningLikeElement(<JsxOpeningLikeElement>parent); | ||
case SyntaxKind.CaseClause: { | ||
if (node.kind === SyntaxKind.CaseKeyword) { // for completions after `case ` | ||
const switchStatement = (parent as CaseClause).parent.parent; | ||
return getTypeOfExpression(switchStatement.expression); | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
This file contains 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 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 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 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////enum E {} | ||
////declare const e: E; | ||
////e === /**/ | ||
////enum Enu {} | ||
////declare const e: Enu; | ||
////e === /*a*/; | ||
////e === E/*b*/ | ||
|
||
goTo.marker(); | ||
verify.completionListContains("E", "enum E", "", "enum", undefined, undefined, { isRecommended: true }); | ||
goTo.eachMarker(["a", "b"], () => { | ||
verify.completionListContains("Enu", "enum Enu", "", "enum", undefined, undefined, { isRecommended: true }); | ||
}); |
This file contains 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 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 |
---|---|---|
@@ -1,18 +1,37 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////enum E {} | ||
////class C {} | ||
////abstract class A {} | ||
////const e: E = /*e*/ | ||
////const c: C = new /*c*/ | ||
////const a: A = new /*a*/ | ||
////enum Enu {} | ||
////class Cls {} | ||
////abstract class Abs {} | ||
////const e: Enu = E/*e0*/; | ||
////const e: Enu = /*e1*/; | ||
////const c: Cls = new C/*c0*/; | ||
////const c: Cls = new /*c1*/; | ||
////const a: Abs = new A/*a0*/; | ||
////const a: Abs = new /*a1*/; | ||
|
||
goTo.marker("e"); | ||
verify.completionListContains("E", "enum E", "", "enum", undefined, undefined, { isRecommended: true }); | ||
// Also works on mutations | ||
////let enu: Enu; | ||
////enu = E/*let0*/; | ||
////enu = E/*let1*/; | ||
|
||
goTo.marker("c"); | ||
verify.completionListContains("C", "class C", "", "class", undefined, undefined, { isRecommended: true }); | ||
goTo.eachMarker(["e0"], () => {//, "e1", "let0", "let1" | ||
verify.completionListContains("Enu", "enum Enu", "", "enum", undefined, undefined, { isRecommended: true }); | ||
}); | ||
|
||
goTo.marker("a"); | ||
// Not recommended, because it's an abstract class | ||
verify.completionListContains("A", "class A", "", "class"); | ||
goTo.eachMarker(["c0", "c1"], (_, idx) => { | ||
verify.completionListContains( | ||
"Cls", | ||
idx === 0 ? "constructor Cls(): Cls" : "class Cls", | ||
"", | ||
"class", | ||
undefined, | ||
undefined, { | ||
isRecommended: true, | ||
}); | ||
}); | ||
|
||
goTo.eachMarker(["a0", "a1"], (_, idx) => { | ||
// Not recommended, because it's an abstract class | ||
verify.completionListContains("Abs", idx == 0 ? "constructor Abs(): Abs" : "class Abs", "", "class"); | ||
}); |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does
=
go away? I guess that it's because in batch compilation,=
doesn't have a type, andnode
is in fact only ever=
when called from services.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this code was added in #20020 and this PR moves the special cases to
completions.ts
.