Skip to content
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
36 changes: 18 additions & 18 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,24 +494,6 @@ export class LuaTransformer {
}
}

// Add static declarations
for (const field of staticFields) {
const fieldName = this.transformPropertyName(field.name);
const value = this.transformExpression(field.initializer);

const classField = tstl.createTableIndexExpression(
this.addExportToIdentifier(tstl.cloneIdentifier(className)),
fieldName
);

const fieldAssign = tstl.createAssignmentStatement(
classField,
value
);

result.push(fieldAssign);
}

// Find first constructor with body
if (!isExtension && !isMetaExtension) {
const constructor = statement.members
Expand Down Expand Up @@ -573,6 +555,24 @@ export class LuaTransformer {
result.push(this.transformMethodDeclaration(method, className, isExtension || isMetaExtension));
});

// Add static declarations
for (const field of staticFields) {
const fieldName = this.transformPropertyName(field.name);
const value = this.transformExpression(field.initializer);

const classField = tstl.createTableIndexExpression(
this.addExportToIdentifier(tstl.cloneIdentifier(className)),
fieldName
);

const fieldAssign = tstl.createAssignmentStatement(
classField,
value
);

result.push(fieldAssign);
}

this.classStack.pop();

return result;
Expand Down
11 changes: 11 additions & 0 deletions test/unit/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,15 @@ export class ClassTests {
"Cannot construct classes with decorator '@extension' or '@metaExtension'."
);
}

@Test("Class static instance of self")
public classStaticInstanceOfSelf(): void {
const code =
`class Foo {
bar = "foobar";
static instance = new Foo();
}
return Foo.instance.bar;`;
Expect(util.transpileAndExecute(code)).toBe("foobar");
}
}