on master help sign in
Support reporting of warnings
af717ebEvan Czaplicki
src/Elm/Compiler.hs+8−1
@@ -3,7 +3,7 @@ module Elm.Compiler
3 ( version, rawVersion
3 ( version, rawVersion
4 , parseDependencies, compile
4 , parseDependencies, compile
5 , Error, errorToString
5 , Error, errorToString
6− , Warning
6+ , Warning, warningToString
7 ) where
7 ) where
8
8
9import qualified Data.Map as Map
9import qualified Data.Map as Map
@@ -92,3 +92,10 @@ errorToString location source (Error err) =
92-- WARNINGS
92-- WARNINGS
93
93
94newtype Warning = Warning (A.Located Warning.Warning)
94newtype 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 @@
1module Reporting.Warning where
1module Reporting.Warning where
2
2
3+import qualified Text.PrettyPrint as P
4+import Text.PrettyPrint ((<+>))
5+
3import qualified AST.Module as Module
6import qualified AST.Module as Module
4import qualified AST.Type as Type
7import 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+
5
12
13+-- ALL POSSIBLE WARNINGS
6
14
7data Warning
15data Warning
8 = UnusedImport Module.Name
16 = UnusedImport Module.Name
9 | MissingTypeAnnotation String Type.Canonical
17 | 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)