Fix completions in return when in function with contextual 'this' (#45340)

This commit is contained in:
Ron Buckton 2021-08-05 22:40:20 -07:00 committed by GitHub
parent c0796c1dfb
commit 4fc4c18299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -1602,6 +1602,12 @@ namespace ts.Completions {
}
if (!isTypeLocation) {
// GH#39946. Pulling on the type of a node inside of a function with a contextual `this` parameter can result in a circularity
// if the `node` is part of the exprssion of a `yield` or `return`. This circularity doesn't exist at compile time because
// we will check (and cache) the type of `this` *before* checking the type of the node.
const container = getThisContainer(node, /*includeArrowFunctions*/ false);
if (!isSourceFile(container) && container.parent) typeChecker.getTypeAtLocation(container);
let type = typeChecker.getTypeAtLocation(node).getNonOptionalType();
let insertQuestionDot = false;
if (type.isNullableType()) {

View File

@ -0,0 +1,31 @@
/// <reference path="fourslash.ts" />
////interface Ctx {
//// foo(): {
//// x: number
//// };
////}
////
////declare function wrap(cb: (this: Ctx) => any): void;
////
////wrap(function () {
//// const xs = this.foo();
//// return xs./*inReturn*/
////});
////
////wrap(function () {
//// const xs = this.foo();
//// const y = xs./*involvedInReturn*/
//// return y;
////});
verify.completions(
{
marker: "inReturn",
exact: ["x"],
},
{
marker: "involvedInReturn",
exact: ["x"],
},
);