Skip to content

Commit

Permalink
ex1.17+: replace :timer.units(x) with the new to_timeout(unit: x)
Browse files Browse the repository at this point in the history
… for `hours|minutes|seconds`

Closes #218
  • Loading branch information
novaugust committed Feb 20, 2025
1 parent 8d921d9 commit c6aab63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ they can and will change without that change being reflected in Styler's semanti

### Improvements

#### Ex1.17+

Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds`

#### Alias Lifting

This release taught Styler to try just that little bit harder when doing alias lifting.

- general improvements around conflict detection, lifting in more correct places and fewer incorrect places (#193, h/t @jsw800)
Expand Down
11 changes: 9 additions & 2 deletions lib/style/deprecations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ defmodule Styler.Style.Deprecations do
do: {:|>, m, [lhs, {f, fm, [lob, opts]}]}
end

if Version.match?(System.version(), ">= 1.17.0-dev") do
for {erl, ex} <- [hours: :hour, minutes: :minute, seconds: :second] do
defp style({{:., _, [{:__block__, _, [:timer]}, unquote(erl)]}, fm, [x]}),
do: {:to_timeout, fm, [[{{:__block__, [format: :keyword, line: fm[:line]], [unquote(ex)]}, x}]]}
end
end

# For ranges where `start > stop`, you need to explicitly include the step
# Enum.slice(enumerable, 1..-2) => Enum.slice(enumerable, 1..-2//1)
# String.slice("elixir", 2..-1) => String.slice("elixir", 2..-1//1)
Expand Down Expand Up @@ -98,7 +105,7 @@ defmodule Styler.Style.Deprecations do

defp style(node), do: node

defp rewrite_range_match({:.., dm, [first, {_, m, _} = last]}), do: {:..//, dm, [first, last, {:_, m, nil}]}
defp rewrite_range_match({:.., dm, [first, {_, m, _} = last]}), do: {:"..//", dm, [first, last, {:_, m, nil}]}
defp rewrite_range_match(x), do: x

defp add_step_to_date_range?(first, last) do
Expand All @@ -117,7 +124,7 @@ defmodule Styler.Style.Deprecations do
{:ok, stop} <- extract_value_from_range(last),
true <- start > stop do
step = {:__block__, [token: "1", line: lm[:line]], [1]}
{:..//, rm, [first, last, step]}
{:"..//", rm, [first, last, step]}
else
_ -> range
end
Expand Down
46 changes: 30 additions & 16 deletions test/style/deprecations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ defmodule Styler.Style.DeprecationsTest do
assert_style "foo |> List.zip", "Enum.zip(foo)"
end

describe "1.16 deprecations" do
@describetag skip: Version.match?(System.version(), "< 1.16.0-dev")

test "File.stream!(path, modes, line_or_bytes) to File.stream!(path, line_or_bytes, modes)" do
assert_style(
"File.stream!(path, [encoding: :utf8, trim_bom: true], :line)",
"File.stream!(path, :line, encoding: :utf8, trim_bom: true)"
)

assert_style(
"f |> File.stream!([encoding: :utf8, trim_bom: true], :line) |> Enum.take(2)",
"f |> File.stream!(:line, encoding: :utf8, trim_bom: true) |> Enum.take(2)"
)
end
end

test "~R is deprecated in favor of ~r" do
assert_style(~s|Regex.match?(~R/foo/, "foo")|, ~s|Regex.match?(~r/foo/, "foo")|)
end
Expand Down Expand Up @@ -131,4 +115,34 @@ defmodule Styler.Style.DeprecationsTest do
assert_style("foo |> bar() |> #{mod}.slice(x..y)")
end
end

describe "1.16+" do
@describetag skip: Version.match?(System.version(), "< 1.16.0-dev")

test "File.stream!(path, modes, line_or_bytes) to File.stream!(path, line_or_bytes, modes)" do
assert_style(
"File.stream!(path, [encoding: :utf8, trim_bom: true], :line)",
"File.stream!(path, :line, encoding: :utf8, trim_bom: true)"
)

assert_style(
"f |> File.stream!([encoding: :utf8, trim_bom: true], :line) |> Enum.take(2)",
"f |> File.stream!(:line, encoding: :utf8, trim_bom: true) |> Enum.take(2)"
)
end
end

describe "1.17+" do
@describetag skip: Version.match?(System.version(), "< 1.17.0-dev")

test "to_timeout/1 vs :timer.units(x)" do
assert_style ":timer.hours(x)", "to_timeout(hour: x)"
assert_style ":timer.minutes(x)", "to_timeout(minute: x)"
assert_style ":timer.seconds(x)", "to_timeout(second: x)"

assert_style "a |> x() |> :timer.hours()"
assert_style "a |> x() |> :timer.minutes()"
assert_style "a |> x() |> :timer.seconds()"
end
end
end

0 comments on commit c6aab63

Please sign in to comment.