{-# LANGUAGE OverloadedStrings #-}
{-
    Copyright 2012-2019 Vidar Holen

    This file is part of ShellCheck.
    https://www.shellcheck.net

    ShellCheck is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ShellCheck is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
-}
module ShellCheck.Formatter.JSON (format) where

import ShellCheck.Interface
import ShellCheck.Formatter.Format

import Data.Aeson
import Data.IORef
import Data.Monoid
import GHC.Exts
import System.IO
import qualified Data.ByteString.Lazy.Char8 as BL

format :: IO Formatter
format :: IO Formatter
format = do
    IORef [PositionedComment]
ref <- [PositionedComment] -> IO (IORef [PositionedComment])
forall a. a -> IO (IORef a)
newIORef []
    Formatter -> IO Formatter
forall (m :: * -> *) a. Monad m => a -> m a
return Formatter :: IO ()
-> (CheckResult -> SystemInterface IO -> IO ())
-> (FilePath -> FilePath -> IO ())
-> IO ()
-> Formatter
Formatter {
        header :: IO ()
header = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return (),
        onResult :: CheckResult -> SystemInterface IO -> IO ()
onResult = IORef [PositionedComment]
-> CheckResult -> SystemInterface IO -> IO ()
forall p. IORef [PositionedComment] -> CheckResult -> p -> IO ()
collectResult IORef [PositionedComment]
ref,
        onFailure :: FilePath -> FilePath -> IO ()
onFailure = FilePath -> FilePath -> IO ()
outputError,
        footer :: IO ()
footer = IORef [PositionedComment] -> IO ()
forall a. ToJSON a => IORef a -> IO ()
finish IORef [PositionedComment]
ref
    }

instance ToJSON Replacement where
    toJSON :: Replacement -> Value
toJSON Replacement
replacement =
        let start :: Position
start = Replacement -> Position
repStartPos Replacement
replacement
            end :: Position
end = Replacement -> Position
repEndPos Replacement
replacement
            str :: FilePath
str = Replacement -> FilePath
repString Replacement
replacement in
        [Pair] -> Value
object [
          Key
"precedence" Key -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Replacement -> Int
repPrecedence Replacement
replacement,
          Key
"insertionPoint"  Key -> FilePath -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=
            case Replacement -> InsertionPoint
repInsertionPoint Replacement
replacement of
                InsertionPoint
InsertBefore -> FilePath
"beforeStart" :: String
                InsertionPoint
InsertAfter  -> FilePath
"afterEnd",
          Key
"line" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posLine Position
start,
          Key
"column" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posColumn Position
start,
          Key
"endLine" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posLine Position
end,
          Key
"endColumn" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posColumn Position
end,
          Key
"replacement" Key -> FilePath -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= FilePath
str
        ]

instance ToJSON PositionedComment where
  toJSON :: PositionedComment -> Value
toJSON PositionedComment
comment =
    let start :: Position
start = PositionedComment -> Position
pcStartPos PositionedComment
comment
        end :: Position
end = PositionedComment -> Position
pcEndPos PositionedComment
comment
        c :: Comment
c = PositionedComment -> Comment
pcComment PositionedComment
comment in
    [Pair] -> Value
object [
      Key
"file" Key -> FilePath -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> FilePath
posFile Position
start,
      Key
"line" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posLine Position
start,
      Key
"endLine" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posLine Position
end,
      Key
"column" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posColumn Position
start,
      Key
"endColumn" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posColumn Position
end,
      Key
"level" Key -> FilePath -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= PositionedComment -> FilePath
severityText PositionedComment
comment,
      Key
"code" Key -> Integer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Comment -> Integer
cCode Comment
c,
      Key
"message" Key -> FilePath -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Comment -> FilePath
cMessage Comment
c,
      Key
"fix" Key -> Maybe Fix -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= PositionedComment -> Maybe Fix
pcFix PositionedComment
comment
    ]

  toEncoding :: PositionedComment -> Encoding
toEncoding PositionedComment
comment =
    let start :: Position
start = PositionedComment -> Position
pcStartPos PositionedComment
comment
        end :: Position
end = PositionedComment -> Position
pcEndPos PositionedComment
comment
        c :: Comment
c = PositionedComment -> Comment
pcComment PositionedComment
comment in
    Series -> Encoding
pairs (
         Key
"file" Key -> FilePath -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> FilePath
posFile Position
start
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"line" Key -> Integer -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posLine Position
start
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"endLine" Key -> Integer -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posLine Position
end
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"column" Key -> Integer -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posColumn Position
start
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"endColumn" Key -> Integer -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Position -> Integer
posColumn Position
end
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"level" Key -> FilePath -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= PositionedComment -> FilePath
severityText PositionedComment
comment
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"code" Key -> Integer -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Comment -> Integer
cCode Comment
c
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"message" Key -> FilePath -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Comment -> FilePath
cMessage Comment
c
      Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"fix" Key -> Maybe Fix -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= PositionedComment -> Maybe Fix
pcFix PositionedComment
comment
    )

instance ToJSON Fix where
    toJSON :: Fix -> Value
toJSON Fix
fix = [Pair] -> Value
object [
        Key
"replacements" Key -> [Replacement] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Fix -> [Replacement]
fixReplacements Fix
fix
        ]

outputError :: FilePath -> FilePath -> IO ()
outputError FilePath
file FilePath
msg = Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
file FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
": " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
msg

collectResult :: IORef [PositionedComment] -> CheckResult -> p -> IO ()
collectResult IORef [PositionedComment]
ref CheckResult
cr p
sys = ([PositionedComment] -> IO ()) -> [[PositionedComment]] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ [PositionedComment] -> IO ()
f [[PositionedComment]]
groups
  where
    comments :: [PositionedComment]
comments = CheckResult -> [PositionedComment]
crComments CheckResult
cr
    groups :: [[PositionedComment]]
groups = (PositionedComment -> FilePath)
-> [PositionedComment] -> [[PositionedComment]]
forall b a. Ord b => (a -> b) -> [a] -> [[a]]
groupWith PositionedComment -> FilePath
sourceFile [PositionedComment]
comments
    f :: [PositionedComment] -> IO ()
    f :: [PositionedComment] -> IO ()
f [PositionedComment]
group = IORef [PositionedComment]
-> ([PositionedComment] -> [PositionedComment]) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef [PositionedComment]
ref (\[PositionedComment]
x -> [PositionedComment]
comments [PositionedComment] -> [PositionedComment] -> [PositionedComment]
forall a. [a] -> [a] -> [a]
++ [PositionedComment]
x)

finish :: IORef a -> IO ()
finish IORef a
ref = do
    a
list <- IORef a -> IO a
forall a. IORef a -> IO a
readIORef IORef a
ref
    ByteString -> IO ()
BL.putStrLn (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
list