From 49282d9fbad7cebcaed1079b296fdc879cd0c499 Mon Sep 17 00:00:00 2001 From: Brandon Bloom Date: Wed, 29 Jan 2020 09:35:23 -0800 Subject: [PATCH] Nested this container (#36495) * Add nestedThisContainer test * Fix #36492 --- src/compiler/checker.ts | 2 +- .../reference/nestedThisContainer.js | 22 ++++++++++++ .../reference/nestedThisContainer.symbols | 26 ++++++++++++++ .../reference/nestedThisContainer.types | 35 +++++++++++++++++++ tests/cases/compiler/nestedThisContainer.ts | 13 +++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/nestedThisContainer.js create mode 100644 tests/baselines/reference/nestedThisContainer.symbols create mode 100644 tests/baselines/reference/nestedThisContainer.types create mode 100644 tests/cases/compiler/nestedThisContainer.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c350a257c0c..d9438f32a95 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 && (parent).operatorToken.kind === SyntaxKind.EqualsToken) { const target = (parent).left; if (isAccessExpression(target)) { diff --git a/tests/baselines/reference/nestedThisContainer.js b/tests/baselines/reference/nestedThisContainer.js new file mode 100644 index 00000000000..5002a6bf95a --- /dev/null +++ b/tests/baselines/reference/nestedThisContainer.js @@ -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; +}); diff --git a/tests/baselines/reference/nestedThisContainer.symbols b/tests/baselines/reference/nestedThisContainer.symbols new file mode 100644 index 00000000000..8f59da4460e --- /dev/null +++ b/tests/baselines/reference/nestedThisContainer.symbols @@ -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)) + +}); + diff --git a/tests/baselines/reference/nestedThisContainer.types b/tests/baselines/reference/nestedThisContainer.types new file mode 100644 index 00000000000..ce57fcabe12 --- /dev/null +++ b/tests/baselines/reference/nestedThisContainer.types @@ -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 + +}); + diff --git a/tests/cases/compiler/nestedThisContainer.ts b/tests/cases/compiler/nestedThisContainer.ts new file mode 100644 index 00000000000..acaacb26029 --- /dev/null +++ b/tests/cases/compiler/nestedThisContainer.ts @@ -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; +});