lua-language-server
lua-language-server copied to clipboard
(feature)refactor: convert between ternary and if/else
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
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 = truex = falsey = nil
Evaluation of condition and x or y returns nil instead of false as you may expect.
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")
Correctness is guaranteed by the user.