Skip to content

fix(49719): Incorrect error 2301 when using ES standard class properties #49725

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 3 commits into from
Jul 20, 2022
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 @@ -2210,7 +2210,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
// 1. When result is undefined, after checking for a missing "this."
// 2. When result is defined
function checkAndReportErrorForInvalidInitializer() {
if (propertyWithInvalidInitializer && !(getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext && useDefineForClassFields)) {
if (propertyWithInvalidInitializer && !(useDefineForClassFields && getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022)) {
// We have a match, but the reference occurred within a property initializer and the identifier also binds
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
// with ESNext+useDefineForClassFields because the scope semantics are different.
Expand Down
3 changes: 2 additions & 1 deletion src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ namespace Harness {
"preserveConstEnums",
"skipLibCheck",
"exactOptionalPropertyTypes",
"useUnknownInCatchVariables"
"useDefineForClassFields",
"useUnknownInCatchVariables",
];
private fileName: string;
private justName: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tests/cases/compiler/classMemberInitializerScoping2.ts(3,9): error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.


==== tests/cases/compiler/classMemberInitializerScoping2.ts (1 errors) ====
const x = 1
class C {
p = x
~
!!! error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
constructor(x: string) { }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [classMemberInitializerScoping2.ts]
const x = 1
class C {
p = x
constructor(x: string) { }
}


//// [classMemberInitializerScoping2.js]
const x = 1;
class C {
constructor(x) {
this.p = x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classMemberInitializerScoping2.ts, 0, 11))

p = x
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))

constructor(x: string) { }
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : 1
>1 : 1

class C {
>C : C

p = x
>p : any
>x : any

constructor(x: string) { }
>x : string
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tests/cases/compiler/classMemberInitializerScoping2.ts(3,9): error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.


==== tests/cases/compiler/classMemberInitializerScoping2.ts (1 errors) ====
const x = 1
class C {
p = x
~
!!! error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
constructor(x: string) { }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [classMemberInitializerScoping2.ts]
const x = 1
class C {
p = x
constructor(x: string) { }
}


//// [classMemberInitializerScoping2.js]
const x = 1;
class C {
constructor(x) {
Object.defineProperty(this, "p", {
enumerable: true,
configurable: true,
writable: true,
value: x
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classMemberInitializerScoping2.ts, 0, 11))

p = x
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))

constructor(x: string) { }
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : 1
>1 : 1

class C {
>C : C

p = x
>p : any
>x : any

constructor(x: string) { }
>x : string
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tests/cases/compiler/classMemberInitializerScoping2.ts(3,9): error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.


==== tests/cases/compiler/classMemberInitializerScoping2.ts (1 errors) ====
const x = 1
class C {
p = x
~
!!! error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
constructor(x: string) { }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [classMemberInitializerScoping2.ts]
const x = 1
class C {
p = x
constructor(x: string) { }
}


//// [classMemberInitializerScoping2.js]
const x = 1;
class C {
constructor(x) {
this.p = x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classMemberInitializerScoping2.ts, 0, 11))

p = x
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))

constructor(x: string) { }
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : 1
>1 : 1

class C {
>C : C

p = x
>p : any
>x : any

constructor(x: string) { }
>x : string
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [classMemberInitializerScoping2.ts]
const x = 1
class C {
p = x
constructor(x: string) { }
}


//// [classMemberInitializerScoping2.js]
const x = 1;
class C {
p = x;
constructor(x) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classMemberInitializerScoping2.ts, 0, 11))

p = x
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))

constructor(x: string) { }
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
const x = 1
>x : 1
>1 : 1

class C {
>C : C

p = x
>p : number
>x : 1

constructor(x: string) { }
>x : string
}

8 changes: 8 additions & 0 deletions tests/cases/compiler/classMemberInitializerScoping2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: es2017,esnext
// @useDefineForClassFields: true,false

const x = 1
class C {
p = x
constructor(x: string) { }
}