Skip to content

Commit

Permalink
shrinkadink
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 12, 2023
1 parent 12f006c commit 9676bab
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand Down

0 comments on commit 9676bab

Please sign in to comment.