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..5e9d7605b 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -2128,13 +2128,15 @@ 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); } result += this.indent + `${className}.__index = ${className}\n`; if (extendsType) { - const baseName = extendsType.symbol.escapedName; + 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`; 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