Nested this container (#36495)

* Add nestedThisContainer test

* Fix #36492
This commit is contained in:
Brandon Bloom
2020-01-29 09:35:23 -08:00
committed by GitHub
parent 9fd0202e9f
commit 49282d9fba
5 changed files with 97 additions and 1 deletions

View File

@@ -21144,7 +21144,7 @@ namespace ts {
}
// In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
// contextual type for 'this' is 'obj'.
const parent = func.parent;
const parent = walkUpParenthesizedExpressions(func.parent);
if (parent.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>parent).operatorToken.kind === SyntaxKind.EqualsToken) {
const target = (<BinaryExpression>parent).left;
if (isAccessExpression(target)) {

View File

@@ -0,0 +1,22 @@
//// [nestedThisContainer.ts]
type Foo = any;
const foo: Foo = {};
foo.bar = function () {
const self: Foo = this;
};
foo.zab = (function () {
const self: Foo = this;
});
//// [nestedThisContainer.js]
var foo = {};
foo.bar = function () {
var self = this;
};
foo.zab = (function () {
var self = this;
});

View File

@@ -0,0 +1,26 @@
=== tests/cases/compiler/nestedThisContainer.ts ===
type Foo = any;
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
const foo: Foo = {};
>foo : Symbol(foo, Decl(nestedThisContainer.ts, 2, 5))
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
foo.bar = function () {
>foo : Symbol(foo, Decl(nestedThisContainer.ts, 2, 5))
const self: Foo = this;
>self : Symbol(self, Decl(nestedThisContainer.ts, 5, 9))
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
};
foo.zab = (function () {
>foo : Symbol(foo, Decl(nestedThisContainer.ts, 2, 5))
const self: Foo = this;
>self : Symbol(self, Decl(nestedThisContainer.ts, 9, 9))
>Foo : Symbol(Foo, Decl(nestedThisContainer.ts, 0, 0))
});

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/nestedThisContainer.ts ===
type Foo = any;
>Foo : any
const foo: Foo = {};
>foo : any
>{} : {}
foo.bar = function () {
>foo.bar = function () { const self: Foo = this;} : () => void
>foo.bar : any
>foo : any
>bar : any
>function () { const self: Foo = this;} : () => void
const self: Foo = this;
>self : any
>this : any
};
foo.zab = (function () {
>foo.zab = (function () { const self: Foo = this;}) : () => void
>foo.zab : any
>foo : any
>zab : any
>(function () { const self: Foo = this;}) : () => void
>function () { const self: Foo = this;} : () => void
const self: Foo = this;
>self : any
>this : any
});

View File

@@ -0,0 +1,13 @@
// @noImplicitThis:true
type Foo = any;
const foo: Foo = {};
foo.bar = function () {
const self: Foo = this;
};
foo.zab = (function () {
const self: Foo = this;
});