Skip to content

Commit 67be949

Browse files
authored
Merge pull request #14616 from Microsoft/inferFromNestedArrowFunctionExpressionsAndPropDeclarations
Infer class property declarations from assignments in nested arrow functions
2 parents d0c77e3 + 3ac54e8 commit 67be949

File tree

5 files changed

+406
-52
lines changed

5 files changed

+406
-52
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,7 @@ namespace ts {
23322332

23332333
function bindThisPropertyAssignment(node: BinaryExpression) {
23342334
Debug.assert(isInJavaScriptFile(node));
2335+
const container = getThisContainer(node, /*includeArrowFunctions*/false);
23352336
switch (container.kind) {
23362337
case SyntaxKind.FunctionDeclaration:
23372338
case SyntaxKind.FunctionExpression:
@@ -2342,6 +2343,7 @@ namespace ts {
23422343
break;
23432344

23442345
case SyntaxKind.Constructor:
2346+
case SyntaxKind.PropertyDeclaration:
23452347
case SyntaxKind.MethodDeclaration:
23462348
case SyntaxKind.GetAccessor:
23472349
case SyntaxKind.SetAccessor:

tests/baselines/reference/inferringClassMembersFromAssignments.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ class C {
2020
this.inMethod = "string"
2121
}
2222
this.inMultiple = "string";
23+
24+
var action = () => {
25+
if (Math.random()) {
26+
this.inNestedArrowFunction = 0;
27+
}
28+
else {
29+
this.inNestedArrowFunction = "string"
30+
}
31+
};
2332
}
2433
get() {
2534
if (Math.random()) {
@@ -38,13 +47,30 @@ class C {
3847
this.inSetter = "string"
3948
}
4049
}
50+
prop = () => {
51+
if (Math.random()) {
52+
this.inPropertyDeclaration = 0;
53+
}
54+
else {
55+
this.inPropertyDeclaration = "string"
56+
}
57+
}
4158
static method() {
4259
if (Math.random()) {
4360
this.inStaticMethod = 0;
4461
}
4562
else {
4663
this.inStaticMethod = "string"
4764
}
65+
66+
var action = () => {
67+
if (Math.random()) {
68+
this.inStaticNestedArrowFunction = 0;
69+
}
70+
else {
71+
this.inStaticNestedArrowFunction = "string"
72+
}
73+
};
4874
}
4975
static get() {
5076
if (Math.random()) {
@@ -62,6 +88,14 @@ class C {
6288
this.inStaticSetter = "string"
6389
}
6490
}
91+
static prop = () => {
92+
if (Math.random()) {
93+
this.inStaticPropertyDeclaration = 0;
94+
}
95+
else {
96+
this.inStaticPropertyDeclaration = "string"
97+
}
98+
}
6599
}
66100

67101
//// [b.ts]
@@ -75,6 +109,8 @@ var stringOrNumberOrUndefined: string | number | undefined;
75109
var stringOrNumberOrUndefined = c.inMethod;
76110
var stringOrNumberOrUndefined = c.inGetter;
77111
var stringOrNumberOrUndefined = c.inSetter;
112+
var stringOrNumberOrUndefined = c.inPropertyDeclaration;
113+
var stringOrNumberOrUndefined = c.inNestedArrowFunction
78114

79115
var stringOrNumberOrBoolean: string | number | boolean;
80116

@@ -84,11 +120,23 @@ var stringOrNumberOrBoolean = c.inMultiple;
84120
var stringOrNumberOrUndefined = C.inStaticMethod;
85121
var stringOrNumberOrUndefined = C.inStaticGetter;
86122
var stringOrNumberOrUndefined = C.inStaticSetter;
123+
var stringOrNumberOrUndefined = C.inStaticPropertyDeclaration;
124+
var stringOrNumberOrUndefined = C.inStaticNestedArrowFunction;
87125

88126

89127
//// [output.js]
128+
var _this = this;
90129
var C = (function () {
91130
function C() {
131+
var _this = this;
132+
this.prop = function () {
133+
if (Math.random()) {
134+
_this.inPropertyDeclaration = 0;
135+
}
136+
else {
137+
_this.inPropertyDeclaration = "string";
138+
}
139+
};
92140
if (Math.random()) {
93141
this.inConstructor = 0;
94142
}
@@ -98,13 +146,22 @@ var C = (function () {
98146
this.inMultiple = 0;
99147
}
100148
C.prototype.method = function () {
149+
var _this = this;
101150
if (Math.random()) {
102151
this.inMethod = 0;
103152
}
104153
else {
105154
this.inMethod = "string";
106155
}
107156
this.inMultiple = "string";
157+
var action = function () {
158+
if (Math.random()) {
159+
_this.inNestedArrowFunction = 0;
160+
}
161+
else {
162+
_this.inNestedArrowFunction = "string";
163+
}
164+
};
108165
};
109166
C.prototype.get = function () {
110167
if (Math.random()) {
@@ -124,12 +181,21 @@ var C = (function () {
124181
}
125182
};
126183
C.method = function () {
184+
var _this = this;
127185
if (Math.random()) {
128186
this.inStaticMethod = 0;
129187
}
130188
else {
131189
this.inStaticMethod = "string";
132190
}
191+
var action = function () {
192+
if (Math.random()) {
193+
_this.inStaticNestedArrowFunction = 0;
194+
}
195+
else {
196+
_this.inStaticNestedArrowFunction = "string";
197+
}
198+
};
133199
};
134200
C.get = function () {
135201
if (Math.random()) {
@@ -149,15 +215,27 @@ var C = (function () {
149215
};
150216
return C;
151217
}());
218+
C.prop = function () {
219+
if (Math.random()) {
220+
_this.inStaticPropertyDeclaration = 0;
221+
}
222+
else {
223+
_this.inStaticPropertyDeclaration = "string";
224+
}
225+
};
152226
var c = new C();
153227
var stringOrNumber;
154228
var stringOrNumber = c.inConstructor;
155229
var stringOrNumberOrUndefined;
156230
var stringOrNumberOrUndefined = c.inMethod;
157231
var stringOrNumberOrUndefined = c.inGetter;
158232
var stringOrNumberOrUndefined = c.inSetter;
233+
var stringOrNumberOrUndefined = c.inPropertyDeclaration;
234+
var stringOrNumberOrUndefined = c.inNestedArrowFunction;
159235
var stringOrNumberOrBoolean;
160236
var stringOrNumberOrBoolean = c.inMultiple;
161237
var stringOrNumberOrUndefined = C.inStaticMethod;
162238
var stringOrNumberOrUndefined = C.inStaticGetter;
163239
var stringOrNumberOrUndefined = C.inStaticSetter;
240+
var stringOrNumberOrUndefined = C.inStaticPropertyDeclaration;
241+
var stringOrNumberOrUndefined = C.inStaticNestedArrowFunction;

0 commit comments

Comments
 (0)