Skip to content

Commit

Permalink
a |> map(m) |> join() -> map_join(a, m)
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed May 22, 2024
1 parent a5dc8aa commit 83103ec
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ See the moduledoc for `Styler.Style.Configs` for more.
* `Timex.now/1` will no longer be rewritten to `DateTime.now!/1` due to Timex accepting a wider domain of "timezones" than the stdlib (#145, h/t @ivymarkwell)
* `with`: skip nodes which (unexpectedly) do not contain a `do` body (#158, h/t @DavidB59)
* `then(&fun/1)`: fix false positives on arithmetic `&1 + x / 1` (#164, h/t @aenglisc)
* rewrite `a |> Enum.map(m) |> Enum.join()` to `map_join(a, m)`. we already did this for `join/2`, but missed the case for `join/1`

### Breaking Changes

Expand Down
6 changes: 3 additions & 3 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ defmodule Styler.Style.Pipes do
defp fix_pipe(
pipe_chain(
lhs,
{{:., dm, [{_, _, [mod]}, :map]}, em, [mapper]},
{{:., _, [{_, _, [:Enum]} = enum, :join]}, _, [joiner]}
{{:., dm, [{_, _, [mod]}, :map]}, em, map_args},
{{:., _, [{_, _, [:Enum]} = enum, :join]}, _, join_args}
)
)
when mod in @enum do
rhs = Style.set_line({{:., dm, [enum, :map_join]}, em, [joiner, mapper]}, dm[:line])
rhs = Style.set_line({{:., dm, [enum, :map_join]}, em, join_args ++ map_args}, dm[:line])
{:|>, [line: dm[:line]], [lhs, rhs]}
end

Expand Down
3 changes: 2 additions & 1 deletion test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ defmodule Styler.Style.PipesTest do

test "map/join" do
for enum <- ~w(Enum Stream) do
assert_style("a|> #{enum}.map(b) |> Enum.join(x)", "Enum.map_join(a, x, b)")
assert_style("a |> #{enum}.map(mapper) |> Enum.join()", "Enum.map_join(a, mapper)")
assert_style("a |> #{enum}.map(mapper) |> Enum.join(joiner)", "Enum.map_join(a, joiner, mapper)")
end
end

Expand Down

0 comments on commit 83103ec

Please sign in to comment.