Skip to content

Commit e083b4b

Browse files
committed
ex1.17+: replace :timer.units(x) with the new to_timeout(unit: x) for hours|minutes|seconds
Closes #218
1 parent 8d921d9 commit e083b4b

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ they can and will change without that change being reflected in Styler's semanti
77

88
### Improvements
99

10+
#### Ex1.17+
11+
12+
Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds`
13+
14+
#### Alias Lifting
15+
1016
This release taught Styler to try just that little bit harder when doing alias lifting.
1117

1218
- general improvements around conflict detection, lifting in more correct places and fewer incorrect places (#193, h/t @jsw800)

lib/style/deprecations.ex

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ defmodule Styler.Style.Deprecations do
5858
do: {:|>, m, [lhs, {f, fm, [lob, opts]}]}
5959
end
6060

61+
if Version.match?(System.version(), ">= 1.17.0-dev") do
62+
for {erl, ex} <- [hours: :hour, minutes: :minute, seconds: :second] do
63+
defp style({{:., _, [{:__block__, _, [:timer]}, unquote(erl)]}, fm, [x]}),
64+
do: {:to_timeout, fm, [[{{:__block__, [format: :keyword, line: fm[:line]], [unquote(ex)]}, x}]]}
65+
end
66+
end
67+
6168
# For ranges where `start > stop`, you need to explicitly include the step
6269
# Enum.slice(enumerable, 1..-2) => Enum.slice(enumerable, 1..-2//1)
6370
# String.slice("elixir", 2..-1) => String.slice("elixir", 2..-1//1)

test/style/deprecations_test.exs

+30-16
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,6 @@ defmodule Styler.Style.DeprecationsTest do
6767
assert_style "foo |> List.zip", "Enum.zip(foo)"
6868
end
6969

70-
describe "1.16 deprecations" do
71-
@describetag skip: Version.match?(System.version(), "< 1.16.0-dev")
72-
73-
test "File.stream!(path, modes, line_or_bytes) to File.stream!(path, line_or_bytes, modes)" do
74-
assert_style(
75-
"File.stream!(path, [encoding: :utf8, trim_bom: true], :line)",
76-
"File.stream!(path, :line, encoding: :utf8, trim_bom: true)"
77-
)
78-
79-
assert_style(
80-
"f |> File.stream!([encoding: :utf8, trim_bom: true], :line) |> Enum.take(2)",
81-
"f |> File.stream!(:line, encoding: :utf8, trim_bom: true) |> Enum.take(2)"
82-
)
83-
end
84-
end
85-
8670
test "~R is deprecated in favor of ~r" do
8771
assert_style(~s|Regex.match?(~R/foo/, "foo")|, ~s|Regex.match?(~r/foo/, "foo")|)
8872
end
@@ -131,4 +115,34 @@ defmodule Styler.Style.DeprecationsTest do
131115
assert_style("foo |> bar() |> #{mod}.slice(x..y)")
132116
end
133117
end
118+
119+
describe "1.16+" do
120+
@describetag skip: Version.match?(System.version(), "< 1.16.0-dev")
121+
122+
test "File.stream!(path, modes, line_or_bytes) to File.stream!(path, line_or_bytes, modes)" do
123+
assert_style(
124+
"File.stream!(path, [encoding: :utf8, trim_bom: true], :line)",
125+
"File.stream!(path, :line, encoding: :utf8, trim_bom: true)"
126+
)
127+
128+
assert_style(
129+
"f |> File.stream!([encoding: :utf8, trim_bom: true], :line) |> Enum.take(2)",
130+
"f |> File.stream!(:line, encoding: :utf8, trim_bom: true) |> Enum.take(2)"
131+
)
132+
end
133+
end
134+
135+
describe "1.17+" do
136+
@describetag skip: Version.match?(System.version(), "< 1.17.0-dev")
137+
138+
test "to_timeout/1 vs :timer.units(x)" do
139+
assert_style ":timer.hours(x)", "to_timeout(hour: x)"
140+
assert_style ":timer.minutes(x)", "to_timeout(minute: x)"
141+
assert_style ":timer.seconds(x)", "to_timeout(second: x)"
142+
143+
assert_style "a |> x() |> :timer.hours()"
144+
assert_style "a |> x() |> :timer.minutes()"
145+
assert_style "a |> x() |> :timer.seconds()"
146+
end
147+
end
134148
end

0 commit comments

Comments
 (0)