Skip to content

Commit

Permalink
lhs |> Enum.reverse() |> Kernel.++(enum) => `lhs |> Enum.reverse(en…
Browse files Browse the repository at this point in the history
…um)`
  • Loading branch information
novaugust committed May 16, 2024
1 parent 2396955 commit da99d87
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ See the moduledoc for `Styler.Style.Configs` for more.
* `@derive`: move `@derive` before `defstruct|schema|embedded_schema` declarations (fixes compiler warning!) #134
* strings: rewrite double-quoted strings to use `~s` when there's 4+ escaped double-quotes
(`"\"\"\"\""` -> `~s("""")`) (`Credo.Check.Readability.StringSigils`) #146
* `Map.drop(foo, [single_key])` => `Map.delete(foo, single_key)` #161
* `Keyword.drop(foo, [single_key])` => `Keyword.delete(foo, single_key)` #161
* `Map.drop(foo, [single_key])` => `Map.delete(foo, single_key)` #161 (also in pipes)
* `Keyword.drop(foo, [single_key])` => `Keyword.delete(foo, single_key)` #161 (also in pipes)
* `lhs |> Enum.reverse() |> Kernel.++(enum)` => `lhs |> Enum.reverse(enum)`

### Fixes

Expand Down
11 changes: 11 additions & 0 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ defmodule Styler.Style.Pipes do
{:|>, [line: meta[:line]], [lhs, {reverse, [line: meta[:line]], [enum]}]}
end

# `lhs |> Enum.reverse() |> Enum.concat(enum)` => `lhs |> Enum.reverse(enum)`
defp fix_pipe(
pipe_chain(
lhs,
{{:., _, [{_, _, [:Enum]}, :reverse]} = reverse, meta, []},
{{:., _, [{_, _, [:Kernel]}, :++]}, _, [enum]}
)
) do
{:|>, [line: meta[:line]], [lhs, {reverse, [line: meta[:line]], [enum]}]}
end

# `lhs |> Enum.filter(filterer) |> Enum.count()` => `lhs |> Enum.count(count)`
defp fix_pipe(
pipe_chain(
Expand Down
19 changes: 19 additions & 0 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,25 @@ defmodule Styler.Style.PipesTest do
)
end

test "reverse/Kernel.++" do
assert_style("a |> Enum.reverse(bar) |> Kernel.++(foo)")
assert_style("a |> Enum.reverse() |> Kernel.++(foo)", "Enum.reverse(a, foo)")

assert_style(
"""
a
|> Enum.reverse()
|> Kernel.++([bar, baz])
|> Enum.sum()
""",
"""
a
|> Enum.reverse([bar, baz])
|> Enum.sum()
"""
)
end

test "filter/count" do
for enum <- ~w(Enum Stream) do
assert_style(
Expand Down

0 comments on commit da99d87

Please sign in to comment.