jxxcarlson/elm-review-codeinstaller/13.0.0

Install.ClauseInCase

Add a clause to a case expression in a specified function in a specified module. For example, if you put the code below in your ReviewConfig.elm file, running elm-review --fix will add the clause ResetCounter to the updateFromFrontend function in the Backend module.

-- code for ReviewConfig.elm:
Install.ClauseInCase.config
    "Backend"
    "updateFromFrontend"
    "ResetCounter"
    "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )"
    |> Install.ClauseInCase.makeRule

Thus we will have

updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg )
updateFromFrontend sessionId clientId msg model =
    case msg of
        CounterIncremented ->
        ...
        CounterDecremented ->
        ...
        ResetCounter ->
            ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )


        ...
type alias Config =
Config

Configuration for rule: add a clause to a case expression in a specified function in a specified module.

config : String -> String -> String -> String -> Config

Basic config to add a new clause to a case expression. If you just need to add a new clause at the end of the case, you can simply use it with the makeRule function like this:

Install.ClauseInCase.config
    "Backend"
    "updateFromFrontend"
    "ResetCounter"
    "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )"
    |> Install.ClauseInCase.makeRule

If you need additional configuration, check the withInsertAfter and withCustomErrorMessage functions.

By default, the clause will be inserted as the last clause. You can change the insertion location using the following functions:

withInsertAfter : String -> Config -> Config

Add a clause after another clause of choice in a case expression. If the clause to insert after is not found, the new clause will be inserted at the end.

Example

Given the following module:

module Philosopher exposing (Philosopher(..), stringToPhilosopher)


type Philosopher
    = Socrates
    | Plato
    | Aristotle


stringToPhilosopher : String -> Maybe Philosopher
stringToPhilosopher str =
    case str of
        "Socrates" ->
            Just Socrates


        "Plato" ->
            Just Plato


        "Aristotle" ->
            Just Aristotle


        _ ->
            Nothing

To add the clause Aspasia after the clause Aristotle, you can use the following configuration:

Install.ClauseInCase.config
    "Philosopher"
    "stringToPhilosopher"
    "Aspasia"
    "Just Aspasia"
    |> Install.ClauseInCase.withInsertAfter "Aristotle"
    |> Install.ClauseInCase.makeRule

This will add the clause Aspasia after the clause Aristotle in the stringToPhilosopher function, resulting in:

stringToPhilosopher : String -> Maybe Philosopher
stringToPhilosopher str =
    case str of
        "Socrates" ->
            Just Socrates


        "Plato" ->
            Just Plato


        "Aristotle" ->
            Just Aristotle


        "Aspasia" ->
            Just Aspasia


        _ ->
            Nothing
withInsertAtBeginning : Config -> Config

Add a clause at the beginning of the case expression.

You also can add the clause after another clause of choice with the withInsertAfter function:

Install.ClauseInCase.config
    "Backend"
    "updateFromFrontend"
    "ResetCounter"
    "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )"
    |> Install.ClauseInCase.withInsertAtBeginning
    |> Install.ClauseInCase.makeRule

In this case we will have

updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg )
updateFromFrontend sessionId clientId msg model =
    case msg of
        ResetCounter ->
            ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )


        CounterIncremented ->
        ...


        CounterDecremented ->
        ...
withCustomErrorMessage : String -> List String -> Config -> Config

Customize the error message that will be displayed when running elm-review --fix or elm-review --fix-all.

Install.ClauseInCase.config
    "Backend"
    "updateFromFrontend"
    "ResetCounter"
    "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )"
    |> Install.ClauseInCase.withCustomErrorMessage "Add handler for ResetCounter" []
    |> Install.ClauseInCase.makeRule