We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a49a240 commit b01d328Copy full SHA for b01d328
lib/style/pipes.ex
@@ -145,8 +145,13 @@ defmodule Styler.Style.Pipes do
145
# a |> then(&fun/1) |> c => a |> fun() |> c()
146
defp fix_pipe({:|>, m, [lhs, {:then, _, [{:&, _, [{:/, _, [{fun, m2, _}, _]}]}]}]}), do: {:|>, m, [lhs, {fun, m2, []}]}
147
# a |> then(&fun(&1, d)) |> c => a |> fun(d) |> c()
148
- defp fix_pipe({:|>, m, [lhs, {:then, _, [{:&, _, [{fun, m2, [{:&, _, _} | args]}]}]}]}),
149
- do: {:|>, m, [lhs, {fun, m2, args}]}
+ defp fix_pipe({:|>, m, [lhs, {:then, _, [{:&, _, [{fun, m2, [{:&, _, _} | args]}]}]}]} = pipe) do
+ rewrite = {fun, m2, args}
150
+ # if `&1` is referenced more than once, we have to continue using `then`
151
+ if rewrite |> Zipper.zip() |> Zipper.any?(&match?({:&, _, _}, &1)),
152
+ do: pipe,
153
+ else: {:|>, m, [lhs, rewrite]}
154
+ end
155
156
# Credo.Check.Readability.PipeIntoAnonymousFunctions
157
# rewrite anonymous function invocation to use `then/2`
lib/zipper.ex
@@ -381,12 +381,23 @@ defmodule Styler.Zipper do
381
"""
382
@spec find(zipper, direction :: :prev | :next, predicate :: (tree -> any)) :: zipper | nil
383
def find({tree, _} = zipper, direction \\ :next, predicate)
384
- when direction in [:next, :prev] and is_function(predicate) do
+ when direction in [:next, :prev] and is_function(predicate, 1) do
385
if predicate.(tree) do
386
zipper
387
else
388
zipper = if direction == :next, do: next(zipper), else: prev(zipper)
389
zipper && find(zipper, direction, predicate)
390
end
391
392
+
393
+ @doc "Traverses `zipper`, returning true when `fun.(Zipper.node(zipper))` is truthy, or false otherwise"
394
+ @spec any?(zipper, (tree -> term)) :: boolean()
395
+ def any?({_, _} = zipper, fun) when is_function(fun, 1) do
396
+ zipper
397
+ |> traverse_while(false, fn {tree, _} = zipper, _ ->
398
+ # {nil, nil} optimizes to not go back to the top of the zipper on a hit
399
+ if fun.(tree), do: {:halt, {nil, nil}, true}, else: {:cont, zipper, false}
400
+ end)
401
+ |> elem(1)
402
403
0 commit comments