Skip to content

Fix/issue 53286 #53885

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 2 commits into from
May 2, 2023
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2811,7 +2811,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return true;
}
if (isUsedInFunctionOrInstanceProperty(usage, declaration)) {
if (getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext && useDefineForClassFields
if (getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022 && useDefineForClassFields
&& getContainingClass(declaration)
&& (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent))) {
return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(2,16): error TS2729: Property 'bar' is used before its initialization.
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(3,16): error TS2729: Property 'foo' is used before its initialization.
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(6,19): error TS2729: Property 'm3' is used before its initialization.
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(12,17): error TS2729: Property 'baz' is used before its initialization.


==== tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts (4 errors) ====
class C {
qux = this.bar // should error
~~~
!!! error TS2729: Property 'bar' is used before its initialization.
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:3:5: 'bar' is declared here.
bar = this.foo // should error
~~~
!!! error TS2729: Property 'foo' is used before its initialization.
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:11:17: 'foo' is declared here.
quiz = this.bar // ok
quench = this.m1() // ok
quanch = this.m3() // should error
~~
!!! error TS2729: Property 'm3' is used before its initialization.
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:10:5: 'm3' is declared here.
m1() {
this.foo // ok
}
m3 = function() { }
constructor(public foo: string) {}
quim = this.baz // should error
~~~
!!! error TS2729: Property 'baz' is used before its initialization.
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:13:5: 'baz' is declared here.
baz = this.foo; // should error
quid = this.baz // ok
m2() {
this.foo // ok
}
}

class D extends C {
quill = this.foo // ok
}

class E {
bar = () => this.foo1 + this.foo2; // both ok
foo1 = '';
constructor(public foo2: string) {}
}

class F {
Inner = class extends F {
p2 = this.p1
}
p1 = 0
}
class G {
Inner = class extends G {
p2 = this.p1
}
constructor(public p1: number) {}
}
class H {
constructor(public p1: C) {}

public p2 = () => {
return this.p1.foo;
}

public p3 = () => this.p1.foo;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//// [assignParameterPropertyToPropertyDeclarationES2022.ts]
class C {
qux = this.bar // should error
bar = this.foo // should error
quiz = this.bar // ok
quench = this.m1() // ok
quanch = this.m3() // should error
m1() {
this.foo // ok
}
m3 = function() { }
constructor(public foo: string) {}
quim = this.baz // should error
baz = this.foo; // should error
quid = this.baz // ok
m2() {
this.foo // ok
}
}

class D extends C {
quill = this.foo // ok
}

class E {
bar = () => this.foo1 + this.foo2; // both ok
foo1 = '';
constructor(public foo2: string) {}
}

class F {
Inner = class extends F {
p2 = this.p1
}
p1 = 0
}
class G {
Inner = class extends G {
p2 = this.p1
}
constructor(public p1: number) {}
}
class H {
constructor(public p1: C) {}

public p2 = () => {
return this.p1.foo;
}

public p3 = () => this.p1.foo;
}


//// [assignParameterPropertyToPropertyDeclarationES2022.js]
class C {
foo;
qux = this.bar; // should error
bar = this.foo; // should error
quiz = this.bar; // ok
quench = this.m1(); // ok
quanch = this.m3(); // should error
m1() {
this.foo; // ok
}
m3 = function () { };
constructor(foo) {
this.foo = foo;
}
quim = this.baz; // should error
baz = this.foo; // should error
quid = this.baz; // ok
m2() {
this.foo; // ok
}
}
class D extends C {
quill = this.foo; // ok
}
class E {
foo2;
bar = () => this.foo1 + this.foo2; // both ok
foo1 = '';
constructor(foo2) {
this.foo2 = foo2;
}
}
class F {
Inner = class extends F {
p2 = this.p1;
};
p1 = 0;
}
class G {
p1;
Inner = class extends G {
p2 = this.p1;
};
constructor(p1) {
this.p1 = p1;
}
}
class H {
p1;
constructor(p1) {
this.p1 = p1;
}
p2 = () => {
return this.p1.foo;
};
p3 = () => this.p1.foo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts ===
class C {
>C : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))

qux = this.bar // should error
>qux : Symbol(C.qux, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 9))
>this.bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))

bar = this.foo // should error
>bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))

quiz = this.bar // ok
>quiz : Symbol(C.quiz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 2, 18))
>this.bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))

quench = this.m1() // ok
>quench : Symbol(C.quench, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 3, 19))
>this.m1 : Symbol(C.m1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 5, 22))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>m1 : Symbol(C.m1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 5, 22))

quanch = this.m3() // should error
>quanch : Symbol(C.quanch, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 4, 22))
>this.m3 : Symbol(C.m3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 8, 5))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>m3 : Symbol(C.m3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 8, 5))

m1() {
>m1 : Symbol(C.m1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 5, 22))

this.foo // ok
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
}
m3 = function() { }
>m3 : Symbol(C.m3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 8, 5))

constructor(public foo: string) {}
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))

quim = this.baz // should error
>quim : Symbol(C.quim, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 38))
>this.baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))

baz = this.foo; // should error
>baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))

quid = this.baz // ok
>quid : Symbol(C.quid, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 12, 19))
>this.baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))

m2() {
>m2 : Symbol(C.m2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 13, 19))

this.foo // ok
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
}
}

class D extends C {
>D : Symbol(D, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 17, 1))
>C : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))

quill = this.foo // ok
>quill : Symbol(D.quill, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 19, 19))
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this : Symbol(D, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 17, 1))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
}

class E {
>E : Symbol(E, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 21, 1))

bar = () => this.foo1 + this.foo2; // both ok
>bar : Symbol(E.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 23, 9))
>this.foo1 : Symbol(E.foo1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 24, 38))
>this : Symbol(E, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 21, 1))
>foo1 : Symbol(E.foo1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 24, 38))
>this.foo2 : Symbol(E.foo2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 26, 16))
>this : Symbol(E, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 21, 1))
>foo2 : Symbol(E.foo2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 26, 16))

foo1 = '';
>foo1 : Symbol(E.foo1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 24, 38))

constructor(public foo2: string) {}
>foo2 : Symbol(E.foo2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 26, 16))
}

class F {
>F : Symbol(F, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 27, 1))

Inner = class extends F {
>Inner : Symbol(F.Inner, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 29, 9))
>F : Symbol(F, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 27, 1))

p2 = this.p1
>p2 : Symbol((Anonymous class).p2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 30, 29))
>this.p1 : Symbol(F.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 32, 5))
>this : Symbol((Anonymous class), Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 30, 11))
>p1 : Symbol(F.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 32, 5))
}
p1 = 0
>p1 : Symbol(F.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 32, 5))
}
class G {
>G : Symbol(G, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 34, 1))

Inner = class extends G {
>Inner : Symbol(G.Inner, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 35, 9))
>G : Symbol(G, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 34, 1))

p2 = this.p1
>p2 : Symbol((Anonymous class).p2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 36, 29))
>this.p1 : Symbol(G.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 39, 16))
>this : Symbol((Anonymous class), Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 36, 11))
>p1 : Symbol(G.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 39, 16))
}
constructor(public p1: number) {}
>p1 : Symbol(G.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 39, 16))
}
class H {
>H : Symbol(H, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 40, 1))

constructor(public p1: C) {}
>p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
>C : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))

public p2 = () => {
>p2 : Symbol(H.p2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 32))

return this.p1.foo;
>this.p1.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this.p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
>this : Symbol(H, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 40, 1))
>p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
}

public p3 = () => this.p1.foo;
>p3 : Symbol(H.p3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 46, 5))
>this.p1.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
>this.p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
>this : Symbol(H, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 40, 1))
>p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
}

Loading