Skip to content

Commit e02b09d

Browse files
authored
Merge pull request #126 from tfiedlerdejanze/rewrite-then-with-named-fun-reference
Rewrite then/2 when function is a named function reference
2 parents 6876dff + 3a5cd94 commit e02b09d

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/style/pipes.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ defmodule Styler.Style.Pipes do
142142

143143
# a |> fun => a |> fun()
144144
defp fix_pipe({:|>, m, [lhs, {fun, m2, nil}]}), do: {:|>, m, [lhs, {fun, m2, []}]}
145+
# a |> then(&fun/1) |> c => a |> fun() |> c()
146+
defp fix_pipe({:|>, m, [lhs, {:then, _, [{:&, _, [{:/, _, [{fun, m2, _}, _]}]}]}]}), do: {:|>, m, [lhs, {fun, m2, []}]}
145147
# Credo.Check.Readability.PipeIntoAnonymousFunctions
146148
# rewrite anonymous function invocation to use `then/2`
147149
# `a |> (& &1).() |> c()` => `a |> then(& &1) |> c()`

test/style/pipes_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,13 @@ defmodule Styler.Style.PipesTest do
498498
assert_style("a |> (fn x -> x end).() |> c", "a |> then(fn x -> x end) |> c()")
499499
end
500500

501+
test "rewrites then/2 when the passed function is a named function reference" do
502+
assert_style("a |> then(&fun/1) |> c", "a |> fun() |> c()")
503+
assert_style("a |> then(&DateTime.from_is8601/1) |> c", "a |> DateTime.from_is8601() |> c()")
504+
assert_style("a |> then(&DateTime.from_is8601/1)", "DateTime.from_is8601(a)")
505+
assert_style("a |> then(&fun(&1, d)) |> c", "a |> then(&fun(&1, d)) |> c()")
506+
end
507+
501508
test "adds parens to 1-arity pipes" do
502509
assert_style("a |> b |> c", "a |> b() |> c()")
503510
end

0 commit comments

Comments
 (0)