module Hint.Smell (
smellModuleHint,
smellHint
) where
import Hint.Type(ModuHint,ModuleEx(..),DeclHint,Idea(..),rawIdea,warn)
import Config.Type
import Data.Generics.Uniplate.DataOnly
import Data.List.Extra
import qualified Data.Map as Map
import BasicTypes
import GHC.Hs
import RdrName
import Outputable
import Bag
import SrcLoc
import Language.Haskell.GhclibParserEx.GHC.Utils.Outputable
smellModuleHint :: [Setting] -> ModuHint
smellModuleHint :: [Setting] -> ModuHint
smellModuleHint settings :: [Setting]
settings scope :: Scope
scope m :: ModuleEx
m =
let (L _ mod :: HsModule GhcPs
mod) = ModuleEx -> GenLocated SrcSpan (HsModule GhcPs)
ghcModule ModuleEx
m
imports :: [LImportDecl GhcPs]
imports = HsModule GhcPs -> [LImportDecl GhcPs]
forall pass. HsModule pass -> [LImportDecl pass]
hsmodImports HsModule GhcPs
mod in
case SmellType -> Map SmellType Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SmellType
SmellManyImports ([Setting] -> Map SmellType Int
smells [Setting]
settings) of
Just n :: Int
n | [LImportDecl GhcPs] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LImportDecl GhcPs]
imports Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n ->
let span :: SrcSpan
span = (SrcSpan -> SrcSpan -> SrcSpan) -> [SrcSpan] -> SrcSpan
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans ([SrcSpan] -> SrcSpan) -> [SrcSpan] -> SrcSpan
forall a b. (a -> b) -> a -> b
$ LImportDecl GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc (LImportDecl GhcPs -> SrcSpan) -> [LImportDecl GhcPs] -> [SrcSpan]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [LImportDecl GhcPs]
imports
displayImports :: String
displayImports = [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ LImportDecl GhcPs -> String
f (LImportDecl GhcPs -> String) -> [LImportDecl GhcPs] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [LImportDecl GhcPs]
imports
in [Severity
-> String
-> SrcSpan
-> String
-> Maybe String
-> [Note]
-> [Refactoring SrcSpan]
-> Idea
rawIdea Severity
Config.Type.Warning "Many imports" SrcSpan
span String
displayImports Maybe String
forall a. Maybe a
Nothing [] [] ]
where
f :: LImportDecl GhcPs -> String
f :: LImportDecl GhcPs -> String
f = String -> String
trimStart (String -> String)
-> (LImportDecl GhcPs -> String) -> LImportDecl GhcPs -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LImportDecl GhcPs -> String
forall a. Outputable a => a -> String
unsafePrettyPrint
_ -> []
smellHint :: [Setting] -> DeclHint
smellHint :: [Setting] -> DeclHint
smellHint settings :: [Setting]
settings scope :: Scope
scope m :: ModuleEx
m d :: LHsDecl GhcPs
d =
(LHsDecl GhcPs -> Int -> [Idea]) -> SmellType -> [Idea]
sniff LHsDecl GhcPs -> Int -> [Idea]
smellLongFunctions SmellType
SmellLongFunctions [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(LHsDecl GhcPs -> Int -> [Idea]) -> SmellType -> [Idea]
sniff LHsDecl GhcPs -> Int -> [Idea]
smellLongTypeLists SmellType
SmellLongTypeLists [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(LHsDecl GhcPs -> Int -> [Idea]) -> SmellType -> [Idea]
sniff LHsDecl GhcPs -> Int -> [Idea]
smellManyArgFunctions SmellType
SmellManyArgFunctions
where
sniff :: (LHsDecl GhcPs -> Int -> [Idea]) -> SmellType -> [Idea]
sniff f :: LHsDecl GhcPs -> Int -> [Idea]
f t :: SmellType
t = (Idea -> Idea) -> [Idea] -> [Idea]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\i :: Idea
i -> Idea
i {ideaTo :: Maybe String
ideaTo = Maybe String
forall a. Maybe a
Nothing }) ([Idea] -> [Idea]) -> ([Idea] -> [Idea]) -> [Idea] -> [Idea]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Idea] -> [Idea]
forall a. Int -> [a] -> [a]
take 1 ([Idea] -> [Idea]) -> [Idea] -> [Idea]
forall a b. (a -> b) -> a -> b
$ [Idea] -> (Int -> [Idea]) -> Maybe Int -> [Idea]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (LHsDecl GhcPs -> Int -> [Idea]
f LHsDecl GhcPs
d) (Maybe Int -> [Idea]) -> Maybe Int -> [Idea]
forall a b. (a -> b) -> a -> b
$ SmellType -> Map SmellType Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SmellType
t ([Setting] -> Map SmellType Int
smells [Setting]
settings)
smellLongFunctions :: LHsDecl GhcPs -> Int -> [Idea]
smellLongFunctions :: LHsDecl GhcPs -> Int -> [Idea]
smellLongFunctions d :: LHsDecl GhcPs
d n :: Int
n = [ Idea
idea
| (span :: SrcSpan
span, idea :: Idea
idea) <- LHsDecl GhcPs -> [(SrcSpan, Idea)]
declSpans LHsDecl GhcPs
d
, SrcSpan -> Int
spanLength SrcSpan
span Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n
]
declSpans :: LHsDecl GhcPs -> [(SrcSpan, Idea)]
declSpans :: LHsDecl GhcPs -> [(SrcSpan, Idea)]
declSpans
(L _ (ValD _
FunBind {fun_matches :: forall idL idR. HsBindLR idL idR -> MatchGroup idR (LHsExpr idR)
fun_matches=MG {
mg_origin :: forall p body. MatchGroup p body -> Origin
mg_origin=Origin
FromSource
, mg_alts :: forall p body. MatchGroup p body -> Located [LMatch p body]
mg_alts=(L _ [L _ Match {
m_ctxt :: forall p body.
Match p body -> HsMatchContext (NameOrRdrName (IdP p))
m_ctxt=HsMatchContext (NameOrRdrName (IdP GhcPs))
ctx
, m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss=GRHSs{grhssGRHSs :: forall p body. GRHSs p body -> [LGRHS p body]
grhssGRHSs=[locGrhs :: LGRHS GhcPs (LHsExpr GhcPs)
locGrhs]
, grhssLocalBinds :: forall p body. GRHSs p body -> LHsLocalBinds p
grhssLocalBinds=LHsLocalBinds GhcPs
where_}}])}})) =
HsMatchContext RdrName
-> LGRHS GhcPs (LHsExpr GhcPs) -> [(SrcSpan, Idea)]
rhsSpans HsMatchContext (NameOrRdrName (IdP GhcPs))
HsMatchContext RdrName
ctx LGRHS GhcPs (LHsExpr GhcPs)
locGrhs [(SrcSpan, Idea)] -> [(SrcSpan, Idea)] -> [(SrcSpan, Idea)]
forall a. [a] -> [a] -> [a]
++ LHsLocalBinds GhcPs -> [(SrcSpan, Idea)]
whereSpans LHsLocalBinds GhcPs
where_
declSpans f :: LHsDecl GhcPs
f@(L l :: SrcSpan
l (ValD _ FunBind {})) = [(SrcSpan
l, String
-> LHsDecl GhcPs -> LHsDecl GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Long function" LHsDecl GhcPs
f LHsDecl GhcPs
f [])]
declSpans _ = []
rhsSpans :: HsMatchContext RdrName -> LGRHS GhcPs (LHsExpr GhcPs) -> [(SrcSpan, Idea)]
rhsSpans :: HsMatchContext RdrName
-> LGRHS GhcPs (LHsExpr GhcPs) -> [(SrcSpan, Idea)]
rhsSpans _ (L _ (GRHS _ _ (L _ RecordCon {}))) = []
rhsSpans ctx :: HsMatchContext RdrName
ctx (L _ r :: GRHS GhcPs (LHsExpr GhcPs)
r@(GRHS _ _ (L l :: SrcSpan
l _))) =
[(SrcSpan
l, Severity
-> String
-> SrcSpan
-> String
-> Maybe String
-> [Note]
-> [Refactoring SrcSpan]
-> Idea
rawIdea Severity
Config.Type.Warning "Long function" SrcSpan
l (SDoc -> String
showSDocUnsafe (HsMatchContext RdrName -> GRHS GhcPs (LHsExpr GhcPs) -> SDoc
forall (idR :: Pass) body idL.
(OutputableBndrId idR, Outputable body) =>
HsMatchContext idL -> GRHS (GhcPass idR) body -> SDoc
pprGRHS HsMatchContext RdrName
ctx GRHS GhcPs (LHsExpr GhcPs)
r)) Maybe String
forall a. Maybe a
Nothing [] [])]
rhsSpans _ _ = []
whereSpans :: LHsLocalBinds GhcPs -> [(SrcSpan, Idea)]
whereSpans :: LHsLocalBinds GhcPs -> [(SrcSpan, Idea)]
whereSpans (L l :: SrcSpan
l (HsValBinds _ (ValBinds _ bs :: LHsBindsLR GhcPs GhcPs
bs _))) =
(GenLocated SrcSpan (HsBind GhcPs) -> [(SrcSpan, Idea)])
-> [GenLocated SrcSpan (HsBind GhcPs)] -> [(SrcSpan, Idea)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (LHsDecl GhcPs -> [(SrcSpan, Idea)]
declSpans (LHsDecl GhcPs -> [(SrcSpan, Idea)])
-> (GenLocated SrcSpan (HsBind GhcPs) -> LHsDecl GhcPs)
-> GenLocated SrcSpan (HsBind GhcPs)
-> [(SrcSpan, Idea)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\(L loc :: SrcSpan
loc bind :: HsBind GhcPs
bind) -> SrcSpan -> HsDecl GhcPs -> LHsDecl GhcPs
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XValD GhcPs -> HsBind GhcPs -> HsDecl GhcPs
forall p. XValD p -> HsBind p -> HsDecl p
ValD NoExtField
XValD GhcPs
noExtField HsBind GhcPs
bind))) (LHsBindsLR GhcPs GhcPs -> [GenLocated SrcSpan (HsBind GhcPs)]
forall a. Bag a -> [a]
bagToList LHsBindsLR GhcPs GhcPs
bs)
whereSpans _ = []
spanLength :: SrcSpan -> Int
spanLength :: SrcSpan -> Int
spanLength (RealSrcSpan span :: RealSrcSpan
span) = RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
span Int -> Int -> Int
forall a. Num a => a -> a -> a
- RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
span Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1
spanLength (UnhelpfulSpan _) = -1
smellLongTypeLists :: LHsDecl GhcPs -> Int -> [Idea]
smellLongTypeLists :: LHsDecl GhcPs -> Int -> [Idea]
smellLongTypeLists d :: LHsDecl GhcPs
d@(L _ (SigD _ (TypeSig _ _ (HsWC _ (HsIB _ (L _ t :: HsType GhcPs
t)))))) n :: Int
n =
String
-> LHsDecl GhcPs -> LHsDecl GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Long type list" LHsDecl GhcPs
d LHsDecl GhcPs
d [] Idea -> [HsType GhcPs] -> [Idea]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (HsType GhcPs -> Bool) -> [HsType GhcPs] -> [HsType GhcPs]
forall a. (a -> Bool) -> [a] -> [a]
filter HsType GhcPs -> Bool
forall pass. HsType pass -> Bool
longTypeList (HsType GhcPs -> [HsType GhcPs]
forall on. Uniplate on => on -> [on]
universe HsType GhcPs
t)
where
longTypeList :: HsType pass -> Bool
longTypeList (HsExplicitListTy _ IsPromoted x :: [LHsType pass]
x) = [LHsType pass] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LHsType pass]
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n
longTypeList _ = Bool
False
smellLongTypeLists _ _ = []
smellManyArgFunctions :: LHsDecl GhcPs -> Int -> [Idea]
smellManyArgFunctions :: LHsDecl GhcPs -> Int -> [Idea]
smellManyArgFunctions d :: LHsDecl GhcPs
d@(L _ (SigD _ (TypeSig _ _ (HsWC _ (HsIB _ (L _ t :: HsType GhcPs
t)))))) n :: Int
n =
String
-> LHsDecl GhcPs -> LHsDecl GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Many arg function" LHsDecl GhcPs
d LHsDecl GhcPs
d [] Idea -> [HsType GhcPs] -> [Idea]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (HsType GhcPs -> Bool) -> [HsType GhcPs] -> [HsType GhcPs]
forall a. (a -> Bool) -> [a] -> [a]
filter HsType GhcPs -> Bool
manyArgFunction (HsType GhcPs -> [HsType GhcPs]
forall on. Uniplate on => on -> [on]
universe HsType GhcPs
t)
where
manyArgFunction :: HsType GhcPs -> Bool
manyArgFunction t :: HsType GhcPs
t = HsType GhcPs -> Int
countFunctionArgs HsType GhcPs
t Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n
smellManyArgFunctions _ _ = []
countFunctionArgs :: HsType GhcPs -> Int
countFunctionArgs :: HsType GhcPs -> Int
countFunctionArgs (HsFunTy _ _ t :: GenLocated SrcSpan (HsType GhcPs)
t) = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HsType GhcPs -> Int
countFunctionArgs (GenLocated SrcSpan (HsType GhcPs)
-> SrcSpanLess (GenLocated SrcSpan (HsType GhcPs))
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc GenLocated SrcSpan (HsType GhcPs)
t)
countFunctionArgs (HsParTy _ t :: GenLocated SrcSpan (HsType GhcPs)
t) = HsType GhcPs -> Int
countFunctionArgs (GenLocated SrcSpan (HsType GhcPs)
-> SrcSpanLess (GenLocated SrcSpan (HsType GhcPs))
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc GenLocated SrcSpan (HsType GhcPs)
t)
countFunctionArgs _ = 0
smells :: [Setting] -> Map.Map SmellType Int
smells :: [Setting] -> Map SmellType Int
smells settings :: [Setting]
settings = [(SmellType, Int)] -> Map SmellType Int
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [ (SmellType
smellType, Int
smellLimit) | SettingSmell smellType :: SmellType
smellType smellLimit :: Int
smellLimit <- [Setting]
settings]