From 5ed89d22411b407b94cab060e6395a235118bc47 Mon Sep 17 00:00:00 2001 From: Ng Zhi An Date: Sat, 20 Oct 2018 23:20:30 -0700 Subject: [PATCH] Change definition of Replacement, add ToJSON instance for it --- src/ShellCheck/Analytics.hs | 18 +++++++++++++----- src/ShellCheck/Formatter/JSON.hs | 18 ++++++++++++++++-- src/ShellCheck/Formatter/TTY.hs | 11 ++++++----- src/ShellCheck/Interface.hs | 17 +++++++++++++---- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index d211add..a034031 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -249,7 +249,11 @@ replace_start id params n r = posColumn = posColumn start + n } in - [R start new_end r] + newReplacement { + repStartPos = start, + repEndPos = new_end, + repString = r + } replace_end id params n r = -- because of the way we count columns 1-based -- we have to offset end columns by 1 @@ -262,9 +266,13 @@ replace_end id params n r = posColumn = posColumn end + 1 } in - [R new_start new_end r] + newReplacement { + repStartPos = new_start, + repEndPos = new_end, + repString = r + } surround_with id params s = - (replace_start id params 0 s) ++ (replace_end id params 0 s) + [replace_start id params 0 s, replace_end id params 0 s] prop_checkEchoWc3 = verify checkEchoWc "n=$(echo $foo | wc -c)" checkEchoWc _ (T_Pipeline id _ [a, b]) = @@ -1363,7 +1371,7 @@ prop_checkBackticks3 = verifyNot checkBackticks "echo `#inlined comment` foo" checkBackticks params (T_Backticked id list) | not (null list) = addComment $ makeCommentWithFix StyleC id 2006 "Use $(...) notation instead of legacy backticked `...`." - ((replace_start id params 1 "$(") ++ (replace_end id params 1 ")")) + [(replace_start id params 1 "$("), (replace_end id params 1 ")")] -- style id 2006 "Use $(...) notation instead of legacy backticked `...`." checkBackticks _ _ = return () @@ -2570,7 +2578,7 @@ checkUncheckedCdPushdPopd params root = && not (isCondition $ getPath (parentMap params) t)) $ -- warn (getId t) 2164 "Use 'cd ... || exit' or 'cd ... || return' in case cd fails." warnWithFix (getId t) 2164 "Use 'cd ... || exit' or 'cd ... || return' in case cd fails." - (replace_end (getId t) params 0 " || exit") + [replace_end (getId t) params 0 " || exit"] checkElement _ = return () name t = fromMaybe "" $ getCommandName t isSafeDir t = case oversimplify t of diff --git a/src/ShellCheck/Formatter/JSON.hs b/src/ShellCheck/Formatter/JSON.hs index aac4d20..072af7e 100644 --- a/src/ShellCheck/Formatter/JSON.hs +++ b/src/ShellCheck/Formatter/JSON.hs @@ -39,6 +39,19 @@ format = do footer = finish ref } +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 + ] + instance ToJSON (PositionedComment) where toJSON comment = let start = pcStartPos comment @@ -52,7 +65,8 @@ instance ToJSON (PositionedComment) where "endColumn" .= posColumn end, "level" .= severityText comment, "code" .= cCode c, - "message" .= cMessage c + "message" .= cMessage c, + "fix" .= pcFix comment ] toEncoding comment = @@ -68,6 +82,7 @@ instance ToJSON (PositionedComment) where <> "level" .= severityText comment <> "code" .= cCode c <> "message" .= cMessage c + <> "replaceWith" .= pcFix comment ) outputError file msg = hPutStrLn stderr $ file ++ ": " ++ msg @@ -77,4 +92,3 @@ collectResult ref result _ = finish ref = do list <- readIORef ref BL.putStrLn $ encode list - diff --git a/src/ShellCheck/Formatter/TTY.hs b/src/ShellCheck/Formatter/TTY.hs index a1c98b1..224d6fe 100644 --- a/src/ShellCheck/Formatter/TTY.hs +++ b/src/ShellCheck/Formatter/TTY.hs @@ -142,11 +142,12 @@ fixedString comment line = apply_replacement rs line 0 where apply_replacement [] s _ = s - apply_replacement ((R startp endp r):xs) s offset = - let start = posColumn startp - end = posColumn endp - z = do_replace start end s r - len_r = (fromIntegral . length) r in + apply_replacement (rep:xs) s offset = + let replacementString = repString rep + start = (posColumn . repStartPos) rep + end = (posColumn . repEndPos) rep + z = do_replace start end s replacementString + len_r = (fromIntegral . length) replacementString in apply_replacement xs z (offset + (end - start) + len_r) -- start and end comes from pos, which is 1 based diff --git a/src/ShellCheck/Interface.hs b/src/ShellCheck/Interface.hs index ef9dd44..a429a88 100644 --- a/src/ShellCheck/Interface.hs +++ b/src/ShellCheck/Interface.hs @@ -50,7 +50,8 @@ module ShellCheck.Interface , newPositionedComment , newComment , Fix - , Replacement(R) + , Replacement(repStartPos, repEndPos, repString) + , newReplacement ) where import ShellCheck.AST @@ -196,9 +197,17 @@ newComment = Comment { } -- only support single line for now -data Replacement = - R Position Position String - deriving (Show, Eq) +data Replacement = Replacement { + repStartPos :: Position, + repEndPos :: Position, + repString :: String +} deriving (Show, Eq) + +newReplacement = Replacement { + repStartPos = newPosition, + repEndPos = newPosition, + repString = "" +} type Fix = [Replacement]