Skip to content

Commit

Permalink
fix |> Timex.now() being mistaken for Timex.now/0, #66
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Sep 22, 2023
1 parent 52b6066 commit 9147eb4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## main

### Fixes

* fix mistaking `Timex.now/1` in a pipe for `Timex.now/0` (#66, h/t @sabiwara)

### Removed style

* stop rewriting `Timex.today/0` given that we allow `Timex.today/1` -- too inconsistent.

## v0.9.4

### Improvements
Expand Down
7 changes: 5 additions & 2 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,14 @@ defmodule Styler.Style.Pipes do
# a |> fun => a |> fun()
defp fix_pipe({:|>, m, [lhs, {fun, m2, nil}]}), do: {:|>, m, [lhs, {fun, m2, []}]}
# rewrite anonymous function invocation to use `then/2`
# `a |> (& &1).() |> c` => `a |> then(& &1) |> c()`
# `a |> (fn x -> x end).() |> c` => `a |> then(fn x -> x end) |> c()`
# `a |> (& &1).() |> c()` => `a |> then(& &1) |> c()`
defp fix_pipe({:|>, m, [lhs, {{:., m2, [{anon_fun, _, _}] = fun}, _, []}]}) when anon_fun in [:&, :fn],
do: {:|>, m, [lhs, {:then, m2, fun}]}

# `|> Timex.now()` => `|> DateTime.now!()`
defp fix_pipe({:|>, m, [lhs, {{:., dm, [{:__aliases__, am, [:Timex]}, :now]}, funm, []}]}),
do: {:|>, m, [lhs, {{:., dm, [{:__aliases__, am, [:DateTime]}, :now!]}, funm, []}]}

# `lhs |> Enum.reverse() |> Enum.concat(enum)` => `lhs |> Enum.reverse(enum)`
defp fix_pipe(
pipe_chain(
Expand Down
17 changes: 9 additions & 8 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,30 @@ defmodule Styler.Style.SingleNode do
{:__block__, meta, [number]}
end

## DEPRECATED & INEFFICIENT FUNCTION REWRITES
# Keep in mind when rewriting a `/n::pos_integer` arity function here that it should also be added
# to the pipes rewriting rules, where it will appear as `/n-1`

# Enum.into(enum, empty_map[, ...]) => Map.new(enum[, ...])
defp style({{:., dm, [{:__aliases__, am, [:Enum]}, :into]}, funm, [enum, collectable | rest]} = node) do
if Style.empty_map?(collectable), do: {{:., dm, [{:__aliases__, am, [:Map]}, :new]}, funm, [enum | rest]}, else: node
end

# Logger.warn -> Logger.warning
# Logger.warn => Logger.warning
defp style({{:., dm, [{:__aliases__, am, [:Logger]}, :warn]}, funm, args}),
do: {{:., dm, [{:__aliases__, am, [:Logger]}, :warning]}, funm, args}

# Timex.today() => DateTime.utc_today()
defp style({{:., dm, [{:__aliases__, am, [:Timex]}, :today]}, funm, []}),
do: {{:., dm, [{:__aliases__, am, [:Date]}, :utc_today]}, funm, []}

# Timex.now() => DateTime.utc_now()
defp style({{:., dm, [{:__aliases__, am, [:Timex]}, :now]}, funm, []}),
do: {{:., dm, [{:__aliases__, am, [:DateTime]}, :utc_now]}, funm, []}

# Timex.now("Europe/London") => DateTime.now!()
# Timex.now(tz) => DateTime.now!(tz)
defp style({{:., dm, [{:__aliases__, am, [:Timex]}, :now]}, funm, [tz]}),
do: {{:., dm, [{:__aliases__, am, [:DateTime]}, :now!]}, funm, [tz]}

if Version.match?(System.version(), ">= 1.15.0-dev") do
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :lt -> {DateTime,NaiveDateTime,Time,Date}.before?(a, b)
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :gt -> {DateTime,NaiveDateTime,Time,Date}.after?(a, b)
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :lt => {DateTime,NaiveDateTime,Time,Date}.before?(a, b)
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :gt => {DateTime,NaiveDateTime,Time,Date}.after?(a, b)
defp style({:==, _, [{{:., dm, [{:__aliases__, am, [mod]}, :compare]}, funm, args}, {:__block__, _, [result]}]})
when mod in ~w[DateTime NaiveDateTime Time Date]a and result in [:lt, :gt] do
fun = if result == :lt, do: :before?, else: :after?
Expand Down
27 changes: 20 additions & 7 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,29 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("Logger.warn(foo, bar)", "Logger.warning(foo, bar)")
end

test "Timex.now => DateTime.utc_now/now!" do
assert_style("Timex.now()", "DateTime.utc_now()")
assert_style(~S|Timex.now("Some/Timezone")|, ~S|DateTime.now!("Some/Timezone")|)
end
describe "Timex.now/0,1" do
test "Timex.now/0 => DateTime.utc_now/0" do
assert_style("Timex.now()", "DateTime.utc_now()")
assert_style("Timex.now() |> foo() |> bar()", "DateTime.utc_now() |> foo() |> bar()")
end

test "Timex.today => Date.utc_today" do
assert_style("Timex.today()", "Date.utc_today()")
assert_style(~S|Timex.today("Some/Timezone")|, ~S|Timex.today("Some/Timezone")|)
test "Timex.now/1 => DateTime.now!/1" do
assert_style("Timex.now(tz)", "DateTime.now!(tz)")

assert_style("""
timezone
|> Timex.now()
|> foo()
""",
"""
timezone
|> DateTime.now!()
|> foo()
""")
end
end


if Version.match?(System.version(), ">= 1.15.0-dev") do
test "{DateTime,NaiveDateTime,Time,Date}.compare to {DateTime,NaiveDateTime,Time,Date}.before?" do
assert_style("DateTime.compare(foo, bar) == :lt", "DateTime.before?(foo, bar)")
Expand Down

0 comments on commit 9147eb4

Please sign in to comment.