Skip to content

Commit

Permalink
Pipes: maintain line meta in more rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed May 16, 2023
1 parent ceaa0b1 commit 75e61aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
32 changes: 16 additions & 16 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule Styler.Style.Pipes do

# `foo(a, ...) |> ...` => `a |> foo(...) |> ...`
defp extract_start({fun, meta, [arg | args]}) do
{{:|>, [], [arg, {fun, meta, args}]}, nil}
{{:|>, [line: meta[:line]], [arg, {fun, meta, args}]}, nil}
end

# `pipe_chain(a, b, c)` generates the ast for `a |> b |> c`
Expand All @@ -125,37 +125,37 @@ defmodule Styler.Style.Pipes do
defp fix_pipe(
pipe_chain(
lhs,
{{:., _, [{_, _, [:Enum]}, :reverse]} = reverse, _, []},
{{:., _, [{_, _, [:Enum]}, :reverse]} = reverse, meta, []},
{{:., _, [{_, _, [:Enum]}, :concat]}, _, [enum]}
)
) do
{:|>, [], [lhs, {reverse, [], [enum]}]}
{:|>, [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(
lhs,
{{:., _, [{_, _, [:Enum]}, :filter]}, _, [filterer]},
{{:., _, [{_, _, [:Enum]}, :filter]}, meta, [filterer]},
{{:., _, [{_, _, [:Enum]}, :count]} = count, _, []}
)
) do
{:|>, [], [lhs, {count, [], [filterer]}]}
{:|>, [line: meta[:line]], [lhs, {count, [line: meta[:line]], [filterer]}]}
end

# `lhs |> Enum.map(mapper) |> Enum.join(joiner)` => `lhs |> Enum.map_join(joiner, mapper)`
defp fix_pipe(
pipe_chain(
lhs,
{{:., _, [{_, _, [:Enum]}, :map]}, _, [mapper]},
{{:., dm, [{_, _, [:Enum]} = enum, :map]}, em, [mapper]},
{{:., _, [{_, _, [:Enum]}, :join]}, _, [joiner]}
)
) do
# Delete line info to keep things shrunk on the rewrite
joiner = Style.drop_line_meta(joiner)
mapper = Style.drop_line_meta(mapper)
rhs = {{:., [], [{:__aliases__, [], [:Enum]}, :map_join]}, [], [joiner, mapper]}
{:|>, [], [lhs, rhs]}
rhs = {{:., dm, [enum, :map_join]}, em, [joiner, mapper]}
{:|>, [line: dm[:line]], [lhs, rhs]}
end

# `lhs |> Enum.map(mapper) |> Enum.into(empty_map)` => `lhs |> Map.new(mapper)
Expand All @@ -164,27 +164,27 @@ defmodule Styler.Style.Pipes do
defp fix_pipe(
pipe_chain(
lhs,
{{:., _, [{_, _, [:Enum]}, :map]}, _, [mapper]},
{{:., dm, [{_, am, [:Enum]}, :map]}, em, [mapper]},
{{:., _, [{_, _, [:Enum]}, :into]} = into, _, [collectable]}
)
) do
mapper = Style.drop_line_meta(mapper)

rhs =
if empty_map?(collectable),
do: {{:., [], [{:__aliases__, [], [:Map]}, :new]}, [], [mapper]},
else: {into, [], [Style.drop_line_meta(collectable), mapper]}
do: {{:., dm, [{:__aliases__, am, [:Map]}, :new]}, em, [mapper]},
else: {into, em, [Style.drop_line_meta(collectable), mapper]}

{:|>, [], [lhs, rhs]}
{:|>, [line: dm[:line]], [lhs, rhs]}
end

defp fix_pipe({:|>, meta, [lhs, {{:., dm, [{_, _, [:Enum]}, :into]}, _, [collectable]}]} = node) do
if empty_map?(collectable), do: {:|>, meta, [lhs, {{:., dm, [{:__aliases__, [], [:Map]}, :new]}, [], []}]}, else: node
defp fix_pipe({:|>, meta, [lhs, {{:., dm, [{_, am, [:Enum]}, :into]}, em, [collectable]}]} = node) do
if empty_map?(collectable), do: {:|>, meta, [lhs, {{:., dm, [{:__aliases__, am, [:Map]}, :new]}, em, []}]}, else: node
end

defp fix_pipe({:|>, meta, [lhs, {{:., dm, [{_, _, [:Enum]}, :into]}, _, [collectable, mapper]}]} = node) do
defp fix_pipe({:|>, meta, [lhs, {{:., dm, [{_, am, [:Enum]}, :into]}, em, [collectable, mapper]}]} = node) do
if empty_map?(collectable),
do: {:|>, meta, [lhs, {{:., dm, [{:__aliases__, [], [:Map]}, :new]}, [], [Style.drop_line_meta(mapper)]}]},
do: {:|>, meta, [lhs, {{:., dm, [{:__aliases__, am, [:Map]}, :new]}, em, [Style.drop_line_meta(mapper)]}]},
else: node
end

Expand Down
36 changes: 18 additions & 18 deletions test/style/styles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ defmodule Styler.Style.StylesTest do

describe "pipes + defs" do
test "pipes doesnt abuse meta and break defs" do
assert_style(
"""
foo
|> bar(fn baz ->
def widget() do
:bop
end
end)
""",
"""
bar(foo, fn baz ->
def widget do
:bop
end
end)
"""
)
end
assert_style(
"""
foo
|> bar(fn baz ->
def widget() do
:bop
end
end)
""",
"""
bar(foo, fn baz ->
def widget do
:bop
end
end)
"""
)
end
end
end

0 comments on commit 75e61aa

Please sign in to comment.