on 54dfb21 help sign in
elk/src/Reporting/Warning.hs
80 lines 2.3 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.Helpers as Help
11import qualified AST.Module.Name as ModuleName
12import qualified AST.Type as Type
13import qualified Reporting.Annotation as A
14import qualified Reporting.PrettyPrint as P
15import qualified Reporting.Report as Report
18-- ALL POSSIBLE WARNINGS
20data Warning
21 = UnusedImport ModuleName.Raw
22 | MissingTypeAnnotation String Type.Canonical
25-- TO STRING
27toString :: P.Dealiaser -> String -> String -> A.Located Warning -> String
28toString dealiaser location source (A.A region warning) =
29 Report.toString location region (toReport dealiaser warning) source
32print :: P.Dealiaser -> String -> String -> A.Located Warning -> IO ()
33print dealiaser location source (A.A region warning) =
34 Report.printWarning location region (toReport dealiaser warning) source
37toReport :: P.Dealiaser -> Warning -> Report.Report
38toReport dealiaser warning =
39 case warning of
40 UnusedImport moduleName ->
41 Report.simple
42 "unused import"
43 ("Module `" ++ ModuleName.toString moduleName ++ "` is unused.")
44 ""
46 MissingTypeAnnotation name inferredType ->
47 Report.simple
48 "missing type annotation"
49 ("Top-level value `" ++ name ++ "` does not have a type annotation.")
50 ( "The type annotation you want looks something like this:\n\n"
51 ++ P.render (P.nest 4 typeDoc)
52 )
53 where
54 nameDoc =
55 if Help.isOp name then
56 P.parens (P.text name)
57 else
58 P.text name
60 typeDoc =
61 P.hang
62 (nameDoc <+> P.colon)
63 4
64 (P.pretty dealiaser False inferredType)
67-- TO JSON
69toJson :: P.Dealiaser -> FilePath -> A.Located Warning -> Json.Value
70toJson dealiaser filePath (A.A region warning) =
71 let
72 (maybeRegion, additionalFields) =
73 Report.toJson [] (toReport dealiaser warning)
74 in
75 Json.object $
76 [ "file" .= filePath
77 , "region" .= maybe region id maybeRegion
78 , "type" .= ("warning" :: String)
79 ]
80 ++ additionalFields