Fix/issue 53286 (#53885)

This commit is contained in:
Steven
2023-05-03 06:53:42 +08:00
committed by GitHub
parent 632ad0b0de
commit ec1205cec1
6 changed files with 574 additions and 1 deletions

View File

@@ -2814,7 +2814,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);

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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))
}

View File

@@ -0,0 +1,175 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts ===
class C {
>C : C
qux = this.bar // should error
>qux : string
>this.bar : string
>this : this
>bar : string
bar = this.foo // should error
>bar : string
>this.foo : string
>this : this
>foo : string
quiz = this.bar // ok
>quiz : string
>this.bar : string
>this : this
>bar : string
quench = this.m1() // ok
>quench : void
>this.m1() : void
>this.m1 : () => void
>this : this
>m1 : () => void
quanch = this.m3() // should error
>quanch : void
>this.m3() : void
>this.m3 : () => void
>this : this
>m3 : () => void
m1() {
>m1 : () => void
this.foo // ok
>this.foo : string
>this : this
>foo : string
}
m3 = function() { }
>m3 : () => void
>function() { } : () => void
constructor(public foo: string) {}
>foo : string
quim = this.baz // should error
>quim : string
>this.baz : string
>this : this
>baz : string
baz = this.foo; // should error
>baz : string
>this.foo : string
>this : this
>foo : string
quid = this.baz // ok
>quid : string
>this.baz : string
>this : this
>baz : string
m2() {
>m2 : () => void
this.foo // ok
>this.foo : string
>this : this
>foo : string
}
}
class D extends C {
>D : D
>C : C
quill = this.foo // ok
>quill : string
>this.foo : string
>this : this
>foo : string
}
class E {
>E : E
bar = () => this.foo1 + this.foo2; // both ok
>bar : () => string
>() => this.foo1 + this.foo2 : () => string
>this.foo1 + this.foo2 : string
>this.foo1 : string
>this : this
>foo1 : string
>this.foo2 : string
>this : this
>foo2 : string
foo1 = '';
>foo1 : string
>'' : ""
constructor(public foo2: string) {}
>foo2 : string
}
class F {
>F : F
Inner = class extends F {
>Inner : typeof (Anonymous class)
>class extends F { p2 = this.p1 } : typeof (Anonymous class)
>F : F
p2 = this.p1
>p2 : number
>this.p1 : number
>this : this
>p1 : number
}
p1 = 0
>p1 : number
>0 : 0
}
class G {
>G : G
Inner = class extends G {
>Inner : typeof (Anonymous class)
>class extends G { p2 = this.p1 } : typeof (Anonymous class)
>G : G
p2 = this.p1
>p2 : number
>this.p1 : number
>this : this
>p1 : number
}
constructor(public p1: number) {}
>p1 : number
}
class H {
>H : H
constructor(public p1: C) {}
>p1 : C
public p2 = () => {
>p2 : () => string
>() => { return this.p1.foo; } : () => string
return this.p1.foo;
>this.p1.foo : string
>this.p1 : C
>this : this
>p1 : C
>foo : string
}
public p3 = () => this.p1.foo;
>p3 : () => string
>() => this.p1.foo : () => string
>this.p1.foo : string
>this.p1 : C
>this : this
>p1 : C
>foo : string
}

View File

@@ -0,0 +1,52 @@
// @useDefineForClassFields: true
// @target: es2022
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;
}