Skip to content

Commit 20e86da

Browse files
authored
Emit class fields as-is with target: es2022 (microsoft#47018)
* Emit class fields as-is with `target: es2022` Closes microsoft#47017 * wave 2
1 parent c282771 commit 20e86da

File tree

294 files changed

+13844
-84
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+13844
-84
lines changed

Diff for: src/compiler/transformers/classFields.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ namespace ts {
133133
const languageVersion = getEmitScriptTarget(compilerOptions);
134134
const useDefineForClassFields = getUseDefineForClassFields(compilerOptions);
135135

136-
const shouldTransformPrivateElementsOrClassStaticBlocks = languageVersion < ScriptTarget.ESNext;
136+
const shouldTransformPrivateElementsOrClassStaticBlocks = languageVersion < ScriptTarget.ES2022;
137137

138138
// We don't need to transform `super` property access when targeting ES5, ES3 because
139139
// the es2015 transformation handles those.
@@ -172,7 +172,7 @@ namespace ts {
172172
function transformSourceFile(node: SourceFile) {
173173
const options = context.getCompilerOptions();
174174
if (node.isDeclarationFile
175-
|| useDefineForClassFields && getEmitScriptTarget(options) === ScriptTarget.ESNext) {
175+
|| useDefineForClassFields && getEmitScriptTarget(options) >= ScriptTarget.ES2022) {
176176
return node;
177177
}
178178
const visited = visitEachChild(node, visitor, context);
@@ -1201,7 +1201,7 @@ namespace ts {
12011201
if (useDefineForClassFields) {
12021202
// If we are using define semantics and targeting ESNext or higher,
12031203
// then we don't need to transform any class properties.
1204-
return languageVersion < ScriptTarget.ESNext;
1204+
return languageVersion < ScriptTarget.ES2022;
12051205
}
12061206
return isInitializedProperty(member) || shouldTransformPrivateElementsOrClassStaticBlocks && isPrivateIdentifierClassElementDeclaration(member);
12071207
}

Diff for: src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6239,7 +6239,7 @@ namespace ts {
62396239
}
62406240

62416241
export function getUseDefineForClassFields(compilerOptions: CompilerOptions): boolean {
6242-
return compilerOptions.useDefineForClassFields === undefined ? getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext : compilerOptions.useDefineForClassFields;
6242+
return compilerOptions.useDefineForClassFields === undefined ? getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022 : compilerOptions.useDefineForClassFields;
62436243
}
62446244

62456245
export function compilerOptionsAffectSemanticDiagnostics(newOptions: CompilerOptions, oldOptions: CompilerOptions): boolean {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [classStaticBlock1.ts]
2+
const a = 2;
3+
4+
class C {
5+
static {
6+
const a = 1;
7+
8+
a;
9+
}
10+
}
11+
12+
13+
//// [classStaticBlock1.js]
14+
const a = 2;
15+
class C {
16+
static {
17+
const a = 1;
18+
a;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
2+
const a = 2;
3+
>a : Symbol(a, Decl(classStaticBlock1.ts, 0, 5))
4+
5+
class C {
6+
>C : Symbol(C, Decl(classStaticBlock1.ts, 0, 12))
7+
8+
static {
9+
const a = 1;
10+
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))
11+
12+
a;
13+
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))
14+
}
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
2+
const a = 2;
3+
>a : 2
4+
>2 : 2
5+
6+
class C {
7+
>C : C
8+
9+
static {
10+
const a = 1;
11+
>a : 1
12+
>1 : 1
13+
14+
a;
15+
>a : 1
16+
}
17+
}
18+

Diff for: tests/baselines/reference/classStaticBlock10(target=es2015).js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class C2 {
2525
const b1 = 222;
2626
const b2 = 222;
2727
}
28-
}
28+
}
29+
2930

3031
//// [classStaticBlock10.js]
3132
var a1 = 1;

Diff for: tests/baselines/reference/classStaticBlock10(target=es2015).symbols

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ class C2 {
5656
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
5757
}
5858
}
59+

Diff for: tests/baselines/reference/classStaticBlock10(target=es2015).types

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ class C2 {
7070
>222 : 222
7171
}
7272
}
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [classStaticBlock10.ts]
2+
var a1 = 1;
3+
var a2 = 1;
4+
const b1 = 2;
5+
const b2 = 2;
6+
7+
function f () {
8+
var a1 = 11;
9+
const b1 = 22;
10+
11+
class C1 {
12+
static {
13+
var a1 = 111;
14+
var a2 = 111;
15+
const b1 = 222;
16+
const b2 = 222;
17+
}
18+
}
19+
}
20+
21+
class C2 {
22+
static {
23+
var a1 = 111;
24+
var a2 = 111;
25+
const b1 = 222;
26+
const b2 = 222;
27+
}
28+
}
29+
30+
31+
//// [classStaticBlock10.js]
32+
var a1 = 1;
33+
var a2 = 1;
34+
const b1 = 2;
35+
const b2 = 2;
36+
function f() {
37+
var a1 = 11;
38+
const b1 = 22;
39+
class C1 {
40+
static {
41+
var a1 = 111;
42+
var a2 = 111;
43+
const b1 = 222;
44+
const b2 = 222;
45+
}
46+
}
47+
}
48+
class C2 {
49+
static {
50+
var a1 = 111;
51+
var a2 = 111;
52+
const b1 = 222;
53+
const b2 = 222;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock10.ts ===
2+
var a1 = 1;
3+
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 0, 3))
4+
5+
var a2 = 1;
6+
>a2 : Symbol(a2, Decl(classStaticBlock10.ts, 1, 3))
7+
8+
const b1 = 2;
9+
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 2, 5))
10+
11+
const b2 = 2;
12+
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 3, 5))
13+
14+
function f () {
15+
>f : Symbol(f, Decl(classStaticBlock10.ts, 3, 13))
16+
17+
var a1 = 11;
18+
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 6, 7))
19+
20+
const b1 = 22;
21+
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 7, 9))
22+
23+
class C1 {
24+
>C1 : Symbol(C1, Decl(classStaticBlock10.ts, 7, 18))
25+
26+
static {
27+
var a1 = 111;
28+
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 11, 15))
29+
30+
var a2 = 111;
31+
>a2 : Symbol(a2, Decl(classStaticBlock10.ts, 12, 15))
32+
33+
const b1 = 222;
34+
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 13, 17))
35+
36+
const b2 = 222;
37+
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 14, 17))
38+
}
39+
}
40+
}
41+
42+
class C2 {
43+
>C2 : Symbol(C2, Decl(classStaticBlock10.ts, 17, 1))
44+
45+
static {
46+
var a1 = 111;
47+
>a1 : Symbol(a1, Decl(classStaticBlock10.ts, 21, 11))
48+
49+
var a2 = 111;
50+
>a2 : Symbol(a2, Decl(classStaticBlock10.ts, 22, 11))
51+
52+
const b1 = 222;
53+
>b1 : Symbol(b1, Decl(classStaticBlock10.ts, 23, 13))
54+
55+
const b2 = 222;
56+
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
57+
}
58+
}
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock10.ts ===
2+
var a1 = 1;
3+
>a1 : number
4+
>1 : 1
5+
6+
var a2 = 1;
7+
>a2 : number
8+
>1 : 1
9+
10+
const b1 = 2;
11+
>b1 : 2
12+
>2 : 2
13+
14+
const b2 = 2;
15+
>b2 : 2
16+
>2 : 2
17+
18+
function f () {
19+
>f : () => void
20+
21+
var a1 = 11;
22+
>a1 : number
23+
>11 : 11
24+
25+
const b1 = 22;
26+
>b1 : 22
27+
>22 : 22
28+
29+
class C1 {
30+
>C1 : C1
31+
32+
static {
33+
var a1 = 111;
34+
>a1 : number
35+
>111 : 111
36+
37+
var a2 = 111;
38+
>a2 : number
39+
>111 : 111
40+
41+
const b1 = 222;
42+
>b1 : 222
43+
>222 : 222
44+
45+
const b2 = 222;
46+
>b2 : 222
47+
>222 : 222
48+
}
49+
}
50+
}
51+
52+
class C2 {
53+
>C2 : C2
54+
55+
static {
56+
var a1 = 111;
57+
>a1 : number
58+
>111 : 111
59+
60+
var a2 = 111;
61+
>a2 : number
62+
>111 : 111
63+
64+
const b1 = 222;
65+
>b1 : 222
66+
>222 : 222
67+
68+
const b2 = 222;
69+
>b2 : 222
70+
>222 : 222
71+
}
72+
}
73+

Diff for: tests/baselines/reference/classStaticBlock10(target=es5).js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class C2 {
2525
const b1 = 222;
2626
const b2 = 222;
2727
}
28-
}
28+
}
29+
2930

3031
//// [classStaticBlock10.js]
3132
var a1 = 1;

Diff for: tests/baselines/reference/classStaticBlock10(target=es5).symbols

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ class C2 {
5656
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
5757
}
5858
}
59+

Diff for: tests/baselines/reference/classStaticBlock10(target=es5).types

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ class C2 {
7070
>222 : 222
7171
}
7272
}
73+

Diff for: tests/baselines/reference/classStaticBlock10(target=esnext).js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class C2 {
2525
const b1 = 222;
2626
const b2 = 222;
2727
}
28-
}
28+
}
29+
2930

3031
//// [classStaticBlock10.js]
3132
var a1 = 1;

Diff for: tests/baselines/reference/classStaticBlock10(target=esnext).symbols

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ class C2 {
5656
>b2 : Symbol(b2, Decl(classStaticBlock10.ts, 24, 13))
5757
}
5858
}
59+

Diff for: tests/baselines/reference/classStaticBlock10(target=esnext).types

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ class C2 {
7070
>222 : 222
7171
}
7272
}
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [classStaticBlock11.ts]
2+
let getX;
3+
class C {
4+
#x = 1
5+
constructor(x: number) {
6+
this.#x = x;
7+
}
8+
9+
static {
10+
// getX has privileged access to #x
11+
getX = (obj: C) => obj.#x;
12+
}
13+
}
14+
15+
16+
//// [classStaticBlock11.js]
17+
let getX;
18+
class C {
19+
#x = 1;
20+
constructor(x) {
21+
this.#x = x;
22+
}
23+
static {
24+
// getX has privileged access to #x
25+
getX = (obj) => obj.#x;
26+
}
27+
}

0 commit comments

Comments
 (0)