Skip to content

Commit

Permalink
Fix bug in delete => drop logic. Closes #177
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Jun 13, 2024
1 parent b156a2a commit d95ad27
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ defmodule Styler.Style.SingleNode do
defp style({{:., dm, [{_, _, [unquote(m)]} = module, :merge]}, m, [lhs, [{key, value}]]}),
do: {{:., dm, [module, :put]}, m, [lhs, key, value]}

# lhs |> Map.drop([key]) => lhs |> Map.delete(key)
defp style({:|>, pm, [lhs, {{:., dm, [{_, _, [unquote(m)]} = module, :drop]}, m, [{:__block__, _, [[key]]}]}]}),
do: {:|>, pm, [lhs, {{:., dm, [module, :delete]}, m, [key]}]}
# (lhs |>) Map.drop([key]) => Map.delete(key)
defp style({{:., dm, [{_, _, [unquote(m)]} = module, :drop]}, m, [{:__block__, _, [[{op, _, _} = key]]}]})
when op != :|,
do: {{:., dm, [module, :delete]}, m, [key]}

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

# Timex.now() => DateTime.utc_now()
Expand Down
2 changes: 2 additions & 0 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("foo |> #{module}.drop([]) |> bar()")
assert_style("foo |> #{module}.drop([a, b]) |> bar()")
assert_style("foo |> #{module}.drop(keys) |> bar()")
assert_style("foo |> #{module}.drop([:key | others]) |> bar()")
end
end

Expand All @@ -64,6 +65,7 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("#{module}.drop(foo, [])")
assert_style("#{module}.drop(foo, [a, b])")
assert_style("#{module}.drop(foo, keys)")
assert_style("#{module}.drop([:key | others])")
end
end
end
Expand Down

0 comments on commit d95ad27

Please sign in to comment.