Skip to content

Commit 851205b

Browse files
TheLartiansPerryvw
authored andcommitted
Fix renamed class extension (#343)
* transpile type node instead of using typename * lint * use correct __base * correct formatting * added test
1 parent e9ce425 commit 851205b

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/TSHelper.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,27 @@ export class TSHelper {
5454
return result;
5555
}
5656

57-
public static getExtendedType(node: ts.ClassLikeDeclarationBase, checker: ts.TypeChecker): ts.Type | undefined {
57+
public static getExtendedTypeNode(node: ts.ClassLikeDeclarationBase, checker: ts.TypeChecker):
58+
ts.ExpressionWithTypeArguments | undefined {
5859
if (node && node.heritageClauses) {
5960
for (const clause of node.heritageClauses) {
6061
if (clause.token === ts.SyntaxKind.ExtendsKeyword) {
6162
const superType = checker.getTypeAtLocation(clause.types[0]);
6263
const decorators = this.getCustomDecorators(superType, checker);
6364
if (!decorators.has(DecoratorKind.PureAbstract)) {
64-
return superType;
65+
return clause.types[0];
6566
}
6667
}
6768
}
6869
}
6970
return undefined;
7071
}
7172

73+
public static getExtendedType(node: ts.ClassLikeDeclarationBase, checker: ts.TypeChecker): ts.Type | undefined {
74+
const extendedTypeNode = this.getExtendedTypeNode(node, checker);
75+
return extendedTypeNode && checker.getTypeAtLocation(extendedTypeNode);
76+
}
77+
7278
public static isFileModule(sourceFile: ts.SourceFile): boolean {
7379
if (sourceFile) {
7480
// Vanilla ts flags files as external module if they have an import or

src/Transpiler.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,13 +2128,15 @@ export abstract class LuaTranspiler {
21282128
result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}{}\n`;
21292129
this.pushExport(className, node);
21302130
} else {
2131-
const baseName = extendsType.symbol.escapedName;
2131+
const extendedTypeNode = tsHelper.getExtendedTypeNode(node, this.checker);
2132+
const baseName = this.transpileNode(extendedTypeNode.expression);
21322133
result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}${baseName}.new()\n`;
21332134
this.pushExport(className, node);
21342135
}
21352136
result += this.indent + `${className}.__index = ${className}\n`;
21362137
if (extendsType) {
2137-
const baseName = extendsType.symbol.escapedName;
2138+
const extendedTypeNode = tsHelper.getExtendedTypeNode(node, this.checker);
2139+
const baseName = this.transpileNode(extendedTypeNode.expression);
21382140
result += this.indent + `${className}.__base = ${baseName}\n`;
21392141
}
21402142
result += this.indent + `function ${className}.new(construct, ...)\n`;

test/unit/class.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,31 @@ export class ClassTests {
333333
Expect(result).toBe(10);
334334
}
335335

336+
@Test("renamedClassExtends")
337+
public renamedClassExtends(): void {
338+
// Transpile
339+
const lua = util.transpileString(
340+
`namespace Classes{
341+
export class Base{
342+
value:number;
343+
constructor(){ this.value = 3; }
344+
}
345+
}
346+
const A = Classes.Base;
347+
class B extends A{
348+
constructor(){ super(); }
349+
};
350+
const b = new B();
351+
return b.value;`
352+
);
353+
354+
// Execute
355+
const result = util.executeLua(lua);
356+
357+
// Assert
358+
Expect(result).toBe(3);
359+
}
360+
336361
@Test("ClassMethodCall")
337362
public classMethodCall(): void {
338363
// Transpile

0 commit comments

Comments
 (0)