sparksp / elm-review-ports / NoUnsafePorts

rule : Check -> Review.Rule.Rule

Forbid unsafe types in ports.

config : List Rule
config =
    [ NoUnsafePorts.rule NoUnsafePorts.any
    ]

This rule reports any ports that do not send/receive a Json.Encode.Value.

Why is this a problem?

If a port expecting an Int receives a Float it will cause a runtime error. We can prevent this by just wrapping the incoming data as Json.Encode.Value and handling the data through a Decoder instead. This guarantees that your application provides some sort of error handling. This is discussed in the Elm guide under Verifying Flags.

Success

port action : (Json.Decode.Value -> msg) -> Sub msg

port alarm : Json.Encode.Value -> Cmd msg

Failure

port action : (Int -> msg) -> Sub msg

port alarm : String -> Cmd msg

Caveats

Try it out

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

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

Config

any : Check

Check both incoming and outgoing ports.

config : List Rule
config =
    [ NoUnsafePorts.rule NoUnsafePorts.any
    ]

This is the option you will want most of the time.

onlyIncomingPorts : Check

Check incoming ports only.

config : List Rule
config =
    [ NoUnsafePorts.rule NoUnsafePorts.onlyIncomingPorts
    ]

Use this option if you want to allow basic types in outgoing ports.


type Check

Check type.