on master help sign in
Add public API for turning warnings into JSON
commitdf0fb5d04be57367b658f6170f439bcea5d0ff77 authorEvan Czaplicki <info@elm-lang.org> dateWed 13 May 2015, 17:11 (11 years ago) parentfb1a6d2
4 files changed+61−35
src/Elm/Compiler.hs+6−1 src/Reporting/Error.hs+6−23 src/Reporting/Report.hs+27−9 src/Reporting/Warning.hs+22−2
src/Elm/Compiler.hs+6−1
@@ -3,7 +3,7 @@ module Elm.Compiler
33 ( version, rawVersion
44 , parseDependencies, compile
55 , Error, errorToString, errorToJson
6− , Warning, warningToString
6+ , Warning, warningToString, warningToJson
77 ) where
88
99import qualified Data.Aeson as Json
@@ -106,3 +106,8 @@ warningToString location source (Warning err) =
106106 Warning.toString location source err
107107
108108
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
44
55import Data.Aeson ((.=))
66import qualified Data.Aeson as Json
7−import qualified Data.Aeson.Types as Json
87
98import qualified Reporting.Annotation as A
109import qualified Reporting.Error.Canonicalize as Canonicalize
1110import qualified Reporting.Error.Syntax as Syntax
1211import qualified Reporting.Error.Type as Type
13−import qualified Reporting.Region as Region
1412import qualified Reporting.Report as Report
1513
1614
@@ -43,13 +41,13 @@ toString location source (A.A region err) =
4341
4442-- TO JSON
4543
46−toJson :: String -> A.Located Error -> Json.Value
47−toJson location (A.A region err) =
44+toJson :: FilePath -> A.Located Error -> Json.Value
45+toJson filePath (A.A region err) =
4846 let
4947 (maybeRegion, additionalFields) =
5048 case err of
5149 Syntax syntaxError ->
52− reportToJson [] (Syntax.toReport syntaxError)
50+ Report.toJson [] (Syntax.toReport syntaxError)
5351
5452 Canonicalize canonicalizeError ->
5553 let
@@ -58,29 +56,14 @@ toJson location (A.A region err) =
5856 (\s -> ["suggestions" .= s])
5957 (Canonicalize.extractSuggestions canonicalizeError)
6058 in
61− reportToJson suggestions (Canonicalize.toReport canonicalizeError)
59+ Report.toJson suggestions (Canonicalize.toReport canonicalizeError)
6260
6361 Type typeError ->
64− reportToJson [] (Type.toReport typeError)
62+ Report.toJson [] (Type.toReport typeError)
6563 in
6664 Json.object $
67− [ "file" .= location
65+ [ "file" .= filePath
6866 , "region" .= maybe region id maybeRegion
6967 , "type" .= ("error" :: String)
7068 ]
7169 ++ 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 #-}
13module Reporting.Report
24 ( Report(Report)
35 , simple
46 , toString
7+ , toJson
58 ) where
69
10+import Data.Aeson ((.=))
11+import qualified Data.Aeson.Types as Json
12+
713import Elm.Utils ((|>))
814import qualified Reporting.Region as R
915
1016
1117data Report = Report
12− { title :: String
13− , highlight :: Maybe R.Region
14− , preHint :: String
15− , postHint :: String
18+ { _title :: String
19+ , _highlight :: Maybe R.Region
20+ , _preHint :: String
21+ , _postHint :: String
1622 }
1723
1824
@@ -22,15 +28,27 @@ simple title pre post =
2228
2329
2430toString :: String -> R.Region -> Report -> String -> String
25−toString location region report source =
31+toString location region (Report title highlight pre post) source =
2632 concat
27− [ messageBar (title report) location
28− , preHint report ++ "\n\n"
29− , grabRegion (highlight report) region source ++ "\n"
30− , postHint report ++ "\n\n\n"
33+ [ messageBar title location
34+ , pre ++ "\n\n"
35+ , grabRegion highlight region source ++ "\n"
36+ , if null post then "\n" else post ++ "\n\n\n"
3137 ]
3238
3339
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+
3452-- REPORT HEADER
3553
3654messageBar :: String -> String -> String
src/Reporting/Warning.hs+22−2
@@ -1,5 +1,9 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
13module Reporting.Warning where
24
5+import Data.Aeson ((.=))
6+import qualified Data.Aeson as Json
37import qualified Text.PrettyPrint as P
48import Text.PrettyPrint ((<+>))
59
@@ -29,13 +33,13 @@ toReport warning =
2933 case warning of
3034 UnusedImport moduleName ->
3135 Report.simple
32− "warning"
36+ "unused import"
3337 ("Module '" ++ Module.nameToString moduleName ++ "' is unused.")
3438 ""
3539
3640 MissingTypeAnnotation name inferredType ->
3741 Report.simple
38− "warning"
42+ "missing type annotation"
3943 ("Top-level value '" ++ name ++ "' does not have a type annotation.")
4044 ( "The type annotation you want looks something like this:\n\n"
4145 ++ P.render (P.nest 4 typeDoc)
@@ -46,3 +50,19 @@ toReport warning =
4650 (P.text name <+> P.colon)
4751 4
4852 (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