Skip to content

Commit 0e5df55

Browse files
authored
Change class initialization order (#1458)
1 parent 308d9d8 commit 0e5df55

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ function transformClassLikeDeclaration(
160160
}
161161

162162
// Transform class members
163+
164+
// First transform the methods, in case the static properties call them
165+
for (const member of classDeclaration.members) {
166+
if (ts.isMethodDeclaration(member)) {
167+
// Methods
168+
const statements = transformMethodDeclaration(context, member, localClassName);
169+
result.push(...statements);
170+
}
171+
}
172+
173+
// Then transform the rest
163174
for (const member of classDeclaration.members) {
164175
if (ts.isAccessor(member)) {
165176
// Accessors
@@ -170,10 +181,6 @@ function transformClassLikeDeclaration(
170181
if (accessorsResult) {
171182
result.push(accessorsResult);
172183
}
173-
} else if (ts.isMethodDeclaration(member)) {
174-
// Methods
175-
const statements = transformMethodDeclaration(context, member, localClassName);
176-
result.push(...statements);
177184
} else if (ts.isPropertyDeclaration(member)) {
178185
// Properties
179186
if (isStaticNode(member)) {

test/unit/classes/classes.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,18 @@ test("static initialization block (#1447)", () => {
818818
export const result2 = A.staticProperty3;
819819
`.expectToMatchJsResult();
820820
});
821+
822+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1457
823+
test("static member definition order (#1457)", () => {
824+
util.testFunction`
825+
class A {
826+
static a = A.foo()
827+
828+
static foo(): number {
829+
return 5
830+
}
831+
}
832+
833+
return A.a;
834+
`.expectToMatchJsResult();
835+
});

0 commit comments

Comments
 (0)