Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,10 @@ namespace ts {
return !findAncestor(usage, n => isComputedPropertyName(n) && n.parent.parent === declaration);
}
else if (isPropertyDeclaration(declaration)) {
// still might be illegal if the property has no initializer.
// still might be illegal if a self-referencing property initializer (eg private x = this.x)
return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ false);
return !isUninitializedPropertyReferencedWithinDeclaration(declaration, usage)
&& !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ false);
}
else if (isParameterPropertyDeclaration(declaration, declaration.parent)) {
// foo = this.bar is illegal in esnext+useDefineForClassFields when bar is a parameter property
Expand Down Expand Up @@ -1486,6 +1488,36 @@ namespace ts {
});
}

function isUninitializedPropertyReferencedWithinDeclaration(declaration: PropertyDeclaration, usage: Node) {
if (!strictNullChecks || !strictPropertyInitialization || declaration.flags & NodeFlags.Ambient) {
return;
}
const ancestor = findAncestor(usage, (node: Node) => {
switch (node.kind) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.ArrowFunction:
case SyntaxKind.Block:
return "quit";
case SyntaxKind.PropertyDeclaration:
return true;
default:
return isStatement(node) ? "quit" : false;
}
});

if (!ancestor || ancestor.parent !== declaration.parent) {
return false;
}

if (!isInstancePropertyWithoutInitializer(declaration)) {
return false;
}

const type = getTypeOfSymbol(getSymbolOfNode(declaration));
return !(type.flags & TypeFlags.AnyOrUnknown || getFalsyFlags(type) & TypeFlags.Undefined);
}

/** stopAtAnyPropertyDeclaration is used for detecting ES-standard class field use-before-def errors */
function isPropertyImmediatelyReferencedWithinDeclaration(declaration: PropertyDeclaration | ParameterPropertyDeclaration, usage: Node, stopAtAnyPropertyDeclaration: boolean) {
// always legal if usage is after declaration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
tests/cases/compiler/initializerWithThisPropertyAccess.ts(3,14): error TS2729: Property 'a' is used before its initialization.
tests/cases/compiler/initializerWithThisPropertyAccess.ts(5,19): 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) ====
==== tests/cases/compiler/initializerWithThisPropertyAccess.ts (3 errors) ====
class A {
a: number;
b = this.a; // Error
Expand All @@ -11,6 +12,9 @@ tests/cases/compiler/initializerWithThisPropertyAccess.ts(24,29): error TS2729:
!!! related TS2728 tests/cases/compiler/initializerWithThisPropertyAccess.ts:2:5: 'a' is declared here.
c = () => this.a;
d = (new A()).a;
~
!!! error TS2729: Property 'a' is used before its initialization.
!!! related TS2728 tests/cases/compiler/initializerWithThisPropertyAccess.ts:2:5: 'a' is declared here.
constructor() {
this.a = 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/compiler/useBeforeDeclaration_propertyDeclaration.ts(7,21): error TS2729: Property 'num' is used before its initialization.


==== tests/cases/compiler/useBeforeDeclaration_propertyDeclaration.ts (1 errors) ====
class A {
num: number;
constructor(num:number) {
this.num = num;
}

computed = this.num * 10;
~~~
!!! error TS2729: Property 'num' is used before its initialization.
!!! related TS2728 tests/cases/compiler/useBeforeDeclaration_propertyDeclaration.ts:2:5: 'num' is declared here.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we tack on another related span to this message that says where num is initialized, since that's what the error is about?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [useBeforeDeclaration_propertyDeclaration.ts]
class A {
num: number;
constructor(num:number) {
this.num = num;
}

computed = this.num * 10;
}

//// [useBeforeDeclaration_propertyDeclaration.js]
"use strict";
var A = /** @class */ (function () {
function A(num) {
this.computed = this.num * 10;
this.num = num;
}
return A;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/useBeforeDeclaration_propertyDeclaration.ts ===
class A {
>A : Symbol(A, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 0))

num: number;
>num : Symbol(A.num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 9))

constructor(num:number) {
>num : Symbol(num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 2, 16))

this.num = num;
>this.num : Symbol(A.num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 9))
>this : Symbol(A, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 0))
>num : Symbol(A.num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 9))
>num : Symbol(num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 2, 16))
}

computed = this.num * 10;
>computed : Symbol(A.computed, Decl(useBeforeDeclaration_propertyDeclaration.ts, 4, 5))
>this.num : Symbol(A.num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 9))
>this : Symbol(A, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 0))
>num : Symbol(A.num, Decl(useBeforeDeclaration_propertyDeclaration.ts, 0, 9))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/useBeforeDeclaration_propertyDeclaration.ts ===
class A {
>A : A

num: number;
>num : number

constructor(num:number) {
>num : number

this.num = num;
>this.num = num : number
>this.num : number
>this : this
>num : number
>num : number
}

computed = this.num * 10;
>computed : number
>this.num * 10 : number
>this.num : number
>this : this
>num : number
>10 : 10
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @strict: true
class A {
num: number;
constructor(num:number) {
this.num = num;
}

computed = this.num * 10;
}