Repository: adobe/elixir-styler (elixir)

Issue:
[Bug] Invalid code produced when piping operator statement with parentheses into function
## Elixir / OTP Version

use `elixir --version`

## Current Behaviour

While debugging some code I wrote the following:

```elixir
(call(a, b) == 1) |> dbg()
```

`styler` output the following, invalid code:


```elixir
call(a, b) |> ==(1) |> dbg()
```

## Expected Behaviour

I would expect the output to remain unchanged, or resolve to this:

```elixir
dbg(call(a, b) == 1)
```

Pull request description:
Fix Pipes Bugs
Closes #15 

- `a |> b` created invalid ast due to `b` having ast `{:b, _meta, nil = _args}`, causing us to create the improper list `[a | nil]`
- `==` was mistakenly not included in the list of math/comparison operators that are valid starts to a pipe

also snuck in another QOL improvement for helping show invalid AST when running tests

Patch:
diff --git a/lib/style/pipes.ex b/lib/style/pipes.ex
index b816496..2985718 100644
--- a/lib/style/pipes.ex
+++ b/lib/style/pipes.ex
@@ -31,7 +31,7 @@ defmodule Styler.Style.Pipes do
   def run({{:|>, _, [lhs, {fun, meta, args}]}, _} = zipper, ctx) do
     if valid_pipe_start?(lhs) do
       # `a |> f(b, c)` => `f(a, b, c)`
-      {:cont, Zipper.replace(zipper, {fun, meta, [lhs | args]}), ctx}
+      {:cont, Zipper.replace(zipper, {fun, meta, [lhs | args || []]}), ctx}
     else
       zipper = fix_start(zipper)
       {maybe_block, _, _} = lhs
@@ -170,7 +170,7 @@ defmodule Styler.Style.Pipes do
   # most of these values were lifted directly from credo's pipe_chain_start.ex
   @value_constructors ~w(% %{} .. <<>> @ {} & fn)a
   @simple_operators ~w(++ -- && ||)a
-  @math_operators ~w(- * + / > < <= >=)a
+  @math_operators ~w(- * + / > < <= >= ==)a
   @binary_operators ~w(<> <- ||| &&& <<< >>> <<~ ~>> <~ ~> <~> <|> ^^^ ~~~)a
   defp valid_pipe_start?({op, _, _})
        when op in @value_constructors or op in @simple_operators or op in @math_operators or op in @binary_operators,


Test patch:
diff --git a/test/style/pipes_test.exs b/test/style/pipes_test.exs
index 0ab3652..2eb8dba 100644
--- a/test/style/pipes_test.exs
+++ b/test/style/pipes_test.exs
@@ -132,7 +132,7 @@ defmodule Styler.Style.PipesTest do
     end
   end
 
-  describe "run on single pipe + start issues" do
+  describe "single pipe + start issues" do
     test "anon functio is finen" do
       assert_style("""
       fn
@@ -159,11 +159,19 @@ defmodule Styler.Style.PipesTest do
     end
   end
 
-  describe "run on single pipe issues" do
+  describe "single pipe issues" do
     test "fixes single pipe" do
       assert_style("a |> f()", "f(a)")
     end
 
+    test "recognizes `==` as a valid pipe start" do
+      assert_style("(bar() == 1) |> foo()", "foo(bar() == 1)")
+    end
+
+    test "handles 1-arity functions written without parens" do
+      assert_style("x |> bar", "bar(x)")
+    end
+
     test "fixes single pipe in function head" do
       assert_style(
         """
diff --git a/test/support/style_case.ex b/test/support/style_case.ex
index 6aa7017..b0697cd 100644
--- a/test/support/style_case.ex
+++ b/test/support/style_case.ex
@@ -66,7 +66,14 @@ defmodule Styler.StyleCase do
       |> Zipper.traverse_while(%{comments: comments, file: "test"}, &style.run/2)
 
     styled_ast = Zipper.root(zipper)
-    styled_code = Styler.quoted_to_string(styled_ast, comments)
-    {styled_ast, styled_code, comments}
+
+    try do
+      styled_code = Styler.quoted_to_string(styled_ast, comments)
+      {styled_ast, styled_code, comments}
+    rescue
+      exception ->
+        IO.inspect(styled_ast, label: [IO.ANSI.red(), "**Style created invalid ast:**", IO.ANSI.light_red()])
+        reraise exception, __STACKTRACE__
+    end
   end
 end