2020-04-08 01:14:03 +03:00
|
|
|
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`.
|
2020-04-21 00:11:02 +03:00
|
|
|
For that, enable [`NoMissingTypeAnnotationInLetIn`](./NoMissingTypeAnnotationInLetIn).
|
2020-04-08 01:14:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
## Fail
|
|
|
|
|
|
|
|
a =
|
|
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
## Success
|
|
|
|
|
|
|
|
a : number
|
|
|
|
a =
|
|
|
|
1
|
|
|
|
|
|
|
|
b : number
|
|
|
|
b =
|
|
|
|
let
|
|
|
|
c =
|
|
|
|
2
|
|
|
|
in
|
|
|
|
c
|
|
|
|
|
2020-08-09 19:55:15 +03:00
|
|
|
|
|
|
|
## Try it out
|
|
|
|
|
|
|
|
You can try this rule out by running the following command:
|
|
|
|
|
|
|
|
```bash
|
2020-09-22 20:40:30 +03:00
|
|
|
elm-review --template jfmengels/elm-review-common/example --rules NoMissingTypeAnnotation
|
2020-08-09 19:55:15 +03:00
|
|
|
```
|
|
|
|
|
2020-04-08 01:14:03 +03:00
|
|
|
-}
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-06-28 08:49:27 +03:00
|
|
|
Rule.newModuleRuleSchema "NoMissingTypeAnnotation" ()
|
|
|
|
|> Rule.withSimpleDeclarationVisitor declarationVisitor
|
|
|
|
|> Rule.fromModuleRuleSchema
|
2020-04-08 01:14:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
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 _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[]
|