No error on this exprs in static property inits (#36781)

No error on `this` expressions in static property declaration
initialisers when targetting ESNext and with useDefineForClassFields. In
this case the emit is correct and the types are correct, so the error
should not be issued.
This commit is contained in:
Nathan Shively-Sanders 2020-02-26 09:07:45 -08:00 committed by GitHub
parent 454cdb8279
commit af901ba911
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 1 deletions

View file

@ -20947,7 +20947,7 @@ namespace ts {
break;
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
if (hasModifier(container, ModifierFlags.Static)) {
if (hasModifier(container, ModifierFlags.Static) && !(compilerOptions.target === ScriptTarget.ESNext && compilerOptions.useDefineForClassFields)) {
error(node, Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer);
// do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
}

View file

@ -0,0 +1,20 @@
//// [thisInClassBodyStaticESNext.ts]
// all are allowed with es-compliant class field emit
class Foo {
x = this
static t = this
static at = () => this
static ft = function () { return this }
static mt() { return this }
}
//// [thisInClassBodyStaticESNext.js]
// all are allowed with es-compliant class field emit
class Foo {
x = this;
static t = this;
static at = () => this;
static ft = function () { return this; };
static mt() { return this; }
}

View file

@ -0,0 +1,25 @@
=== tests/cases/compiler/thisInClassBodyStaticESNext.ts ===
// all are allowed with es-compliant class field emit
class Foo {
>Foo : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
x = this
>x : Symbol(Foo.x, Decl(thisInClassBodyStaticESNext.ts, 1, 11))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
static t = this
>t : Symbol(Foo.t, Decl(thisInClassBodyStaticESNext.ts, 2, 12))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
static at = () => this
>at : Symbol(Foo.at, Decl(thisInClassBodyStaticESNext.ts, 3, 19))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
static ft = function () { return this }
>ft : Symbol(Foo.ft, Decl(thisInClassBodyStaticESNext.ts, 4, 26))
static mt() { return this }
>mt : Symbol(Foo.mt, Decl(thisInClassBodyStaticESNext.ts, 5, 43))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
}

View file

@ -0,0 +1,28 @@
=== tests/cases/compiler/thisInClassBodyStaticESNext.ts ===
// all are allowed with es-compliant class field emit
class Foo {
>Foo : Foo
x = this
>x : this
>this : this
static t = this
>t : typeof Foo
>this : typeof Foo
static at = () => this
>at : () => typeof Foo
>() => this : () => typeof Foo
>this : typeof Foo
static ft = function () { return this }
>ft : () => any
>function () { return this } : () => any
>this : any
static mt() { return this }
>mt : () => typeof Foo
>this : typeof Foo
}

View file

@ -0,0 +1,11 @@
// @target: esnext
// @useDefineForClassFields: true
// all are allowed with es-compliant class field emit
class Foo {
x = this
static t = this
static at = () => this
static ft = function () { return this }
static mt() { return this }
}