Skip to content

Commit

Permalink
pipes: enum.map(x) |> map.new() => map.new(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Jan 24, 2024
1 parent 27d02eb commit 6876dff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 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

* deprecations: `~R` -> `~r`, `Date.range/2` -> `Date.range/3` with decreasing dates (h/t @milmazz)
* pipes: `|> Enum.map(foo) |> Map.new()` => `|> Map.new(foo)`

## v0.11.6

### Fixes
Expand Down
7 changes: 7 additions & 0 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ defmodule Styler.Style.Pipes do
else: node
end

# `lhs |> Enum.map(mapper) |> Map.new()` => `lhs |> Map.new(mapper)
defp fix_pipe(
pipe_chain(lhs, {{:., _, [{_, _, [:Enum]}, :map]}, _, [mapper]}, {{:., _, [{_, _, [:Map]}, :new]} = new, nm, []})
) do
Style.set_line({:|>, [], [lhs, {new, nm, [mapper]}]}, nm[:line])
end

for mod <- [:Map, :Keyword] do
# lhs |> Map.merge(%{key: value}) => lhs |> Map.put(key, value)
defp fix_pipe({:|>, pm, [lhs, {{:., dm, [{_, _, [unquote(mod)]} = mod, :merge]}, m, [{:%{}, _, [{key, value}]}]}]}),
Expand Down
4 changes: 4 additions & 0 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ defmodule Styler.Style.PipesTest do
)
end

test "Enum.map(x) |> Map.new()" do
assert_style("a |> Enum.map(b) |> Map.new()", "Map.new(a, b)")
end

test "into a new map" do
assert_style("a |> Enum.into(foo) |> b()")
assert_style("a |> Enum.into(%{}) |> b()", "a |> Map.new() |> b()")
Expand Down

0 comments on commit 6876dff

Please sign in to comment.