Skip to content

Commit aad609c

Browse files
Merge pull request #29298 from ajafff/this-reference-in-parameter
Allow referencing 'this' in parameters of functions in the constructor
2 parents caa8dc2 + 4cd859a commit aad609c

9 files changed

+56
-18
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17019,7 +17019,7 @@ namespace ts {
1701917019
}
1702017020

1702117021
function isInConstructorArgumentInitializer(node: Node, constructorDecl: Node): boolean {
17022-
return !!findAncestor(node, n => n === constructorDecl ? "quit" : n.kind === SyntaxKind.Parameter);
17022+
return !!findAncestor(node, n => isFunctionLikeDeclaration(n) ? "quit" : n.kind === SyntaxKind.Parameter && n.parent === constructorDecl);
1702317023
}
1702417024

1702517025
function checkSuperExpression(node: Node): Type {

tests/baselines/reference/superAccess2.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ tests/cases/compiler/superAccess2.ts(11,33): error TS1034: 'super' must be follo
77
tests/cases/compiler/superAccess2.ts(11,40): error TS2336: 'super' cannot be referenced in constructor arguments.
88
tests/cases/compiler/superAccess2.ts(11,40): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
99
tests/cases/compiler/superAccess2.ts(11,45): error TS1034: 'super' must be followed by an argument list or member access.
10-
tests/cases/compiler/superAccess2.ts(11,59): error TS2336: 'super' cannot be referenced in constructor arguments.
1110
tests/cases/compiler/superAccess2.ts(11,59): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
1211
tests/cases/compiler/superAccess2.ts(11,64): error TS1034: 'super' must be followed by an argument list or member access.
1312
tests/cases/compiler/superAccess2.ts(15,19): error TS1034: 'super' must be followed by an argument list or member access.
@@ -16,7 +15,7 @@ tests/cases/compiler/superAccess2.ts(20,26): error TS1034: 'super' must be follo
1615
tests/cases/compiler/superAccess2.ts(21,15): error TS2339: Property 'x' does not exist on type 'typeof P'.
1716

1817

19-
==== tests/cases/compiler/superAccess2.ts (16 errors) ====
18+
==== tests/cases/compiler/superAccess2.ts (15 errors) ====
2019
class P {
2120
x() { }
2221
static y() { }
@@ -47,8 +46,6 @@ tests/cases/compiler/superAccess2.ts(21,15): error TS2339: Property 'x' does not
4746
~
4847
!!! error TS1034: 'super' must be followed by an argument list or member access.
4948
~~~~~
50-
!!! error TS2336: 'super' cannot be referenced in constructor arguments.
51-
~~~~~
5249
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
5350
~
5451
!!! error TS1034: 'super' must be followed by an argument list or member access.

tests/baselines/reference/superAccess2.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Q extends P {
2525
>z : Symbol(Q.z, Decl(superAccess2.ts, 10, 16))
2626
>zz : Symbol(zz, Decl(superAccess2.ts, 10, 33))
2727
>zzz : Symbol(zzz, Decl(superAccess2.ts, 10, 45))
28+
>super : Symbol(P, Decl(superAccess2.ts, 0, 0))
2829

2930
super();
3031
>super : Symbol(P, Decl(superAccess2.ts, 0, 0))

tests/baselines/reference/superAccess2.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Q extends P {
3838
>zzz : () => any
3939
>() => super : () => any
4040
>super : any
41-
>super : any
41+
>super : P
4242
> : any
4343

4444
super();

tests/baselines/reference/thisInConstructorParameter2.errors.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ tests/cases/compiler/thisInConstructorParameter2.ts(5,39): error TS2333: 'this'
1010
~~~~
1111
!!! error TS2334: 'this' cannot be referenced in a static property initializer.
1212

13-
constructor(public z = this, zz = this) { }
13+
constructor(public z = this, zz = this, zzz = (p = this) => this) {
1414
~~~~
1515
!!! error TS2333: 'this' cannot be referenced in constructor arguments.
1616
~~~~
1717
!!! error TS2333: 'this' cannot be referenced in constructor arguments.
18+
zzz = (p = this) => this;
19+
}
1820

1921
foo(zz = this) { zz.x; }
2022
static bar(zz = this) { zz.y; }

tests/baselines/reference/thisInConstructorParameter2.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ class P {
33
x = this;
44
static y = this;
55

6-
constructor(public z = this, zz = this) { }
6+
constructor(public z = this, zz = this, zzz = (p = this) => this) {
7+
zzz = (p = this) => this;
8+
}
79

810
foo(zz = this) { zz.x; }
911
static bar(zz = this) { zz.y; }
@@ -12,11 +14,20 @@ class P {
1214
//// [thisInConstructorParameter2.js]
1315
var _this = this;
1416
var P = /** @class */ (function () {
15-
function P(z, zz) {
17+
function P(z, zz, zzz) {
1618
if (z === void 0) { z = this; }
1719
if (zz === void 0) { zz = this; }
20+
if (zzz === void 0) { zzz = function (p) {
21+
if (p === void 0) { p = _this; }
22+
return _this;
23+
}; }
24+
var _this = this;
1825
this.z = z;
1926
this.x = this;
27+
zzz = function (p) {
28+
if (p === void 0) { p = _this; }
29+
return _this;
30+
};
2031
}
2132
P.prototype.foo = function (zz) {
2233
if (zz === void 0) { zz = this; }

tests/baselines/reference/thisInConstructorParameter2.symbols

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,36 @@ class P {
1010
>y : Symbol(P.y, Decl(thisInConstructorParameter2.ts, 1, 13))
1111
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
1212

13-
constructor(public z = this, zz = this) { }
13+
constructor(public z = this, zz = this, zzz = (p = this) => this) {
1414
>z : Symbol(P.z, Decl(thisInConstructorParameter2.ts, 4, 16))
1515
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
1616
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 4, 32))
1717
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
18+
>zzz : Symbol(zzz, Decl(thisInConstructorParameter2.ts, 4, 43))
19+
>p : Symbol(p, Decl(thisInConstructorParameter2.ts, 4, 51))
20+
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
21+
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
22+
23+
zzz = (p = this) => this;
24+
>zzz : Symbol(zzz, Decl(thisInConstructorParameter2.ts, 4, 43))
25+
>p : Symbol(p, Decl(thisInConstructorParameter2.ts, 5, 15))
26+
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
27+
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
28+
}
1829

1930
foo(zz = this) { zz.x; }
20-
>foo : Symbol(P.foo, Decl(thisInConstructorParameter2.ts, 4, 47))
21-
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 6, 8))
31+
>foo : Symbol(P.foo, Decl(thisInConstructorParameter2.ts, 6, 5))
32+
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 8, 8))
2233
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
2334
>zz.x : Symbol(P.x, Decl(thisInConstructorParameter2.ts, 0, 9))
24-
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 6, 8))
35+
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 8, 8))
2536
>x : Symbol(P.x, Decl(thisInConstructorParameter2.ts, 0, 9))
2637

2738
static bar(zz = this) { zz.y; }
28-
>bar : Symbol(P.bar, Decl(thisInConstructorParameter2.ts, 6, 28))
29-
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 7, 15))
39+
>bar : Symbol(P.bar, Decl(thisInConstructorParameter2.ts, 8, 28))
40+
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 9, 15))
3041
>this : Symbol(P, Decl(thisInConstructorParameter2.ts, 0, 0))
3142
>zz.y : Symbol(P.y, Decl(thisInConstructorParameter2.ts, 1, 13))
32-
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 7, 15))
43+
>zz : Symbol(zz, Decl(thisInConstructorParameter2.ts, 9, 15))
3344
>y : Symbol(P.y, Decl(thisInConstructorParameter2.ts, 1, 13))
3445
}

tests/baselines/reference/thisInConstructorParameter2.types

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@ class P {
1010
>y : typeof P
1111
>this : typeof P
1212

13-
constructor(public z = this, zz = this) { }
13+
constructor(public z = this, zz = this, zzz = (p = this) => this) {
1414
>z : this
1515
>this : this
1616
>zz : this
1717
>this : this
18+
>zzz : (p?: this) => this
19+
>(p = this) => this : (p?: this) => this
20+
>p : this
21+
>this : this
22+
>this : this
23+
24+
zzz = (p = this) => this;
25+
>zzz = (p = this) => this : (p?: this) => this
26+
>zzz : (p?: this) => this
27+
>(p = this) => this : (p?: this) => this
28+
>p : this
29+
>this : this
30+
>this : this
31+
}
1832

1933
foo(zz = this) { zz.x; }
2034
>foo : (zz?: this) => void

tests/cases/compiler/thisInConstructorParameter2.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class P {
22
x = this;
33
static y = this;
44

5-
constructor(public z = this, zz = this) { }
5+
constructor(public z = this, zz = this, zzz = (p = this) => this) {
6+
zzz = (p = this) => this;
7+
}
68

79
foo(zz = this) { zz.x; }
810
static bar(zz = this) { zz.y; }

0 commit comments

Comments
 (0)