Skip to content

Commit

Permalink
use not over ! when rewriting unless a (> >= < <= in) b
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Sep 23, 2024
1 parent cde90d3 commit 926ad51
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,7 @@ defmodule Styler.Style.Blocks do
defp invert({:===, m, [a, b]}), do: {:!==, m, [a, b]}
defp invert({:!, _, [condition]}), do: condition
defp invert({:not, _, [condition]}), do: condition

defp invert({fun, m, _} = ast) do
meta = [line: m[:line]]

if fun == :|>,
do: {:|>, meta, [ast, {{:., meta, [Kernel, :!]}, meta, []}]},
else: {:!, meta, [ast]}
end
defp invert({:|>, m, _} = ast), do: {:|>, m, [ast, {{:., m, [Kernel, :!]}, m, []}]}
defp invert({bool, m, [_, _]} = ast) when bool in ~w(> >= < <= in)a, do: {:not, m, [ast]}
defp invert({_, m, _} = ast), do: {:!, [line: m[:line]], [ast]}
end
8 changes: 8 additions & 0 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,14 @@ defmodule Styler.Style.BlocksTest do
test "unless with pipes" do
assert_style "unless a |> b() |> c(), do: x", "if a |> b() |> c() |> Kernel.!(), do: x"
end

test "kernel boolean operators" do
assert_style "unless a in b, do: x", "if a not in b, do: x"

for bool <- ~w(> >= < <=)a do
assert_style "unless a #{bool} b, do: x", "if not (a #{bool} b), do: x"
end
end
end

describe "if" do
Expand Down

0 comments on commit 926ad51

Please sign in to comment.