Skip to content

Commit c964803

Browse files
committed
Added !Extension decorator
1 parent bafbf92 commit c964803

4 files changed

Lines changed: 65 additions & 34 deletions

File tree

dist/TSHelper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ var TSHelper = /** @class */ (function () {
5151
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
5252
&& this.hasCustomDecorator(type, "!PureAbstract");
5353
};
54+
TSHelper.isExtensionClass = function (type) {
55+
return type.symbol
56+
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
57+
&& this.hasCustomDecorator(type, "!Extension");
58+
};
5459
TSHelper.hasCustomDecorator = function (type, decorator) {
5560
if (type.symbol) {
5661
var comment = type.symbol.getDocumentationComment();

dist/Transpiler.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -666,27 +666,37 @@ var LuaTranspiler = /** @class */ (function () {
666666
noClassOr = TSHelper_1.TSHelper.hasCustomDecorator(superType, "!NoClassOr");
667667
}
668668
});
669-
// Write class declaration
670669
var className = node.name.escapedText;
671-
var classOr = noClassOr ? "" : className + " or ";
672670
var result = "";
673-
if (!extendsType) {
674-
result += this.indent + (className + " = " + classOr + "{}\n");
671+
// Skip header if this is an extension class
672+
var isExtension = TSHelper_1.TSHelper.isExtensionClass(this.checker.getTypeAtLocation(node));
673+
if (!isExtension) {
674+
// Write class declaration
675+
var classOr = noClassOr ? "" : className + " or ";
676+
if (!extendsType) {
677+
result += this.indent + (className + " = " + classOr + "{}\n");
678+
}
679+
else {
680+
var baseName = extendsType.expression.escapedText;
681+
result += this.indent + (className + " = " + classOr + baseName + ".new()\n");
682+
}
683+
result += this.indent + (className + ".__index = " + className + "\n");
684+
if (extendsType) {
685+
var baseName = extendsType.expression.escapedText;
686+
result += this.indent + (className + ".__base = " + baseName + "\n");
687+
}
688+
result += this.indent + ("function " + className + ".new(construct, ...)\n");
689+
result += this.indent + (" local instance = setmetatable({}, " + className + ")\n");
690+
result += this.indent + (" if construct and " + className + ".constructor then " + className + ".constructor(instance, ...) end\n");
691+
result += this.indent + " return instance\n";
692+
result += this.indent + "end\n";
675693
}
676694
else {
677-
var baseName = extendsType.expression.escapedText;
678-
result += this.indent + (className + " = " + classOr + baseName + ".new()\n");
679-
}
680-
result += this.indent + (className + ".__index = " + className + "\n");
681-
if (extendsType) {
682-
var baseName = extendsType.expression.escapedText;
683-
result += this.indent + (className + ".__base = " + baseName + "\n");
684-
}
685-
result += this.indent + ("function " + className + ".new(construct, ...)\n");
686-
result += this.indent + (" local instance = setmetatable({}, " + className + ")\n");
687-
result += this.indent + (" if construct and " + className + ".constructor then " + className + ".constructor(instance, ...) end\n");
688-
result += this.indent + " return instance\n";
689-
result += this.indent + "end\n";
695+
// Overwrite the original className with the class we are overriding for extensions
696+
if (extendsType) {
697+
className = extendsType.expression.escapedText;
698+
}
699+
}
690700
// Get all properties with value
691701
var properties = node.members.filter(ts.isPropertyDeclaration)
692702
.filter(function (_) { return _.initializer; });

src/TSHelper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export class TSHelper {
5757
&& this.hasCustomDecorator(type, "!PureAbstract");
5858
}
5959

60+
static isExtensionClass(type: ts.Type): boolean {
61+
return type.symbol
62+
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
63+
&& this.hasCustomDecorator(type, "!Extension");
64+
}
65+
6066
static hasCustomDecorator(type: ts.Type, decorator: string): boolean {
6167
if (type.symbol) {
6268
var comment = type.symbol.getDocumentationComment();

src/Transpiler.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -732,26 +732,36 @@ export class LuaTranspiler {
732732
}
733733
});
734734

735-
// Write class declaration
736-
const className = <string>node.name.escapedText;
737-
const classOr = noClassOr ? "" : `${className} or `;
735+
let className = <string>node.name.escapedText;
738736
let result = "";
739-
if (!extendsType) {
740-
result += this.indent + `${className} = ${classOr}{}\n`;
741-
} else {
742-
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
743-
result += this.indent + `${className} = ${classOr}${baseName}.new()\n`
744-
}
745-
result += this.indent + `${className}.__index = ${className}\n`;
746-
if (extendsType) {
737+
738+
// Skip header if this is an extension class
739+
var isExtension = tsEx.isExtensionClass(this.checker.getTypeAtLocation(node));
740+
if (!isExtension) {
741+
// Write class declaration
742+
const classOr = noClassOr ? "" : `${className} or `;
743+
if (!extendsType) {
744+
result += this.indent + `${className} = ${classOr}{}\n`;
745+
} else {
747746
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
748-
result += this.indent + `${className}.__base = ${baseName}\n`;
747+
result += this.indent + `${className} = ${classOr}${baseName}.new()\n`
748+
}
749+
result += this.indent + `${className}.__index = ${className}\n`;
750+
if (extendsType) {
751+
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
752+
result += this.indent + `${className}.__base = ${baseName}\n`;
753+
}
754+
result += this.indent + `function ${className}.new(construct, ...)\n`;
755+
result += this.indent + ` local instance = setmetatable({}, ${className})\n`;
756+
result += this.indent + ` if construct and ${className}.constructor then ${className}.constructor(instance, ...) end\n`;
757+
result += this.indent + ` return instance\n`;
758+
result += this.indent + `end\n`;
759+
} else {
760+
// Overwrite the original className with the class we are overriding for extensions
761+
if (extendsType) {
762+
className = <string>(<ts.Identifier>extendsType.expression).escapedText;
763+
}
749764
}
750-
result += this.indent + `function ${className}.new(construct, ...)\n`;
751-
result += this.indent + ` local instance = setmetatable({}, ${className})\n`;
752-
result += this.indent + ` if construct and ${className}.constructor then ${className}.constructor(instance, ...) end\n`;
753-
result += this.indent + ` return instance\n`;
754-
result += this.indent + `end\n`;
755765

756766
// Get all properties with value
757767
const properties = node.members.filter(ts.isPropertyDeclaration)

0 commit comments

Comments
 (0)