Skip to content

Commit

Permalink
Fix kwl arg unpiping in nested contexts (Closes #60)
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Jul 24, 2023
1 parent 60a66bd commit 5dcfc6f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,22 @@ defmodule Styler.Style.Pipes do

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

# If the first arg is a syntax-sugared kwl, we need to manually desugar it to cover all scenarios
arg =
case arg do
[{{:__block__, bm, _}, {:__block__, _, _}} | _] ->
if bm[:format] == :keyword do
{:__block__, [line: line, closing: [line: line]], [arg]}
else
arg
end

arg -> arg
end

{{:|>, [line: line], [arg, {fun, meta, args}]}, nil}
end

# `pipe_chain(a, b, c)` generates the ast for `a |> b |> c`
Expand Down
8 changes: 7 additions & 1 deletion test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,13 @@ defmodule Styler.Style.PipesTest do
end
end

describe "valid pipe starts" do
describe "valid pipe starts & unpiping" do
test "writes brackets for unpiped kwl" do
assert_style "foo(kwl: :arg) |> bar()", "[kwl: :arg] |> foo() |> bar()"
assert_style "%{a: foo(a: :b, c: :d) |> bar()}", "%{a: [a: :b, c: :d] |> foo() |> bar()}"
assert_style "%{a: foo([a: :b, c: :d]) |> bar()}", "%{a: [a: :b, c: :d] |> foo() |> bar()}"
end

test "allows fn" do
assert_style("""
fn
Expand Down

0 comments on commit 5dcfc6f

Please sign in to comment.