on ba0b721 help sign in
elk/src/Reporting/Warning.hs
106 lines 2.9 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Warning
4 ( Warning(..)
5 , Context(..)
6 , toReport
7 , toJson
8 )
9 where
11import Data.Aeson ((.=))
12import qualified Data.Aeson as Json
13import Data.Text (Text)
15import qualified AST.Canonical as Can
16import qualified Elm.Name as N
17import qualified Reporting.Annotation as A
18import qualified Reporting.Region as R
19import qualified Reporting.Report as Report
20import qualified Reporting.Render.Type as RenderType
21import qualified Reporting.Helpers as Help
22import Reporting.Helpers ((<>), text)
26-- ALL POSSIBLE WARNINGS
29data Warning
30 = UnusedImport N.Name
31 | UnusedVariable R.Region Context N.Name
32 | MissingTypeAnnotation Text Can.Type
35data Context = Def | Pattern
39-- TO REPORT
42toReport :: RenderType.Localizer -> Warning -> Report.Report
43toReport localizer warning =
44 case warning of
45 UnusedImport moduleName ->
46 Report.report
47 "unused import"
48 Nothing
49 ("Nothing from the `" <> moduleName <> "` module is used in this file.")
50 (text "I recommend removing unused imports.")
52 UnusedVariable _ context name ->
53 Report.report
54 (defOrPat context "unused definition" "unused variable")
55 Nothing
56 ("You are not using `" <> name <> "` anywhere.")
57 ( Help.stack
58 [ Help.reflowParagraph $
59 "Is there a typo? Maybe you intended to use `" <> name
60 <> "` but typed a similar name instead?"
61 , Help.reflowParagraph $
62 defOrPat context
63 ( "If you are sure there is no typo, remove the definition.\
64 \ This way future readers will not have to wonder!"
65 )
66 ( "If you are sure there is no typo, replace `" <> name
67 <> "` with _ so future readers will not have to wonder!"
68 )
69 ]
70 )
72 MissingTypeAnnotation name inferredType ->
73 Report.report
74 "missing type annotation"
75 Nothing
76 ("Top-level value " <> Help.functionName name <> " does not have a type annotation.")
77 ( Help.stack
78 [ text "I inferred the type annotation so you can copy it into your code:"
79 , RenderType.annotation localizer name inferredType
80 ]
81 )
84defOrPat :: Context -> a -> a -> a
85defOrPat context def pat =
86 case context of
87 Def -> def
88 Pattern -> pat
92-- TO JSON
95toJson :: RenderType.Localizer -> FilePath -> A.Located Warning -> Json.Value
96toJson localizer filePath (A.At region warning) =
97 let
98 (maybeRegion, additionalFields) =
99 Report.toJson [] (toReport localizer warning)
100 in
101 Json.object $
102 [ "file" .= filePath
103 , "region" .= maybe region id maybeRegion
104 , "type" .= ("warning" :: Text)
105 ]
106 ++ additionalFields