Skip to content

Commit

Permalink
Merge branch 'main' into me/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed May 16, 2024
2 parents 4a0d416 + cdf8164 commit e857163
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ See the moduledoc for `Styler.Style.Configs` for more.
* `@derive`: move `@derive` before `defstruct|schema|embedded_schema` declarations (fixes compiler warning!) #134
* strings: rewrite double-quoted strings to use `~s` when there's 4+ escaped double-quotes
(`"\"\"\"\""` -> `~s("""")`) (`Credo.Check.Readability.StringSigils`) #146
* `Map.drop(foo, [single_key])` => `Map.delete(foo, single_key)` #161
* `Keyword.drop(foo, [single_key])` => `Keyword.delete(foo, single_key)` #161

### Fixes

Expand All @@ -61,6 +63,7 @@ See the moduledoc for `Styler.Style.Configs` for more.
* pipes: fix a comment-shifting scenario when unpiping
* `Timex.now/1` will no longer be rewritten to `DateTime.now!/1` due to Timex accepting a wider domain of "timezones" than the stdlib (#145, h/t @ivymarkwell)
* `with`: skip nodes which (unexpectedly) do not contain a `do` body (#158, h/t @DavidB59)
* `then(&fun/1)`: fix false positives on arithmetic `&1 + x / 1` (#164, h/t @aenglisc)

### Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ defmodule Styler.Style.Pipes do

# a |> then(&fun/1) |> c => a |> fun() |> c()
# recurses to add the `()` to `fun` as it gets unwound
defp fix_pipe({:|>, m, [lhs, {:then, _, [{:&, _, [{:/, _, [fun, {:__block__, _, [1]}]}]}]}]}),
defp fix_pipe({:|>, m, [lhs, {:then, _, [{:&, _, [{:/, _, [{_, _, nil} = fun, {:__block__, _, [1]}]}]}]}]}),
do: fix_pipe({:|>, m, [lhs, fun]})

# Credo.Check.Readability.PipeIntoAnonymousFunctions
Expand Down
4 changes: 4 additions & 0 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ defmodule Styler.Style.SingleNode do
# Map.merge(foo, one_key: :bar) => Map.put(foo, :one_key, :bar)
defp style({{:., dm, [{_, _, [unquote(mod)]} = module, :merge]}, m, [lhs, [{key, value}]]}),
do: {{:., dm, [module, :put]}, m, [lhs, key, value]}

# Map.drop(foo, [one_key]) => Map.delete(foo, one_key)
defp style({{:., dm, [{_, _, [unquote(mod)]} = module, :drop]}, m, [lhs, {:__block__, _, [[key]]}]}),
do: {{:., dm, [module, :delete]}, m, [lhs, key]}
end

# Timex.now() => DateTime.utc_now()
Expand Down
1 change: 1 addition & 0 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ defmodule Styler.Style.PipesTest do
test "rewrites then/2 when the passed function is a named function reference" do
assert_style "a |> then(&fun/1) |> c", "a |> fun() |> c()"
assert_style "a |> then(&(&1 / 1)) |> c", "a |> Kernel./(1) |> c()"
assert_style "a |> then(&(&1 * 2 / 1)) |> c()"
assert_style "a |> then(&fun/1)", "fun(a)"
assert_style "a |> then(&fun(&1)) |> c", "a |> fun() |> c()"
assert_style "a |> then(&fun(&1, d)) |> c", "a |> fun(d) |> c()"
Expand Down
10 changes: 10 additions & 0 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ defmodule Styler.Style.SingleNodeTest do
end
end

test "{Map/Keyword}.drop with a single key" do
for module <- ~w(Map Keyword) do
assert_style("#{module}.drop(foo, [key])", "#{module}.delete(foo, key)")
assert_style("#{module}.drop(foo, [:key])", "#{module}.delete(foo, :key)")
assert_style("#{module}.drop(foo, [])")
assert_style("#{module}.drop(foo, [a, b])")
assert_style("#{module}.drop(foo, keys)")
end
end

describe "Timex.now/0,1" do
test "Timex.now/0 => DateTime.utc_now/0" do
assert_style("Timex.now()", "DateTime.utc_now()")
Expand Down

0 comments on commit e857163

Please sign in to comment.