on 267f708 help sign in
elk/src/Reporting/Warning.hs
67 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 Text.PrettyPrint.ANSI.Leijen (text)
9import qualified AST.Module.Name as ModuleName
10import qualified AST.Type as Type
11import qualified Reporting.Annotation as A
12import qualified Reporting.Error.Helpers as Help
13import qualified Reporting.Report as Report
14import qualified Reporting.Render.Type as RenderType
18-- ALL POSSIBLE WARNINGS
21data Warning
22 = UnusedImport ModuleName.Raw
23 | MissingTypeAnnotation String Type.Canonical
27-- TO REPORT
30toReport :: RenderType.Localizer -> Warning -> Report.Report
31toReport localizer warning =
32 case warning of
33 UnusedImport moduleName ->
34 Report.report
35 "unused import"
36 Nothing
37 ("Module `" ++ ModuleName.toString moduleName ++ "` is unused.")
38 (text "Best to remove it. Don't save code quality for later!")
40 MissingTypeAnnotation name inferredType ->
41 Report.report
42 "missing type annotation"
43 Nothing
44 ("Top-level value " ++ Help.functionName name ++ " does not have a type annotation.")
45 ( Help.stack
46 [ text "I inferred the type annotation so you can copy it into your code:"
47 , RenderType.annotation localizer name inferredType
48 ]
49 )
53-- TO JSON
56toJson :: RenderType.Localizer -> FilePath -> A.Located Warning -> Json.Value
57toJson localizer filePath (A.A region warning) =
58 let
59 (maybeRegion, additionalFields) =
60 Report.toJson [] (toReport localizer warning)
61 in
62 Json.object $
63 [ "file" .= filePath
64 , "region" .= maybe region id maybeRegion
65 , "type" .= ("warning" :: String)
66 ]
67 ++ additionalFields