Skip to content

Commit

Permalink
fix RedundantWithClauseResult creating invalid with statements when t…
Browse files Browse the repository at this point in the history
…here are no more clauses. closes #138
  • Loading branch information
novaugust committed Jul 15, 2024
1 parent c16d3a9 commit ac82224
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
**Note** Styler's only public API is its usage as a formatter plugin. While you're welcome to play with its internals,
they can and will change without that change being reflected in Styler's semantic version.

## main

### Fixes

* fix `with` arrow replacement + redundant body removal creating invalid statements (#184, h/t @JesseHerrick)

## 1.0.0-rc.2

### Fixes
Expand Down
12 changes: 6 additions & 6 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,23 @@ defmodule Styler.Style.Blocks do
with_children = Enum.reverse(reversed_clauses, [[{do_block, do_body} | elses]])
zipper = Zipper.replace(zipper, {:with, with_meta, with_children})

# recurse if the # of `<-` have changed (this `with` could now be eligible for a `case` rewrite)
cond do
# oops! RedundantWithClauseResult removed the final arrow in this. no more need for a with statement!
Enum.empty?(reversed_clauses) ->
{:cont, replace_with_statement(zipper, preroll ++ with_children), ctx}

# recurse if the # of `<-` have changed (this `with` could now be eligible for a `case` rewrite)
Enum.any?(preroll) ->
# put the preroll before the with statement in either a block we create or the existing parent block
zipper
|> Style.find_nearest_block()
|> Zipper.prepend_siblings(preroll)
|> run(ctx)

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

Enum.empty?(reversed_clauses) ->
# oops! RedundantWithClauseResult removed the final arrow in this. no more need for a with statement!
{:cont, replace_with_statement(zipper, with_children), ctx}

true ->
# of clauess didn't change, so don't reecurse or we'll loop FOREEEVEERR
{:cont, zipper, ctx}
Expand Down
10 changes: 10 additions & 0 deletions test/style/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,16 @@ defmodule Styler.Style.BlocksTest do
)
"""
)

assert_style(
"""
with a <- b(c), {:ok, result} <- x(y, z), do: {:ok, result}
""",
"""
a = b(c)
x(y, z)
"""
)
end

test "doesn't false positive with vars" do
Expand Down

0 comments on commit ac82224

Please sign in to comment.