TypeScriptToLua / TypeScriptToLua

Typescript to lua transpiler. https://typescripttolua.github.io/
MIT License
2.14k stars 171 forks source link

Incorrect transform this arg for optional super method call #1585

Closed pilaoda closed 1 month ago

pilaoda commented 1 month ago

playground: https://typescripttolua.github.io/play/#code/FAYwNghgzlAEAKEBOBTAdgF1gb2LfsAsgIwAUAlDgL54GEBMA-BdcDaJDLAMIAWAlmAAmsFAA8M6IXESpMOWvhItcBNbCgBXAA4okAOmXlFsGmoYqTarboMNG+iiZpUgA

key input:

class Child extends Parent {
    M2() {
        super.M2?.()
    }
}

incorrect output (this become Parent.prototype, it should be self)

function Child.prototype.M2(self)
    local ____this_1
    ____this_1 = Parent.prototype
    local ____opt_0 = ____this_1.M2
    if ____opt_0 ~= nil then
        ____opt_0(____this_1)
    end
end

expected output

function Child.prototype.M2(self)
    local ____opt_0 = Parent.prototype.M2
    if ____opt_0 ~= nil then
        ____opt_0(self)
    end
end
Perryvw commented 1 month ago

Do you happen to have an example where this actually causes problems?

pilaoda commented 1 month ago

Do you happen to have an example where this actually causes problems?

https://typescripttolua.github.io/play/#code/FAYwNghgzlAEAKEBOBTAdgF1gb2LfsADkgJYBuEGKsaEAttQLywBEhJkA9gCYQsDceAgFkAjAAoAlDiKlM4lmIBcsOgE8a9aiSgsANLAwALHQDpaDSf1gBfIfmEAmAPxSZxEvMWOV6zQ1gdfUMTKHMtK1tgO1BIGFgAYRMwblgUAA8qNG44RFRMHHtYMTdcAnLYKABXQhQkUxLJIrtyp1Ki8ura+qdnUylm6OBQTjQoLBBYZjQUAHdE5O4pQRAGiStQBsdloA

just run this lua code output:

D:\lua_projects\test4>lua main.lua M1: my name is pilaoda M2: my name is nil