cekrem/elm-review-positional-arguments/1.1.1

NOTE: the latest version is 1.1.2

elm-review-positional-arguments

Provides an elm-review rule to limit the number of positional arguments to functions.

Provided rule

  • NoTooManyPositionalArguments - Reports functions that have too many positional arguments and can auto-fix them to use a record parameter instead.

Configuration

module ReviewConfig exposing (config)

import NoTooManyPositionalArguments
import Review.Rule exposing (Rule)

config : List Rule
config =
    [ NoTooManyPositionalArguments.rule NoTooManyPositionalArguments.defaults
    ]

The default maximum is 4 positional arguments. Functions with 5 or more will be flagged.

To customize the threshold:

config : List Rule
config =
    [ NoTooManyPositionalArguments.rule
        (NoTooManyPositionalArguments.withMaxArguments 3)
    ]

What it does

When a function has too many positional arguments, the rule reports an error and provides an automatic fix that:

  1. Converts the function declaration to accept a single record with named fields
  2. Updates the type annotation (if present) to use a record type
  3. Updates all call sites within the same module to pass a record

Before

createUser : String -> String -> Int -> Bool -> String -> User
createUser name email age isAdmin role =
    { name = name, email = email, age = age, isAdmin = isAdmin, role = role }

register =
    createUser "Alice" "[email protected]" 30 False "admin"

After

createUser : { name : String, email : String, age : Int, isAdmin : Bool, role : String } -> User
createUser { name, email, age, isAdmin, role } =
    { name = name, email = email, age = age, isAdmin = isAdmin, role = role }

register =
    createUser { name = "Alice", email = "[email protected]", age = 30, isAdmin = False, role = "admin" }

Partial application awareness

The rule accounts for partial application (currying). If a function is partially applied somewhere in the module, the curried arguments are not counted toward the positional argument limit.

For example, if createUser has 5 parameters but is used as List.map (createUser "default" "email" 30), only the remaining 2 arguments count as positional. Since 2 is within the default threshold of 4, no error is reported.

If the effective arity still exceeds the threshold, the error is reported but the auto-fix is suppressed (converting to a record would break curried usage).

When the fix is suppressed

The auto-fix is intentionally suppressed when:

  • Partial application exists -- the function is partially applied somewhere in the module, so converting to a record would break curried usage.
  • Parameters use destructuring patterns -- e.g. foo (Just x) (Ok y) _ _ _ has no usable names for record fields.

In these cases, the error is still reported but must be fixed manually.

Try it out

You can try the example configuration above out by running the following command:

elm-review --template cekrem/elm-review-positional-arguments/example