Skip to content

Commit 445c29d

Browse files
rbucktonBobobUnicorn
authored andcommitted
Fix completions in return when in function with contextual 'this' (microsoft#45340)
1 parent 9480e5c commit 445c29d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/services/completions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,12 @@ namespace ts.Completions {
16021602
}
16031603

16041604
if (!isTypeLocation) {
1605+
// GH#39946. Pulling on the type of a node inside of a function with a contextual `this` parameter can result in a circularity
1606+
// if the `node` is part of the exprssion of a `yield` or `return`. This circularity doesn't exist at compile time because
1607+
// we will check (and cache) the type of `this` *before* checking the type of the node.
1608+
const container = getThisContainer(node, /*includeArrowFunctions*/ false);
1609+
if (!isSourceFile(container) && container.parent) typeChecker.getTypeAtLocation(container);
1610+
16051611
let type = typeChecker.getTypeAtLocation(node).getNonOptionalType();
16061612
let insertQuestionDot = false;
16071613
if (type.isNullableType()) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////interface Ctx {
4+
//// foo(): {
5+
//// x: number
6+
//// };
7+
////}
8+
////
9+
////declare function wrap(cb: (this: Ctx) => any): void;
10+
////
11+
////wrap(function () {
12+
//// const xs = this.foo();
13+
//// return xs./*inReturn*/
14+
////});
15+
////
16+
////wrap(function () {
17+
//// const xs = this.foo();
18+
//// const y = xs./*involvedInReturn*/
19+
//// return y;
20+
////});
21+
22+
verify.completions(
23+
{
24+
marker: "inReturn",
25+
exact: ["x"],
26+
},
27+
{
28+
marker: "involvedInReturn",
29+
exact: ["x"],
30+
},
31+
);

0 commit comments

Comments
 (0)