Skip to content

Commit aa3e7ce

Browse files
committed
Revert "if: drop empty do bodies. Closes #227"
This reverts commit 6b42462.
1 parent 17434b6 commit aa3e7ce

File tree

4 files changed

+22
-47
lines changed

4 files changed

+22
-47
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ they can and will change without that change being reflected in Styler's semanti
55

66
## main
77

8-
### Improvements
9-
10-
- `if`: drop empty `do` bodies like `if a, do: nil, else: b` => `if !a, do: b` (#227)
11-
128
### Fixes
139

1410
- fix bug that mangled large blocks of comments when sorting in configs or with `# styler:sort` (#230, h/t @cschmatzler)

docs/control_flow_macros.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ We advocate for `case` and `if` as the first tools to be considered for any cont
2121

2222
Never! `unless` [is being deprecated](https://github.com/elixir-lang/elixir/pull/13769#issuecomment-2334878315) and so should not be used.
2323

24-
Styler replaces `unless` statements with their `if` equivalent similar to using the `mix format --migrate_unless` flag.
25-
2624
### use `with` when...
2725

2826
> `with` great power comes great responsibility
@@ -58,26 +56,34 @@ if a, do: b, else: nil
5856
if a, do: b
5957
```
6058

61-
It also removes `do: nil` when an `else` is present, inverting the head to maintain semantics
62-
63-
```elixir
64-
if a, do: nil, else: b
65-
# styled:
66-
if !a, do: b
67-
```
68-
6959
### Negation Inversion
7060

71-
Styler removes negators in the head of `if` statements by "inverting" the statement.
61+
Styler removes negators in the head of `if` and `unless` statements by "inverting" the statement.
7262
The following operators are considered "negators": `!`, `not`, `!=`, `!==`
7363

64+
7465
Examples:
7566

7667
```elixir
68+
# negated `if` statement with no `else` clause are rewritten to `unless`
69+
if not x, do: y
70+
# Styled:
71+
unless x, do: y
72+
7773
# negated `if` statements with an `else` clause have their clauses inverted and negation removed
7874
if !x, do: y, else: z
7975
# Styled:
8076
if x, do: z, else: y
77+
78+
# negated `unless` statements are rewritten to `if`
79+
unless x != y, do: z
80+
# B styled:
81+
if x == y, do: z
82+
83+
# `unless` with `else` is verboten; these are always rewritten to `if` statements
84+
unless x, do: y, else: z
85+
# styled:
86+
if x, do: z, else: y
8187
```
8288

8389
Because elixir relies on truthy/falsey values for its `if` statements, boolean casting is unnecessary and so double negation is simply removed.

lib/style/blocks.ex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ defmodule Styler.Style.Blocks do
3131
alias Styler.Zipper
3232

3333
defguardp is_negator(n) when elem(n, 0) in [:!, :not, :!=, :!==]
34-
defguardp is_empty_body(n) when elem(n, 0) == :__block__ and elem(n, 2) in [[nil], []]
3534

3635
# case statement with exactly 2 `->` cases
3736
# rewrite to `if` if it's any of 3 trivial cases
@@ -140,13 +139,13 @@ defmodule Styler.Style.Blocks do
140139
[negator, [{do_, do_body}, {else_, else_body}]] when is_negator(negator) ->
141140
zipper |> Zipper.replace({:if, m, [invert(negator), [{do_, else_body}, {else_, do_body}]]}) |> run(ctx)
142141

143-
# drop `else end` and `else: nil`
144-
[head, [do_block, {_, else_body}]] when is_empty_body(else_body) ->
142+
# drop `else end`
143+
[head, [do_block, {_, {:__block__, _, []}}]] ->
145144
{:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx}
146145

147-
# invert and drop `do: nil`
148-
[head, [{do_, do_body}, {_, else_body}]] when is_empty_body(do_body) ->
149-
{:cont, Zipper.replace(zipper, {:if, m, [invert(head), [{do_, else_body}]]}), ctx}
146+
# drop `else: nil`
147+
[head, [do_block, {_, {:__block__, _, [nil]}}]] ->
148+
{:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx}
150149

151150
[head, [do_, else_]] ->
152151
if Style.max_line(do_) > Style.max_line(else_) do

test/style/blocks_test.exs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -988,32 +988,6 @@ defmodule Styler.Style.BlocksTest do
988988
)
989989
end
990990

991-
test "inverts do nil" do
992-
assert_style("if a, do: b, else: nil", "if a, do: b")
993-
994-
assert_style("if a do nil else b end", """
995-
if !a do
996-
b
997-
end
998-
""")
999-
1000-
assert_style(
1001-
"""
1002-
if a == b do
1003-
# comment
1004-
else
1005-
:ok
1006-
end
1007-
""",
1008-
"""
1009-
if a != b do
1010-
# comment
1011-
:ok
1012-
end
1013-
"""
1014-
)
1015-
end
1016-
1017991
test "double negator rewrites" do
1018992
for a <- ~w(not !), block <- ["do: z", "do: z, else: zz"] do
1019993
assert_style "if #{a} (x != y), #{block}", "if x == y, #{block}"

0 commit comments

Comments
 (0)