Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Date/DateTime interactions #62

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Ex${{matrix.elixir}}/OTP${{matrix.otp}}
strategy:
matrix:
elixir: ['1.14.2']
elixir: ['1.14.2', '1.15.0']
otp: ['25.1.2']
steps:
- uses: actions/checkout@v3
Expand Down
17 changes: 17 additions & 0 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ 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}

defp style({{:., dm, [{:__aliases__, am, [:Timex]}, :now]}, funm, args}),
do: {{:., dm, [{:__aliases__, am, [:DateTime]}, :utc_now]}, funm, args}
Comment on lines +99 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me sit on these ones -- i'd considered doing the same earlier, but nixed it as too specific to adbe internals 🤔 still, this repo is all about forcing our views on others xD


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)
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?
{{:., dm, [{:__aliases__, am, [mod]}, fun]}, funm, args}
end
end

# Remove parens from 0 arity funs (Credo.Check.Readability.ParenthesesOnZeroArityDefs)
defp style({def, dm, [{fun, funm, []} | rest]}) when def in ~w(def defp)a and is_atom(fun),
do: style({def, dm, [{fun, Keyword.delete(funm, :closing), nil} | rest]})
Expand Down
24 changes: 24 additions & 0 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("Logger.warn(foo, bar)", "Logger.warning(foo, bar)")
end

test "Timex.now -> DateTime.utc_now" do
assert_style("Timex.now()", "DateTime.utc_now()")
end

test "Timex.today -> Date.utc_today" do
assert_style("Timex.today()", "Date.utc_today()")
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)")
assert_style("NaiveDateTime.compare(foo, bar) == :lt", "NaiveDateTime.before?(foo, bar)")
assert_style("Time.compare(foo, bar) == :lt", "Time.before?(foo, bar)")
assert_style("Date.compare(foo, bar) == :lt", "Date.before?(foo, bar)")
end

test "{DateTime,NaiveDateTime,Time,Date}.compare to {DateTime,NaiveDateTime,Time,Date}.after?" do
assert_style("DateTime.compare(foo, bar) == :gt", "DateTime.after?(foo, bar)")
assert_style("NaiveDateTime.compare(foo, bar) == :gt", "NaiveDateTime.after?(foo, bar)")
assert_style("Time.compare(foo, bar) == :gt", "Time.after?(foo, bar)")
assert_style("Time.compare(foo, bar) == :gt", "Time.after?(foo, bar)")
end
end

describe "def / defp" do
test "0-arity functions have parens removed" do
assert_style("def foo(), do: :ok", "def foo, do: :ok")
Expand Down