forked from ndmitchell/hlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCC.hs
147 lines (127 loc) · 3.88 KB
/
CC.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{-# LANGUAGE ImportQualifiedPost #-}
{-# 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 Data.Text qualified as T
import Data.ByteString.Lazy.Char8 qualified as C8
import Idea (Idea(..), Severity(..))
import GHC.Types.SrcLoc qualified as GHC
import GHC.Util qualified as GHC
data Issue = Issue
{ issueType :: Text
, issueCheckName :: Text
, issueDescription :: Text
, issueContent :: Text
, issueCategories :: [Text]
, issueLocation :: Location
, issueRemediationPoints :: Int
}
data Location = Location FilePath Position Position
data Position = Position Int Int
instance ToJSON Issue where
toJSON Issue{..} = object
[ "type" .= issueType
, "check_name" .= issueCheckName
, "description" .= issueDescription
, "content" .= object
[ "body" .= issueContent
]
, "categories" .= issueCategories
, "location" .= issueLocation
, "remediation_points" .= issueRemediationPoints
]
instance ToJSON Location where
toJSON (Location path begin end) = object
[ "path" .= path
, "positions" .= object
[ "begin" .= begin
, "end" .= end
]
]
instance ToJSON Position where
toJSON (Position line column) = object
[ "line" .= line
, "column" .= 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 = C8.putStrLn . (<> "\0") . encode
-- | Convert an hlint @'Idea'@ to a datatype more easily serialized for CC
fromIdea :: Idea -> Issue
fromIdea Idea{..} = Issue
{ issueType = "issue"
, issueCheckName = "HLint/" <> T.pack (camelize ideaHint)
, issueDescription = T.pack ideaHint
, issueContent = content ideaFrom ideaTo <> listNotes ideaNote
, issueCategories = categories ideaHint
, issueLocation = fromSrcSpan ideaSpan
, issueRemediationPoints = points ideaSeverity
}
where
content from Nothing = T.unlines
[ "Found"
, ""
, "```"
, T.pack from
, "```"
, ""
, "remove it."
]
content from (Just to) = T.unlines
[ "Found"
, ""
, "```"
, T.pack from
, "```"
, ""
, "Perhaps"
, ""
, "```"
, T.pack to
, "```"
]
listNotes [] = ""
listNotes notes = T.unlines $
[ ""
, "Applying this change:"
, ""
] ++ map (("* " <>) . T.pack . show) notes
categories _ = ["Style"]
points Ignore = 0
points Suggestion = basePoints
points Warning = 5 * basePoints
points Error = 10 * basePoints
fromSrcSpan :: GHC.SrcSpan -> Location
fromSrcSpan GHC.SrcSpan{..} = Location
(locationFileName srcSpanFilename)
(Position srcSpanStartLine' srcSpanStartColumn)
(Position srcSpanEndLine' srcSpanEndColumn)
where
locationFileName ('.':'/':x) = x
locationFileName x = x
camelize :: String -> String
camelize = concatMap capitalize . words
capitalize :: String -> String
capitalize [] = []
capitalize (c:rest) = toUpper c : 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 = 50000