Skip to content

Emit class fields as-is with target: es2022 #47018

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
Dec 16, 2021
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
6 changes: 3 additions & 3 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace ts {
const languageVersion = getEmitScriptTarget(compilerOptions);
const useDefineForClassFields = getUseDefineForClassFields(compilerOptions);

const shouldTransformPrivateElementsOrClassStaticBlocks = languageVersion < ScriptTarget.ESNext;
const shouldTransformPrivateElementsOrClassStaticBlocks = languageVersion < ScriptTarget.ES2022;

// We don't need to transform `super` property access when targeting ES5, ES3 because
// the es2015 transformation handles those.
Expand Down Expand Up @@ -172,7 +172,7 @@ namespace ts {
function transformSourceFile(node: SourceFile) {
const options = context.getCompilerOptions();
if (node.isDeclarationFile
|| useDefineForClassFields && getEmitScriptTarget(options) === ScriptTarget.ESNext) {
|| useDefineForClassFields && getEmitScriptTarget(options) >= ScriptTarget.ES2022) {
return node;
}
const visited = visitEachChild(node, visitor, context);
Expand Down Expand Up @@ -1201,7 +1201,7 @@ namespace ts {
if (useDefineForClassFields) {
// If we are using define semantics and targeting ESNext or higher,
// then we don't need to transform any class properties.
return languageVersion < ScriptTarget.ESNext;
return languageVersion < ScriptTarget.ES2022;
}
return isInitializedProperty(member) || shouldTransformPrivateElementsOrClassStaticBlocks && isPrivateIdentifierClassElementDeclaration(member);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6239,7 +6239,7 @@ namespace ts {
}

export function getUseDefineForClassFields(compilerOptions: CompilerOptions): boolean {
return compilerOptions.useDefineForClassFields === undefined ? getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext : compilerOptions.useDefineForClassFields;
return compilerOptions.useDefineForClassFields === undefined ? getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022 : compilerOptions.useDefineForClassFields;
}

export function compilerOptionsAffectSemanticDiagnostics(newOptions: CompilerOptions, oldOptions: CompilerOptions): boolean {
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es2022).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [classStaticBlock1.ts]
const a = 2;

class C {
static {
const a = 1;

a;
}
}


//// [classStaticBlock1.js]
const a = 2;
class C {
static {
const a = 1;
a;
}
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es2022).symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
const a = 2;
>a : Symbol(a, Decl(classStaticBlock1.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classStaticBlock1.ts, 0, 12))

static {
const a = 1;
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))

a;
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es2022).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
const a = 2;
>a : 2
>2 : 2

class C {
>C : C

static {
const a = 1;
>a : 1
>1 : 1

a;
>a : 1
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class C2 {
const b1 = 222;
const b2 = 222;
}
}
}


//// [classStaticBlock10.js]
var a1 = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ class C2 {
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ class C2 {
>222 : 222
}
}

55 changes: 55 additions & 0 deletions tests/baselines/reference/classStaticBlock10(target=es2022).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//// [classStaticBlock10.ts]
var a1 = 1;
var a2 = 1;
const b1 = 2;
const b2 = 2;

function f () {
var a1 = 11;
const b1 = 22;

class C1 {
static {
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
}
}
}

class C2 {
static {
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
}
}


//// [classStaticBlock10.js]
var a1 = 1;
var a2 = 1;
const b1 = 2;
const b2 = 2;
function f() {
var a1 = 11;
const b1 = 22;
class C1 {
static {
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
}
}
}
class C2 {
static {
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock10.ts ===
var a1 = 1;
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 0, 3))

var a2 = 1;
>a2 : Symbol(a2, Decl(classStaticBlock10.ts, 1, 3))

const b1 = 2;
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 2, 5))

const b2 = 2;
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 3, 5))

function f () {
>f : Symbol(f, Decl(classStaticBlock10.ts, 3, 13))

var a1 = 11;
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 6, 7))

const b1 = 22;
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 7, 9))

class C1 {
>C1 : Symbol(C1, Decl(classStaticBlock10.ts, 7, 18))

static {
var a1 = 111;
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 11, 15))

var a2 = 111;
>a2 : Symbol(a2, Decl(classStaticBlock10.ts, 12, 15))

const b1 = 222;
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 13, 17))

const b2 = 222;
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 14, 17))
}
}
}

class C2 {
>C2 : Symbol(C2, Decl(classStaticBlock10.ts, 17, 1))

static {
var a1 = 111;
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 21, 11))

var a2 = 111;
>a2 : Symbol(a2, Decl(classStaticBlock10.ts, 22, 11))

const b1 = 222;
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 23, 13))

const b2 = 222;
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
}
}

73 changes: 73 additions & 0 deletions tests/baselines/reference/classStaticBlock10(target=es2022).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock10.ts ===
var a1 = 1;
>a1 : number
>1 : 1

var a2 = 1;
>a2 : number
>1 : 1

const b1 = 2;
>b1 : 2
>2 : 2

const b2 = 2;
>b2 : 2
>2 : 2

function f () {
>f : () => void

var a1 = 11;
>a1 : number
>11 : 11

const b1 = 22;
>b1 : 22
>22 : 22

class C1 {
>C1 : C1

static {
var a1 = 111;
>a1 : number
>111 : 111

var a2 = 111;
>a2 : number
>111 : 111

const b1 = 222;
>b1 : 222
>222 : 222

const b2 = 222;
>b2 : 222
>222 : 222
}
}
}

class C2 {
>C2 : C2

static {
var a1 = 111;
>a1 : number
>111 : 111

var a2 = 111;
>a2 : number
>111 : 111

const b1 = 222;
>b1 : 222
>222 : 222

const b2 = 222;
>b2 : 222
>222 : 222
}
}

3 changes: 2 additions & 1 deletion tests/baselines/reference/classStaticBlock10(target=es5).js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class C2 {
const b1 = 222;
const b2 = 222;
}
}
}


//// [classStaticBlock10.js]
var a1 = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ class C2 {
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ class C2 {
>222 : 222
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class C2 {
const b1 = 222;
const b2 = 222;
}
}
}


//// [classStaticBlock10.js]
var a1 = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ class C2 {
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ class C2 {
>222 : 222
}
}

27 changes: 27 additions & 0 deletions tests/baselines/reference/classStaticBlock11(target=es2022).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [classStaticBlock11.ts]
let getX;
class C {
#x = 1
constructor(x: number) {
this.#x = x;
}

static {
// getX has privileged access to #x
getX = (obj: C) => obj.#x;
}
}


//// [classStaticBlock11.js]
let getX;
class C {
#x = 1;
constructor(x) {
this.#x = x;
}
static {
// getX has privileged access to #x
getX = (obj) => obj.#x;
}
}
Loading