seaofvoices / darklua

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

A rule for removing (converting) `continue` statements #202

Open sircfenner opened 2 months ago

sircfenner commented 2 months ago

As with other rules like remove_interpolated_string and remove_compound_assignment, this would be useful for translating Luau into Lua 5.1 source code.

I implementated this myself a while ago in a different project. It converted the following:

for i = 1, 4 do
    print("start", i)
    if i % 2 == 0 then
        print("two", i)
        continue
    elseif i % 3 == 0 then
        print("three", i)
        break
    end
    print("end", i)
end

into:

for i = 1, 4 do
    local _continue_0 = false
    repeat
        print("start", i)
        if i % 2 == 0 then
            print("two", i)
            _continue_0 = true
            break
        elseif i % 3 == 0 then
            print("three", i)
            break
        end
        print("end", i)
        _continue_0 = true
    until true
    if not _continue_0 then
        break
    end
end

The repeat until true construct is used to execute a block of statements once where it can be broken out of using break.