seaofvoices / darklua

A command line tool that transforms Lua code
https://darklua.com/
MIT License
71 stars 9 forks source link

feat: `remove_dead_function` #191

Closed Stefanuk12 closed 3 months ago

Stefanuk12 commented 3 months ago

To improve DX, a developer may make a function like this:

local function DebugPrint(...)
    if _G.DEBUG then
        print(...)
    end
end

When combined with remove_unused_if_branch, compute_expression, and inject_global_value, it can make writing debug prints and similar code a lot simpler.


local function DebugPrint(...)
    if _G.DEBUG then
        print(...)
    end
end

local function OtherFunction()
    -- actual code here
end

DebugPrint("a debug print here")
OtherFunction()

would bundle down to

local function OtherFunction()
    -- actual code here
end

OtherFunction()

This is related to #5, this needs to be done first before this can be achieved to have more robust variable tracking.

jeparlefrancais commented 3 months ago

Thanks for taking the time to submit this issue! I will link it to this other issue: https://github.com/seaofvoices/darklua/issues/44

It is a trivial sub-case of constant function inlining, where the function is simply empty 😄 It is not really a duplicate but I'll still close it as it will get done using the same rule. I'll add a comment to the issue to remember to support that use case!