on b09bb00 help sign in
elk/src/Reporting/Warning.hs
68 lines 1.9 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Warning where
5import Data.Aeson ((.=))
6import qualified Data.Aeson as Json
7import qualified Data.Text as Text
8import Text.PrettyPrint.ANSI.Leijen (text)
10import qualified AST.Module.Name as ModuleName
11import qualified AST.Type as Type
12import qualified Reporting.Annotation as A
13import qualified Reporting.Error.Helpers as Help
14import qualified Reporting.Report as Report
15import qualified Reporting.Render.Type as RenderType
19-- ALL POSSIBLE WARNINGS
22data Warning
23 = UnusedImport ModuleName.Raw
24 | MissingTypeAnnotation String 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 `" ++ Text.unpack (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" :: String)
67 ]
68 ++ additionalFields