on master help sign in
Add public API for turning warnings into JSON
df0fb5dEvan Czaplicki
src/Elm/Compiler.hs+6−1
@@ -3,7 +3,7 @@ module Elm.Compiler
3 ( version, rawVersion
3 ( version, rawVersion
4 , parseDependencies, compile
4 , parseDependencies, compile
5 , Error, errorToString, errorToJson
5 , Error, errorToString, errorToJson
6− , Warning, warningToString
6+ , Warning, warningToString, warningToJson
7 ) where
7 ) where
8
8
9import qualified Data.Aeson as Json
9import qualified Data.Aeson as Json
@@ -106,3 +106,8 @@ warningToString location source (Warning err) =
106 Warning.toString location source err
106 Warning.toString location source err
107
107
108
108
109+warningToJson :: String -> Warning -> Json.Value
110+warningToJson location (Warning err) =
111+ Warning.toJson location err
112+
113+
src/Reporting/Error.hs+6−23
@@ -4,13 +4,11 @@ module Reporting.Error where
4
4
5import Data.Aeson ((.=))
5import Data.Aeson ((.=))
6import qualified Data.Aeson as Json
6import qualified Data.Aeson as Json
7−import qualified Data.Aeson.Types as Json
8
7
9import qualified Reporting.Annotation as A
8import qualified Reporting.Annotation as A
10import qualified Reporting.Error.Canonicalize as Canonicalize
9import qualified Reporting.Error.Canonicalize as Canonicalize
11import qualified Reporting.Error.Syntax as Syntax
10import qualified Reporting.Error.Syntax as Syntax
12import qualified Reporting.Error.Type as Type
11import qualified Reporting.Error.Type as Type
13−import qualified Reporting.Region as Region
14import qualified Reporting.Report as Report
12import qualified Reporting.Report as Report
15
13
16
14
@@ -43,13 +41,13 @@ toString location source (A.A region err) =
43
41
44-- TO JSON
42-- TO JSON
45
43
46−toJson :: String -> A.Located Error -> Json.Value
44+toJson :: FilePath -> A.Located Error -> Json.Value
47−toJson location (A.A region err) =
45+toJson filePath (A.A region err) =
48 let
46 let
49 (maybeRegion, additionalFields) =
47 (maybeRegion, additionalFields) =
50 case err of
48 case err of
51 Syntax syntaxError ->
49 Syntax syntaxError ->
52− reportToJson [] (Syntax.toReport syntaxError)
50+ Report.toJson [] (Syntax.toReport syntaxError)
53
51
54 Canonicalize canonicalizeError ->
52 Canonicalize canonicalizeError ->
55 let
53 let
@@ -58,29 +56,14 @@ toJson location (A.A region err) =
58 (\s -> ["suggestions" .= s])
56 (\s -> ["suggestions" .= s])
59 (Canonicalize.extractSuggestions canonicalizeError)
57 (Canonicalize.extractSuggestions canonicalizeError)
60 in
58 in
61− reportToJson suggestions (Canonicalize.toReport canonicalizeError)
59+ Report.toJson suggestions (Canonicalize.toReport canonicalizeError)
62
60
63 Type typeError ->
61 Type typeError ->
64− reportToJson [] (Type.toReport typeError)
62+ Report.toJson [] (Type.toReport typeError)
65 in
63 in
66 Json.object $
64 Json.object $
67− [ "file" .= location
65+ [ "file" .= filePath
68 , "region" .= maybe region id maybeRegion
66 , "region" .= maybe region id maybeRegion
69 , "type" .= ("error" :: String)
67 , "type" .= ("error" :: String)
70 ]
68 ]
71 ++ additionalFields
69 ++ additionalFields
72−
73−
74−reportToJson
75− :: [Json.Pair]
76− -> Report.Report
77− -> (Maybe Region.Region, [Json.Pair])
78−reportToJson extraFields (Report.Report title subregion pre post) =
79− let
80− fields =
81− [ "tag" .= title
82− , "overview" .= pre
83− , "details" .= post
84− ]
85− in
86− (subregion, fields ++ extraFields)
src/Reporting/Report.hs+27−9
@@ -1,18 +1,24 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
1module Reporting.Report
3module Reporting.Report
2 ( Report(Report)
4 ( Report(Report)
3 , simple
5 , simple
4 , toString
6 , toString
7+ , toJson
5 ) where
8 ) where
6
9
10+import Data.Aeson ((.=))
11+import qualified Data.Aeson.Types as Json
12+
7import Elm.Utils ((|>))
13import Elm.Utils ((|>))
8import qualified Reporting.Region as R
14import qualified Reporting.Region as R
9
15
10
16
11data Report = Report
17data Report = Report
12− { title :: String
18+ { _title :: String
13− , highlight :: Maybe R.Region
19+ , _highlight :: Maybe R.Region
14− , preHint :: String
20+ , _preHint :: String
15− , postHint :: String
21+ , _postHint :: String
16 }
22 }
17
23
18
24
@@ -22,15 +28,27 @@ simple title pre post =
22
28
23
29
24toString :: String -> R.Region -> Report -> String -> String
30toString :: String -> R.Region -> Report -> String -> String
25−toString location region report source =
31+toString location region (Report title highlight pre post) source =
26 concat
32 concat
27− [ messageBar (title report) location
33+ [ messageBar title location
28− , preHint report ++ "\n\n"
34+ , pre ++ "\n\n"
29− , grabRegion (highlight report) region source ++ "\n"
35+ , grabRegion highlight region source ++ "\n"
30− , postHint report ++ "\n\n\n"
36+ , if null post then "\n" else post ++ "\n\n\n"
31 ]
37 ]
32
38
33
39
40+toJson :: [Json.Pair] -> Report -> (Maybe R.Region, [Json.Pair])
41+toJson extraFields (Report title subregion pre post) =
42+ let
43+ fields =
44+ [ "tag" .= title
45+ , "overview" .= pre
46+ , "details" .= post
47+ ]
48+ in
49+ (subregion, fields ++ extraFields)
50+
51+
34-- REPORT HEADER
52-- REPORT HEADER
35
53
36messageBar :: String -> String -> String
54messageBar :: String -> String -> String
src/Reporting/Warning.hs+22−2
@@ -1,5 +1,9 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
1module Reporting.Warning where
3module Reporting.Warning where
2
4
5+import Data.Aeson ((.=))
6+import qualified Data.Aeson as Json
3import qualified Text.PrettyPrint as P
7import qualified Text.PrettyPrint as P
4import Text.PrettyPrint ((<+>))
8import Text.PrettyPrint ((<+>))
5
9
@@ -29,13 +33,13 @@ toReport warning =
29 case warning of
33 case warning of
30 UnusedImport moduleName ->
34 UnusedImport moduleName ->
31 Report.simple
35 Report.simple
32− "warning"
36+ "unused import"
33 ("Module '" ++ Module.nameToString moduleName ++ "' is unused.")
37 ("Module '" ++ Module.nameToString moduleName ++ "' is unused.")
34 ""
38 ""
35
39
36 MissingTypeAnnotation name inferredType ->
40 MissingTypeAnnotation name inferredType ->
37 Report.simple
41 Report.simple
38− "warning"
42+ "missing type annotation"
39 ("Top-level value '" ++ name ++ "' does not have a type annotation.")
43 ("Top-level value '" ++ name ++ "' does not have a type annotation.")
40 ( "The type annotation you want looks something like this:\n\n"
44 ( "The type annotation you want looks something like this:\n\n"
41 ++ P.render (P.nest 4 typeDoc)
45 ++ P.render (P.nest 4 typeDoc)
@@ -46,3 +50,19 @@ toReport warning =
46 (P.text name <+> P.colon)
50 (P.text name <+> P.colon)
47 4
51 4
48 (P.pretty False inferredType)
52 (P.pretty False inferredType)
53+
54+
55+-- TO JSON
56+
57+toJson :: FilePath -> A.Located Warning -> Json.Value
58+toJson filePath (A.A region warning) =
59+ let
60+ (maybeRegion, additionalFields) =
61+ Report.toJson [] (toReport warning)
62+ in
63+ Json.object $
64+ [ "file" .= filePath
65+ , "region" .= maybe region id maybeRegion
66+ , "type" .= ("warning" :: String)
67+ ]
68+ ++ additionalFields