Skip to content

lue-bird/elm-review-record-alias-constructor

 
 

Repository files navigation

elm-review-record-alias-constructor

elm-review rule: forbid record type alias constructors. Read more about the why in no-record-type-alias-constructor-function.

In contrast to lxierita's no-typealias-constructor-call, this rule also reports constructors that aren't called (for example in Json.Decode.mapN functions). ↑ all differences

examples

type alias User =
    { name : String, age : Int }
User "Balsa" 42

will be marked as error and automatically fixed:

{ name = "Balsa", age = 42 }

The same goes for cases where no arguments are applied:

map2 User
    (field "name" string)
    (field "age" int)

fixed

map2 (\name age -> { name = name, age = age })
    (field "name" string)
    (field "age" int)

Skip to "try it out"

comparison to no-typealias-constructor-call

lxierita/no-typealias-constructor-call...

  • ..only reports record alias constructors that are called directly
    (User <| "Ann") <| 18
    (identity User) "Bill" 42
    User |> (\user -> user "Cem" 66)
    aren't reported for example
  • ..doesn't provide automatic fixes → refactoring is inconvenient
  • ..only looks for type aliases in the same module. This package finds every used record alias

examples

type alias User =
    { name : String, age : Int }

will be marked as error and automatically fixed:

import RecordWithoutConstructorFunction exposing (RecordWithoutConstructorFunction)

type alias User =
    RecordWithoutConstructorFunction
        { name : String, age : Int }

disallowing constructor functions from being created.

try it out

elm-review --template lue-bird/elm-record-alias-constructor/example

use it

After adding elm-review to your project, import this rule from your ReviewConfig.elm file and add it to the config. E.g.:

import NoRecordAliasConstructor
import NoRecordAliasWithConstructor
import Review.Rule exposing (Rule)

config : List Rule
config =
    [ NoRecordAliasConstructor.rule
    , NoRecordAliasWithConstructor.rule
    ]