Skip to content

Commit 9a8b1ba

Browse files
committed
Added !NoClassOr flag
1 parent 24da494 commit 9a8b1ba

4 files changed

Lines changed: 37 additions & 16 deletions

File tree

dist/TSHelper.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ var TSHelper = /** @class */ (function () {
4444
return type.symbol
4545
&& ((type.symbol.flags & ts.SymbolFlags.Enum) != 0)
4646
&& type.symbol.getDocumentationComment()[0] != undefined
47-
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!CompileMembersOnly");
47+
&& this.hasCustomDecorator(type, "!CompileMembersOnly");
4848
};
4949
TSHelper.isPureAbstractClass = function (type) {
5050
return type.symbol
5151
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
52-
&& type.symbol.getDocumentationComment()[0] != undefined
53-
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!PureAbstract");
52+
&& this.hasCustomDecorator(type, "!PureAbstract");
53+
};
54+
TSHelper.hasCustomDecorator = function (type, decorator) {
55+
if (type.symbol) {
56+
var comment = type.symbol.getDocumentationComment();
57+
var decorators = comment.filter(function (_) { return _.kind == "text"; }).map(function (_) { return _.text.trim(); }).filter(function (_) { return _[0] == "!"; });
58+
return decorators.indexOf(decorator) > -1;
59+
}
60+
return false;
5461
};
5562
return TSHelper;
5663
}());

dist/Transpiler.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,25 +652,28 @@ var LuaTranspiler = /** @class */ (function () {
652652
var _this = this;
653653
// Find extends class, ignore implements
654654
var extendsType;
655+
var noClassOr = false;
655656
if (node.heritageClauses)
656657
node.heritageClauses.forEach(function (clause) {
657658
if (clause.token == ts.SyntaxKind.ExtendsKeyword) {
658-
var superType = clause.types[0];
659+
var superType = _this.checker.getTypeAtLocation(clause.types[0]);
659660
// Ignore purely abstract types (decorated with /** @PureAbstract */)
660-
if (!TSHelper_1.TSHelper.isPureAbstractClass(_this.checker.getTypeAtLocation(superType))) {
661+
if (!TSHelper_1.TSHelper.isPureAbstractClass(superType)) {
661662
extendsType = clause.types[0];
662663
}
664+
noClassOr = TSHelper_1.TSHelper.hasCustomDecorator(superType, "!NoClassOr");
663665
}
664666
});
665667
// Write class declaration
666668
var className = node.name.escapedText;
669+
var classOr = noClassOr ? "" : className + " or ";
667670
var result = "";
668671
if (!extendsType) {
669-
result += this.indent + (className + " = " + className + " or {}\n");
672+
result += this.indent + (className + " = " + classOr + "{}\n");
670673
}
671674
else {
672675
var baseName = extendsType.expression.escapedText;
673-
result += this.indent + (className + " = " + className + " or " + baseName + ".new()\n");
676+
result += this.indent + (className + " = " + classOr + baseName + ".new()\n");
674677
}
675678
result += this.indent + (className + ".__index = " + className + "\n");
676679
if (extendsType) {

src/TSHelper.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,25 @@ export class TSHelper {
4444
&& (<ts.TypeReference>type).typeArguments != undefined;
4545
}
4646

47-
static isCompileMembersOnlyEnum(type: ts.Type) {
47+
static isCompileMembersOnlyEnum(type: ts.Type): boolean {
4848
return type.symbol
4949
&& ((type.symbol.flags & ts.SymbolFlags.Enum) != 0)
5050
&& type.symbol.getDocumentationComment()[0] != undefined
51-
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!CompileMembersOnly");
51+
&& this.hasCustomDecorator(type, "!CompileMembersOnly");
5252
}
5353

54-
static isPureAbstractClass(type: ts.Type) {
54+
static isPureAbstractClass(type: ts.Type): boolean {
5555
return type.symbol
5656
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
57-
&& type.symbol.getDocumentationComment()[0] != undefined
58-
&& (type.symbol.getDocumentationComment()[0].text.trim() == "!PureAbstract");
57+
&& this.hasCustomDecorator(type, "!PureAbstract");
58+
}
59+
60+
static hasCustomDecorator(type: ts.Type, decorator: string): boolean {
61+
if (type.symbol) {
62+
var comment = type.symbol.getDocumentationComment();
63+
var decorators = comment.filter(_ => _.kind == "text").map(_ => _.text.trim()).filter(_ => _[0] == "!");
64+
return decorators.indexOf(decorator) > -1;
65+
}
66+
return false;
5967
}
6068
}

src/Transpiler.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,24 +718,27 @@ export class LuaTranspiler {
718718
transpileClass(node: ts.ClassDeclaration): string {
719719
// Find extends class, ignore implements
720720
let extendsType;
721+
let noClassOr = false;
721722
if (node.heritageClauses) node.heritageClauses.forEach(clause => {
722723
if (clause.token == ts.SyntaxKind.ExtendsKeyword) {
723-
const superType = clause.types[0];
724+
const superType = this.checker.getTypeAtLocation(clause.types[0]);
724725
// Ignore purely abstract types (decorated with /** @PureAbstract */)
725-
if (!tsEx.isPureAbstractClass(this.checker.getTypeAtLocation(superType))) {
726+
if (!tsEx.isPureAbstractClass(superType)) {
726727
extendsType = clause.types[0];
727728
}
729+
noClassOr = tsEx.hasCustomDecorator(superType, "!NoClassOr");
728730
}
729731
});
730732

731733
// Write class declaration
732734
const className = <string>node.name.escapedText;
735+
const classOr = noClassOr ? "" : `${className} or `;
733736
let result = "";
734737
if (!extendsType) {
735-
result += this.indent + `${className} = ${className} or {}\n`;
738+
result += this.indent + `${className} = ${classOr}{}\n`;
736739
} else {
737740
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
738-
result += this.indent + `${className} = ${className} or ${baseName}.new()\n`
741+
result += this.indent + `${className} = ${classOr}${baseName}.new()\n`
739742
}
740743
result += this.indent + `${className}.__index = ${className}\n`;
741744
if (extendsType) {

0 commit comments

Comments
 (0)