ghc-mod/Language/Haskell/GhcMod/Lint.hs

35 lines
1.4 KiB
Haskell
Raw Normal View History

2013-05-17 01:00:01 +00:00
module Language.Haskell.GhcMod.Lint where
2010-05-06 06:29:55 +00:00
import Exception (ghandle)
import Control.Exception (SomeException(..))
2014-04-28 12:47:08 +00:00
import Language.Haskell.GhcMod.Logger (checkErrorPrefix)
2014-05-11 22:40:00 +00:00
import Language.Haskell.GhcMod.Convert
import Language.Haskell.GhcMod.Types
import Language.Haskell.GhcMod.Monad
2016-01-05 07:14:07 +00:00
import Language.Haskell.HLint3
2010-05-06 06:29:55 +00:00
import Language.Haskell.GhcMod.Utils (withMappedFile)
import Language.Haskell.Exts.SrcLoc (SrcLoc(..))
import System.IO
2015-07-02 18:45:07 +00:00
2013-05-20 05:28:56 +00:00
-- | Checking syntax of a target file using hlint.
-- Warnings and errors are returned.
lint :: IOish m
2015-12-07 16:57:33 +00:00
=> LintOpts -- ^ Configuration parameters
2015-12-05 20:55:12 +00:00
-> FilePath -- ^ A target file.
-> GhcModT m String
2016-01-05 07:14:07 +00:00
lint opt file = ghandle handler $
withMappedFile file $ \tempfile -> do
(flags, classify, hint) <- liftIO $ argsSettings $ optLintHlintOpts opt
hSrc <- liftIO $ openFile tempfile ReadMode
2016-01-12 19:09:33 +00:00
liftIO $ hSetEncoding hSrc (encoding flags)
res <- liftIO $ parseModuleEx flags file =<< Just `fmap` hGetContents hSrc
2016-01-05 07:14:07 +00:00
case res of
2016-07-11 14:49:18 +00:00
Right m -> pack . map show $ filter ((/=Ignore) . ideaSeverity) $ applyHints classify hint [m]
2016-01-05 07:14:07 +00:00
Left ParseError{parseErrorLocation=loc, parseErrorMessage=err} ->
return $ showSrcLoc loc ++ ":Error:" ++ err ++ "\n"
2016-01-05 07:14:07 +00:00
where
2015-07-02 18:45:07 +00:00
pack = convert' . map init -- init drops the last \n.
2014-04-25 02:08:29 +00:00
handler (SomeException e) = return $ checkErrorPrefix ++ show e ++ "\n"
showSrcLoc (SrcLoc f l c) = concat [f, ":", show l, ":", show c]