on master help sign in
Support reporting of warnings
commitaf717ebe4114378076e218bb629cd2dca5ca80dd authorEvan Czaplicki <info@elm-lang.org> dateTue 12 May 2015, 12:03 (11 years ago) parentdca97ae
2 files changed+47−1
src/Elm/Compiler.hs+8−1 src/Reporting/Warning.hs+39
src/Elm/Compiler.hs+8−1
@@ -3,7 +3,7 @@ module Elm.Compiler
33 ( version, rawVersion
44 , parseDependencies, compile
55 , Error, errorToString
6− , Warning
6+ , Warning, warningToString
77 ) where
88
99import qualified Data.Map as Map
@@ -92,3 +92,10 @@ errorToString location source (Error err) =
9292-- WARNINGS
9393
9494newtype Warning = Warning (A.Located Warning.Warning)
95+
96+
97+warningToString :: String -> String -> Warning -> String
98+warningToString location source (Warning err) =
99+ Warning.toString location source err
100+
101+
src/Reporting/Warning.hs+39
@@ -1,9 +1,48 @@
11module Reporting.Warning where
22
3+import qualified Text.PrettyPrint as P
4+import Text.PrettyPrint ((<+>))
5+
36import qualified AST.Module as Module
47import qualified AST.Type as Type
8+import qualified Reporting.Annotation as A
9+import qualified Reporting.PrettyPrint as P
10+import qualified Reporting.Report as Report
11+
512
13+-- ALL POSSIBLE WARNINGS
614
715data Warning
816 = UnusedImport Module.Name
917 | MissingTypeAnnotation String Type.Canonical
18+
19+
20+-- TO STRING
21+
22+toString :: String -> String -> A.Located Warning -> String
23+toString location source (A.A region warning) =
24+ Report.toString location region (toReport warning) source
25+
26+
27+toReport :: Warning -> Report.Report
28+toReport warning =
29+ case warning of
30+ UnusedImport moduleName ->
31+ Report.simple
32+ "warning"
33+ ("Module '" ++ Module.nameToString moduleName ++ "' is unused.")
34+ ""
35+
36+ MissingTypeAnnotation name inferredType ->
37+ Report.simple
38+ "warning"
39+ ("Top-level value '" ++ name ++ "' does not have a type annotation.")
40+ ( "The type annotation you want looks something like this:\n\n"
41+ ++ P.render (P.nest 4 typeDoc)
42+ )
43+ where
44+ typeDoc =
45+ P.hang
46+ (P.text name <+> P.colon)
47+ 4
48+ (P.pretty False inferredType)