Skip to content

Commit a46c43f

Browse files
committed
pipify nested function calls with pipe as the first argument. closes #216
1 parent fc6fb5d commit a46c43f

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This release taught Styler to try just that little bit harder when doing alias l
4949
### Fixes
5050

5151
- `pipes`: handle pipifying when the first arg is itself a pipe: `c(a |> b, d)` => `a |> b() |> c(d)` (#214, h/t @kybishop)
52+
- `pipes`: handle pipifying nested functions `d(c(a |> b))` => `a |> b |> c() |> d` (#216, h/t @emkguts)
5253
- `with`: correctly handle a stabby `with` `, else: (_ -> :ok)` being rewritten to a case (#219, h/t @iamhassangm)
5354

5455
## 1.3.3

lib/style/pipes.ex

+6-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ defmodule Styler.Style.Pipes do
132132

133133
# a(b |> c[, ...args])
134134
# The first argument to a function-looking node is a pipe.
135-
# Maybe pipe the whole thing?
135+
# Maybe pipify the whole thing?
136136
def run({{f, m, [{:|>, _, _} = pipe | args]}, _} = zipper, ctx) do
137137
parent =
138138
case Zipper.up(zipper) do
@@ -168,10 +168,11 @@ defmodule Styler.Style.Pipes do
168168
{:cont, zipper, ctx}
169169

170170
true ->
171-
# Recurse in case this is a multi pipe
172-
zipper
173-
|> Zipper.replace({:|>, m, [pipe, {f, m, args}]})
174-
|> run(ctx)
171+
zipper = Zipper.replace(zipper, {:|>, m, [pipe, {f, m, args}]})
172+
# it's possible this is a nested function call `c(b(a |> b))`, so we should walk up the tree for de-nesting
173+
zipper = Zipper.up(zipper) || zipper
174+
# recursion ensures we get those nested function calls and any additional pipes
175+
run(zipper, ctx)
175176
end
176177
end
177178

test/style/pipes_test.exs

+2-3
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,8 @@ defmodule Styler.Style.PipesTest do
943943
end
944944

945945
test "pipifying" do
946-
assert_style "d(a |> b |> c)", "a |> b() |> c() |> d()"
947-
assert_style("c(a |> b, d)", "a |> b() |> c(d)")
948-
946+
assert_style("e(d(a |> b |> c), f)", "a |> b() |> c() |> d() |> e(f)")
947+
949948
assert_style(
950949
"""
951950
# d

test/support/style_case.ex

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ defmodule Styler.StyleCase do
8282
_ -> false
8383
end
8484

85+
# This isn't enabled in any test, but can be a useful audit
8586
if @ordered_siblings do
8687
case Zipper.left(zipper) do
8788
{{_, prev_meta, _} = prev, _} ->

0 commit comments

Comments
 (0)