Skip to content

Commit 9404d5f

Browse files
committed
pipes: handle pipifying functions whose first arg is itself a pipe. closes #193
1 parent fc71aee commit 9404d5f

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ This release taught Styler to try just that little bit harder when doing alias l
2626
C.bar()
2727
C.baz()
2828

29+
### Fixes
30+
31+
- `pipes`: handle pipifying when the first arg is itself a pipe: `c(a |> b, d)` => `a |> b() |> c(d)` (#213, h/t @kybishop)
2932

3033
## 1.3.3
3134

lib/style/module_directives.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,16 @@ defmodule Styler.Style.ModuleDirectives do
370370
# - required: its first comes _after_ last, so we aren't promoting an alias that changes the meaning of the other alias we're doing
371371
# - preferred: take a collider we know we want to lift (we've seen it multiple times)
372372
lift =
373-
Enum.find(colliders, fn {[first | _], seen?} -> seen? and first > last end) ||
374-
Enum.find(colliders, fn {[first | _], _} -> first > last end) ||
375-
:collision_with_first
373+
Enum.reduce_while(colliders, :collision_with_first, fn
374+
{[first | _], true} = liftable, _ when first > last ->
375+
{:halt, liftable}
376+
377+
{[first | _], _false} = promotable, :collision_with_first when first > last ->
378+
{:cont, promotable}
379+
380+
_, result ->
381+
{:cont, result}
382+
end)
376383

377384
Map.put(lifts, first, lift)
378385

lib/style/pipes.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ defmodule Styler.Style.Pipes do
168168
{:cont, zipper, ctx}
169169

170170
true ->
171-
{:cont, Zipper.replace(zipper, {:|>, m, [pipe, {f, m, args}]}), ctx}
171+
# Recurse in case the function-looking is a multi pipe
172+
zipper
173+
|> Zipper.replace({:|>, m, [pipe, {f, m, args}]})
174+
|> run(ctx)
172175
end
173176
end
174177

test/style/pipes_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,9 @@ defmodule Styler.Style.PipesTest do
934934
assert_style ~s<"\#{#{pipe}}">
935935
end
936936

937+
test "pipifying pipes" do
938+
end
939+
937940
test "when it's not actually the first argument!" do
938941
assert_style """
939942
a
@@ -944,6 +947,7 @@ defmodule Styler.Style.PipesTest do
944947

945948
test "pipifying" do
946949
assert_style "d(a |> b |> c)", "a |> b() |> c() |> d()"
950+
assert_style("c(a |> b, d)", "a |> b() |> c(d)")
947951

948952
assert_style(
949953
"""

0 commit comments

Comments
 (0)