@@ -1,4 +1,4 @@
1−{-# OPTIONS_GHC -Wall #-}
1+{-# LANGUAGE OverloadedStrings #-}
22module Elm.Compiler
33 ( version
44 , Context(..)
@@ -6,6 +6,7 @@ module Elm.Compiler
66 , compile
77 , Error.Error
88 , errorsToDoc
9+ , errorsToJson
910 , Warning.Warning
1011 )
1112 where
@@ -14,14 +15,17 @@ module Elm.Compiler
1415import qualified Data.ByteString as BS
1516import qualified Data.Map as Map
1617import qualified Data.Text as Text
18+import qualified Text.PrettyPrint.ANSI.Leijen as P
1719
1820import qualified Compile
1921import qualified Elm.Compiler.Module as M
2022import qualified Elm.Compiler.Version
2123import qualified Elm.Package as Pkg
24+import qualified Json.Encode as Encode
2225import qualified Reporting.Error as Error
2326import qualified Reporting.Helpers as H
2427import qualified Reporting.Render.Code as Code
28+import qualified Reporting.Region as Region
2529import qualified Reporting.Report as Report
2630import qualified Reporting.Result as Result
2731import qualified Reporting.Warning as Warning
@@ -59,12 +63,42 @@ compile (Context pkg exposed importDict interfaces) source =
5963
6064
6165
62−-- ERRORS
66+-- ERRORS TO DOC
6367
6468
6569errorsToDoc :: FilePath -> Text.Text -> [Error.Error] -> H.Doc
6670errorsToDoc filePath source errors =
67− H.vcat $
68− map
69− (Report.toDoc filePath)
70− (concatMap (Error.toReports (Code.toSource source) Map.empty) errors)
71+ let
72+ reports =
73+ concatMap (Error.toReports (Code.toSource source) Map.empty) errors
74+ in
75+ H.vcat $ map (Report.toDoc filePath) reports
76+
77+
78+
79+-- ERRORS TO JSON
80+
81+
82+errorsToJson :: FilePath -> Text.Text -> [Error.Error] -> Encode.Value
83+errorsToJson filePath source errors =
84+ let
85+ reports =
86+ concatMap (Error.toReports (Code.toSource source) Map.empty) errors
87+ in
88+ Encode.object
89+ [ ("path", Encode.text (Text.pack filePath))
90+ , ("errors", Encode.array (map reportToJson reports))
91+ ]
92+
93+
94+reportToJson :: Report.Report -> Encode.Value
95+reportToJson (Report.Report title region _sgstns message) =
96+ let
97+ messageString =
98+ P.displayS (P.renderPretty 1 80 message) ""
99+ in
100+ Encode.object
101+ [ ("title", Encode.text (Text.pack title))
102+ , ("region", Region.encode region)
103+ , ("message", Encode.text (Text.pack messageString))
104+ ]