We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 78ced6b commit 8089b0dCopy full SHA for 8089b0d
4 files changed
CHANGELOG.md
@@ -7,6 +7,9 @@ they can and will change without that change being reflected in Styler's semanti
7
8
### Improvements
9
10
+- `enum |> Enum.sort() |> Enum.reverse()` => `Enum.sort(enum, :desc)`
11
+- Req pipe optimizations (see below!)
12
+
13
#### Req pipe optimizations
14
15
[Req](https://github.com/wojtekmach/req) is a popular HTTP Client. If you aren't using it, you can just ignore this whole section!
docs/pipes.md
@@ -110,6 +110,10 @@ a |> Enum.filter(fun) |> List.first(default) |> ...
110
# Styled:
111
a |> Enum.find(fun) |> ...
112
a |> Enum.find(default, fun) |> ...
113
114
+# Given:
115
+a |> Enum.sort() |> Enum.reverse() |> ...
116
+a |> Enum.sort(:desc) |> ...
117
```
118
119
## Unpiping Single Pipes
lib/style/pipes.ex
@@ -294,6 +294,22 @@ defmodule Styler.Style.Pipes do
294
defp fix_pipe({:|>, m, [lhs, {{:., m2, [{anon_fun, _, _}] = fun}, _, []}]}) when anon_fun in [:&, :fn],
295
do: {:|>, m, [lhs, {:then, m2, fun}]}
296
297
+ # `lhs |> Enum.sort([:asc, :desc]) |> Enum.reverse()` => `lhs |> Enum.sort(:desc | :asc)`
298
+ defp fix_pipe(
299
+ pipe_chain(
300
+ pm,
301
+ lhs,
302
+ {{:., _, [{_, _, [:Enum]}, :sort]} = sort, meta, sort_args},
303
+ {{:., _, [{_, _, [:Enum]}, :reverse]}, _, []}
304
+ ) = node
305
+ ) do
306
+ case sort_args do
307
+ [] -> {:|>, pm, [lhs, {sort, meta, [{:__block__, [line: meta[:line]], [:desc]}]}]}
308
+ [{_, m, [:desc]}] -> {:|>, pm, [lhs, {sort, meta, [{:__block__, m, [:asc]}]}]}
309
+ [{_, m, [:asc]}] -> {:|>, pm, [lhs, {sort, meta, [{:__block__, m, [:desc]}]}]}
310
+ _ -> node
311
+ end
312
313
# `lhs |> Enum.reverse() |> Enum.concat(enum)` => `lhs |> Enum.reverse(enum)`
314
defp fix_pipe(
315
pipe_chain(
test/style/pipes_test.exs
@@ -576,6 +576,13 @@ defmodule Styler.Style.PipesTest do
576
)
577
end
578
579
+ test "Enum.sort/Enum.reverse" do
580
+ assert_style("a |> Enum.sort(direction) |> Enum.reverse()")
581
+ assert_style("a |> Enum.sort(:asc) |> Enum.reverse()", "Enum.sort(a, :desc)")
582
+ assert_style("a |> Enum.sort(:desc) |> Enum.reverse()", "Enum.sort(a, :asc)")
583
+ assert_style("a |> Enum.sort() |> Enum.reverse()", "Enum.sort(a, :desc)")
584
585
586
test "reverse/concat" do
587
assert_style("a |> Enum.reverse() |> Enum.concat()")
588
assert_style("a |> Enum.reverse(bar) |> Enum.concat()")
0 commit comments