From 5c60a2c173f91f3edd636bfbeb7a9915c8ca41ef Mon Sep 17 00:00:00 2001 From: Solo-steven Date: Tue, 18 Apr 2023 15:43:21 +0800 Subject: [PATCH 1/2] fix(53286): raise property assignment before use diagnose when target is ES2022 --- input.ts | 4 + src/compiler/checker.ts | 2 +- test.js | 36 ++++ ...ertyToPropertyDeclarationES2022.errors.txt | 70 +++++++ ...eterPropertyToPropertyDeclarationES2022.js | 111 +++++++++++ ...ropertyToPropertyDeclarationES2022.symbols | 165 +++++++++++++++++ ...rPropertyToPropertyDeclarationES2022.types | 175 ++++++++++++++++++ ...eterPropertyToPropertyDeclarationES2022.ts | 52 ++++++ 8 files changed, 614 insertions(+), 1 deletion(-) create mode 100644 input.ts create mode 100644 test.js create mode 100644 tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.errors.txt create mode 100644 tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.js create mode 100644 tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.symbols create mode 100644 tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.types create mode 100644 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts diff --git a/input.ts b/input.ts new file mode 100644 index 0000000000000..486826ad750e3 --- /dev/null +++ b/input.ts @@ -0,0 +1,4 @@ +class Foo { + bar = this.buz; + constructor(private buz: unknown) {} +} \ No newline at end of file diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7d940cf747ee8..1e5e4216843fa 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); diff --git a/test.js b/test.js new file mode 100644 index 0000000000000..d8d996f8aabe0 --- /dev/null +++ b/test.js @@ -0,0 +1,36 @@ +const ts = require("./built/local/typescript"); + +function compile(fileNames,options) { + const program = ts.createProgram(fileNames, options); + const emitResult = program.emit(); + const allDiagnostics = ts + .getPreEmitDiagnostics(program) + .concat(emitResult.diagnostics); + + allDiagnostics.forEach(diagnostic => { + if (diagnostic.file) { + const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); + const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); + console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); + } + else { + console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")); + } + }); + + const exitCode = emitResult.emitSkipped ? 1 : 0; + console.log(`Process exiting with code '${exitCode}'.`); + + // My Code + const astSourceFile = program.getSourceFile("input.ts"); + console.log(program.getSymbolCount()); + process.exit(exitCode); +} + +compile(["./input.ts"], { + strict: true, + noEmitOnError: true, + noImplicitAny: true, + target: ts.ScriptTarget.ES2022, + module: ts.ModuleKind.CommonJS +}); \ No newline at end of file diff --git a/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.errors.txt b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.errors.txt new file mode 100644 index 0000000000000..1fb4bd61473ab --- /dev/null +++ b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.errors.txt @@ -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; + } + \ No newline at end of file diff --git a/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.js b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.js new file mode 100644 index 0000000000000..af8f469a3915b --- /dev/null +++ b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.js @@ -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; +} diff --git a/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.symbols b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.symbols new file mode 100644 index 0000000000000..a1e97476b8b02 --- /dev/null +++ b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.types b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.types new file mode 100644 index 0000000000000..a2616449713d1 --- /dev/null +++ b/tests/baselines/reference/assignParameterPropertyToPropertyDeclarationES2022.types @@ -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 +} + diff --git a/tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts b/tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts new file mode 100644 index 0000000000000..f6568022a501f --- /dev/null +++ b/tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts @@ -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; +} From 5a42fff4cdef34d9d45bbf0c7fb366472a070445 Mon Sep 17 00:00:00 2001 From: Solo-steven Date: Tue, 18 Apr 2023 15:49:38 +0800 Subject: [PATCH 2/2] chore: delete unrelated files --- input.ts | 4 ---- test.js | 36 ------------------------------------ 2 files changed, 40 deletions(-) delete mode 100644 input.ts delete mode 100644 test.js diff --git a/input.ts b/input.ts deleted file mode 100644 index 486826ad750e3..0000000000000 --- a/input.ts +++ /dev/null @@ -1,4 +0,0 @@ -class Foo { - bar = this.buz; - constructor(private buz: unknown) {} -} \ No newline at end of file diff --git a/test.js b/test.js deleted file mode 100644 index d8d996f8aabe0..0000000000000 --- a/test.js +++ /dev/null @@ -1,36 +0,0 @@ -const ts = require("./built/local/typescript"); - -function compile(fileNames,options) { - const program = ts.createProgram(fileNames, options); - const emitResult = program.emit(); - const allDiagnostics = ts - .getPreEmitDiagnostics(program) - .concat(emitResult.diagnostics); - - allDiagnostics.forEach(diagnostic => { - if (diagnostic.file) { - const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); - const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); - console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); - } - else { - console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")); - } - }); - - const exitCode = emitResult.emitSkipped ? 1 : 0; - console.log(`Process exiting with code '${exitCode}'.`); - - // My Code - const astSourceFile = program.getSourceFile("input.ts"); - console.log(program.getSymbolCount()); - process.exit(exitCode); -} - -compile(["./input.ts"], { - strict: true, - noEmitOnError: true, - noImplicitAny: true, - target: ts.ScriptTarget.ES2022, - module: ts.ModuleKind.CommonJS -}); \ No newline at end of file