jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

Merge record updates #235

Open jfmengels opened 9 months ago

jfmengels commented 9 months ago

In the following example, updatedState is only referenced in the let in body value, and it's only done once and as the value to be updated in a record update expression.

let
  updatedState = { state | field1 = state.field1 + 1 }
  cmd = createCmd ()
in
( { updatedState | field2 = state.field2 + 1 }, cmd )

Given these peculiar situations, we can merge updatedState into the return value:

( { state | field1 = state.field1 + 1, field2 = state.field2 + 1 }, cmd )

Other situations to handle

Collapsable let declarations

let
  b = { a | x = 1 }
  c = { b | y = 1 }
in ...
-->
let
  c = { a | x = 1, y = 1 }
in ...

Overlapping fields

let
  b = { a | x = 1 }
  c = { b | x = 2 }
in ...
-->
let
  c = { a | x = 2 }
in ...

References to previous step's unchanged fields

let
  b = { a | x = 1 }
  c = { b | y = b.z }
in ...
-->
let
  c = { a | x = 2, y = a.z }
in ...

References to previous step's changed fields. I think in this case we don't want to report an error.

let
  b = { a | x = someValue }
  c = { b | y = b.x + 1 }
in ...

Did I miss other cases we'd need to handle?