Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix with redundant body + non-arrow behind redundant clause #39

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,35 +182,37 @@ defmodule Quokka.Style.Blocks do

# 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
with [{{_, _, [:else]}, [{:->, _, [[left], right]}]}] <- elses,
true <- nodes_equivalent?(left, right),
do: [],
else: (_ -> elses)

# Remove Redundant body
{postroll, reversed_clauses, do_body} =
if Enum.empty?(postroll) and Enum.empty?(elses) and nodes_equivalent?(lhs, do_body) do
# removing redundant RHS can expose more non-arrows behind it, so repeat our postroll process
{postroll, reversed_clauses} = Enum.split_while(rest, &(not left_arrow?(&1)))
{postroll, reversed_clauses, rhs}
else
{postroll, reversed_clauses, do_body}
end

# Put the postroll into the body
{reversed_clauses, do_body} =
cond do
# Put the postroll into the body
Enum.any?(postroll) ->
{node, do_body_meta, do_children} = do_body
do_children = if node == :__block__, do: do_children, else: [do_body]
do_body = {:__block__, Keyword.take(do_body_meta, [:line]), Enum.reverse(postroll, do_children)}
{reversed_clauses, do_body}

# Credo.Check.Refactor.RedundantWithClauseResult
Enum.empty?(elses) and nodes_equivalent?(lhs, do_body) ->
{rest, rhs}

# no change
true ->
{reversed_clauses, do_body}
if Enum.any?(postroll) do
{node, do_body_meta, do_children} = do_body
do_children = if node == :__block__, do: do_children, else: [do_body]
do_body = {:__block__, Keyword.take(do_body_meta, [:line]), Enum.reverse(postroll, do_children)}
{reversed_clauses, do_body}
else
{reversed_clauses, do_body}
end

do_line = do_meta[:line]
final_clause_line = final_clause_meta[:line]

do_line =
cond do
do_meta[:format] == :keyword && final_clause_line + 1 >= do_line -> do_line
do_meta[:format] == :keyword && final_clause_line + 1 >= do_meta[:line] -> do_meta[:line]
do_meta[:format] == :keyword -> final_clause_line + 1
true -> final_clause_line
end
Expand Down Expand Up @@ -238,12 +240,12 @@ defmodule Quokka.Style.Blocks do
|> Zipper.prepend_siblings(preroll)
|> run(ctx)

# the # of `<-` canged, so we should have another look at this with statement
# the # of `<-` changed, so we should have another look at this with statement
Enum.any?(postroll) ->
run(zipper, ctx)

true ->
# of clauess didn't change, so don't reecurse or we'll loop FOREEEVEERR
# of clauses didn't change, so don't reecurse or we'll loop FOREEEVEERR
{:cont, zipper, ctx}
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ defmodule Quokka.Style.BlocksTest do
assert_style(
"""
with {:ok, a} <- foo(),
x = y,
{:ok, b} <- bar(a) do
{:ok, b}
else
Expand All @@ -423,6 +424,7 @@ defmodule Quokka.Style.BlocksTest do
""",
"""
with {:ok, a} <- foo() do
x = y
bar(a)
end
"""
Expand Down