Skip to content

Commit

Permalink
Bug: Pipes(#16)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
novaugust authored Apr 16, 2023
1 parent 74114e4 commit 3c7816f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
"""
Expand Down
11 changes: 9 additions & 2 deletions test/support/style_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3c7816f

Please sign in to comment.