diff --git a/lib/style/blocks.ex b/lib/style/blocks.ex index 9f0c3f36..1a1d0421 100644 --- a/lib/style/blocks.ex +++ b/lib/style/blocks.ex @@ -83,20 +83,18 @@ defmodule Styler.Style.Blocks do rewrite_body? = Enum.empty?(postroll) and Enum.empty?(elses) and nodes_equivalent?(lhs, do_body) {reversed_clauses, do_body} = - if rewrite_body? do - {rest, rhs} - else - body = - case Enum.reverse(postroll, [do_body]) do - [{:__block__, _, _} = block] -> - block - - body -> - {_, do_body_meta, _} = do_body - {:__block__, do_body_meta, body} - end - - {reversed_clauses, body} + cond do + rewrite_body? -> + {rest, rhs} + + Enum.any?(postroll) -> + body = Enum.reverse(postroll, [do_body]) + {_, do_body_meta, _} = do_body + new_do_body = {:__block__, do_body_meta, body} + {reversed_clauses, new_do_body} + + true -> + {reversed_clauses, do_body} end # drop singleton identity else clauses like `else foo -> foo end` @@ -106,19 +104,14 @@ defmodule Styler.Style.Blocks do _ -> elses end - children = Enum.reverse(reversed_clauses, [[{do_block, do_body} | new_elses]]) + rewritten = {:with, m, Enum.reverse(reversed_clauses, [[{do_block, do_body} | new_elses]])} # only rewrite if it needs rewriting! # can probably stop optimizing like this once we handle comments nicely cond do - Enum.any?(preroll) -> - {:__block__, m, preroll ++ [{:with, m, children}]} - - rewrite_body? or Enum.any?(postroll) or new_elses != elses -> - {:with, m, children} - - true -> - with + Enum.any?(preroll) -> {:__block__, m, preroll ++ [rewritten]} + rewrite_body? or Enum.any?(postroll) or new_elses != elses -> rewritten + true -> with end else # maybe this isn't a with statement - could be a function named `with`