seaofvoices / darklua

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

Add `remove_continue` rule #211

Open jiwonz opened 2 months ago

jiwonz commented 2 months ago

Closes #202

Cases

Case: There are continue and break together in a loop but there are more continue than break

Input

for i = 1, 10 do
    if i % 3 == 0 then
        continue
    end
    if i % 6 == 0 then
        continue
    end
    if i % 9 == 0 then
        continue
    end
    if i % 10 == 0 then
        break
    end
end

Output

for i = 1, 10 do
local __DARKLUA_REMOVE_CONTINUE_break5784919bbab63ddf=false repeat  if i % 3 == 0 then
break   
end
    if i % 6 == 0 then
break   
end
    if i % 9 == 0 then
break   
end
    if i % 10 == 0 then
__DARKLUA_REMOVE_CONTINUE_break5784919bbab63ddf=true break  
end
until true if __DARKLUA_REMOVE_CONTINUE_break5784919bbab63ddf then break end end

Case: There are continue and break together in a loop but there are more break than continue

Input

for i = 1, 10 do
    if i % 3 == 0 then
        break
    end
    if i % 6 == 0 then
        break
    end
    if i % 9 == 0 then
        break
    end
    if i % 10 == 0 then
        continue
    end
end

Output

for i = 1, 10 do
local __DARKLUA_REMOVE_CONTINUE_continue5784919bbab63ddf=false repeat   if i % 3 == 0 then
break   
end
    if i % 6 == 0 then
break   
end
    if i % 9 == 0 then
break   
end
    if i % 10 == 0 then
__DARKLUA_REMOVE_CONTINUE_continue5784919bbab63ddf=true break   
end
until true if not __DARKLUA_REMOVE_CONTINUE_continue5784919bbab63ddf then break end end

Case: There is only continue in a loop

Input

for i = 1, 10 do
    if i % 5 == 0 then
        continue
    end
    print(i)
end

Output

for i = 1, 10 do
repeat  if i % 5 == 0 then
break   
end
    print(i)
until true end

Note

TO-DOs