Skip to content

Commit 2075f74

Browse files
sandersnDanielRosenwasser
authored andcommitted
useDefineForClassFields skips emit of ambient properties (#35058)
* useDefineForClassFields skips emit of ambient properties Previously: ```ts class C { declare p } ``` would incorrectly emit ```js class C { constructor() { Object.defineProperty(this, "p", { enumerable: true, configurable: true, writable: true, value: void 0 }); } } ``` when useDefineForClassFields was turned on (for targets <ESNext). * Fix bug for ESNext as well This moves the check earlier in the pipeline. * update baselines
1 parent 3da85df commit 2075f74

11 files changed

+196
-4
lines changed

src/compiler/transformers/ts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,9 @@ namespace ts {
18731873
}
18741874

18751875
function visitPropertyDeclaration(node: PropertyDeclaration) {
1876+
if (node.flags & NodeFlags.Ambient) {
1877+
return undefined;
1878+
}
18761879
const updated = updateProperty(
18771880
node,
18781881
/*decorators*/ undefined,

tests/baselines/reference/definePropertyES5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class A {
66
["computed"] = 13
77
;[x] = 14
88
m() { }
9+
declare notEmitted: boolean;
910
}
1011

1112

tests/baselines/reference/definePropertyES5.symbols

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ class A {
2121

2222
m() { }
2323
>m : Symbol(A.m, Decl(definePropertyES5.ts, 5, 13))
24+
25+
declare notEmitted: boolean;
26+
>notEmitted : Symbol(A.notEmitted, Decl(definePropertyES5.ts, 6, 11))
2427
}
2528

tests/baselines/reference/definePropertyES5.types

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ class A {
2525

2626
m() { }
2727
>m : () => void
28+
29+
declare notEmitted: boolean;
30+
>notEmitted : boolean
2831
}
2932

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [definePropertyESNext.ts]
2+
var x: "p" = "p"
3+
class A {
4+
a = 12
5+
b
6+
["computed"] = 13
7+
;[x] = 14
8+
m() { }
9+
constructor(public readonly y: number) { }
10+
declare notEmitted;
11+
}
12+
class B {
13+
}
14+
class C extends B {
15+
z = this.ka
16+
constructor(public ka: number) {
17+
super()
18+
}
19+
ki = this.ka
20+
}
21+
22+
23+
//// [definePropertyESNext.js]
24+
var x = "p";
25+
class A {
26+
y;
27+
a = 12;
28+
b;
29+
["computed"] = 13;
30+
[x] = 14;
31+
m() { }
32+
constructor(y) {
33+
this.y = y;
34+
}
35+
}
36+
class B {
37+
}
38+
class C extends B {
39+
ka;
40+
z = this.ka;
41+
constructor(ka) {
42+
super();
43+
this.ka = ka;
44+
}
45+
ki = this.ka;
46+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/definePropertyESNext.ts ===
2+
var x: "p" = "p"
3+
>x : Symbol(x, Decl(definePropertyESNext.ts, 0, 3))
4+
5+
class A {
6+
>A : Symbol(A, Decl(definePropertyESNext.ts, 0, 16))
7+
8+
a = 12
9+
>a : Symbol(A.a, Decl(definePropertyESNext.ts, 1, 9))
10+
11+
b
12+
>b : Symbol(A.b, Decl(definePropertyESNext.ts, 2, 10))
13+
14+
["computed"] = 13
15+
>["computed"] : Symbol(A["computed"], Decl(definePropertyESNext.ts, 3, 5))
16+
>"computed" : Symbol(A["computed"], Decl(definePropertyESNext.ts, 3, 5))
17+
18+
;[x] = 14
19+
>[x] : Symbol(A[x], Decl(definePropertyESNext.ts, 5, 5))
20+
>x : Symbol(x, Decl(definePropertyESNext.ts, 0, 3))
21+
22+
m() { }
23+
>m : Symbol(A.m, Decl(definePropertyESNext.ts, 5, 13))
24+
25+
constructor(public readonly y: number) { }
26+
>y : Symbol(A.y, Decl(definePropertyESNext.ts, 7, 16))
27+
28+
declare notEmitted;
29+
>notEmitted : Symbol(A.notEmitted, Decl(definePropertyESNext.ts, 7, 46))
30+
}
31+
class B {
32+
>B : Symbol(B, Decl(definePropertyESNext.ts, 9, 1))
33+
}
34+
class C extends B {
35+
>C : Symbol(C, Decl(definePropertyESNext.ts, 11, 1))
36+
>B : Symbol(B, Decl(definePropertyESNext.ts, 9, 1))
37+
38+
z = this.ka
39+
>z : Symbol(C.z, Decl(definePropertyESNext.ts, 12, 19))
40+
>this.ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 14, 16))
41+
>this : Symbol(C, Decl(definePropertyESNext.ts, 11, 1))
42+
>ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 14, 16))
43+
44+
constructor(public ka: number) {
45+
>ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 14, 16))
46+
47+
super()
48+
>super : Symbol(B, Decl(definePropertyESNext.ts, 9, 1))
49+
}
50+
ki = this.ka
51+
>ki : Symbol(C.ki, Decl(definePropertyESNext.ts, 16, 5))
52+
>this.ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 14, 16))
53+
>this : Symbol(C, Decl(definePropertyESNext.ts, 11, 1))
54+
>ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 14, 16))
55+
}
56+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/definePropertyESNext.ts ===
2+
var x: "p" = "p"
3+
>x : "p"
4+
>"p" : "p"
5+
6+
class A {
7+
>A : A
8+
9+
a = 12
10+
>a : number
11+
>12 : 12
12+
13+
b
14+
>b : any
15+
16+
["computed"] = 13
17+
>["computed"] : number
18+
>"computed" : "computed"
19+
>13 : 13
20+
21+
;[x] = 14
22+
>[x] : number
23+
>x : "p"
24+
>14 : 14
25+
26+
m() { }
27+
>m : () => void
28+
29+
constructor(public readonly y: number) { }
30+
>y : number
31+
32+
declare notEmitted;
33+
>notEmitted : any
34+
}
35+
class B {
36+
>B : B
37+
}
38+
class C extends B {
39+
>C : C
40+
>B : B
41+
42+
z = this.ka
43+
>z : number
44+
>this.ka : number
45+
>this : this
46+
>ka : number
47+
48+
constructor(public ka: number) {
49+
>ka : number
50+
51+
super()
52+
>super() : void
53+
>super : typeof B
54+
}
55+
ki = this.ka
56+
>ki : number
57+
>this.ka : number
58+
>this : this
59+
>ka : number
60+
}
61+

tests/baselines/reference/derivedUninitializedPropertyDeclaration.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ var BDBang = /** @class */ (function (_super) {
113113
var BOther = /** @class */ (function (_super) {
114114
__extends(BOther, _super);
115115
function BOther() {
116-
var _this = _super !== null && _super.apply(this, arguments) || this;
117-
_this.property = 'y'; // initialiser not allowed with declare
118-
return _this;
116+
return _super !== null && _super.apply(this, arguments) || this;
119117
}
120118
BOther.prototype.m = function () { return 2; }; // not allowed on methods
121119
return BOther;

tests/baselines/reference/illegalModifiersOnClassElements.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
//// [illegalModifiersOnClassElements.js]
88
var C = /** @class */ (function () {
99
function C() {
10-
this.foo = 1;
1110
this.bar = 1;
1211
}
1312
return C;

tests/cases/conformance/classes/propertyMemberDeclarations/definePropertyES5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ class A {
77
["computed"] = 13
88
;[x] = 14
99
m() { }
10+
declare notEmitted: boolean;
1011
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @target: esnext
2+
// @useDefineForClassFields: true
3+
var x: "p" = "p"
4+
class A {
5+
a = 12
6+
b
7+
["computed"] = 13
8+
;[x] = 14
9+
m() { }
10+
constructor(public readonly y: number) { }
11+
declare notEmitted;
12+
}
13+
class B {
14+
}
15+
class C extends B {
16+
z = this.ka
17+
constructor(public ka: number) {
18+
super()
19+
}
20+
ki = this.ka
21+
}

0 commit comments

Comments
 (0)