Skip to content

Commit a2ee2f7

Browse files
authored
Add support for class static initialization blocks (#1451)
* Tests for new decorators * Rename old decorate function to DecorateLegacy * Method decorators and partially accessor decorators rewritten * Modern decorators except field decorators working but with some regression to legacy * All decorators legacy and modern work, except for field decorators * Finalize property decorators * Fix unit tests * Add support for class static initialization blocks
1 parent ecf6d08 commit a2ee2f7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ function transformClassLikeDeclaration(
186186
lua.createExpressionStatement(createClassPropertyDecoratingExpression(context, member, className))
187187
);
188188
}
189+
} else if (ts.isClassStaticBlockDeclaration(member)) {
190+
if (member.body.statements.length > 0) {
191+
const bodyStatements = context.transformStatements(member.body.statements);
192+
const iif = lua.createFunctionExpression(lua.createBlock(bodyStatements), [
193+
lua.createIdentifier("self"),
194+
]);
195+
const iife = lua.createCallExpression(iif, [className]);
196+
result.push(lua.createExpressionStatement(iife, member));
197+
}
189198
}
190199
}
191200

test/unit/classes/classes.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,22 @@ test.each(['(this["bar"])', '((((this["bar"]))))'])("methods in parentheses pass
799799
export const result = inst.foo();
800800
`.expectToMatchJsResult();
801801
});
802+
803+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1447
804+
test("static initialization block (#1447)", () => {
805+
util.testModule`
806+
class A {
807+
private static staticProperty1 = 3;
808+
public static staticProperty2;
809+
static {
810+
this.staticProperty2 = this.staticProperty1 + 5;
811+
}
812+
public static staticProperty3;
813+
static {
814+
this.staticProperty3 = this.staticProperty1 + this.staticProperty2;
815+
}
816+
}
817+
export const result1 = A.staticProperty2;
818+
export const result2 = A.staticProperty3;
819+
`.expectToMatchJsResult();
820+
});

0 commit comments

Comments
 (0)