Skip to content

Commit 620f6d2

Browse files
TypeScript Botrbuckton
authored and
dar
committed
🤖 Pick PR microsoft#53547 (Fix double-emit in constructor) into release-5.0 (microsoft#53550)
Co-authored-by: Ron Buckton <[email protected]>
1 parent 0a901bd commit 620f6d2

File tree

4 files changed

+129
-54
lines changed

4 files changed

+129
-54
lines changed

‎src/compiler/transformers/esDecorators.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,15 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
10741074
const statements: Statement[] = [];
10751075
const nonPrologueStart = factory.copyPrologue(node.body.statements, statements, /*ensureUseStrict*/ false, visitor);
10761076
const superStatementIndex = findSuperStatementIndex(node.body.statements, nonPrologueStart);
1077-
const indexOfFirstStatementAfterSuper = superStatementIndex >= 0 ? superStatementIndex + 1 : undefined;
1078-
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, nonPrologueStart, indexOfFirstStatementAfterSuper ? indexOfFirstStatementAfterSuper - nonPrologueStart : undefined));
1079-
addRange(statements, initializerStatements);
1080-
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper));
1077+
if (superStatementIndex >= 0) {
1078+
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, nonPrologueStart, superStatementIndex + 1 - nonPrologueStart));
1079+
addRange(statements, initializerStatements);
1080+
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, superStatementIndex + 1));
1081+
}
1082+
else {
1083+
addRange(statements, initializerStatements);
1084+
addRange(statements, visitNodes(node.body.statements, visitor, isStatement));
1085+
}
10811086
body = factory.createBlock(statements, /*multiLine*/ true);
10821087
setOriginalNode(body, node.body);
10831088
setTextRange(body, node.body);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//// [esDecorators-classDeclaration-classSuper.7.ts]
2+
class A {}
3+
class B extends A {
4+
public constructor() {
5+
'inject';
6+
super();
7+
const a = 1;
8+
const b = 1;
9+
}
10+
11+
@foo
12+
public m(): void {}
13+
}
14+
15+
function foo(method: any, _context: any): any {
16+
return function (this: any) {
17+
method.call(this);
18+
};
19+
}
20+
21+
new B();
22+
23+
// https://github.com/microsoft/TypeScript/issues/53448
24+
class C {
25+
public constructor() {
26+
this.val;
27+
}
28+
29+
@foo
30+
public get val(): number { return 3; }
31+
}
32+
class D extends A {
33+
public constructor() {
34+
super();
35+
this.val;
36+
}
37+
38+
@foo
39+
public get val(): number { return 3; }
40+
}
41+
42+
43+
//// [esDecorators-classDeclaration-classSuper.7.js]
44+
class A {
45+
}
46+
let B = (() => {
47+
let _instanceExtraInitializers = [];
48+
let _m_decorators;
49+
return class B extends A {
50+
static {
51+
_m_decorators = [foo];
52+
__esDecorate(this, null, _m_decorators, { kind: "method", name: "m", static: false, private: false, access: { has: obj => "m" in obj, get: obj => obj.m } }, null, _instanceExtraInitializers);
53+
}
54+
constructor() {
55+
'inject';
56+
super();
57+
__runInitializers(this, _instanceExtraInitializers);
58+
const a = 1;
59+
const b = 1;
60+
}
61+
m() { }
62+
};
63+
})();
64+
function foo(method, _context) {
65+
return function () {
66+
method.call(this);
67+
};
68+
}
69+
new B();
70+
// https://github.com/microsoft/TypeScript/issues/53448
71+
let C = (() => {
72+
let _instanceExtraInitializers_1 = [];
73+
let _get_val_decorators;
74+
return class C {
75+
static {
76+
_get_val_decorators = [foo];
77+
__esDecorate(this, null, _get_val_decorators, { kind: "getter", name: "val", static: false, private: false, access: { has: obj => "val" in obj, get: obj => obj.val } }, null, _instanceExtraInitializers_1);
78+
}
79+
constructor() {
80+
__runInitializers(this, _instanceExtraInitializers_1);
81+
this.val;
82+
}
83+
get val() { return 3; }
84+
};
85+
})();
86+
let D = (() => {
87+
let _instanceExtraInitializers_2 = [];
88+
let _get_val_decorators;
89+
return class D extends A {
90+
static {
91+
_get_val_decorators = [foo];
92+
__esDecorate(this, null, _get_val_decorators, { kind: "getter", name: "val", static: false, private: false, access: { has: obj => "val" in obj, get: obj => obj.val } }, null, _instanceExtraInitializers_2);
93+
}
94+
constructor() {
95+
super();
96+
__runInitializers(this, _instanceExtraInitializers_2);
97+
this.val;
98+
}
99+
get val() { return 3; }
100+
};
101+
})();

‎tests/baselines/reference/esDecorators-classExpression-classSuper.7.js

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,22 @@ function foo(method: any, _context: any): any {
2222
}
2323

2424
new B();
25+
26+
// https://github.com/microsoft/TypeScript/issues/53448
27+
class C {
28+
public constructor() {
29+
this.val;
30+
}
31+
32+
@foo
33+
public get val(): number { return 3; }
34+
}
35+
class D extends A {
36+
public constructor() {
37+
super();
38+
this.val;
39+
}
40+
41+
@foo
42+
public get val(): number { return 3; }
43+
}

0 commit comments

Comments
 (0)