Skip to content

Commit 3df8dfb

Browse files
authored
class name support (#520)
-name property accessable on classes and instance constructor property -now using name of class expressions for variable and name property, if it's specified
1 parent dd7e5de commit 3df8dfb

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/LuaTransformer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,14 @@ export class LuaTransformer {
613613
);
614614
}
615615

616+
// className.name = className
617+
result.push(
618+
tstl.createAssignmentStatement(
619+
tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("name")),
620+
tstl.createStringLiteral(className.text)
621+
)
622+
);
623+
616624
// className.____getters = {}
617625
if (statement.members.some(m => ts.isGetAccessor(m) && tsHelper.isStatic(m))) {
618626
const classGetters = tstl.createTableIndexExpression(
@@ -2751,8 +2759,10 @@ export class LuaTransformer {
27512759
}
27522760

27532761
public transformClassExpression(expression: ts.ClassExpression): ExpressionVisitResult {
2754-
const className = tstl.createAnnonymousIdentifier();
2755-
const classDeclaration = this.transformClassDeclaration(expression as ts.ClassExpression, className);
2762+
const className = expression.name !== undefined
2763+
? this.transformIdentifier(expression.name)
2764+
: tstl.createAnnonymousIdentifier();
2765+
const classDeclaration = this.transformClassDeclaration(expression, className);
27562766
return this.createImmediatelyInvokedFunctionExpression(classDeclaration, className, expression);
27572767
}
27582768

test/translation/__snapshots__/transformation.spec.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ end"
4343

4444
exports[`Transformation (classPureAbstract) 1`] = `
4545
"ClassB = {}
46+
ClassB.name = \\"ClassB\\"
4647
ClassB.__index = ClassB
4748
ClassB.prototype = {}
4849
ClassB.prototype.__index = ClassB.prototype
@@ -263,6 +264,7 @@ end"
263264
exports[`Transformation (getSetAccessors) 1`] = `
264265
"require(\\"lualib_bundle\\");
265266
MyClass = {}
267+
MyClass.name = \\"MyClass\\"
266268
MyClass.__index = MyClass
267269
MyClass.prototype = {}
268270
MyClass.prototype.____getters = {}
@@ -296,6 +298,7 @@ a.abc = \\"def\\""
296298

297299
exports[`Transformation (methodRestArguments) 1`] = `
298300
"MyClass = {}
301+
MyClass.name = \\"MyClass\\"
299302
MyClass.__index = MyClass
300303
MyClass.prototype = {}
301304
MyClass.prototype.__index = MyClass.prototype
@@ -322,6 +325,7 @@ exports[`Transformation (modulesClassExport) 1`] = `
322325
"local ____exports = {}
323326
____exports.TestClass = {}
324327
local TestClass = ____exports.TestClass
328+
TestClass.name = \\"TestClass\\"
325329
TestClass.__index = TestClass
326330
TestClass.prototype = {}
327331
TestClass.prototype.__index = TestClass.prototype
@@ -340,6 +344,7 @@ exports[`Transformation (modulesClassWithMemberExport) 1`] = `
340344
"local ____exports = {}
341345
____exports.TestClass = {}
342346
local TestClass = ____exports.TestClass
347+
TestClass.name = \\"TestClass\\"
343348
TestClass.__index = TestClass
344349
TestClass.prototype = {}
345350
TestClass.prototype.__index = TestClass.prototype
@@ -484,6 +489,7 @@ end"
484489

485490
exports[`Transformation (namespaceMerge) 1`] = `
486491
"MergedClass = {}
492+
MergedClass.name = \\"MergedClass\\"
487493
MergedClass.__index = MergedClass
488494
MergedClass.prototype = {}
489495
MergedClass.prototype.__index = MergedClass.prototype

test/unit/class.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,54 @@ test("Class static instance of self", () => {
746746
`;
747747
expect(util.transpileAndExecute(code)).toBe("foobar");
748748
});
749+
750+
test("Class name", () => {
751+
const code = `
752+
class Foo {}
753+
return Foo.name;
754+
`;
755+
expect(util.transpileAndExecute(code)).toBe("Foo");
756+
});
757+
758+
test("Class name via constructor", () => {
759+
const code = `
760+
class Foo {}
761+
const foo = new Foo();
762+
return foo.constructor.name;
763+
`;
764+
expect(util.transpileAndExecute(code)).toBe("Foo");
765+
});
766+
767+
test("Class expression name", () => {
768+
const code = `
769+
const foo = class Foo {};
770+
return foo.name;
771+
`;
772+
expect(util.transpileAndExecute(code)).toBe("Foo");
773+
});
774+
775+
test("Class expression name via constructor", () => {
776+
const code = `
777+
const foo = class Foo {};
778+
const bar = new foo();
779+
return bar.constructor.name;
780+
`;
781+
expect(util.transpileAndExecute(code)).toBe("Foo");
782+
});
783+
784+
test("Class annonymous expression name", () => {
785+
const code = `
786+
const foo = class {};
787+
return foo.name;
788+
`;
789+
expect(util.transpileAndExecute(code)).toBe("____");
790+
});
791+
792+
test("Class annonymous expression name via constructor", () => {
793+
const code = `
794+
const foo = class {};
795+
const bar = new foo();
796+
return bar.constructor.name;
797+
`;
798+
expect(util.transpileAndExecute(code)).toBe("____");
799+
});

0 commit comments

Comments
 (0)