on ea77dcf help sign in
elk/src/Reporting/Warning.hs
98 lines 2.6 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Warning
4 ( Warning(..)
5 , Unused(..)
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.Report as Report
19import qualified Reporting.Render.Type as RenderType
20import qualified Reporting.Helpers as Help
21import Reporting.Helpers ((<>), text)
25-- ALL POSSIBLE WARNINGS
28data Warning
29 = UnusedImport N.Name
30 | UnusedVariable Unused N.Name
31 | MissingTypeAnnotation Text Can.Type
34data Unused = Pattern | Binding
38-- TO REPORT
41toReport :: RenderType.Localizer -> Warning -> Report.Report
42toReport localizer warning =
43 case warning of
44 UnusedImport moduleName ->
45 Report.report
46 "unused import"
47 Nothing
48 ("Nothing from the `" <> moduleName <> "` module is used in this file.")
49 (text "I recommend removing unused imports.")
51 UnusedVariable Pattern name ->
52 Report.report
53 "unused variable"
54 Nothing
55 ("The `" <> name <> "` variable is unused.")
56 ( Help.reflowParagraph $
57 "Maybe this indicates there is a mistake around here? Switching `"
58 <> name <> "` to _ will indicate that it is intentionally unused."
59 )
61 UnusedVariable Binding name ->
62 Report.report
63 "unused binding"
64 Nothing
65 ("The `" <> name <> "` binding is unused.")
66 ( Help.reflowParagraph $
67 "Maybe this indicates there is a mistake around here? Or maybe it\
68 \ just is not used anymore and should be removed?"
69 )
71 MissingTypeAnnotation name inferredType ->
72 Report.report
73 "missing type annotation"
74 Nothing
75 ("Top-level value " <> Help.functionName name <> " does not have a type annotation.")
76 ( Help.stack
77 [ text "I inferred the type annotation so you can copy it into your code:"
78 , RenderType.annotation localizer name inferredType
79 ]
80 )
84-- TO JSON
87toJson :: RenderType.Localizer -> FilePath -> A.Located Warning -> Json.Value
88toJson localizer filePath (A.A region warning) =
89 let
90 (maybeRegion, additionalFields) =
91 Report.toJson [] (toReport localizer warning)
92 in
93 Json.object $
94 [ "file" .= filePath
95 , "region" .= maybe region id maybeRegion
96 , "type" .= ("warning" :: Text)
97 ]
98 ++ additionalFields