packages feed

hs-inspector 0.2.1.0 → 0.3.0.0

raw patch · 3 files changed

+58/−11 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Language.Haskell.Inspector: testAnyWithBindingExpr :: (EO -> Bool) -> Binding -> Code -> Bool
- Language.Haskell.Inspector: testAnyWithBindingRhs :: (HsRhs -> Bool) -> Binding -> Code -> Bool
+ Language.Haskell.Inspector: hasConditional :: Inspection
+ Language.Haskell.Inspector: hasIf :: Inspection
+ Language.Haskell.Inspector: isBindingEO :: (EO -> Bool) -> Binding -> Code -> Bool
+ Language.Haskell.Inspector: isBindingRhs :: (HsRhs -> Bool) -> Binding -> Code -> Bool
+ Language.Haskell.Inspector.Smell: hasRedundantBooleanComparison :: Inspection
+ Language.Haskell.Inspector.Smell: hasRedundantGuards :: Inspection
+ Language.Haskell.Inspector.Smell: hasRedundantIf :: Inspection
+ Language.Haskell.Inspector.Smell: hasRedundantLambda :: Inspection
+ Language.Haskell.Inspector.Smell: isBooleanLiteral :: HsExp -> Bool

Files

hs-inspector.cabal view
@@ -2,7 +2,7 @@ --  http://haskell.org/cabal/users-guide/  name:                hs-inspector-version:             0.2.1.0+version:             0.3.0.0 license:             MIT license-file:        LICENSE author:              Franco Leonardo Bulgarelli@@ -21,8 +21,9 @@   hs-source-dirs:       src   exposed-modules:-    Language.Haskell.Inspector+    Language.Haskell.Inspector.Smell     Language.Haskell.Explorer+    Language.Haskell.Inspector   build-depends:     base                      >= 4     && < 5,     haskell-src               >= 1     && < 1.1
src/Language/Haskell/Inspector.hs view
@@ -14,22 +14,33 @@ -- | Inspection that tells whether a binding uses the composition operator '.' -- in its definition hasComposition :: Inspection-hasComposition = testAnyWithBindingExpr f+hasComposition = isBindingEO f   where f (O (HsQVarOp (UnQual (HsSymbol ".")))) = True         f _ = False  -- | Inspection that tells whether a binding uses guards -- in its definition hasGuards :: Inspection-hasGuards = testAnyWithBindingRhs f+hasGuards = isBindingRhs f   where f (HsGuardedRhss _) = True         f _ = False +-- | Inspection that tells whether a binding uses ifs+-- in its definition+hasIf :: Inspection+hasIf = isBindingEO f+  where f (E (HsIf _ _ _)) = True+        f _ = False +-- | Inspection that tells whether a binding uses ifs or guards+-- in its definition+hasConditional :: Inspection+hasConditional target code = hasIf target code || hasGuards target code+ -- | Inspection that tells whether a binding uses a lambda expression -- in its definition hasLambda :: Inspection-hasLambda = testAnyWithBindingExpr f+hasLambda = isBindingEO f   where f (E (HsLambda _ _ _)) = True         f _ = False @@ -38,11 +49,10 @@ hasDirectRecursion :: Inspection hasDirectRecursion binding = hasUsage binding binding - -- | Inspection that tells whether a binding uses the the given target binding -- in its definition hasUsage :: String -> Inspection-hasUsage target = testAnyWithBindingExpr f+hasUsage target = isBindingEO f   where f (O (HsQVarOp name)) = isTarget name         f (E (HsVar    name)) = isTarget name         f _ = False@@ -57,7 +67,7 @@ -- | Inspection that tells whether a binding uses lists comprehensions -- in its definition hasComprehension :: Inspection-hasComprehension = testAnyWithBindingExpr f+hasComprehension = isBindingEO f   where f (E (HsListComp _ _)) = True         f _ = False @@ -68,10 +78,12 @@ isParseable :: Code -> Bool isParseable = testWithCode (const True) -testAnyWithBindingExpr f = testAnyWithBindingRhs testExprs-  where testExprs rhs = exploreExprs f $ topExprs rhs+-- =================================================== -testAnyWithBindingRhs f = testWithBindingRhs (any f)+isBindingEO f = isBindingRhs isExpr+  where isExpr rhs = exploreExprs f $ topExprs rhs++isBindingRhs f = testWithBindingRhs (any f)  testWithBindingRhs :: ([HsRhs] -> Bool) -> Binding -> Code -> Bool testWithBindingRhs f binding  = orFalse . withBindingRhs f binding
+ src/Language/Haskell/Inspector/Smell.hs view
@@ -0,0 +1,34 @@+module Language.Haskell.Inspector.Smell where++import Language.Haskell.Explorer+import Language.Haskell.Syntax+import Language.Haskell.Inspector++hasRedundantBooleanComparison :: Inspection+hasRedundantBooleanComparison = isBindingEO f+  where f (E (HsInfixApp x (HsQVarOp (UnQual (HsSymbol c))) y)) = any isBooleanLiteral [x, y] && isComp c+        f _ = False++        isComp c = c == "==" || c == "/="++hasRedundantIf :: Inspection+hasRedundantIf = isBindingEO f+  where f (E (HsIf _ x y)) = all isBooleanLiteral [x, y]+        f _            = False+++hasRedundantGuards :: Inspection+hasRedundantGuards = isBindingRhs f -- TODO not true when condition is a pattern+  where f (HsGuardedRhss [+            HsGuardedRhs _ _ x,+            HsGuardedRhs _ (HsVar (UnQual (HsIdent "otherwise"))) y]) = all isBooleanLiteral [x, y]+        f _ = False++hasRedundantLambda :: Inspection+hasRedundantLambda = isBindingEO f+  where f (E (HsLambda _ [HsPVar (HsIdent x)] (HsApp _ (HsVar (UnQual (HsIdent y)))))) = x == y+        f _ = False -- TODO consider parenthesis and symbols++isBooleanLiteral (HsCon (UnQual (HsIdent "True")))  = True+isBooleanLiteral (HsCon (UnQual (HsIdent "False"))) = True+isBooleanLiteral _                                  = False