{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-- |
--
-- Utility for formatting @'Idea'@ data in accordance with the Code Climate
-- spec: <https://github.com/codeclimate/spec>
--
module CC
    ( printIssue
    , fromIdea
    ) where

import Data.Aeson (ToJSON(..), (.=), encode, object)
import Data.Char (toUpper)
import Data.Text (Text)

import qualified Data.Text as T
import qualified Data.ByteString.Lazy.Char8 as C8

import Idea (Idea(..), Severity(..))

import qualified SrcLoc as GHC
import qualified GHC.Util as GHC

data Issue = Issue
    { Issue -> Text
issueType :: Text
    , Issue -> Text
issueCheckName :: Text
    , Issue -> Text
issueDescription :: Text
    , Issue -> Text
issueContent :: Text
    , Issue -> [Text]
issueCategories :: [Text]
    , Issue -> Location
issueLocation :: Location
    , Issue -> Int
issueRemediationPoints :: Int
    }

data Location = Location FilePath Position Position
data Position = Position Int Int

instance ToJSON Issue where
    toJSON :: Issue -> Value
toJSON Issue{Int
[Text]
Text
Location
issueRemediationPoints :: Int
issueLocation :: Location
issueCategories :: [Text]
issueContent :: Text
issueDescription :: Text
issueCheckName :: Text
issueType :: Text
issueRemediationPoints :: Issue -> Int
issueLocation :: Issue -> Location
issueCategories :: Issue -> [Text]
issueContent :: Issue -> Text
issueDescription :: Issue -> Text
issueCheckName :: Issue -> Text
issueType :: Issue -> Text
..} = [Pair] -> Value
object
        [ Text
"type" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
issueType
        , Text
"check_name" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
issueCheckName
        , Text
"description" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
issueDescription
        , Text
"content" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [Pair] -> Value
object
            [ Text
"body" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
issueContent
            ]
        , Text
"categories" Text -> [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [Text]
issueCategories
        , Text
"location" Text -> Location -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Location
issueLocation
        , Text
"remediation_points" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Int
issueRemediationPoints
        ]

instance ToJSON Location where
    toJSON :: Location -> Value
toJSON (Location FilePath
path Position
begin Position
end) = [Pair] -> Value
object
        [ Text
"path" Text -> FilePath -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= FilePath
path
        , Text
"positions" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [Pair] -> Value
object
            [ Text
"begin" Text -> Position -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Position
begin
            , Text
"end" Text -> Position -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Position
end
            ]
        ]

instance ToJSON Position where
    toJSON :: Position -> Value
toJSON (Position Int
line Int
column) = [Pair] -> Value
object
        [ Text
"line" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Int
line
        , Text
"column" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Int
column
        ]

-- | Print an @'Issue'@ with trailing null-terminator and newline
--
-- The trailing newline will be ignored, but makes the output more readable
--
printIssue :: Issue -> IO ()
printIssue :: Issue -> IO ()
printIssue = ByteString -> IO ()
C8.putStrLn (ByteString -> IO ()) -> (Issue -> ByteString) -> Issue -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\0") (ByteString -> ByteString)
-> (Issue -> ByteString) -> Issue -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Issue -> ByteString
forall a. ToJSON a => a -> ByteString
encode

-- | Convert an hlint @'Idea'@ to a datatype more easily serialized for CC
fromIdea :: Idea -> Issue
fromIdea :: Idea -> Issue
fromIdea Idea{FilePath
[FilePath]
[Refactoring SrcSpan]
[Note]
Maybe FilePath
SrcSpan
Severity
ideaRefactoring :: Idea -> [Refactoring SrcSpan]
ideaNote :: Idea -> [Note]
ideaTo :: Idea -> Maybe FilePath
ideaFrom :: Idea -> FilePath
ideaSpan :: Idea -> SrcSpan
ideaHint :: Idea -> FilePath
ideaSeverity :: Idea -> Severity
ideaDecl :: Idea -> [FilePath]
ideaModule :: Idea -> [FilePath]
ideaRefactoring :: [Refactoring SrcSpan]
ideaNote :: [Note]
ideaTo :: Maybe FilePath
ideaFrom :: FilePath
ideaSpan :: SrcSpan
ideaHint :: FilePath
ideaSeverity :: Severity
ideaDecl :: [FilePath]
ideaModule :: [FilePath]
..} = Issue :: Text -> Text -> Text -> Text -> [Text] -> Location -> Int -> Issue
Issue
    { issueType :: Text
issueType = Text
"issue"
    , issueCheckName :: Text
issueCheckName = Text
"HLint/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
T.pack (FilePath -> FilePath
camelize FilePath
ideaHint)
    , issueDescription :: Text
issueDescription = FilePath -> Text
T.pack FilePath
ideaHint
    , issueContent :: Text
issueContent = FilePath -> Maybe FilePath -> Text
content FilePath
ideaFrom Maybe FilePath
ideaTo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Note] -> Text
forall a. Show a => [a] -> Text
listNotes [Note]
ideaNote
    , issueCategories :: [Text]
issueCategories = FilePath -> [Text]
forall a p. IsString a => p -> [a]
categories FilePath
ideaHint
    , issueLocation :: Location
issueLocation = SrcSpan -> Location
fromSrcSpan SrcSpan
ideaSpan
    , issueRemediationPoints :: Int
issueRemediationPoints = Severity -> Int
points Severity
ideaSeverity
    }

  where
    content :: FilePath -> Maybe FilePath -> Text
content FilePath
from Maybe FilePath
Nothing = [Text] -> Text
T.unlines
        [ Text
"Found"
        , Text
""
        , Text
"```"
        , FilePath -> Text
T.pack FilePath
from
        , Text
"```"
        , Text
""
        , Text
"remove it."
        ]

    content FilePath
from (Just FilePath
to) = [Text] -> Text
T.unlines
        [ Text
"Found"
        , Text
""
        , Text
"```"
        , FilePath -> Text
T.pack FilePath
from
        , Text
"```"
        , Text
""
        , Text
"Perhaps"
        , Text
""
        , Text
"```"
        , FilePath -> Text
T.pack FilePath
to
        , Text
"```"
        ]

    listNotes :: [a] -> Text
listNotes [] = Text
""
    listNotes [a]
notes = [Text] -> Text
T.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
        [ Text
""
        , Text
"Applying this change:"
        , Text
""
        ] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (a -> Text) -> [a] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ((Text
"* " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
T.pack (FilePath -> Text) -> (a -> FilePath) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> FilePath
forall a. Show a => a -> FilePath
show) [a]
notes

    categories :: p -> [a]
categories p
_ = [a
"Style"]

    points :: Severity -> Int
points Severity
Ignore = Int
0
    points Severity
Suggestion = Int
basePoints
    points Severity
Warning = Int
5 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
basePoints
    points Severity
Error = Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
basePoints

fromSrcSpan :: GHC.SrcSpan -> Location
fromSrcSpan :: SrcSpan -> Location
fromSrcSpan GHC.SrcSpan{Int
FilePath
srcSpanEndColumn :: SrcSpan -> Int
srcSpanEndLine' :: SrcSpan -> Int
srcSpanStartColumn :: SrcSpan -> Int
srcSpanStartLine' :: SrcSpan -> Int
srcSpanFilename :: SrcSpan -> FilePath
srcSpanEndColumn :: Int
srcSpanEndLine' :: Int
srcSpanStartColumn :: Int
srcSpanStartLine' :: Int
srcSpanFilename :: FilePath
..} = FilePath -> Position -> Position -> Location
Location
    (FilePath -> FilePath
locationFileName FilePath
srcSpanFilename)
    (Int -> Int -> Position
Position Int
srcSpanStartLine' Int
srcSpanStartColumn)
    (Int -> Int -> Position
Position Int
srcSpanEndLine' Int
srcSpanEndColumn)
  where
    locationFileName :: FilePath -> FilePath
locationFileName (Char
'.':Char
'/':FilePath
x) = FilePath
x
    locationFileName FilePath
x = FilePath
x

camelize :: String -> String
camelize :: FilePath -> FilePath
camelize = (FilePath -> FilePath) -> [FilePath] -> FilePath
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap FilePath -> FilePath
capitalize ([FilePath] -> FilePath)
-> (FilePath -> [FilePath]) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
words

capitalize :: String -> String
capitalize :: FilePath -> FilePath
capitalize [] = []
capitalize (Char
c:FilePath
rest) = Char -> Char
toUpper Char
c Char -> FilePath -> FilePath
forall a. a -> [a] -> [a]
: FilePath
rest

-- "The baseline remediation points value is 50,000, which is the time it takes
-- to fix a trivial code style issue like a missing semicolon on a single line,
-- including the time for the developer to open the code, make the change, and
-- confidently commit the fix. All other remediation points values are expressed
-- in multiples of that Basic Remediation Point Value."
basePoints :: Int
basePoints :: Int
basePoints = Int
50000