Skip to content

Commit 49282d9

Browse files
authored
Nested this container (#36495)
* Add nestedThisContainer test * Fix #36492
1 parent 9fd0202 commit 49282d9

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21144,7 +21144,7 @@ namespace ts {
2114421144
}
2114521145
// In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
2114621146
// contextual type for 'this' is 'obj'.
21147-
const parent = func.parent;
21147+
const parent = walkUpParenthesizedExpressions(func.parent);
2114821148
if (parent.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>parent).operatorToken.kind === SyntaxKind.EqualsToken) {
2114921149
const target = (<BinaryExpression>parent).left;
2115021150
if (isAccessExpression(target)) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [nestedThisContainer.ts]
2+
type Foo = any;
3+
4+
const foo: Foo = {};
5+
6+
foo.bar = function () {
7+
const self: Foo = this;
8+
};
9+
10+
foo.zab = (function () {
11+
const self: Foo = this;
12+
});
13+
14+
15+
//// [nestedThisContainer.js]
16+
var foo = {};
17+
foo.bar = function () {
18+
var self = this;
19+
};
20+
foo.zab = (function () {
21+
var self = this;
22+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/nestedThisContainer.ts ===
2+
type Foo = any;
3+
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
4+
5+
const foo: Foo = {};
6+
>foo : Symbol(foo, Decl(nestedThisContainer.ts, 2, 5))
7+
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
8+
9+
foo.bar = function () {
10+
>foo : Symbol(foo, Decl(nestedThisContainer.ts, 2, 5))
11+
12+
const self: Foo = this;
13+
>self : Symbol(self, Decl(nestedThisContainer.ts, 5, 9))
14+
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
15+
16+
};
17+
18+
foo.zab = (function () {
19+
>foo : Symbol(foo, Decl(nestedThisContainer.ts, 2, 5))
20+
21+
const self: Foo = this;
22+
>self : Symbol(self, Decl(nestedThisContainer.ts, 9, 9))
23+
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
24+
25+
});
26+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/nestedThisContainer.ts ===
2+
type Foo = any;
3+
>Foo : any
4+
5+
const foo: Foo = {};
6+
>foo : any
7+
>{} : {}
8+
9+
foo.bar = function () {
10+
>foo.bar = function () { const self: Foo = this;} : () => void
11+
>foo.bar : any
12+
>foo : any
13+
>bar : any
14+
>function () { const self: Foo = this;} : () => void
15+
16+
const self: Foo = this;
17+
>self : any
18+
>this : any
19+
20+
};
21+
22+
foo.zab = (function () {
23+
>foo.zab = (function () { const self: Foo = this;}) : () => void
24+
>foo.zab : any
25+
>foo : any
26+
>zab : any
27+
>(function () { const self: Foo = this;}) : () => void
28+
>function () { const self: Foo = this;} : () => void
29+
30+
const self: Foo = this;
31+
>self : any
32+
>this : any
33+
34+
});
35+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @noImplicitThis:true
2+
3+
type Foo = any;
4+
5+
const foo: Foo = {};
6+
7+
foo.bar = function () {
8+
const self: Foo = this;
9+
};
10+
11+
foo.zab = (function () {
12+
const self: Foo = this;
13+
});

0 commit comments

Comments
 (0)