Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,27 @@ 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];
}
}
}
}
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
Expand Down
6 changes: 4 additions & 2 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
25 changes: 25 additions & 0 deletions test/unit/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down