From 0fd339735b0979e56229595915878ae498fc3367 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 21 Oct 2023 12:28:42 +0200 Subject: [PATCH 1/2] Fix static super calls breaking with nil reference exception --- src/transformation/visitors/class/index.ts | 12 +++++++++++- test/unit/classes/classes.spec.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/transformation/visitors/class/index.ts b/src/transformation/visitors/class/index.ts index 9574d1efc..1e74e324f 100644 --- a/src/transformation/visitors/class/index.ts +++ b/src/transformation/visitors/class/index.ts @@ -21,6 +21,7 @@ import { createClassSetup } from "./setup"; import { LuaTarget } from "../../../CompilerOptions"; import { transformInPrecedingStatementScope } from "../../utils/preceding-statements"; import { createClassPropertyDecoratingExpression } from "./decorators"; +import { findFirstNodeAbove } from "../../utils/typescript"; export const transformClassDeclaration: FunctionVisitor = (declaration, context) => { // If declaration is a default export, transform to export variable assignment instead @@ -249,5 +250,14 @@ export const transformSuperExpression: FunctionVisitor = (ex baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression); } - return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype")); + const f = findFirstNodeAbove(expression, ts.isFunctionLike); + if (f && ts.canHaveModifiers(f) && isStaticNode(f)) + { + // In static method, don't add prototype to super call + return baseClassName; + } + else + { + return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype")); + } }; diff --git a/test/unit/classes/classes.spec.ts b/test/unit/classes/classes.spec.ts index 93c7640cf..a93177c88 100644 --- a/test/unit/classes/classes.spec.ts +++ b/test/unit/classes/classes.spec.ts @@ -833,3 +833,22 @@ test("static member definition order (#1457)", () => { return A.a; `.expectToMatchJsResult(); }); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1504 +test("Calling static inherited functions works (#1504)", () => { + util.testFunction` + class A { + static Get() { + return "A"; + } + } + + class B extends A { + static Get() { + return super.Get() + "B"; + } + } + + return B.Get(); + `.expectToMatchJsResult(); +}); From e9b3721dc5dcbd9b58d63aafda7ac80265ef0d1f Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 21 Oct 2023 12:34:07 +0200 Subject: [PATCH 2/2] Fix prettier --- src/transformation/visitors/class/index.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/transformation/visitors/class/index.ts b/src/transformation/visitors/class/index.ts index 1e74e324f..8c54afe12 100644 --- a/src/transformation/visitors/class/index.ts +++ b/src/transformation/visitors/class/index.ts @@ -251,13 +251,10 @@ export const transformSuperExpression: FunctionVisitor = (ex } const f = findFirstNodeAbove(expression, ts.isFunctionLike); - if (f && ts.canHaveModifiers(f) && isStaticNode(f)) - { + if (f && ts.canHaveModifiers(f) && isStaticNode(f)) { // In static method, don't add prototype to super call return baseClassName; - } - else - { + } else { return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype")); - } + } };