Skip to content

Commit

Permalink
ship struct update to map update changes after all
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Feb 20, 2025
1 parent d015a99 commit 6896d97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
17 changes: 7 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ This release taught Styler to try just that little bit harder when doing alias l
C.bar()
C.baz()

#### Ex1.17+

- Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds`
- Handle `, else: (_ -> x)` bugs introduced by `(_ -> x)` being termed a literal (#219, h/t @iamhassangm)

#### Ex1.19+ (experimental)
#### Struct Updates => Map Updates

1.19 deprecates struct update syntax in favor of map update syntax. Styler will do this update for you if you're on Elixir 1.19.0-dev or later.
1.19 deprecates struct update syntax in favor of map update syntax.

```elixir
# This
Expand All @@ -53,15 +48,17 @@ This release taught Styler to try just that little bit harder when doing alias l
%{x | y}
```

**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.
**WARNING** Double check your diffs to make sure your variable is pattern matching against the same struct if you want to harness 1.19's type checking features. Apologies to folks who hoped Styler would do this step for you <3 (#199, h/t @SteffenDE)

#### Ex1.17+

A future version of Styler may be smart enough to do this check for you and perform the appropriate updates to the assignment location; no guarantees though. Track via #199, h/t @SteffenDE
- Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds` (This style is only applied if you're on 1.17+)

### Fixes

- `pipes`: handle pipifying when the first arg is itself a pipe: `c(a |> b, d)` => `a |> b() |> c(d)` (#214, h/t @kybishop)
- `pipes`: handle pipifying nested functions `d(c(a |> b))` => `a |> b |> c() |> d` (#216, h/t @emkguts)
- `with`: correctly handle a stabby `with` `, else: (_ -> :ok)` being rewritten to a case (#219, h/t @iamhassangm)
- `with`: fix a stabby `with` `, else: (_ -> :ok)` being rewritten to a case (#219, h/t @iamhassangm)

## 1.3.3

Expand Down
10 changes: 3 additions & 7 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ This is covered by the Elixir Formatter with the `--migrate` flag, but Styler br

Rewrite `unless x` to `if !x`

### 1.19
### Change Struct Updates to Map Updates

#### Change Struct Updates to Map Updates (Experimental)

1.19 deprecates struct update syntax in favor of map update syntax. Styler will do this update for you if you're on Elixir 1.19.0-dev or later.
1.19 deprecates struct update syntax in favor of map update syntax.

```elixir
# This
Expand All @@ -38,9 +36,7 @@ Rewrite `unless x` to `if !x`
%{x | y}
```

**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.

A future version of Styler may be smart enough to do this check for you and perform the appropriate updates to the assignment location; no guarantees though. Track via #199, h/t @SteffenDE
**WARNING** Double check your diffs to make sure your variable is pattern matching against the same struct if you want to harness 1.19's type checking features.

### 1.18

Expand Down
7 changes: 3 additions & 4 deletions lib/style/deprecations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ defmodule Styler.Style.Deprecations do
end
end

if Version.match?(System.version(), ">= 1.19.0-dev") do
# Struct update syntax was deprecated `%Foo{x | y} => %{x | y}`
defp style({:%, _, [_struct, {:%{}, _, [{:|, _, _}]} = update]}), do: update
end
# Struct update syntax is deprecated in 1.19
# `%Foo{x | y} => %{x | y}`
defp style({:%, _, [_struct, {:%{}, _, [{:|, _, _}]} = update]}), do: update

# For ranges where `start > stop`, you need to explicitly include the step
# Enum.slice(enumerable, 1..-2) => Enum.slice(enumerable, 1..-2//1)
Expand Down
12 changes: 4 additions & 8 deletions test/style/deprecations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ defmodule Styler.Style.DeprecationsTest do
end
end

test "struct update, deprecated in 1.19" do
assert_style "%Foo{widget | bar: :baz}", "%{widget | bar: :baz}"
end

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

Expand Down Expand Up @@ -145,12 +149,4 @@ defmodule Styler.Style.DeprecationsTest do
assert_style "a |> x() |> :timer.seconds()"
end
end

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

test "struct update" do
assert_style "%Foo{widget | bar: :baz}", "%{widget | bar: :baz}"
end
end
end

0 comments on commit 6896d97

Please sign in to comment.