From fd3474e02b8b91aabc04ee704e97d91133f79117 Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Sun, 12 Nov 2023 08:45:19 -0700 Subject: [PATCH] add a regression test for what our cleanup fixed --- lib/style/blocks.ex | 18 +++++++++--------- test/style/blocks_test.exs | 2 ++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/style/blocks.ex b/lib/style/blocks.ex index 2176bc67..98378061 100644 --- a/lib/style/blocks.ex +++ b/lib/style/blocks.ex @@ -79,9 +79,16 @@ defmodule Styler.Style.Blocks do {postroll, reversed_clauses} = Enum.split_while(reversed_clauses, &(not left_arrow?(&1))) [{:<-, _, [lhs, rhs]} = _final_clause | rest] = reversed_clauses - # Credo.Check.Refactor.RedundantWithClauseResult + # drop singleton identity else clauses like `else foo -> foo end` + elses = + case elses do + [{{_, _, [:else]}, [{:->, _, [[left], right]}]}] -> if nodes_equivalent?(left, right), do: [], else: elses + _ -> elses + end + {reversed_clauses, do_body} = cond do + # Credo.Check.Refactor.RedundantWithClauseResult Enum.empty?(postroll) and Enum.empty?(elses) and nodes_equivalent?(lhs, do_body) -> {rest, rhs} @@ -95,14 +102,7 @@ defmodule Styler.Style.Blocks do {reversed_clauses, do_body} end - # drop singleton identity else clauses like `else foo -> foo end` - new_elses = - case elses do - [{{_, _, [:else]}, [{:->, _, [[left], right]}]}] -> if nodes_equivalent?(left, right), do: [], else: elses - _ -> elses - end - - children = Enum.reverse(reversed_clauses, [[{do_block, do_body} | new_elses]]) + children = Enum.reverse(reversed_clauses, [[{do_block, do_body} | elses]]) if Enum.any?(preroll), do: {:__block__, m, preroll ++ [{:with, m, children}]}, diff --git a/test/style/blocks_test.exs b/test/style/blocks_test.exs index fd073ec2..d38b0c01 100644 --- a/test/style/blocks_test.exs +++ b/test/style/blocks_test.exs @@ -211,6 +211,8 @@ defmodule Styler.Style.BlocksTest do with {:ok, a} <- foo(), {:ok, b} <- bar(a) do {:ok, b} + else + error -> error end """, """