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

closing #129

parent ccdb664d
...@@ -12,54 +12,106 @@ ...@@ -12,54 +12,106 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"data Thing = Thing Int Int" "do\n",
" return 3\n",
" \n",
"a <- return 3"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
"prompt_number": 5 {
"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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"f :: Int -> Thing -> Int\n", "return 3"
"f i (Thing a b) = i *( a + b)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"3"
]
}
],
"prompt_number": 6 "prompt_number": 6
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"x = Thing 10 9" "data X = X"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 7 "prompt_number": 3
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"f 3 x" "f X"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [ "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": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data",
"text": [ "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", "cell_type": "code",
......
...@@ -48,11 +48,12 @@ lint blocks = do ...@@ -48,11 +48,12 @@ lint blocks = do
writeFile (fromString filename) fileContents writeFile (fromString filename) fileContents
suggestions <- catMaybes <$> map parseSuggestion <$> hlint [filename, "--quiet"] suggestions <- catMaybes <$> map parseSuggestion <$> hlint [filename, "--quiet"]
return $ return $ Display $
if null suggestions if null suggestions
then Display [] then []
else Display else
[plain $ concatMap plainSuggestion suggestions, html $ htmlSuggestions suggestions] [plain $ concatMap plainSuggestion suggestions,
html $ htmlSuggestions suggestions]
where where
-- Join together multiple valid file blocks into a single file. -- Join together multiple valid file blocks into a single file.
-- However, join them with padding so that the line numbers are -- However, join them with padding so that the line numbers are
...@@ -117,6 +118,7 @@ parseSuggestion :: Suggestion -> Maybe LintSuggestion ...@@ -117,6 +118,7 @@ parseSuggestion :: Suggestion -> Maybe LintSuggestion
parseSuggestion suggestion = do parseSuggestion suggestion = do
let str = showSuggestion (show suggestion) let str = showSuggestion (show suggestion)
severity = suggestionSeverity suggestion severity = suggestionSeverity suggestion
guard (severity /= HLint.Ignore) guard (severity /= HLint.Ignore)
let lintSeverity = case severity of let lintSeverity = case severity of
Warning -> LintWarning Warning -> LintWarning
...@@ -147,23 +149,37 @@ parseSuggestion suggestion = do ...@@ -147,23 +149,37 @@ parseSuggestion suggestion = do
showSuggestion :: String -> String showSuggestion :: String -> String
showSuggestion = showSuggestion =
replace ("return " ++ lintIdent) "" . remove ("return " ++ lintIdent) .
replace (lintIdent ++ "=") "" . remove (lintIdent ++ "=") .
dropDo dropDo
where where
remove str = replace str ""
-- drop leading ' do ', and blank spaces following -- Drop leading ' do ', and blank spaces following.
dropDo :: String -> String dropDo :: String -> String
dropDo = unlines . f . lines dropDo string =
where -- If this is not a statement, we don't need to drop the do statement.
f :: [String] -> [String] if ("return " ++ lintIdent) `isInfixOf` string
f ((stripPrefix " do " -> Just a) : as) = then unlines . clean . lines $ string
let as' = catMaybes 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 $ takeWhile isJust
$ map (stripPrefix " ") as $ map (stripPrefix " ") as
in a : as' ++ f (drop (length as') as) fullDo = a:unindented
f (x:xs) = x : f xs afterDo = drop (length unindented) as
f [] = [] 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. -- | 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. -- The line number on the output is the same as on the input.
...@@ -193,7 +209,7 @@ makeValid (Located line block) = Located line $ ...@@ -193,7 +209,7 @@ makeValid (Located line block) = Located line $
$ filter (not . all isSpace) $ filter (not . all isSpace)
(lines (expandTabs stmt)) (lines (expandTabs stmt))
finalReturn = replicate nLeading ' ' ++ "return " ++ lintIdent 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. -- Modules, declarations, and type signatures are fine as is.
Module mod -> mod 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