{-# LANGUAGE CPP #-}

module Hhp.Lint where

#ifdef HLINT
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 = (SomeException -> IO FilePath) -> IO FilePath -> IO FilePath
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle SomeException -> IO FilePath
forall {m :: * -> *}. Monad m => SomeException -> m FilePath
handler (IO FilePath -> IO FilePath) -> IO FilePath -> IO FilePath
forall a b. (a -> b) -> a -> b
$ [Idea] -> FilePath
pack ([Idea] -> FilePath) -> IO [Idea] -> IO FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [FilePath] -> IO [Idea]
hlint (FilePath
file FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: FilePath
"--quiet" FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: [FilePath]
hopts)
  where
    pack :: [Idea] -> FilePath
pack = Options -> [FilePath] -> FilePath
forall a. ToString a => Options -> a -> FilePath
convert Options
opt ([FilePath] -> FilePath)
-> ([Idea] -> [FilePath]) -> [Idea] -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Idea -> FilePath) -> [Idea] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> FilePath
forall a. HasCallStack => [a] -> [a]
init (FilePath -> FilePath) -> (Idea -> FilePath) -> Idea -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Idea -> FilePath
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) = FilePath -> m FilePath
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath -> m FilePath) -> FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
checkErrorPrefix FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ e -> FilePath
forall a. Show a => a -> FilePath
show e
e FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\n"
#else
import Hhp.Types

lintSyntax :: Options
           -> FilePath  -- ^ A target file.
           -> IO String
lintSyntax _ _ = return ""
#endif