elm-review/tests/NoMissingTypeAnnotationInLetIn.elm
2020-06-25 19:23:09 +02:00

100 lines
2.5 KiB
Elm

module NoMissingTypeAnnotationInLetIn exposing (rule)
{-|
@docs rule
-}
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Review.Rule as Rule exposing (Error, Rule)
import Review.Rule3 as Rule3
{-| Reports `let in` declarations that do not have a type annotation.
Type annotations help you understand what happens in the code, and it will help the compiler give better error messages.
config =
[ NoMissingTypeAnnotationInLetIn.rule
]
This rule does not report top-level declarations without a type annotation inside a `let in`.
For that, enable [`NoMissingTypeAnnotation`](./NoMissingTypeAnnotation).
## Fail
a : number
a =
let
-- Missing annotation
b =
2
in
b
## Success
-- Top-level annotation is not necessary, but good to have!
a : number
a =
let
b : number
b =
2
in
b
-}
rule : Rule
rule =
Rule3.newModuleRuleSchema_New "NoMissingTypeAnnotationInLetIn" ()
|> Rule3.withSimpleExpressionVisitor_New expressionVisitor
|> Rule3.fromModuleRuleSchema_New
expressionVisitor : Node Expression -> List (Error {})
expressionVisitor expression =
case Node.value expression of
Expression.LetExpression { declarations } ->
List.filterMap
(\declaration ->
case Node.value declaration of
Expression.LetFunction function ->
reportFunctionWithoutSignature function
_ ->
Nothing
)
declarations
_ ->
[]
reportFunctionWithoutSignature : Expression.Function -> Maybe (Error {})
reportFunctionWithoutSignature function =
case function.signature of
Just _ ->
Nothing
Nothing ->
let
name : Node String
name =
function.declaration
|> Node.value
|> .name
in
Rule.error
{ message = "Missing type annotation for `" ++ Node.value name ++ "`"
, details =
[ "Type annotations help you understand what happens in the code, and it will help the compiler give better error messages."
]
}
(Node.range name)
|> Just