sparksp / elm-review-ports / NoUnusedPorts

rule : Review.Rule.Rule

Forbid ports that are never used in your project.

config : List Rule
config =
    [ NoUnusedPorts.rule
    ]

This rule reports any ports that are not used anywhere in the project. A port is only considered used if it can be traced to a main function.

Why is this a problem?

Elm is very good at eliminating dead code from the compiled JavaScript. When a port is unused it will not be present in the compiled JavaScript, and when no ports are used the app.ports object will be undefined. This can lead to JavaScript runtime errors that could take you some time to figure out.

var app = Elm.Main.init({
    node: document.getElementById('elm')
});

app.ports.myPort // undefined

When (not) to use this rule

Ports are not allowed in Elm packages - you should not enable this when developing an Elm package.

Failure

port module Main exposing (main)

import Html
import Json.Encode as Encode


-- Port `action` is never used.
port action : (Encode.Value -> msg) -> Sub msg

port alarm : Encode.Value -> msg

-- Port `alarm` is never used, because `play` is never used.
play : Cmd msg
play =
    alarm (Encode.string "play")

main =
    Html.text "Hello"

Try it out

You can try this rule out by running the following command:

elm-review --template sparksp/elm-review-ports/example --rules NoUnusedPorts