lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

(feature)refactor: convert between ternary and if/else

Open max397574 opened this issue 3 years ago • 3 comments

it would be really cool if there would be a code action (available when inside one of the two) that allows to change between ternary assignments and if/else statements example:

if y then
    x = 3
else
    x = 4
x = y and 3 or 4

max397574 avatar Aug 25 '22 11:08 max397574

While this is may be a good idea, this does not always work. If I have condition and x or y, it only works if either x or y are truth values. If none of them are truth values, the ternary doesn't work.

Let:

  • condition = true
  • x = false
  • y = nil

Evaluation of condition and x or y returns nil instead of false as you may expect.

MikuAuahDark avatar Aug 29 '22 01:08 MikuAuahDark

This seems like it would require some serious thinking to consider and cover all the use cases, as @MikuAuahDark pointed out. Personally, I think ternary operations and if statements, while sometimes interchangeable, are different enough that a feature like this would only end up being practical in very simple cases such as the below:

if x then
    print("True")
else
    print("False")
end

-- convert to
x and print("True") or print("False")

carsakiller avatar Aug 29 '22 15:08 carsakiller

Correctness is guaranteed by the user.

sumneko avatar Sep 19 '22 08:09 sumneko