mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 21:36:50 -05:00
Nested this container (#36495)
* Add nestedThisContainer test * Fix #36492
This commit is contained in:
@@ -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)) {
|
||||
|
||||
22
tests/baselines/reference/nestedThisContainer.js
Normal file
22
tests/baselines/reference/nestedThisContainer.js
Normal 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;
|
||||
});
|
||||
26
tests/baselines/reference/nestedThisContainer.symbols
Normal file
26
tests/baselines/reference/nestedThisContainer.symbols
Normal 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))
|
||||
|
||||
});
|
||||
|
||||
35
tests/baselines/reference/nestedThisContainer.types
Normal file
35
tests/baselines/reference/nestedThisContainer.types
Normal 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
|
||||
|
||||
});
|
||||
|
||||
13
tests/cases/compiler/nestedThisContainer.ts
Normal file
13
tests/cases/compiler/nestedThisContainer.ts
Normal 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;
|
||||
});
|
||||
Reference in New Issue
Block a user