Skip to content

Commit 0bbae3c

Browse files
tomblindPerryvw
authored andcommitted
moved static field initialization to end of class declaration (#473)
1 parent 73b13b6 commit 0bbae3c

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/LuaTransformer.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -494,24 +494,6 @@ export class LuaTransformer {
494494
}
495495
}
496496

497-
// Add static declarations
498-
for (const field of staticFields) {
499-
const fieldName = this.transformPropertyName(field.name);
500-
const value = this.transformExpression(field.initializer);
501-
502-
const classField = tstl.createTableIndexExpression(
503-
this.addExportToIdentifier(tstl.cloneIdentifier(className)),
504-
fieldName
505-
);
506-
507-
const fieldAssign = tstl.createAssignmentStatement(
508-
classField,
509-
value
510-
);
511-
512-
result.push(fieldAssign);
513-
}
514-
515497
// Find first constructor with body
516498
if (!isExtension && !isMetaExtension) {
517499
const constructor = statement.members
@@ -573,6 +555,24 @@ export class LuaTransformer {
573555
result.push(this.transformMethodDeclaration(method, className, isExtension || isMetaExtension));
574556
});
575557

558+
// Add static declarations
559+
for (const field of staticFields) {
560+
const fieldName = this.transformPropertyName(field.name);
561+
const value = this.transformExpression(field.initializer);
562+
563+
const classField = tstl.createTableIndexExpression(
564+
this.addExportToIdentifier(tstl.cloneIdentifier(className)),
565+
fieldName
566+
);
567+
568+
const fieldAssign = tstl.createAssignmentStatement(
569+
classField,
570+
value
571+
);
572+
573+
result.push(fieldAssign);
574+
}
575+
576576
this.classStack.pop();
577577

578578
return result;

test/unit/class.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,4 +805,15 @@ export class ClassTests {
805805
"Cannot construct classes with decorator '@extension' or '@metaExtension'."
806806
);
807807
}
808+
809+
@Test("Class static instance of self")
810+
public classStaticInstanceOfSelf(): void {
811+
const code =
812+
`class Foo {
813+
bar = "foobar";
814+
static instance = new Foo();
815+
}
816+
return Foo.instance.bar;`;
817+
Expect(util.transpileAndExecute(code)).toBe("foobar");
818+
}
808819
}

0 commit comments

Comments
 (0)