on 0bdfd73 help sign in
elk/src/Reporting/Warning.hs
103 lines 3.1 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Warning
4 ( Warning(..)
5 , Context(..)
6 , toReport
7 )
8 where
11import Data.Monoid ((<>))
13import qualified AST.Canonical as Can
14import qualified AST.Utils.Type as Type
15import qualified Elm.Name as N
16import qualified Reporting.Region as R
17import qualified Reporting.Report as Report
18import qualified Reporting.Render.Code as Code
19import qualified Reporting.Render.Type as RT
20import qualified Reporting.Helpers as H
24-- ALL POSSIBLE WARNINGS
27data Warning
28 = UnusedImport R.Region N.Name
29 | UnusedVariable R.Region Context N.Name
30 | MissingTypeAnnotation R.Region N.Name Can.Type
33data Context = Def | Pattern
37-- TO REPORT
40toReport :: Code.Source -> Warning -> Report.Report
41toReport source warning =
42 case warning of
43 UnusedImport region moduleName ->
44 Report.Report "unused import" region [] $
45 Report.toCodeSnippet source region Nothing
46 (
47 H.reflow $
48 "Nothing from the `" <> N.toString moduleName <> "` module is used in this file."
49 ,
50 "I recommend removing unused imports."
51 )
53 UnusedVariable region context name ->
54 let title = defOrPat context "unused definition" "unused variable" in
55 Report.Report title region [] $
56 Report.toCodeSnippet source region Nothing
57 (
58 H.reflow $
59 "You are not using `" <> N.toString name <> "` anywhere."
60 ,
61 H.stack
62 [ H.reflow $
63 "Is there a typo? Maybe you intended to use `" <> N.toString name
64 <> "` somewhere but typed another name instead?"
65 , H.reflow $
66 defOrPat context
67 ( "If you are sure there is no typo, remove the definition.\
68 \ This way future readers will not have to wonder why it is there!"
69 )
70 ( "If you are sure there is no typo, replace `" <> N.toString name
71 <> "` with _ so future readers will not have to wonder why it is there!"
72 )
73 ]
74 )
76 MissingTypeAnnotation region name inferredType ->
77 Report.Report "missing type annotation" region [] $
78 Report.toCodeSnippet source region Nothing
79 (
80 H.reflow $
81 case Type.deepDealias inferredType of
82 Can.TLambda _ _ ->
83 "The `" <> N.toString name <> "` function has no type annotation."
85 _ ->
86 "The `" <> N.toString name <> "` definition has no type annotation."
87 ,
88 H.stack
89 [ "I inferred the type annotation myself though! You can copy it into your code:"
90 , H.green $ H.hang 4 $ H.sep $
91 [ H.nameToDoc name <> " :"
92 , RT.canToDoc RT.None inferredType
93 ]
94 ]
95 )
98defOrPat :: Context -> a -> a -> a
99defOrPat context def pat =
100 case context of
101 Def -> def
102 Pattern -> pat