Skip to content

Commit

Permalink
blocks: invert if/unless with !=. Closes #132
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Mar 8, 2024
1 parent 6fbaa8e commit bcb4bdc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## main

### Improvements

* blocks: invert if and unless with `!=` or `!==`, like we do for `!` and `not`. Closes #132

## v0.11.9

### Improvements
Expand Down
18 changes: 11 additions & 7 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Styler.Style.Blocks do
alias Styler.Style
alias Styler.Zipper

defguardp is_negator(n) when n in [:!, :not]
defguardp is_negator(n) when elem(n, 0) in [:!, :not, :!=, :!==]

# case statement with exactly 2 `->` cases
# rewrite to `if` if it's any of 3 trivial cases
Expand Down Expand Up @@ -163,8 +163,8 @@ defmodule Styler.Style.Blocks do
zipper |> Zipper.replace({:if, m, [{:!, hm, [head]}, do_else]}) |> run(ctx)

# Credo.Check.Refactor.NegatedConditionsInUnless
[{negator, _, [expr]}, [{do_, do_body}]] when is_negator(negator) ->
zipper |> Zipper.replace({:if, m, [expr, [{do_, do_body}]]}) |> run(ctx)
[negator, [{do_, do_body}]] when is_negator(negator) ->
zipper |> Zipper.replace({:if, m, [invert(negator), [{do_, do_body}]]}) |> run(ctx)

_ ->
{:cont, zipper, ctx}
Expand All @@ -174,12 +174,12 @@ defmodule Styler.Style.Blocks do
def run({{:if, m, children}, _} = zipper, ctx) do
case children do
# Credo.Check.Refactor.NegatedConditionsWithElse
[{negator, _, [expr]}, [{do_, do_body}, {else_, else_body}]] when is_negator(negator) ->
zipper |> Zipper.replace({:if, m, [expr, [{do_, else_body}, {else_, do_body}]]}) |> run(ctx)
[negator, [{do_, do_body}, {else_, else_body}]] when is_negator(negator) ->
zipper |> Zipper.replace({:if, m, [invert(negator), [{do_, else_body}, {else_, do_body}]]}) |> run(ctx)

# if not x, do: y => unless x, do: y
[{negator, _, [expr]}, [do_block]] when is_negator(negator) ->
zipper |> Zipper.replace({:unless, m, [expr, [do_block]]}) |> run(ctx)
[negator, [do_block]] when is_negator(negator) ->
zipper |> Zipper.replace({:unless, m, [invert(negator), [do_block]]}) |> run(ctx)

# drop `else: nil`
[head, [do_block, {_, {:__block__, _, [nil]}}]] ->
Expand Down Expand Up @@ -317,4 +317,8 @@ defmodule Styler.Style.Blocks do

max_line
end

defp invert({:!=, m, [a, b]}), do: {:==, m, [a, b]}
defp invert({:!==, m, [a, b]}), do: {:===, m, [a, b]}
defp invert({_, _, [expr]}), do: expr
end
59 changes: 59 additions & 0 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,25 @@ defmodule Styler.Style.BlocksTest do
)
end

for negator <- ["!=", "!=="], inverse = String.replace(negator, "!", "=") do
assert_style(
"""
unless x #{negator} y do
b
else
c
end
""",
"""
if x #{inverse} y do
b
else
c
end
"""
)
end

assert_style(
"""
unless a do
Expand Down Expand Up @@ -828,6 +847,23 @@ defmodule Styler.Style.BlocksTest do
"""
)
end

for negator <- ["!=", "!=="], inverse = String.replace(negator, "!", "=") do
assert_style("unless a #{negator} b, do: :bar", "if a #{inverse} b, do: :bar")

assert_style(
"""
unless a #{negator} b do
c
end
""",
"""
if a #{inverse} b do
c
end
"""
)
end
end

test "Credo.Check.Refactor.NegatedConditionsWithElse" do
Expand All @@ -851,6 +887,27 @@ defmodule Styler.Style.BlocksTest do
"""
)
end

for negator <- ["!=", "!=="], inverse = String.replace(negator, "!", "=") do
assert_style("if a #{negator} b, do: :bar, else: :baz", "if a #{inverse} b, do: :baz, else: :bar")

assert_style(
"""
if a #{negator} b do
bar
else
baz
end
""",
"""
if a #{inverse} b do
baz
else
bar
end
"""
)
end
end

test "recurses" do
Expand Down Expand Up @@ -887,6 +944,8 @@ defmodule Styler.Style.BlocksTest do
end
"""
)

assert_style("if not (a != b), do: c", "if a == b, do: c")
end

test "comments and flips" do
Expand Down

0 comments on commit bcb4bdc

Please sign in to comment.