diff --git a/lib/style/pipes.ex b/lib/style/pipes.ex index 0e31e808..537c5ca2 100644 --- a/lib/style/pipes.ex +++ b/lib/style/pipes.ex @@ -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` diff --git a/test/style/pipes_test.exs b/test/style/pipes_test.exs index 7e4f3bf8..fa0a2b16 100644 --- a/test/style/pipes_test.exs +++ b/test/style/pipes_test.exs @@ -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