Commit 9b9e9bab authored by Andrew Gibiansky's avatar Andrew Gibiansky

closing #129

parent ccdb664d
......@@ -12,54 +12,106 @@
"cell_type": "code",
"collapsed": false,
"input": [
"data Thing = Thing Int Int"
"do\n",
" return 3\n",
" \n",
"a <- return 3"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
"outputs": [
{
"html": [
" <div class=\"suggestion-name\" style=\"clear:both;\"> Redundant do</div> <div class=\"suggestion-row\" style=\"float: left;\"> <div class=\"suggestion-error\">Found:</div> <div class=\"highlight-code\" id=\"haskell\">return 3\n",
"</div> </div> <div class=\"suggestion-row\" style=\"float: left;\"> <div class=\"suggestion-error\">Why Not:</div> <div class=\"highlight-code\" id=\"haskell\"> return 3\n",
"</div> </div> <div class=\"suggestion-name\" style=\"clear:both;\"> Use let</div> <div class=\"suggestion-row\" style=\"float: left;\"> <div class=\"suggestion-warning\">Found:</div> <div class=\"highlight-code\" id=\"haskell\">a <- return 3\n",
"\n",
"</div> </div> <div class=\"suggestion-row\" style=\"float: left;\"> <div class=\"suggestion-warning\">Why Not:</div> <div class=\"highlight-code\" id=\"haskell\">let a = 3\n",
"\n",
"</div> </div> "
],
"metadata": {},
"output_type": "display_data",
"text": [
"Line 1: Redundant do\n",
"Found:\n",
"return 3\n",
"\n",
"Why not:\n",
" return 3\n",
"Line 4: Use let\n",
"Found:\n",
"a <- return 3\n",
"\n",
"\n",
"Why not:\n",
"let a = 3"
]
},
{
"metadata": {},
"output_type": "display_data",
"text": [
"3"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f :: Int -> Thing -> Int\n",
"f i (Thing a b) = i *( a + b)"
"return 3"
],
"language": "python",
"metadata": {},
"outputs": [],
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"3"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = Thing 10 9"
"data X = X"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f 3 x"
"f X"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span class='err-msg'>Couldn't match expected type `main:X' with actual type `X'<br/>In the first argument of `f', namely `X'<br/>In the expression: f X<br/>In an equation for `it': it = f X</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"57"
"Couldn't match expected type `main:X' with actual type `X'\n",
"In the first argument of `f', namely `X'\n",
"In the expression: f X\n",
"In an equation for `it': it = f X"
]
}
],
"prompt_number": 8
"prompt_number": 4
},
{
"cell_type": "code",
......
......@@ -48,11 +48,12 @@ lint blocks = do
writeFile (fromString filename) fileContents
suggestions <- catMaybes <$> map parseSuggestion <$> hlint [filename, "--quiet"]
return $
return $ Display $
if null suggestions
then Display []
else Display
[plain $ concatMap plainSuggestion suggestions, html $ htmlSuggestions suggestions]
then []
else
[plain $ concatMap plainSuggestion suggestions,
html $ htmlSuggestions suggestions]
where
-- Join together multiple valid file blocks into a single file.
-- However, join them with padding so that the line numbers are
......@@ -117,6 +118,7 @@ parseSuggestion :: Suggestion -> Maybe LintSuggestion
parseSuggestion suggestion = do
let str = showSuggestion (show suggestion)
severity = suggestionSeverity suggestion
guard (severity /= HLint.Ignore)
let lintSeverity = case severity of
Warning -> LintWarning
......@@ -147,23 +149,37 @@ parseSuggestion suggestion = do
showSuggestion :: String -> String
showSuggestion =
replace ("return " ++ lintIdent) "" .
replace (lintIdent ++ "=") "" .
remove ("return " ++ lintIdent) .
remove (lintIdent ++ "=") .
dropDo
where
remove str = replace str ""
-- drop leading ' do ', and blank spaces following
-- Drop leading ' do ', and blank spaces following.
dropDo :: String -> String
dropDo = unlines . f . lines
where
f :: [String] -> [String]
f ((stripPrefix " do " -> Just a) : as) =
let as' = catMaybes
dropDo string =
-- If this is not a statement, we don't need to drop the do statement.
if ("return " ++ lintIdent) `isInfixOf` string
then unlines . clean . lines $ string
else string
clean :: [String] -> [String]
-- If the first line starts with a `do`...
-- Note that hlint always indents by two spaces in its output.
clean ((stripPrefix " do " -> Just a) : as) =
-- Take all indented lines and unindent them.
let unindented = catMaybes
$ takeWhile isJust
$ map (stripPrefix " ") as
in a : as' ++ f (drop (length as') as)
f (x:xs) = x : f xs
f [] = []
fullDo = a:unindented
afterDo = drop (length unindented) as
in
--
fullDo ++ clean afterDo
-- Ignore other list elements - just proceed onwards.
clean (x:xs) = x : clean xs
clean [] = []
-- | Convert a code chunk into something that could go into a file.
-- The line number on the output is the same as on the input.
......@@ -193,7 +209,7 @@ makeValid (Located line block) = Located line $
$ filter (not . all isSpace)
(lines (expandTabs stmt))
finalReturn = replicate nLeading ' ' ++ "return " ++ lintIdent
in intercalate ("\n ") ((lintIdent ++ " $ do") : lines stmt ++ [finalReturn])
in intercalate "\n " ((lintIdent ++ " $ do") : lines stmt ++ [finalReturn])
-- Modules, declarations, and type signatures are fine as is.
Module mod -> mod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment