From a1907336518368e72eecd8b6eaa81525ec95e3de Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Fri, 18 Jan 2019 12:12:39 +0100 Subject: [PATCH 1/5] transpile type node instead of using typename --- src/TSHelper.ts | 10 ++++++++-- src/Transpiler.ts | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/TSHelper.ts b/src/TSHelper.ts index 1259ab423..fe8f642ca 100644 --- a/src/TSHelper.ts +++ b/src/TSHelper.ts @@ -54,14 +54,15 @@ export class TSHelper { return result; } - public static getExtendedType(node: ts.ClassLikeDeclarationBase, checker: ts.TypeChecker): ts.Type | undefined { + public static getExtendedTypeNode(node: ts.ClassLikeDeclarationBase, checker: ts.TypeChecker): + ts.ExpressionWithTypeArguments | undefined { if (node && node.heritageClauses) { for (const clause of node.heritageClauses) { if (clause.token === ts.SyntaxKind.ExtendsKeyword) { const superType = checker.getTypeAtLocation(clause.types[0]); const decorators = this.getCustomDecorators(superType, checker); if (!decorators.has(DecoratorKind.PureAbstract)) { - return superType; + return clause.types[0]; } } } @@ -69,6 +70,11 @@ export class TSHelper { return undefined; } + public static getExtendedType(node: ts.ClassLikeDeclarationBase, checker: ts.TypeChecker): ts.Type | undefined { + const extendedTypeNode = this.getExtendedTypeNode(node, checker); + return extendedTypeNode && checker.getTypeAtLocation(extendedTypeNode); + } + public static isFileModule(sourceFile: ts.SourceFile): boolean { if (sourceFile) { // Vanilla ts flags files as external module if they have an import or diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 39289c37e..e25ec7b3a 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -2128,7 +2128,8 @@ export abstract class LuaTranspiler { result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}{}\n`; this.pushExport(className, node); } else { - const baseName = extendsType.symbol.escapedName; + const extendedTypeNode = tsHelper.getExtendedTypeNode(node,this.checker); + const baseName = this.transpileNode(extendedTypeNode.expression); result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}${baseName}.new()\n`; this.pushExport(className, node); } From 4163f0ee777c0af30adeb670d6ece619e59f49af Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Fri, 18 Jan 2019 12:13:21 +0100 Subject: [PATCH 2/5] lint --- src/Transpiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index e25ec7b3a..1451f5e59 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -2128,7 +2128,7 @@ export abstract class LuaTranspiler { result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}{}\n`; this.pushExport(className, node); } else { - const extendedTypeNode = tsHelper.getExtendedTypeNode(node,this.checker); + const extendedTypeNode = tsHelper.getExtendedTypeNode(node, this.checker); const baseName = this.transpileNode(extendedTypeNode.expression); result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}${baseName}.new()\n`; this.pushExport(className, node); From 38b2624839e58c61ef1936da46deed2339c126ab Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Fri, 18 Jan 2019 12:25:02 +0100 Subject: [PATCH 3/5] use correct __base --- src/Transpiler.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 1451f5e59..420d4f335 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -2135,8 +2135,9 @@ export abstract class LuaTranspiler { } result += this.indent + `${className}.__index = ${className}\n`; if (extendsType) { - const baseName = extendsType.symbol.escapedName; - result += this.indent + `${className}.__base = ${baseName}\n`; + const extendedTypeNode = tsHelper.getExtendedTypeNode(node, this.checker); + const baseName = this.transpileNode(extendedTypeNode.expression); + result += this.indent + `${className}.__base = ${baseName}\n`; } result += this.indent + `function ${className}.new(construct, ...)\n`; result += this.indent + ` local self = setmetatable({}, ${className})\n`; From 976b6fb12a3049ff63e6be924994767e00164ca6 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Fri, 18 Jan 2019 12:36:46 +0100 Subject: [PATCH 4/5] correct formatting --- src/Transpiler.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 420d4f335..5e9d7605b 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -2135,9 +2135,9 @@ export abstract class LuaTranspiler { } result += this.indent + `${className}.__index = ${className}\n`; if (extendsType) { - const extendedTypeNode = tsHelper.getExtendedTypeNode(node, this.checker); - const baseName = this.transpileNode(extendedTypeNode.expression); - result += this.indent + `${className}.__base = ${baseName}\n`; + const extendedTypeNode = tsHelper.getExtendedTypeNode(node, this.checker); + const baseName = this.transpileNode(extendedTypeNode.expression); + result += this.indent + `${className}.__base = ${baseName}\n`; } result += this.indent + `function ${className}.new(construct, ...)\n`; result += this.indent + ` local self = setmetatable({}, ${className})\n`; From ef9e00e92ded770934063f3079787e36b314b164 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Sat, 19 Jan 2019 19:47:10 +0100 Subject: [PATCH 5/5] added test --- test/unit/class.spec.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/unit/class.spec.ts b/test/unit/class.spec.ts index 81851ab9e..6e953181b 100644 --- a/test/unit/class.spec.ts +++ b/test/unit/class.spec.ts @@ -333,6 +333,31 @@ export class ClassTests { Expect(result).toBe(10); } + @Test("renamedClassExtends") + public renamedClassExtends(): void { + // Transpile + const lua = util.transpileString( + `namespace Classes{ + export class Base{ + value:number; + constructor(){ this.value = 3; } + } + } + const A = Classes.Base; + class B extends A{ + constructor(){ super(); } + }; + const b = new B(); + return b.value;` + ); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(3); + } + @Test("ClassMethodCall") public classMethodCall(): void { // Transpile