module Hhp.Lint where

import Control.Exception (handle, SomeException(..))
import Language.Haskell.HLint (hlint)

import Hhp.Logger (checkErrorPrefix)
import Hhp.Types

-- | Checking syntax of a target file using hlint.
--   Warnings and errors are returned.
lintSyntax :: Options
           -> FilePath  -- ^ A target file.
           -> IO String
lintSyntax :: Options -> FilePath -> IO FilePath
lintSyntax Options
opt FilePath
file = forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle forall {m :: * -> *}. Monad m => SomeException -> m FilePath
handler forall a b. (a -> b) -> a -> b
$ [Idea] -> FilePath
pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [FilePath] -> IO [Idea]
hlint (FilePath
file forall a. a -> [a] -> [a]
: FilePath
"--quiet" forall a. a -> [a] -> [a]
: [FilePath]
hopts)
  where
    pack :: [Idea] -> FilePath
pack = forall a. ToString a => Options -> a -> FilePath
convert Options
opt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall a. [a] -> [a]
init forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> FilePath
show) -- init drops the last \n.
    hopts :: [FilePath]
hopts = Options -> [FilePath]
hlintOpts Options
opt
    handler :: SomeException -> m FilePath
handler (SomeException e
e) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath
checkErrorPrefix forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show e
e forall a. [a] -> [a] -> [a]
++ FilePath
"\n"