Skip to content

Commit

Permalink
Fix kwl arg unpiping in nested contexts (#61)
Browse files Browse the repository at this point in the history
Closes #60
  • Loading branch information
novaugust authored Jul 24, 2023
1 parent 60a66bd commit c932755
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,23 @@ 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 c932755

Please sign in to comment.