Repository: adobe/elixir-styler (elixir)

Issue:
Keyword list as sole argument in a pipe in a map: syntax error after formatting
## Versions

* Elixir: 1.15.0
* Styler: 0.8.1

## Example Input

```elixir
    %{
      a: Foo.bar(baz: true) |> Quux.quux()
    }
```

## Current output

```elixir
    %{
      a: baz: true |> Foo.bar() |> Quux.quux()
    }
```

## Expected output

```elixir
    %{
      a: [baz: true] |> Foo.bar() |> Quux.quux()
    }
```


Pull request description:
Fix kwl arg unpiping in nested contexts 
Closes #60

Patch:
diff --git a/lib/style/pipes.ex b/lib/style/pipes.ex
index 0e31e80..b7fb510 100644
--- a/lib/style/pipes.ex
+++ b/lib/style/pipes.ex
@@ -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`


Test patch:
diff --git a/test/style/pipes_test.exs b/test/style/pipes_test.exs
index 7e4f3bf..f42bf56 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