-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error on this.xxx access of previously declared but uninitialized pro…
…perty (#38030) * Error on this.xxx access of previously declared but uninitialized property * Add tests * Accept new baselines
- Loading branch information
1 parent
9d8a70c
commit 16d2eb7
Showing
8 changed files
with
431 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/initializerWithThisPropertyAccess.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
tests/cases/compiler/initializerWithThisPropertyAccess.ts(3,14): error TS2729: Property 'a' is used before its initialization. | ||
tests/cases/compiler/initializerWithThisPropertyAccess.ts(24,29): error TS2729: Property 'bar' is used before its initialization. | ||
|
||
|
||
==== tests/cases/compiler/initializerWithThisPropertyAccess.ts (2 errors) ==== | ||
class A { | ||
a: number; | ||
b = this.a; // Error | ||
~ | ||
!!! error TS2729: Property 'a' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/initializerWithThisPropertyAccess.ts:2:5: 'a' is declared here. | ||
c = () => this.a; | ||
d = (new A()).a; | ||
constructor() { | ||
this.a = 1; | ||
} | ||
} | ||
|
||
class B extends A { | ||
x = this.a; | ||
} | ||
|
||
class C { | ||
a!: number; | ||
b = this.a; | ||
} | ||
|
||
// Repro from #37979 | ||
|
||
class Foo { | ||
private bar: Bar; | ||
readonly barProp = this.bar.prop; | ||
~~~ | ||
!!! error TS2729: Property 'bar' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/initializerWithThisPropertyAccess.ts:23:13: 'bar' is declared here. | ||
constructor() { | ||
this.bar = new Bar(); | ||
} | ||
} | ||
|
||
class Bar { | ||
readonly prop = false; | ||
} | ||
|
114 changes: 114 additions & 0 deletions
114
tests/baselines/reference/initializerWithThisPropertyAccess.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//// [initializerWithThisPropertyAccess.ts] | ||
class A { | ||
a: number; | ||
b = this.a; // Error | ||
c = () => this.a; | ||
d = (new A()).a; | ||
constructor() { | ||
this.a = 1; | ||
} | ||
} | ||
|
||
class B extends A { | ||
x = this.a; | ||
} | ||
|
||
class C { | ||
a!: number; | ||
b = this.a; | ||
} | ||
|
||
// Repro from #37979 | ||
|
||
class Foo { | ||
private bar: Bar; | ||
readonly barProp = this.bar.prop; | ||
constructor() { | ||
this.bar = new Bar(); | ||
} | ||
} | ||
|
||
class Bar { | ||
readonly prop = false; | ||
} | ||
|
||
|
||
//// [initializerWithThisPropertyAccess.js] | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var A = /** @class */ (function () { | ||
function A() { | ||
var _this = this; | ||
this.b = this.a; // Error | ||
this.c = function () { return _this.a; }; | ||
this.d = (new A()).a; | ||
this.a = 1; | ||
} | ||
return A; | ||
}()); | ||
var B = /** @class */ (function (_super) { | ||
__extends(B, _super); | ||
function B() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.x = _this.a; | ||
return _this; | ||
} | ||
return B; | ||
}(A)); | ||
var C = /** @class */ (function () { | ||
function C() { | ||
this.b = this.a; | ||
} | ||
return C; | ||
}()); | ||
// Repro from #37979 | ||
var Foo = /** @class */ (function () { | ||
function Foo() { | ||
this.barProp = this.bar.prop; | ||
this.bar = new Bar(); | ||
} | ||
return Foo; | ||
}()); | ||
var Bar = /** @class */ (function () { | ||
function Bar() { | ||
this.prop = false; | ||
} | ||
return Bar; | ||
}()); | ||
|
||
|
||
//// [initializerWithThisPropertyAccess.d.ts] | ||
declare class A { | ||
a: number; | ||
b: number; | ||
c: () => number; | ||
d: number; | ||
constructor(); | ||
} | ||
declare class B extends A { | ||
x: number; | ||
} | ||
declare class C { | ||
a: number; | ||
b: number; | ||
} | ||
declare class Foo { | ||
private bar; | ||
readonly barProp = false; | ||
constructor(); | ||
} | ||
declare class Bar { | ||
readonly prop = false; | ||
} |
90 changes: 90 additions & 0 deletions
90
tests/baselines/reference/initializerWithThisPropertyAccess.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
=== tests/cases/compiler/initializerWithThisPropertyAccess.ts === | ||
class A { | ||
>A : Symbol(A, Decl(initializerWithThisPropertyAccess.ts, 0, 0)) | ||
|
||
a: number; | ||
>a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
|
||
b = this.a; // Error | ||
>b : Symbol(A.b, Decl(initializerWithThisPropertyAccess.ts, 1, 14)) | ||
>this.a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
>this : Symbol(A, Decl(initializerWithThisPropertyAccess.ts, 0, 0)) | ||
>a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
|
||
c = () => this.a; | ||
>c : Symbol(A.c, Decl(initializerWithThisPropertyAccess.ts, 2, 15)) | ||
>this.a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
>this : Symbol(A, Decl(initializerWithThisPropertyAccess.ts, 0, 0)) | ||
>a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
|
||
d = (new A()).a; | ||
>d : Symbol(A.d, Decl(initializerWithThisPropertyAccess.ts, 3, 21)) | ||
>(new A()).a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
>A : Symbol(A, Decl(initializerWithThisPropertyAccess.ts, 0, 0)) | ||
>a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
|
||
constructor() { | ||
this.a = 1; | ||
>this.a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
>this : Symbol(A, Decl(initializerWithThisPropertyAccess.ts, 0, 0)) | ||
>a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
} | ||
} | ||
|
||
class B extends A { | ||
>B : Symbol(B, Decl(initializerWithThisPropertyAccess.ts, 8, 1)) | ||
>A : Symbol(A, Decl(initializerWithThisPropertyAccess.ts, 0, 0)) | ||
|
||
x = this.a; | ||
>x : Symbol(B.x, Decl(initializerWithThisPropertyAccess.ts, 10, 19)) | ||
>this.a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
>this : Symbol(B, Decl(initializerWithThisPropertyAccess.ts, 8, 1)) | ||
>a : Symbol(A.a, Decl(initializerWithThisPropertyAccess.ts, 0, 9)) | ||
} | ||
|
||
class C { | ||
>C : Symbol(C, Decl(initializerWithThisPropertyAccess.ts, 12, 1)) | ||
|
||
a!: number; | ||
>a : Symbol(C.a, Decl(initializerWithThisPropertyAccess.ts, 14, 9)) | ||
|
||
b = this.a; | ||
>b : Symbol(C.b, Decl(initializerWithThisPropertyAccess.ts, 15, 15)) | ||
>this.a : Symbol(C.a, Decl(initializerWithThisPropertyAccess.ts, 14, 9)) | ||
>this : Symbol(C, Decl(initializerWithThisPropertyAccess.ts, 12, 1)) | ||
>a : Symbol(C.a, Decl(initializerWithThisPropertyAccess.ts, 14, 9)) | ||
} | ||
|
||
// Repro from #37979 | ||
|
||
class Foo { | ||
>Foo : Symbol(Foo, Decl(initializerWithThisPropertyAccess.ts, 17, 1)) | ||
|
||
private bar: Bar; | ||
>bar : Symbol(Foo.bar, Decl(initializerWithThisPropertyAccess.ts, 21, 11)) | ||
>Bar : Symbol(Bar, Decl(initializerWithThisPropertyAccess.ts, 27, 1)) | ||
|
||
readonly barProp = this.bar.prop; | ||
>barProp : Symbol(Foo.barProp, Decl(initializerWithThisPropertyAccess.ts, 22, 21)) | ||
>this.bar.prop : Symbol(Bar.prop, Decl(initializerWithThisPropertyAccess.ts, 29, 11)) | ||
>this.bar : Symbol(Foo.bar, Decl(initializerWithThisPropertyAccess.ts, 21, 11)) | ||
>this : Symbol(Foo, Decl(initializerWithThisPropertyAccess.ts, 17, 1)) | ||
>bar : Symbol(Foo.bar, Decl(initializerWithThisPropertyAccess.ts, 21, 11)) | ||
>prop : Symbol(Bar.prop, Decl(initializerWithThisPropertyAccess.ts, 29, 11)) | ||
|
||
constructor() { | ||
this.bar = new Bar(); | ||
>this.bar : Symbol(Foo.bar, Decl(initializerWithThisPropertyAccess.ts, 21, 11)) | ||
>this : Symbol(Foo, Decl(initializerWithThisPropertyAccess.ts, 17, 1)) | ||
>bar : Symbol(Foo.bar, Decl(initializerWithThisPropertyAccess.ts, 21, 11)) | ||
>Bar : Symbol(Bar, Decl(initializerWithThisPropertyAccess.ts, 27, 1)) | ||
} | ||
} | ||
|
||
class Bar { | ||
>Bar : Symbol(Bar, Decl(initializerWithThisPropertyAccess.ts, 27, 1)) | ||
|
||
readonly prop = false; | ||
>prop : Symbol(Bar.prop, Decl(initializerWithThisPropertyAccess.ts, 29, 11)) | ||
} | ||
|
97 changes: 97 additions & 0 deletions
97
tests/baselines/reference/initializerWithThisPropertyAccess.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
=== tests/cases/compiler/initializerWithThisPropertyAccess.ts === | ||
class A { | ||
>A : A | ||
|
||
a: number; | ||
>a : number | ||
|
||
b = this.a; // Error | ||
>b : number | ||
>this.a : number | ||
>this : this | ||
>a : number | ||
|
||
c = () => this.a; | ||
>c : () => number | ||
>() => this.a : () => number | ||
>this.a : number | ||
>this : this | ||
>a : number | ||
|
||
d = (new A()).a; | ||
>d : number | ||
>(new A()).a : number | ||
>(new A()) : A | ||
>new A() : A | ||
>A : typeof A | ||
>a : number | ||
|
||
constructor() { | ||
this.a = 1; | ||
>this.a = 1 : 1 | ||
>this.a : number | ||
>this : this | ||
>a : number | ||
>1 : 1 | ||
} | ||
} | ||
|
||
class B extends A { | ||
>B : B | ||
>A : A | ||
|
||
x = this.a; | ||
>x : number | ||
>this.a : number | ||
>this : this | ||
>a : number | ||
} | ||
|
||
class C { | ||
>C : C | ||
|
||
a!: number; | ||
>a : number | ||
|
||
b = this.a; | ||
>b : number | ||
>this.a : number | ||
>this : this | ||
>a : number | ||
} | ||
|
||
// Repro from #37979 | ||
|
||
class Foo { | ||
>Foo : Foo | ||
|
||
private bar: Bar; | ||
>bar : Bar | ||
|
||
readonly barProp = this.bar.prop; | ||
>barProp : false | ||
>this.bar.prop : false | ||
>this.bar : Bar | ||
>this : this | ||
>bar : Bar | ||
>prop : false | ||
|
||
constructor() { | ||
this.bar = new Bar(); | ||
>this.bar = new Bar() : Bar | ||
>this.bar : Bar | ||
>this : this | ||
>bar : Bar | ||
>new Bar() : Bar | ||
>Bar : typeof Bar | ||
} | ||
} | ||
|
||
class Bar { | ||
>Bar : Bar | ||
|
||
readonly prop = false; | ||
>prop : false | ||
>false : false | ||
} | ||
|
Oops, something went wrong.