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
14 changes: 12 additions & 2 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ export class LuaTransformer {
);
}

// className.name = className
result.push(
tstl.createAssignmentStatement(
tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("name")),
tstl.createStringLiteral(className.text)
)
);

// className.____getters = {}
if (statement.members.some(m => ts.isGetAccessor(m) && tsHelper.isStatic(m))) {
const classGetters = tstl.createTableIndexExpression(
Expand Down Expand Up @@ -2751,8 +2759,10 @@ export class LuaTransformer {
}

public transformClassExpression(expression: ts.ClassExpression): ExpressionVisitResult {
const className = tstl.createAnnonymousIdentifier();
const classDeclaration = this.transformClassDeclaration(expression as ts.ClassExpression, className);
const className = expression.name !== undefined
? this.transformIdentifier(expression.name)
: tstl.createAnnonymousIdentifier();
const classDeclaration = this.transformClassDeclaration(expression, className);
return this.createImmediatelyInvokedFunctionExpression(classDeclaration, className, expression);
}

Expand Down
6 changes: 6 additions & 0 deletions test/translation/__snapshots__/transformation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ end"

exports[`Transformation (classPureAbstract) 1`] = `
"ClassB = {}
ClassB.name = \\"ClassB\\"
ClassB.__index = ClassB
ClassB.prototype = {}
ClassB.prototype.__index = ClassB.prototype
Expand Down Expand Up @@ -263,6 +264,7 @@ end"
exports[`Transformation (getSetAccessors) 1`] = `
"require(\\"lualib_bundle\\");
MyClass = {}
MyClass.name = \\"MyClass\\"
MyClass.__index = MyClass
MyClass.prototype = {}
MyClass.prototype.____getters = {}
Expand Down Expand Up @@ -296,6 +298,7 @@ a.abc = \\"def\\""

exports[`Transformation (methodRestArguments) 1`] = `
"MyClass = {}
MyClass.name = \\"MyClass\\"
MyClass.__index = MyClass
MyClass.prototype = {}
MyClass.prototype.__index = MyClass.prototype
Expand All @@ -322,6 +325,7 @@ exports[`Transformation (modulesClassExport) 1`] = `
"local ____exports = {}
____exports.TestClass = {}
local TestClass = ____exports.TestClass
TestClass.name = \\"TestClass\\"
TestClass.__index = TestClass
TestClass.prototype = {}
TestClass.prototype.__index = TestClass.prototype
Expand All @@ -340,6 +344,7 @@ exports[`Transformation (modulesClassWithMemberExport) 1`] = `
"local ____exports = {}
____exports.TestClass = {}
local TestClass = ____exports.TestClass
TestClass.name = \\"TestClass\\"
TestClass.__index = TestClass
TestClass.prototype = {}
TestClass.prototype.__index = TestClass.prototype
Expand Down Expand Up @@ -484,6 +489,7 @@ end"

exports[`Transformation (namespaceMerge) 1`] = `
"MergedClass = {}
MergedClass.name = \\"MergedClass\\"
MergedClass.__index = MergedClass
MergedClass.prototype = {}
MergedClass.prototype.__index = MergedClass.prototype
Expand Down
51 changes: 51 additions & 0 deletions test/unit/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,54 @@ test("Class static instance of self", () => {
`;
expect(util.transpileAndExecute(code)).toBe("foobar");
});

test("Class name", () => {
const code = `
class Foo {}
return Foo.name;
`;
expect(util.transpileAndExecute(code)).toBe("Foo");
});

test("Class name via constructor", () => {
const code = `
class Foo {}
const foo = new Foo();
return foo.constructor.name;
`;
expect(util.transpileAndExecute(code)).toBe("Foo");
});

test("Class expression name", () => {
const code = `
const foo = class Foo {};
return foo.name;
`;
expect(util.transpileAndExecute(code)).toBe("Foo");
});

test("Class expression name via constructor", () => {
const code = `
const foo = class Foo {};
const bar = new foo();
return bar.constructor.name;
`;
expect(util.transpileAndExecute(code)).toBe("Foo");
});

test("Class annonymous expression name", () => {
const code = `
const foo = class {};
return foo.name;
`;
expect(util.transpileAndExecute(code)).toBe("____");
});

test("Class annonymous expression name via constructor", () => {
const code = `
const foo = class {};
const bar = new foo();
return bar.constructor.name;
`;
expect(util.transpileAndExecute(code)).toBe("____");
});