Skip to content

Commit

Permalink
Cherry-pick PR microsoft#53268 into release-5.0
Browse files Browse the repository at this point in the history
Component commits:
71f24f3 fix(53204): call __runInitializers after super() call

9ce2780 call __runInitializers exclusively after super() call
  • Loading branch information
a-tarasyuk authored and typescript-bot committed Mar 15, 2023
1 parent cb69c8a commit ec22b19
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/compiler/transformers/esDecorators.ts
Expand Up @@ -36,6 +36,7 @@ import {
Expression,
ExpressionStatement,
findComputedPropertyNameCacheAssignment,
findSuperStatementIndex,
firstOrUndefined,
forEachEntry,
ForStatement,
Expand Down Expand Up @@ -1072,8 +1073,11 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
if (initializerStatements) {
const statements: Statement[] = [];
const nonPrologueStart = factory.copyPrologue(node.body.statements, statements, /*ensureUseStrict*/ false, visitor);
const superStatementIndex = findSuperStatementIndex(node.body.statements, nonPrologueStart);
const indexOfFirstStatementAfterSuper = superStatementIndex >= 0 ? superStatementIndex + 1 : undefined;
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, nonPrologueStart, indexOfFirstStatementAfterSuper ? indexOfFirstStatementAfterSuper - nonPrologueStart : undefined));
addRange(statements, initializerStatements);
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, nonPrologueStart));
addRange(statements, visitNodes(node.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper));
body = factory.createBlock(statements, /*multiLine*/ true);
setOriginalNode(body, node.body);
setTextRange(body, node.body);
Expand Down
@@ -0,0 +1,50 @@
//// [esDecorators-classExpression-classSuper.7.ts]
class A {}
class B extends A {
public constructor() {
'inject';
super();
const a = 1;
const b = 1;
}

@foo
public m(): void {}
}

function foo(method: any, _context: any): any {
return function (this: any) {
method.call(this);
};
}

new B();


//// [esDecorators-classExpression-classSuper.7.js]
class A {
}
let B = (() => {
let _instanceExtraInitializers = [];
let _m_decorators;
return class B extends A {
static {
_m_decorators = [foo];
__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);
}
constructor() {
'inject';
super();
__runInitializers(this, _instanceExtraInitializers);
const a = 1;
const b = 1;
}
m() { }
};
})();
function foo(method, _context) {
return function () {
method.call(this);
};
}
new B();
@@ -0,0 +1,24 @@
// @target: es2022
// @noEmitHelpers: true
// @noTypesAndSymbols: true

class A {}
class B extends A {
public constructor() {
'inject';
super();
const a = 1;
const b = 1;
}

@foo
public m(): void {}
}

function foo(method: any, _context: any): any {
return function (this: any) {
method.call(this);
};
}

new B();

0 comments on commit ec22b19

Please sign in to comment.