Skip to content

Commit dbcae83

Browse files
authored
Fix static super calls breaking with nil reference exception (#1511)
* Fix static super calls breaking with nil reference exception * Fix prettier
1 parent 40e4c22 commit dbcae83

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { createClassSetup } from "./setup";
2121
import { LuaTarget } from "../../../CompilerOptions";
2222
import { transformInPrecedingStatementScope } from "../../utils/preceding-statements";
2323
import { createClassPropertyDecoratingExpression } from "./decorators";
24+
import { findFirstNodeAbove } from "../../utils/typescript";
2425

2526
export const transformClassDeclaration: FunctionVisitor<ts.ClassLikeDeclaration> = (declaration, context) => {
2627
// If declaration is a default export, transform to export variable assignment instead
@@ -249,5 +250,11 @@ export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (ex
249250
baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression);
250251
}
251252

252-
return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype"));
253+
const f = findFirstNodeAbove(expression, ts.isFunctionLike);
254+
if (f && ts.canHaveModifiers(f) && isStaticNode(f)) {
255+
// In static method, don't add prototype to super call
256+
return baseClassName;
257+
} else {
258+
return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype"));
259+
}
253260
};

test/unit/classes/classes.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,3 +833,22 @@ test("static member definition order (#1457)", () => {
833833
return A.a;
834834
`.expectToMatchJsResult();
835835
});
836+
837+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1504
838+
test("Calling static inherited functions works (#1504)", () => {
839+
util.testFunction`
840+
class A {
841+
static Get() {
842+
return "A";
843+
}
844+
}
845+
846+
class B extends A {
847+
static Get() {
848+
return super.Get() + "B";
849+
}
850+
}
851+
852+
return B.Get();
853+
`.expectToMatchJsResult();
854+
});

0 commit comments

Comments
 (0)