on e00a3f3 help sign in
elk/src/Reporting/Warning.hs
68 lines 1.8 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Warning where
5import Data.Aeson ((.=))
6import qualified Data.Aeson as Json
7import Data.Text (Text)
9import qualified AST.Module.Name as ModuleName
10import qualified AST.Type as Type
11import qualified Reporting.Annotation as A
12import qualified Reporting.Report as Report
13import qualified Reporting.Render.Type as RenderType
14import qualified Reporting.Helpers as Help
15import Reporting.Helpers ((<>), text)
19-- ALL POSSIBLE WARNINGS
22data Warning
23 = UnusedImport ModuleName.Raw
24 | MissingTypeAnnotation Text Type.Canonical
28-- TO REPORT
31toReport :: RenderType.Localizer -> Warning -> Report.Report
32toReport localizer warning =
33 case warning of
34 UnusedImport moduleName ->
35 Report.report
36 "unused import"
37 Nothing
38 ("Module `" <> ModuleName.toText moduleName <> "` is unused.")
39 (text "Best to remove it. Don't save code quality for later!")
41 MissingTypeAnnotation name inferredType ->
42 Report.report
43 "missing type annotation"
44 Nothing
45 ("Top-level value " <> Help.functionName name <> " does not have a type annotation.")
46 ( Help.stack
47 [ text "I inferred the type annotation so you can copy it into your code:"
48 , RenderType.annotation localizer name inferredType
49 ]
50 )
54-- TO JSON
57toJson :: RenderType.Localizer -> FilePath -> A.Located Warning -> Json.Value
58toJson localizer filePath (A.A region warning) =
59 let
60 (maybeRegion, additionalFields) =
61 Report.toJson [] (toReport localizer warning)
62 in
63 Json.object $
64 [ "file" .= filePath
65 , "region" .= maybe region id maybeRegion
66 , "type" .= ("warning" :: Text)
67 ]
68 ++ additionalFields