From 93577390b1614af14c3b705b33aa37ad1302149f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 29 Jun 2022 09:54:03 -0700 Subject: [PATCH 1/5] Add test case --- tests/baselines/reference/thisInObjectJs.js | 31 +++++++++++++ .../reference/thisInObjectJs.symbols | 36 +++++++++++++++ .../baselines/reference/thisInObjectJs.types | 44 +++++++++++++++++++ tests/cases/compiler/thisInObjectJs.ts | 17 +++++++ 4 files changed, 128 insertions(+) create mode 100644 tests/baselines/reference/thisInObjectJs.js create mode 100644 tests/baselines/reference/thisInObjectJs.symbols create mode 100644 tests/baselines/reference/thisInObjectJs.types create mode 100644 tests/cases/compiler/thisInObjectJs.ts diff --git a/tests/baselines/reference/thisInObjectJs.js b/tests/baselines/reference/thisInObjectJs.js new file mode 100644 index 0000000000000..d761e2db8aeca --- /dev/null +++ b/tests/baselines/reference/thisInObjectJs.js @@ -0,0 +1,31 @@ +//// [index.js] +export { } +let obj = { + x: 10, + y: [1], + fun: function() { + this.x = 1 + this/*1*/ + }, + f2: function() { + this.x + this/*2*/ + } +} + + +//// [index.js] +"use strict"; +exports.__esModule = true; +var obj = { + x: 10, + y: [1], + fun: function () { + this.x = 1; + this; /*1*/ + }, + f2: function () { + this.x; + this; /*2*/ + } +}; diff --git a/tests/baselines/reference/thisInObjectJs.symbols b/tests/baselines/reference/thisInObjectJs.symbols new file mode 100644 index 0000000000000..1386b85263917 --- /dev/null +++ b/tests/baselines/reference/thisInObjectJs.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/index.js === +export { } +let obj = { +>obj : Symbol(obj, Decl(index.js, 1, 3)) + + x: 10, +>x : Symbol(x, Decl(index.js, 1, 11)) + + y: [1], +>y : Symbol(y, Decl(index.js, 2, 8)) + + fun: function() { +>fun : Symbol(fun, Decl(index.js, 3, 9)) + + this.x = 1 +>this.x : Symbol(fun.x, Decl(index.js, 4, 19)) +>this : Symbol(fun, Decl(index.js, 4, 6)) +>x : Symbol(fun.x, Decl(index.js, 4, 19)) + + this/*1*/ +>this : Symbol(fun, Decl(index.js, 4, 6)) + + }, + f2: function() { +>f2 : Symbol(f2, Decl(index.js, 7, 4)) + + this.x +>this.x : Symbol(x, Decl(index.js, 1, 11)) +>this : Symbol(obj, Decl(index.js, 1, 9)) +>x : Symbol(x, Decl(index.js, 1, 11)) + + this/*2*/ +>this : Symbol(obj, Decl(index.js, 1, 9)) + } +} + diff --git a/tests/baselines/reference/thisInObjectJs.types b/tests/baselines/reference/thisInObjectJs.types new file mode 100644 index 0000000000000..96952321ed88c --- /dev/null +++ b/tests/baselines/reference/thisInObjectJs.types @@ -0,0 +1,44 @@ +=== tests/cases/compiler/index.js === +export { } +let obj = { +>obj : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>{ x: 10, y: [1], fun: function() { this.x = 1 this/*1*/ }, f2: function() { this.x this/*2*/ }} : { x: number; y: number[]; fun: typeof fun; f2: () => void; } + + x: 10, +>x : number +>10 : 10 + + y: [1], +>y : number[] +>[1] : number[] +>1 : 1 + + fun: function() { +>fun : typeof fun +>function() { this.x = 1 this/*1*/ } : typeof fun + + this.x = 1 +>this.x = 1 : 1 +>this.x : any +>this : this +>x : any +>1 : 1 + + this/*1*/ +>this : this + + }, + f2: function() { +>f2 : () => void +>function() { this.x this/*2*/ } : () => void + + this.x +>this.x : number +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>x : number + + this/*2*/ +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } + } +} + diff --git a/tests/cases/compiler/thisInObjectJs.ts b/tests/cases/compiler/thisInObjectJs.ts new file mode 100644 index 0000000000000..575932655b7ab --- /dev/null +++ b/tests/cases/compiler/thisInObjectJs.ts @@ -0,0 +1,17 @@ +// @allowJs: true +// @outDir: out + +// @filename: index.js +export { } +let obj = { + x: 10, + y: [1], + fun: function() { + this.x = 1 + this/*1*/ + }, + f2: function() { + this.x + this/*2*/ + } +} From 88b88aac16cf41a6eaf6a603328492e3e86610af Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 29 Jun 2022 12:31:30 -0700 Subject: [PATCH 2/5] Don't treat object properties as potential JS contructors without JSDoc class tag --- src/compiler/checker.ts | 3 +++ tests/baselines/reference/functionExpressionNames.symbols | 3 +-- tests/baselines/reference/functionExpressionNames.types | 2 +- tests/baselines/reference/thisInObjectJs.symbols | 6 +++--- tests/baselines/reference/thisInObjectJs.types | 8 ++++---- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8e24fee20606d..cb44d12e02363 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31695,6 +31695,9 @@ namespace ts { // If the node has a @class tag, treat it like a constructor. if (getJSDocClassTag(node)) return true; + // If the node is a property of an object literal. + if (isPropertyAssignment(node.parent)) return false; + // If the symbol of the node has members, treat it like a constructor. const symbol = getSymbolOfNode(func); return !!symbol?.members?.size; diff --git a/tests/baselines/reference/functionExpressionNames.symbols b/tests/baselines/reference/functionExpressionNames.symbols index 1675f159134b3..ca1d8479ae57e 100644 --- a/tests/baselines/reference/functionExpressionNames.symbols +++ b/tests/baselines/reference/functionExpressionNames.symbols @@ -22,8 +22,7 @@ var o = { >C : Symbol(C, Decl(b.js, 5, 9)) this.c = 'nested object' ->this.c : Symbol(C.c, Decl(b.js, 6, 20)) ->this : Symbol(C, Decl(b.js, 6, 6)) +>this : Symbol(o, Decl(b.js, 5, 7)) >c : Symbol(C.c, Decl(b.js, 6, 20)) } } diff --git a/tests/baselines/reference/functionExpressionNames.types b/tests/baselines/reference/functionExpressionNames.types index 50c45ed46d30d..d803666bbb943 100644 --- a/tests/baselines/reference/functionExpressionNames.types +++ b/tests/baselines/reference/functionExpressionNames.types @@ -31,7 +31,7 @@ var o = { this.c = 'nested object' >this.c = 'nested object' : "nested object" >this.c : any ->this : this +>this : { C: typeof C; } >c : any >'nested object' : "nested object" } diff --git a/tests/baselines/reference/thisInObjectJs.symbols b/tests/baselines/reference/thisInObjectJs.symbols index 1386b85263917..9d53324d86fd2 100644 --- a/tests/baselines/reference/thisInObjectJs.symbols +++ b/tests/baselines/reference/thisInObjectJs.symbols @@ -13,12 +13,12 @@ let obj = { >fun : Symbol(fun, Decl(index.js, 3, 9)) this.x = 1 ->this.x : Symbol(fun.x, Decl(index.js, 4, 19)) ->this : Symbol(fun, Decl(index.js, 4, 6)) +>this.x : Symbol(x, Decl(index.js, 1, 11)) +>this : Symbol(obj, Decl(index.js, 1, 9)) >x : Symbol(fun.x, Decl(index.js, 4, 19)) this/*1*/ ->this : Symbol(fun, Decl(index.js, 4, 6)) +>this : Symbol(obj, Decl(index.js, 1, 9)) }, f2: function() { diff --git a/tests/baselines/reference/thisInObjectJs.types b/tests/baselines/reference/thisInObjectJs.types index 96952321ed88c..1d9ef3749937e 100644 --- a/tests/baselines/reference/thisInObjectJs.types +++ b/tests/baselines/reference/thisInObjectJs.types @@ -19,13 +19,13 @@ let obj = { this.x = 1 >this.x = 1 : 1 ->this.x : any ->this : this ->x : any +>this.x : number +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>x : number >1 : 1 this/*1*/ ->this : this +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } }, f2: function() { From 4f58a1bbffcb09e55b9fdb555fb109e6a2838827 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 29 Jun 2022 21:27:28 -0700 Subject: [PATCH 3/5] Handle object literal methods --- src/compiler/checker.ts | 12 ++-- ...tructorTagOnObjectLiteralMethod.errors.txt | 6 +- .../constructorTagOnObjectLiteralMethod.types | 10 +-- .../reference/objectPropertyAsClass.symbols | 52 ++++++++++++++ .../reference/objectPropertyAsClass.types | 69 +++++++++++++++++++ tests/cases/compiler/objectPropertyAsClass.ts | 30 ++++++++ 6 files changed, 165 insertions(+), 14 deletions(-) create mode 100644 tests/baselines/reference/objectPropertyAsClass.symbols create mode 100644 tests/baselines/reference/objectPropertyAsClass.types create mode 100644 tests/cases/compiler/objectPropertyAsClass.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cb44d12e02363..b697e06743638 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31684,19 +31684,19 @@ namespace ts { * Indicates whether a declaration can be treated as a constructor in a JavaScript * file. */ - function isJSConstructor(node: Node | undefined): node is FunctionDeclaration | FunctionExpression { + function isJSConstructor(node: Node | undefined): node is FunctionDeclaration | FunctionExpression | MethodDeclaration { if (!node || !isInJSFile(node)) { return false; } - const func = isFunctionDeclaration(node) || isFunctionExpression(node) ? node : - isVariableDeclaration(node) && node.initializer && isFunctionExpression(node.initializer) ? node.initializer : + const func = isFunctionDeclaration(node) || isFunctionExpression(node) || isObjectLiteralMethod(node) ? node : + (isVariableDeclaration(node) || isPropertyAssignment(node)) && node.initializer && isFunctionExpression(node.initializer) ? node.initializer : undefined; if (func) { - // If the node has a @class tag, treat it like a constructor. + // If the node has a @class or @constructor tag, treat it like a constructor. if (getJSDocClassTag(node)) return true; - // If the node is a property of an object literal. - if (isPropertyAssignment(node.parent)) return false; + // If the node is a property/method of an object literal. + if (isPropertyAssignment(node.parent) || isObjectLiteralMethod(node)) return false; // If the symbol of the node has members, treat it like a constructor. const symbol = getSymbolOfNode(func); diff --git a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt index 2ebd6b532605d..7b2eb5c75066e 100644 --- a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt +++ b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/jsdoc/example.js(3,16): error TS2339: Property 'bar' does not exist on type '{ Foo(): void; }'. -tests/cases/conformance/jsdoc/example.js(5,2): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. +tests/cases/conformance/jsdoc/example.js(5,17): error TS2339: Property 'bar' does not exist on type 'void'. ==== tests/cases/conformance/jsdoc/example.js (2 errors) ==== @@ -10,6 +10,6 @@ tests/cases/conformance/jsdoc/example.js(5,2): error TS7009: 'new' expression, w !!! error TS2339: Property 'bar' does not exist on type '{ Foo(): void; }'. }; (new obj.Foo()).bar - ~~~~~~~~~~~~~ -!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. + ~~~ +!!! error TS2339: Property 'bar' does not exist on type 'void'. \ No newline at end of file diff --git a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types index c731f3bc632bd..0463429632039 100644 --- a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types +++ b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types @@ -5,7 +5,7 @@ const obj = { /** @constructor */ Foo() { this.bar = "bar" } ->Foo : () => void +>Foo : typeof obj.Foo >this.bar = "bar" : "bar" >this.bar : any >this : { Foo(): void; } @@ -15,10 +15,10 @@ const obj = { }; (new obj.Foo()).bar >(new obj.Foo()).bar : any ->(new obj.Foo()) : any ->new obj.Foo() : any ->obj.Foo : () => void +>(new obj.Foo()) : void +>new obj.Foo() : void +>obj.Foo : typeof obj.Foo >obj : { Foo(): void; } ->Foo : () => void +>Foo : typeof obj.Foo >bar : any diff --git a/tests/baselines/reference/objectPropertyAsClass.symbols b/tests/baselines/reference/objectPropertyAsClass.symbols new file mode 100644 index 0000000000000..ad9712fa52395 --- /dev/null +++ b/tests/baselines/reference/objectPropertyAsClass.symbols @@ -0,0 +1,52 @@ +=== tests/cases/compiler/index.js === +const a1 = { +>a1 : Symbol(a1, Decl(index.js, 0, 5)) + + foo() { +>foo : Symbol(foo, Decl(index.js, 0, 12)) + + this.x = 0; +>this : Symbol(a1, Decl(index.js, 0, 10)) +>x : Symbol(x, Decl(index.js, 1, 11)) + } +} + +const a2 = { +>a2 : Symbol(a2, Decl(index.js, 6, 5)) + + foo: function() { +>foo : Symbol(foo, Decl(index.js, 6, 12)) + + this.x = 0; +>this : Symbol(a2, Decl(index.js, 6, 10)) +>x : Symbol(foo.x, Decl(index.js, 7, 21)) + } +} + +const b1 = { +>b1 : Symbol(b1, Decl(index.js, 12, 5)) + + /** @class */ + foo() { +>foo : Symbol(foo, Decl(index.js, 12, 12)) + + this.x = 0; +>this : Symbol(b1, Decl(index.js, 12, 10)) +>x : Symbol(x, Decl(index.js, 14, 11)) + } +} + +const b2 = { +>b2 : Symbol(b2, Decl(index.js, 19, 5)) + + /** @class */ + foo: function() { +>foo : Symbol(foo, Decl(index.js, 19, 12)) + + this.x = 0; +>this.x : Symbol(foo.x, Decl(index.js, 21, 21)) +>this : Symbol(foo, Decl(index.js, 21, 8)) +>x : Symbol(foo.x, Decl(index.js, 21, 21)) + } +} + diff --git a/tests/baselines/reference/objectPropertyAsClass.types b/tests/baselines/reference/objectPropertyAsClass.types new file mode 100644 index 0000000000000..f336e14baf4f4 --- /dev/null +++ b/tests/baselines/reference/objectPropertyAsClass.types @@ -0,0 +1,69 @@ +=== tests/cases/compiler/index.js === +const a1 = { +>a1 : { foo(): void; } +>{ foo() { this.x = 0; }} : { foo(): void; } + + foo() { +>foo : () => void + + this.x = 0; +>this.x = 0 : 0 +>this.x : any +>this : { foo(): void; } +>x : any +>0 : 0 + } +} + +const a2 = { +>a2 : { foo: typeof foo; } +>{ foo: function() { this.x = 0; }} : { foo: typeof foo; } + + foo: function() { +>foo : typeof foo +>function() { this.x = 0; } : typeof foo + + this.x = 0; +>this.x = 0 : 0 +>this.x : any +>this : { foo: typeof foo; } +>x : any +>0 : 0 + } +} + +const b1 = { +>b1 : { foo(): void; } +>{ /** @class */ foo() { this.x = 0; }} : { foo(): void; } + + /** @class */ + foo() { +>foo : typeof b1.foo + + this.x = 0; +>this.x = 0 : 0 +>this.x : any +>this : { foo(): void; } +>x : any +>0 : 0 + } +} + +const b2 = { +>b2 : { foo: typeof foo; } +>{ /** @class */ foo: function() { this.x = 0; }} : { foo: typeof foo; } + + /** @class */ + foo: function() { +>foo : typeof foo +>function() { this.x = 0; } : typeof foo + + this.x = 0; +>this.x = 0 : 0 +>this.x : any +>this : this +>x : any +>0 : 0 + } +} + diff --git a/tests/cases/compiler/objectPropertyAsClass.ts b/tests/cases/compiler/objectPropertyAsClass.ts new file mode 100644 index 0000000000000..a4b32acfaae44 --- /dev/null +++ b/tests/cases/compiler/objectPropertyAsClass.ts @@ -0,0 +1,30 @@ +// @allowJs: true +// @noEmit: true +// @checkJs: true + +// @filename: index.js +const a1 = { + foo() { + this.x = 0; + } +} + +const a2 = { + foo: function() { + this.x = 0; + } +} + +const b1 = { + /** @class */ + foo() { + this.x = 0; + } +} + +const b2 = { + /** @class */ + foo: function() { + this.x = 0; + } +} From 086c8c9a074d85494e0f9402de22e683be89f32e Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 6 Jul 2022 08:40:18 -0700 Subject: [PATCH 4/5] Don't handle methods --- src/compiler/checker.ts | 8 ++++---- .../constructorTagOnObjectLiteralMethod.errors.txt | 6 +++--- .../constructorTagOnObjectLiteralMethod.types | 10 +++++----- tests/baselines/reference/objectPropertyAsClass.types | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b697e06743638..cbb069f724d58 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31684,19 +31684,19 @@ namespace ts { * Indicates whether a declaration can be treated as a constructor in a JavaScript * file. */ - function isJSConstructor(node: Node | undefined): node is FunctionDeclaration | FunctionExpression | MethodDeclaration { + function isJSConstructor(node: Node | undefined): node is FunctionDeclaration | FunctionExpression { if (!node || !isInJSFile(node)) { return false; } - const func = isFunctionDeclaration(node) || isFunctionExpression(node) || isObjectLiteralMethod(node) ? node : + const func = isFunctionDeclaration(node) || isFunctionExpression(node) ? node : (isVariableDeclaration(node) || isPropertyAssignment(node)) && node.initializer && isFunctionExpression(node.initializer) ? node.initializer : undefined; if (func) { // If the node has a @class or @constructor tag, treat it like a constructor. if (getJSDocClassTag(node)) return true; - // If the node is a property/method of an object literal. - if (isPropertyAssignment(node.parent) || isObjectLiteralMethod(node)) return false; + // If the node is a property of an object literal. + if (isPropertyAssignment(node.parent)) return false; // If the symbol of the node has members, treat it like a constructor. const symbol = getSymbolOfNode(func); diff --git a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt index 7b2eb5c75066e..2ebd6b532605d 100644 --- a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt +++ b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/jsdoc/example.js(3,16): error TS2339: Property 'bar' does not exist on type '{ Foo(): void; }'. -tests/cases/conformance/jsdoc/example.js(5,17): error TS2339: Property 'bar' does not exist on type 'void'. +tests/cases/conformance/jsdoc/example.js(5,2): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. ==== tests/cases/conformance/jsdoc/example.js (2 errors) ==== @@ -10,6 +10,6 @@ tests/cases/conformance/jsdoc/example.js(5,17): error TS2339: Property 'bar' doe !!! error TS2339: Property 'bar' does not exist on type '{ Foo(): void; }'. }; (new obj.Foo()).bar - ~~~ -!!! error TS2339: Property 'bar' does not exist on type 'void'. + ~~~~~~~~~~~~~ +!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. \ No newline at end of file diff --git a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types index 0463429632039..c731f3bc632bd 100644 --- a/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types +++ b/tests/baselines/reference/constructorTagOnObjectLiteralMethod.types @@ -5,7 +5,7 @@ const obj = { /** @constructor */ Foo() { this.bar = "bar" } ->Foo : typeof obj.Foo +>Foo : () => void >this.bar = "bar" : "bar" >this.bar : any >this : { Foo(): void; } @@ -15,10 +15,10 @@ const obj = { }; (new obj.Foo()).bar >(new obj.Foo()).bar : any ->(new obj.Foo()) : void ->new obj.Foo() : void ->obj.Foo : typeof obj.Foo +>(new obj.Foo()) : any +>new obj.Foo() : any +>obj.Foo : () => void >obj : { Foo(): void; } ->Foo : typeof obj.Foo +>Foo : () => void >bar : any diff --git a/tests/baselines/reference/objectPropertyAsClass.types b/tests/baselines/reference/objectPropertyAsClass.types index f336e14baf4f4..7d27103e0118a 100644 --- a/tests/baselines/reference/objectPropertyAsClass.types +++ b/tests/baselines/reference/objectPropertyAsClass.types @@ -38,7 +38,7 @@ const b1 = { /** @class */ foo() { ->foo : typeof b1.foo +>foo : () => void this.x = 0; >this.x = 0 : 0 From 532c2bb20bf7aa24e089c139341f8d15f8ed9242 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 6 Jul 2022 09:05:52 -0700 Subject: [PATCH 5/5] Use correct node for property assignment check --- src/compiler/checker.ts | 2 +- tests/baselines/reference/thisInObjectJs.js | 12 +++++-- .../reference/thisInObjectJs.symbols | 11 ++++++- .../baselines/reference/thisInObjectJs.types | 31 ++++++++++++++----- tests/cases/compiler/thisInObjectJs.ts | 6 +++- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cbb069f724d58..d87fcefe79faf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31696,7 +31696,7 @@ namespace ts { if (getJSDocClassTag(node)) return true; // If the node is a property of an object literal. - if (isPropertyAssignment(node.parent)) return false; + if (isPropertyAssignment(walkUpParenthesizedExpressions(func.parent))) return false; // If the symbol of the node has members, treat it like a constructor. const symbol = getSymbolOfNode(func); diff --git a/tests/baselines/reference/thisInObjectJs.js b/tests/baselines/reference/thisInObjectJs.js index d761e2db8aeca..e70e3c8eb6c46 100644 --- a/tests/baselines/reference/thisInObjectJs.js +++ b/tests/baselines/reference/thisInObjectJs.js @@ -10,7 +10,11 @@ let obj = { f2: function() { this.x this/*2*/ - } + }, + f3: (function() { + this.x = 1 + this/*3*/ + }), } @@ -27,5 +31,9 @@ var obj = { f2: function () { this.x; this; /*2*/ - } + }, + f3: (function () { + this.x = 1; + this; /*3*/ + }) }; diff --git a/tests/baselines/reference/thisInObjectJs.symbols b/tests/baselines/reference/thisInObjectJs.symbols index 9d53324d86fd2..f0ed418f6a691 100644 --- a/tests/baselines/reference/thisInObjectJs.symbols +++ b/tests/baselines/reference/thisInObjectJs.symbols @@ -31,6 +31,15 @@ let obj = { this/*2*/ >this : Symbol(obj, Decl(index.js, 1, 9)) - } + + }, + f3: (function() { +>f3 : Symbol(f3, Decl(index.js, 11, 4)) + + this.x = 1 +>x : Symbol((Anonymous function).x, Decl(index.js, 12, 19)) + + this/*3*/ + }), } diff --git a/tests/baselines/reference/thisInObjectJs.types b/tests/baselines/reference/thisInObjectJs.types index 1d9ef3749937e..3752241f53715 100644 --- a/tests/baselines/reference/thisInObjectJs.types +++ b/tests/baselines/reference/thisInObjectJs.types @@ -1,8 +1,8 @@ === tests/cases/compiler/index.js === export { } let obj = { ->obj : { x: number; y: number[]; fun: typeof fun; f2: () => void; } ->{ x: 10, y: [1], fun: function() { this.x = 1 this/*1*/ }, f2: function() { this.x this/*2*/ }} : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>obj : { x: number; y: number[]; fun: typeof fun; f2: () => void; f3: typeof (Anonymous function); } +>{ x: 10, y: [1], fun: function() { this.x = 1 this/*1*/ }, f2: function() { this.x this/*2*/ }, f3: (function() { this.x = 1 this/*3*/ }),} : { x: number; y: number[]; fun: typeof fun; f2: () => void; f3: typeof (Anonymous function); } x: 10, >x : number @@ -20,12 +20,12 @@ let obj = { this.x = 1 >this.x = 1 : 1 >this.x : number ->this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; f3: typeof (Anonymous function); } >x : number >1 : 1 this/*1*/ ->this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; f3: typeof (Anonymous function); } }, f2: function() { @@ -34,11 +34,28 @@ let obj = { this.x >this.x : number ->this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; f3: typeof (Anonymous function); } >x : number this/*2*/ ->this : { x: number; y: number[]; fun: typeof fun; f2: () => void; } - } +>this : { x: number; y: number[]; fun: typeof fun; f2: () => void; f3: typeof (Anonymous function); } + + }, + f3: (function() { +>f3 : typeof (Anonymous function) +>(function() { this.x = 1 this/*3*/ }) : typeof (Anonymous function) +>function() { this.x = 1 this/*3*/ } : typeof (Anonymous function) + + this.x = 1 +>this.x = 1 : 1 +>this.x : any +>this : any +>x : any +>1 : 1 + + this/*3*/ +>this : any + + }), } diff --git a/tests/cases/compiler/thisInObjectJs.ts b/tests/cases/compiler/thisInObjectJs.ts index 575932655b7ab..631b1b631d1b4 100644 --- a/tests/cases/compiler/thisInObjectJs.ts +++ b/tests/cases/compiler/thisInObjectJs.ts @@ -13,5 +13,9 @@ let obj = { f2: function() { this.x this/*2*/ - } + }, + f3: (function() { + this.x = 1 + this/*3*/ + }), }