JSON.hs 2.8 KB
Newer Older
N
Ng Zhi An 已提交
1
{-# LANGUAGE OverloadedStrings #-}
2 3 4 5
{-
    Copyright 2012-2015 Vidar Holen

    This file is part of ShellCheck.
M
Mike Frysinger 已提交
6
    https://www.shellcheck.net
7 8 9 10 11 12 13 14 15 16 17 18

    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
M
Mike Frysinger 已提交
19
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 21 22 23 24 25
-}
module ShellCheck.Formatter.JSON (format) where

import ShellCheck.Interface
import ShellCheck.Formatter.Format

N
Ng Zhi An 已提交
26
import Data.Aeson
27
import Data.IORef
N
Ng Zhi An 已提交
28
import Data.Monoid
29 30
import GHC.Exts
import System.IO
N
Ng Zhi An 已提交
31
import qualified Data.ByteString.Lazy.Char8 as BL
32 33 34 35 36 37 38 39 40 41

format = do
    ref <- newIORef []
    return Formatter {
        header = return (),
        onResult = collectResult ref,
        onFailure = outputError,
        footer = finish ref
    }

42 43 44 45 46 47 48 49 50 51 52 53 54
instance ToJSON Replacement where
    toJSON replacement =
        let start = repStartPos replacement
            end = repEndPos replacement
            str = repString replacement in
        object [
          "line" .= posLine start,
          "endLine" .= posLine end,
          "column" .= posColumn start,
          "endColumn" .= posColumn end,
          "replaceWith" .= str
        ]

V
Vidar Holen 已提交
55
instance ToJSON PositionedComment where
N
Ng Zhi An 已提交
56 57 58 59
  toJSON comment =
    let start = pcStartPos comment
        end = pcEndPos comment
        c = pcComment comment in
N
Ng Zhi An 已提交
60 61 62 63 64 65 66
    object [
      "file" .= posFile start,
      "line" .= posLine start,
      "endLine" .= posLine end,
      "column" .= posColumn start,
      "endColumn" .= posColumn end,
      "level" .= severityText comment,
N
Ng Zhi An 已提交
67
      "code" .= cCode c,
68 69
      "message" .= cMessage c,
      "fix" .= pcFix comment
N
Ng Zhi An 已提交
70
    ]
71

N
Ng Zhi An 已提交
72 73 74 75
  toEncoding comment =
    let start = pcStartPos comment
        end = pcEndPos comment
        c = pcComment comment in
N
Ng Zhi An 已提交
76 77 78 79 80 81 82
    pairs (
         "file" .= posFile start
      <> "line" .= posLine start
      <> "endLine" .= posLine end
      <> "column" .= posColumn start
      <> "endColumn" .= posColumn end
      <> "level" .= severityText comment
N
Ng Zhi An 已提交
83 84
      <> "code" .= cCode c
      <> "message" .= cMessage c
V
Vidar Holen 已提交
85
      <> "fix" .= pcFix comment
N
Ng Zhi An 已提交
86
    )
87

V
Vidar Holen 已提交
88 89 90 91 92
instance ToJSON Fix where
    toJSON fix = object [
        "replacements" .= fixReplacements fix
        ]

93 94 95 96 97 98
outputError file msg = hPutStrLn stderr $ file ++ ": " ++ msg
collectResult ref result _ =
    modifyIORef ref (\x -> crComments result ++ x)

finish ref = do
    list <- readIORef ref
N
Ng Zhi An 已提交
99
    BL.putStrLn $ encode list