mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-11-23 23:05:35 +03:00
81 lines
2.0 KiB
Elm
81 lines
2.0 KiB
Elm
module NoMissingTypeAnnotation exposing (rule)
|
|
|
|
{-|
|
|
|
|
@docs rule
|
|
|
|
-}
|
|
|
|
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
|
|
import Elm.Syntax.Node as Node exposing (Node)
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
|
|
|
|
|
{-| Reports top-level 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 =
|
|
[ NoMissingTypeAnnotation.rule
|
|
]
|
|
|
|
This rule does not report declarations without a type annotation inside a `let in`.
|
|
For that, enable [`NoMissingTypeAnnotationInLetIn`](./NoMissingTypeAnnotationInLetIn).
|
|
|
|
|
|
## Fail
|
|
|
|
a =
|
|
1
|
|
|
|
|
|
## Success
|
|
|
|
a : number
|
|
a =
|
|
1
|
|
|
|
b : number
|
|
b =
|
|
let
|
|
c =
|
|
2
|
|
in
|
|
c
|
|
|
|
-}
|
|
rule : Rule
|
|
rule =
|
|
Rule.newModuleRuleSchema "NoMissingTypeAnnotation" ()
|
|
|> Rule.withSimpleDeclarationVisitor declarationVisitor
|
|
|> Rule.fromModuleRuleSchema
|
|
|
|
|
|
declarationVisitor : Node Declaration -> List (Error {})
|
|
declarationVisitor declaration =
|
|
case Node.value declaration of
|
|
Declaration.FunctionDeclaration function ->
|
|
case function.signature of
|
|
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 _ ->
|
|
[]
|
|
|
|
_ ->
|
|
[]
|