Skip to content

Commit d0ecf1d

Browse files
committed
1.18+: change struct updates to map updates. Closes #199
1 parent e083b4b commit d0ecf1d

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ they can and will change without that change being reflected in Styler's semanti
1111

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

14+
#### Ex1.18+
15+
16+
Delete deprecated struct update syntax in favor of map update syntax.
17+
18+
```elixir
19+
# This
20+
%Struct{x | y}
21+
# Styles to this
22+
%{x | y}
23+
```
24+
25+
**WARNING** Double check your diffs to make sure your variable is pattern matching against the same struct if you want to harness 1.18's type checking features. (#199, h/t @SteffenDE)
26+
1427
#### Alias Lifting
1528

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

lib/style/deprecations.ex

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ defmodule Styler.Style.Deprecations do
6565
end
6666
end
6767

68+
if Version.match?(System.version(), ">= 1.18.0-dev") do
69+
# Struct update syntax was deprecated `%Foo{x | y} => %{x | y}`
70+
defp style({:%, _, [_struct, {:%{}, _, [{:|, _, _}]} = update]}), do: update
71+
end
72+
6873
# For ranges where `start > stop`, you need to explicitly include the step
6974
# Enum.slice(enumerable, 1..-2) => Enum.slice(enumerable, 1..-2//1)
7075
# String.slice("elixir", 2..-1) => String.slice("elixir", 2..-1//1)

test/style/deprecations_test.exs

+8
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,12 @@ defmodule Styler.Style.DeprecationsTest do
145145
assert_style "a |> x() |> :timer.seconds()"
146146
end
147147
end
148+
149+
describe "1.18+" do
150+
@describetag skip: Version.match?(System.version(), "< 1.18.0-dev")
151+
152+
test "struct update" do
153+
assert_style "%Foo{widget | bar: :baz}", "%{widget | bar: :baz}"
154+
end
155+
end
148156
end

test/style/pipes_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ defmodule Styler.Style.PipesTest do
408408
""",
409409
"""
410410
def halt(exec, halt_message) do
411-
put_halt_message(%__MODULE__{exec | halted: true}, halt_message)
411+
put_halt_message(%{exec | halted: true}, halt_message)
412412
end
413413
"""
414414
)

0 commit comments

Comments
 (0)