Skip to content

Commit a937637

Browse files
committed
Fix double-emit in constructor
1 parent b34371a commit a937637

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
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);

tests/baselines/reference/esDecorators-classExpression-classSuper.7.js renamed to tests/baselines/reference/esDecorators-classDeclaration-classSuper.7.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//// [esDecorators-classExpression-classSuper.7.ts]
1+
//// [esDecorators-classDeclaration-classSuper.7.ts]
22
class A {}
33
class B extends A {
44
public constructor() {
@@ -19,9 +19,19 @@ function foo(method: any, _context: any): any {
1919
}
2020

2121
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+
}
2232

2333

24-
//// [esDecorators-classExpression-classSuper.7.js]
34+
//// [esDecorators-classDeclaration-classSuper.7.js]
2535
class A {
2636
}
2737
let B = (() => {
@@ -48,3 +58,19 @@ function foo(method, _context) {
4858
};
4959
}
5060
new B();
61+
// https://github.com/microsoft/TypeScript/issues/53448
62+
let C = (() => {
63+
let _instanceExtraInitializers_1 = [];
64+
let _get_val_decorators;
65+
return class C {
66+
static {
67+
_get_val_decorators = [foo];
68+
__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);
69+
}
70+
constructor() {
71+
__runInitializers(this, _instanceExtraInitializers_1);
72+
this.val;
73+
}
74+
get val() { return 3; }
75+
};
76+
})();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ 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+
}

0 commit comments

Comments
 (0)