on 446c784 help sign in
elk/src/Reporting/Warning.hs
73 lines 2.1 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 Text.PrettyPrint as P
8import Text.PrettyPrint ((<+>))
10import qualified AST.Module as Module
11import qualified AST.Type as Type
12import qualified Reporting.Annotation as A
13import qualified Reporting.PrettyPrint as P
14import qualified Reporting.Report as Report
17-- ALL POSSIBLE WARNINGS
19data Warning
20 = UnusedImport Module.Name
21 | MissingTypeAnnotation String Type.Canonical
24-- TO STRING
26toString :: P.Dealiaser -> String -> String -> A.Located Warning -> String
27toString dealiaser location source (A.A region warning) =
28 Report.toString location region (toReport dealiaser warning) source
31print :: P.Dealiaser -> String -> String -> A.Located Warning -> IO ()
32print dealiaser location source (A.A region warning) =
33 Report.printWarning location region (toReport dealiaser warning) source
36toReport :: P.Dealiaser -> Warning -> Report.Report
37toReport dealiaser warning =
38 case warning of
39 UnusedImport moduleName ->
40 Report.simple
41 "unused import"
42 ("Module `" ++ Module.nameToString moduleName ++ "` is unused.")
43 ""
45 MissingTypeAnnotation name inferredType ->
46 Report.simple
47 "missing type annotation"
48 ("Top-level value `" ++ name ++ "` does not have a type annotation.")
49 ( "The type annotation you want looks something like this:\n\n"
50 ++ P.render (P.nest 4 typeDoc)
51 )
52 where
53 typeDoc =
54 P.hang
55 (P.text name <+> P.colon)
56 4
57 (P.pretty dealiaser False inferredType)
60-- TO JSON
62toJson :: P.Dealiaser -> FilePath -> A.Located Warning -> Json.Value
63toJson dealiaser filePath (A.A region warning) =
64 let
65 (maybeRegion, additionalFields) =
66 Report.toJson [] (toReport dealiaser warning)
67 in
68 Json.object $
69 [ "file" .= filePath
70 , "region" .= maybe region id maybeRegion
71 , "type" .= ("warning" :: String)
72 ]
73 ++ additionalFields