Skip to content

Commit 80ceca3

Browse files
committed
Added !PureAbstract flag handling
1 parent 5c70288 commit 80ceca3

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

dist/TSHelper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ var TSHelper = /** @class */ (function () {
3838
return type.symbol
3939
&& ((type.symbol.flags & ts.SymbolFlags.Enum) != 0)
4040
&& type.symbol.getDocumentationComment()[0] != undefined
41-
&& (type.symbol.getDocumentationComment()[0].text.trim() == "CompileMembersOnly");
41+
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!CompileMembersOnly");
42+
};
43+
TSHelper.isPureAbstractClass = function (type) {
44+
return type.symbol
45+
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
46+
&& type.symbol.getDocumentationComment()[0] != undefined
47+
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!PureAbstract");
4248
};
4349
return TSHelper;
4450
}());

dist/Transpiler.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ var LuaTranspiler = /** @class */ (function () {
407407
};
408408
LuaTranspiler.prototype.transpileNewExpression = function (node) {
409409
var name = this.transpileExpression(node.expression);
410-
var params = this.transpileArguments(node.arguments);
410+
var params = this.transpileArguments(node.arguments, ts.createTrue());
411411
return name + ".new(" + params + ")";
412412
};
413413
LuaTranspiler.prototype.transpileCallExpression = function (node) {
@@ -431,7 +431,7 @@ var LuaTranspiler = /** @class */ (function () {
431431
if (node.expression.kind == ts.SyntaxKind.SuperKeyword) {
432432
var callPath_2 = this.transpileExpression(node.expression);
433433
var params_2 = this.transpileArguments(node.arguments, ts.createNode(ts.SyntaxKind.ThisKeyword));
434-
return "$self.__base.constructor(" + params_2 + ")";
434+
return "self.__base.constructor(" + params_2 + ")";
435435
}
436436
var callPath = this.transpileExpression(node.expression);
437437
var params = this.transpileArguments(node.arguments);
@@ -579,20 +579,36 @@ var LuaTranspiler = /** @class */ (function () {
579579
// Transpile a class declaration
580580
LuaTranspiler.prototype.transpileClass = function (node) {
581581
var _this = this;
582-
// Figure out inheritance
583-
var isExtension = node.heritageClauses && node.heritageClauses.length > 0;
584-
var baseName = "";
585-
if (isExtension)
586-
baseName = node.heritageClauses[0].types[0].expression.escapedText;
582+
// Find extends class, ignore implements
583+
var extendsType;
584+
if (node.heritageClauses)
585+
node.heritageClauses.forEach(function (clause) {
586+
if (clause.token == ts.SyntaxKind.ExtendsKeyword) {
587+
var superType = clause.types[0];
588+
// Ignore purely abstract types (decorated with /** @PureAbstract */)
589+
if (!TSHelper_1.TSHelper.isPureAbstractClass(_this.checker.getTypeAtLocation(superType))) {
590+
extendsType = clause.types[0];
591+
}
592+
}
593+
});
587594
// Write class declaration
588595
var className = node.name.escapedText;
589-
var result = this.indent + (className + " = " + className + " or {}\n");
596+
var result = "";
597+
if (!extendsType) {
598+
result += this.indent + (className + " = " + className + " or {}\n");
599+
}
600+
else {
601+
var baseName = extendsType.expression.escapedText;
602+
result += this.indent + (className + " = " + className + " or " + baseName + ".new()\n");
603+
}
590604
result += this.indent + (className + ".__index = " + className + "\n");
591-
if (isExtension)
605+
if (extendsType) {
606+
var baseName = extendsType.expression.escapedText;
592607
result += this.indent + (className + ".__base = " + baseName + "\n");
593-
result += this.indent + ("function " + className + ".new(...)\n");
608+
}
609+
result += this.indent + ("function " + className + ".new(construct, ...)\n");
594610
result += this.indent + (" local instance = setmetatable({}, " + className + ")\n");
595-
result += this.indent + (" if " + className + ".constructor then " + className + ".constructor(instance, ...) end\n");
611+
result += this.indent + (" if construct and " + className + ".constructor then " + className + ".constructor(instance, ...) end\n");
596612
result += this.indent + " return instance\n";
597613
result += this.indent + "end\n";
598614
// Get all properties with value

src/TSHelper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ export class TSHelper {
4141
return type.symbol
4242
&& ((type.symbol.flags & ts.SymbolFlags.Enum) != 0)
4343
&& type.symbol.getDocumentationComment()[0] != undefined
44-
&& (type.symbol.getDocumentationComment()[0].text.trim() == "CompileMembersOnly");
44+
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!CompileMembersOnly");
45+
}
46+
47+
static isPureAbstractClass(type: ts.Type) {
48+
return type.symbol
49+
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
50+
&& type.symbol.getDocumentationComment()[0] != undefined
51+
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!PureAbstract");
4552
}
4653
}

src/Transpiler.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ export class LuaTranspiler {
447447

448448
transpileNewExpression(node: ts.NewExpression): string {
449449
const name = this.transpileExpression(node.expression);
450-
const params = this.transpileArguments(node.arguments);
450+
const params = this.transpileArguments(node.arguments, ts.createTrue());
451451

452452
return `${name}.new(${params})`;
453453
}
@@ -475,7 +475,7 @@ export class LuaTranspiler {
475475
if (node.expression.kind == ts.SyntaxKind.SuperKeyword) {
476476
let callPath = this.transpileExpression(node.expression);
477477
const params = this.transpileArguments(node.arguments, <ts.Expression>ts.createNode(ts.SyntaxKind.ThisKeyword));
478-
return `$self.__base.constructor(${params})`;
478+
return `self.__base.constructor(${params})`;
479479
}
480480

481481
let callPath = this.transpileExpression(node.expression);
@@ -653,21 +653,35 @@ export class LuaTranspiler {
653653

654654
// Transpile a class declaration
655655
transpileClass(node: ts.ClassDeclaration): string {
656-
// Figure out inheritance
657-
const isExtension = node.heritageClauses && node.heritageClauses.length > 0;
658-
let baseName = "";
659-
660-
if (isExtension)
661-
baseName = <string>(<ts.Identifier>node.heritageClauses[0].types[0].expression).escapedText;
656+
// Find extends class, ignore implements
657+
let extendsType;
658+
if (node.heritageClauses) node.heritageClauses.forEach(clause => {
659+
if (clause.token == ts.SyntaxKind.ExtendsKeyword) {
660+
const superType = clause.types[0];
661+
// Ignore purely abstract types (decorated with /** @PureAbstract */)
662+
if (!tsEx.isPureAbstractClass(this.checker.getTypeAtLocation(superType))) {
663+
extendsType = clause.types[0];
664+
}
665+
}
666+
});
662667

663668
// Write class declaration
664669
const className = <string>node.name.escapedText;
665-
let result = this.indent + `${className} = ${className} or {}\n`;
670+
let result = "";
671+
if (!extendsType) {
672+
result += this.indent + `${className} = ${className} or {}\n`;
673+
} else {
674+
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
675+
result += this.indent + `${className} = ${className} or ${baseName}.new()\n`
676+
}
666677
result += this.indent + `${className}.__index = ${className}\n`;
667-
if (isExtension) result += this.indent + `${className}.__base = ${baseName}\n`
668-
result += this.indent + `function ${className}.new(...)\n`;
678+
if (extendsType) {
679+
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
680+
result += this.indent + `${className}.__base = ${baseName}\n`;
681+
}
682+
result += this.indent + `function ${className}.new(construct, ...)\n`;
669683
result += this.indent + ` local instance = setmetatable({}, ${className})\n`;
670-
result += this.indent + ` if ${className}.constructor then ${className}.constructor(instance, ...) end\n`;
684+
result += this.indent + ` if construct and ${className}.constructor then ${className}.constructor(instance, ...) end\n`;
671685
result += this.indent + ` return instance\n`;
672686
result += this.indent + `end\n`;
673687

0 commit comments

Comments
 (0)