Skip to content

Fix class alias reference in static initializer for legacy class decorators #54046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/compiler/transformers/legacyDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
isBlock,
isCallToHelper,
isClassElement,
isClassStaticBlockDeclaration,
isComputedPropertyName,
isDecorator,
isExportOrDefaultModifier,
Expand Down Expand Up @@ -339,6 +340,27 @@ export function transformLegacyDecorators(context: TransformationContext): (x: S
let decorationStatements: Statement[] | undefined = [];
({ members, decorationStatements } = transformDecoratorsOfClassElements(node, members));

// If we're emitting to ES2022 or later then we need to reassign the class alias before
// static initializers are evaluated.
const assignClassAliasInStaticBlock =
languageVersion >= ScriptTarget.ES2022 &&
!!classAlias &&
some(members, member =>
isPropertyDeclaration(member) && hasSyntacticModifier(member, ModifierFlags.Static) ||
isClassStaticBlockDeclaration(member));
if (assignClassAliasInStaticBlock) {
members = setTextRange(factory.createNodeArray([
factory.createClassStaticBlockDeclaration(
factory.createBlock([
factory.createExpressionStatement(
factory.createAssignment(classAlias, factory.createThis())
)
])
),
...members
]), members);
}

const classExpression = factory.createClassExpression(
modifiers,
name && isGeneratedIdentifier(name) ? undefined : name,
Expand All @@ -355,7 +377,7 @@ export function transformLegacyDecorators(context: TransformationContext): (x: S
declName,
/*exclamationToken*/ undefined,
/*type*/ undefined,
classAlias ? factory.createAssignment(classAlias, classExpression) : classExpression
classAlias && !assignClassAliasInStaticBlock ? factory.createAssignment(classAlias, classExpression) : classExpression
);
setOriginalNode(varDecl, node);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [staticInitializersAndLegacyClassDecorators.ts]
// https://github.com/microsoft/TypeScript/issues/52004
declare var dec: any;

@dec
class C1
{
static instance = new C1();
}

@dec
class C2
{
static {
new C2();
}
}


//// [staticInitializersAndLegacyClassDecorators.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var C1_1, C2_1;
let C1 = class C1 {
static { C1_1 = this; }
static instance = new C1_1();
};
C1 = C1_1 = __decorate([
dec
], C1);
let C2 = class C2 {
static { C2_1 = this; }
static {
new C2_1();
}
};
C2 = C2_1 = __decorate([
dec
], C2);
20 changes: 20 additions & 0 deletions tests/cases/compiler/staticInitializersAndLegacyClassDecorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @target: es2022
// @experimentalDecorators: true
// @noTypesAndSymbols: true

// https://github.com/microsoft/TypeScript/issues/52004
declare var dec: any;

@dec
class C1
{
static instance = new C1();
}

@dec
class C2
{
static {
new C2();
}
}