Skip to content

Commit

Permalink
Fix timex now/1 and today/1 rewrites. Closes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Aug 10, 2023
1 parent e8fa828 commit 9d1a299
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## main

### Fixes

* Timex related fixes:
* Rewrite `Timex.now/1` to `DateTime.now!/1` instead of `DateTime.utc_now/1`
* Only rewrite `Timex.today/0`, don't change `Timex.today/1`

## v0.8.3

### Improvements
Expand Down
15 changes: 10 additions & 5 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ defmodule Styler.Style.SingleNode do
defp style({{:., dm, [{:__aliases__, am, [:Logger]}, :warn]}, funm, args}),
do: {{:., dm, [{:__aliases__, am, [:Logger]}, :warning]}, funm, args}

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

defp style({{:., dm, [{:__aliases__, am, [:Timex]}, :now]}, funm, args}),
do: {{:., dm, [{:__aliases__, am, [:DateTime]}, :utc_now]}, funm, args}
# 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!()
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)
Expand Down
4 changes: 3 additions & 1 deletion test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("Logger.warn(foo, bar)", "Logger.warning(foo, bar)")
end

test "Timex.now -> DateTime.utc_now" do
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

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")|)
end

if Version.match?(System.version(), ">= 1.15.0-dev") do
Expand Down

0 comments on commit 9d1a299

Please sign in to comment.