Skip to content

Fixed unreported strict property initialization violations. #35891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2020
Merged
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
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32376,7 +32376,7 @@ namespace ts {
}
if (isInstancePropertyWithoutInitializer(member)) {
const propName = (<PropertyDeclaration>member).name;
if (isIdentifier(propName)) {
if (isIdentifier(propName) || isPrivateIdentifier(propName)) {
const type = getTypeOfSymbol(getSymbolOfNode(member));
if (!(type.flags & TypeFlags.AnyOrUnknown || getFalsyFlags(type) & TypeFlags.Undefined)) {
if (!constructor || !isPropertyInitializedInConstructor(propName, type, constructor)) {
Expand All @@ -32395,7 +32395,7 @@ namespace ts {
!(<PropertyDeclaration>node).initializer;
}

function isPropertyInitializedInConstructor(propName: Identifier, propType: Type, constructor: ConstructorDeclaration) {
function isPropertyInitializedInConstructor(propName: Identifier | PrivateIdentifier, propType: Type, constructor: ConstructorDeclaration) {
const reference = createPropertyAccess(createThis(), propName);
reference.expression.parent = reference;
reference.parent = constructor;
Expand Down
46 changes: 41 additions & 5 deletions tests/baselines/reference/strictPropertyInitialization.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(4,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(6,5): error TS2564: Property 'c' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(48,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(71,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(93,22): error TS2565: Property 'a' is used before being assigned.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(94,23): error TS2565: Property 'b' is used before being assigned.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(8,5): error TS2564: Property '#f' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(10,5): error TS2564: Property '#h' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(62,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(63,5): error TS2564: Property '#b' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(90,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(113,22): error TS2565: Property 'a' is used before being assigned.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(114,23): error TS2565: Property 'b' is used before being assigned.
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(115,23): error TS2565: Property '#d' is used before being assigned.


==== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts (6 errors) ====
==== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts (10 errors) ====
// Properties with non-undefined types require initialization

class C1 {
Expand All @@ -18,6 +22,14 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
~
!!! error TS2564: Property 'c' has no initializer and is not definitely assigned in the constructor.
d?: number;
#f: number; //Error
~~
!!! error TS2564: Property '#f' has no initializer and is not definitely assigned in the constructor.
#g: number | undefined;
#h: number | null; //Error
~~
!!! error TS2564: Property '#h' has no initializer and is not definitely assigned in the constructor.
#i?: number;
}

// No strict initialization checks in ambient contexts
Expand All @@ -27,6 +39,11 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
b: number | undefined;
c: number | null;
d?: number;

#f: number;
#g: number | undefined;
#h: number | null;
#i?: number;
}

// No strict initialization checks for static members
Expand All @@ -44,14 +61,19 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
a = 0;
b: number = 0;
c: string = "abc";
#d = 0
#e: number = 0
#f: string= "abc"
}

// Assignment in constructor satisfies strict initialization check

class C5 {
a: number;
#b: number;
constructor() {
this.a = 0;
this.#b = 0;
}
}

Expand All @@ -61,22 +83,29 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
a: number; // Error
~
!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
#b: number
~~
!!! error TS2564: Property '#b' has no initializer and is not definitely assigned in the constructor.
constructor(cond: boolean) {
if (cond) {
return;
}
this.a = 0;
this.#b = 0;
}
}

class C7 {
a: number;
#b: number;
constructor(cond: boolean) {
if (cond) {
this.a = 1;
this.#b = 1;
return;
}
this.a = 0;
this.#b = 1;
}
}

Expand Down Expand Up @@ -106,14 +135,19 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
a: number;
b: number;
c?: number;
#d: number;
constructor() {
let x = this.a; // Error
~
!!! error TS2565: Property 'a' is used before being assigned.
this.a = this.b; // Error
~
!!! error TS2565: Property 'b' is used before being assigned.
this.b = this.#d //Error
~~
!!! error TS2565: Property '#d' is used before being assigned.
this.b = x;
this.#d = x;
let y = this.c;
}
}
Expand All @@ -124,8 +158,10 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial

class C11 {
a: number;
#b: number;
constructor() {
this.a = someValue();
this.#b = someValue();
}
}

Loading