Skip to content

Commit

Permalink
Wrap up todos
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Sep 23, 2024
1 parent 04a1c86 commit 7b9e7eb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 79 deletions.
26 changes: 9 additions & 17 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ defmodule Styler.Style.Blocks do
end
end

# @TODO do i want to be smart and recognize stdlib functions that return bools and use :not instead of :!?
def run({{:unless, m, [head, do_else]}, _} = zipper, ctx) do
zipper
|> Zipper.replace({:if, m, [invert(head), do_else]})
Expand Down Expand Up @@ -262,7 +261,7 @@ defmodule Styler.Style.Blocks do
# c
# d
# )
# @TODO would be nice to changeto
# @TODO would be nice to change to
# a
# b
# c
Expand Down Expand Up @@ -323,25 +322,18 @@ defmodule Styler.Style.Blocks do
|> run(%{ctx | comments: comments})
end

# %{1 => unary_guards, 2 => binary_guards} =
# :functions
# |> Kernel.__info__()
# |> Enum.filter(fn {k, _} ->
# k
# |> to_string()
# |> String.starts_with?("is_")
# end)
# |> Enum.group_by(fn {_, v} -> v end, fn {k, _} -> k end)
#
# @bools ~w(< <= > >= in)

defp invert({:!=, m, [a, b]}), do: {:==, m, [a, b]}
defp invert({:!==, m, [a, b]}), do: {:===, m, [a, b]}
defp invert({:==, m, [a, b]}), do: {:!=, m, [a, b]}
defp invert({:===, m, [a, b]}), do: {:!==, m, [a, b]}
defp invert({:!, _, [condition]}), do: condition
defp invert({:not, _, [condition]}), do: condition
# @TODO get some tests on this one's meta etc
defp invert({:|>, m, _} = pipe), do: {:|>, [line: m[:line]], [pipe, {{:., [line: m[:line]], [Kernel, :!]}, [], []}]}
defp invert({_, m, _} = other), do: {:!, [line: m[:line]], [other]}

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

if fun == :|>,
do: {:|>, meta, [ast, {{:., meta, [Kernel, :!]}, meta, []}]},
else: {:!, meta, [ast]}
end
end
127 changes: 65 additions & 62 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -822,45 +822,8 @@ defmodule Styler.Style.BlocksTest do
end
end

describe "if/unless" do
test "drops if else nil" do
assert_style("if a, do: b, else: nil", "if a, do: b")

assert_style("if a do b else nil end", """
if a do
b
end
""")
end

test "double negator rewrites" do
for a <- ~w(not !), block <- ["do: z", "do: z, else: zz"] do
assert_style "if #{a} (x != y), #{block}", "if x == y, #{block}"
assert_style "if #{a} (x !== y), #{block}", "if x === y, #{block}"
assert_style "if #{a} ! x, #{block}", "if x, #{block}"
assert_style "if #{a} not x, #{block}", "if x, #{block}"
end

assert_style("if not x, do: y", "if not x, do: y")
assert_style("if !x, do: y", "if !x, do: y")

assert_style(
"""
if !!val do
a
else
b
end
""",
"""
if val do
a
else
b
end
"""
)

describe "unless to if" do
test "inverts all the things" do
assert_style(
"""
unless !! not true do
Expand All @@ -877,27 +840,7 @@ defmodule Styler.Style.BlocksTest do
end
"""
)
end

test "regression: negation with empty do body" do
assert_style(
"""
if a != b do
# comment
else
:ok
end
""",
"""
if a == b do
# comment
:ok
end
"""
)
end

test "Credo.Check.Refactor.UnlessWithElse" do
for negator <- ["!", "not "] do
assert_style(
"""
Expand Down Expand Up @@ -952,9 +895,7 @@ defmodule Styler.Style.BlocksTest do
end
"""
)
end

test "Credo.Check.Refactor.NegatedConditionsInUnless" do
for negator <- ["!", "not "] do
assert_style("unless #{negator} foo, do: :bar", "if foo, do: :bar")

Expand Down Expand Up @@ -990,7 +931,69 @@ defmodule Styler.Style.BlocksTest do
end
end

test "Credo.Check.Refactor.NegatedConditionsWithElse" do
test "unless with pipes" do
assert_style "unless a |> b() |> c(), do: x", "if a |> b() |> c() |> Kernel.!(), do: x"
end
end

describe "if" do
test "drops else nil" do
assert_style("if a, do: b, else: nil", "if a, do: b")

assert_style("if a do b else nil end", """
if a do
b
end
""")

assert_style(
"""
if a != b do
# comment
else
:ok
end
""",
"""
if a == b do
# comment
:ok
end
"""
)
end

test "double negator rewrites" do
for a <- ~w(not !), block <- ["do: z", "do: z, else: zz"] do
assert_style "if #{a} (x != y), #{block}", "if x == y, #{block}"
assert_style "if #{a} (x !== y), #{block}", "if x === y, #{block}"
assert_style "if #{a} ! x, #{block}", "if x, #{block}"
assert_style "if #{a} not x, #{block}", "if x, #{block}"
end

assert_style("if not x, do: y", "if not x, do: y")
assert_style("if !x, do: y", "if !x, do: y")

assert_style(
"""
if !!val do
a
else
b
end
""",
"""
if val do
a
else
b
end
"""
)
end

test "single negator do/else swaps" do
# covers Credo.Check.Refactor.NegatedConditionsWithElse
for negator <- ["!", "not "] do
assert_style("if #{negator}foo, do: :bar, else: :baz", "if foo, do: :baz, else: :bar")

Expand Down

0 comments on commit 7b9e7eb

Please sign in to comment.