Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into me/with
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Sep 11, 2023
2 parents a92b759 + 9820f58 commit 8c1fd33
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## main

### Improvements

* Added right-hand-pattern-matching rewrites to `for` and `with` left arrow expressions `<-`
(ex: `with map = %{} <- foo()` => `with %{} = map <- foo`)

## v0.8.5

### Fixes
Expand Down
18 changes: 9 additions & 9 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,17 @@ defmodule Styler.Style.SingleNode do
end

# ARROW REWRITES
# `foo = :foo` => `:foo = foo` within `case`, `fn`, `with`
# there's complexity to `:->` due to `cond` also utilizing the symbol but with different semantics
# `with`, `for` left arrow - if only we could write something this trivial for `->`!
defp style({:<-, cm, [lhs, rhs]}), do: {:<-, cm, [put_matches_on_right(lhs), rhs]}
# there's complexity to `:->` due to `cond` also utilizing the symbol but with different semantics.
# thus, we have to have a clause for each place that `:->` can show up
# `with` elses
defp style({{:__block__, em, [:else]}, arrows}), do: {{:__block__, em, [:else]}, rewrite_arrows(arrows)}
defp style({:case, cm, [head, [{do_block, arrows}]]}), do: {:case, cm, [head, [{do_block, rewrite_arrows(arrows)}]]}
defp style({{:__block__, _, [:else]} = else_, arrows}), do: {else_, rewrite_arrows(arrows)}
defp style({:case, cm, [head, [{do_, arrows}]]}), do: {:case, cm, [head, [{do_, rewrite_arrows(arrows)}]]}
defp style({:fn, m, arrows}), do: {:fn, m, rewrite_arrows(arrows)}
# `with` head - if only we could write something this trivial for `->`!
defp style({:<-, cm, [lhs, rhs]}), do: {:<-, cm, [put_matches_on_right(lhs), rhs]}

defp style(node), do: node

defp left_arrow?({:<-, _, _}), do: true
defp left_arrow?(_), do: false

defp rewrite_arrows(arrows) when is_list(arrows),
do: Enum.map(arrows, fn {:->, m, [lhs, rhs]} -> {:->, m, [put_matches_on_right(lhs), rhs]} end)

Expand All @@ -215,6 +212,9 @@ defmodule Styler.Style.SingleNode do
|> Zipper.node()
end

defp left_arrow?({:<-, _, _}), do: true
defp left_arrow?(_), do: false

# don't write an else clause if it's `false -> nil`
defp if_ast(head, do_body, {:__block__, _, [nil]}), do: {:if, [do: []], [head, [{{:__block__, [], [:do]}, do_body}]]}

Expand Down
9 changes: 7 additions & 2 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("Logger.warn(foo, bar)", "Logger.warning(foo, bar)")
end

test "Timex.now -> DateTime.utc_now/now!" do
test "Timex.now => DateTime.utc_now/now!" do
assert_style("Timex.now()", "DateTime.utc_now()")
assert_style(~S|Timex.now("Some/Timezone")|, ~S|DateTime.now!("Some/Timezone")|)
end

test "Timex.today -> Date.utc_today" do
test "Timex.today => Date.utc_today" do
assert_style("Timex.today()", "Date.utc_today()")
assert_style(~S|Timex.today("Some/Timezone")|, ~S|Timex.today("Some/Timezone")|)
end
Expand Down Expand Up @@ -133,6 +133,11 @@ defmodule Styler.Style.SingleNodeTest do
end

describe "RHS pattern matching" do
test "left arrows" do
assert_style("with {:ok, result = %{}} <- foo, do: result", "with {:ok, %{} = result} <- foo, do: result")
assert_style("for map = %{} <- maps, do: map[:key]", "for %{} = map <- maps, do: map[:key]")
end

test "case statements" do
assert_style(
"""
Expand Down

0 comments on commit 8c1fd33

Please sign in to comment.