hs-inspector 0.5.1.0 → 0.5.2.0
raw patch · 4 files changed
+27/−10 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Language.Haskell.Explorer: data EO
+ Language.Haskell.Explorer: data Expression
+ Language.Haskell.Inspector.Smell: hasRedundantParameter :: Inspection
- Language.Haskell.Explorer: E :: HsExp -> EO
+ Language.Haskell.Explorer: E :: HsExp -> Expression
- Language.Haskell.Explorer: O :: HsQOp -> EO
+ Language.Haskell.Explorer: O :: HsQOp -> Expression
- Language.Haskell.Explorer: expressionToBinding :: EO -> Maybe Binding
+ Language.Haskell.Explorer: expressionToBinding :: Expression -> Maybe Binding
- Language.Haskell.Explorer: expressionsOf :: Binding -> Code -> [EO]
+ Language.Haskell.Explorer: expressionsOf :: Binding -> Code -> [Expression]
- Language.Haskell.Inspector: hasExpression :: (EO -> Bool) -> Inspection
+ Language.Haskell.Inspector: hasExpression :: (Expression -> Bool) -> Inspection
Files
- hs-inspector.cabal +1/−1
- src/Language/Haskell/Explorer.hs +7/−7
- src/Language/Haskell/Inspector.hs +1/−1
- src/Language/Haskell/Inspector/Smell.hs +18/−1
hs-inspector.cabal view
@@ -2,7 +2,7 @@ -- http://haskell.org/cabal/users-guide/ name: hs-inspector-version: 0.5.1.0+version: 0.5.2.0 license: MIT license-file: LICENSE author: Franco Leonardo Bulgarelli
src/Language/Haskell/Explorer.hs view
@@ -7,7 +7,7 @@ transitiveBindingsOf, expressionsOf, expressionToBinding,- EO(..),+ Expression(..), Binding, Code) where @@ -20,7 +20,7 @@ type Binding = String type Code = String -data EO = E HsExp | O HsQOp+data Expression = E HsExp | O HsQOp -- xxxOf functions: take a binding and code -- parseXxx functions: take just code@@ -31,7 +31,7 @@ rhssOf :: Binding -> Code -> [HsRhs] rhssOf binding = concatMap rhsForBinding . declsOf binding -expressionsOf :: Binding -> Code -> [EO]+expressionsOf :: Binding -> Code -> [Expression] expressionsOf binding code = do rhs <- rhssOf binding code top <- topExpressions rhs@@ -53,21 +53,21 @@ parseBindings :: Code -> [Binding] parseBindings = map declName . parseDecls -expressionToBinding :: EO -> Maybe Binding+expressionToBinding :: Expression -> Maybe Binding expressionToBinding (O (HsQVarOp q)) = qName q expressionToBinding (E (HsVar q)) = qName q expressionToBinding _ = Nothing -- private -topExpressions :: HsRhs -> [EO]+topExpressions :: HsRhs -> [Expression] topExpressions (HsUnGuardedRhs e) = [E e] topExpressions (HsGuardedRhss rhss) = rhss >>= \(HsGuardedRhs _ es1 es2) -> [E es1, E es2] -unfoldExpression :: EO -> [EO]+unfoldExpression :: Expression -> [Expression] unfoldExpression expr = expr : concatMap unfoldExpression (subExpressions expr) -subExpressions :: EO -> [EO]+subExpressions :: Expression -> [Expression] subExpressions (E (HsInfixApp a b c)) = [E a, O b, E c] subExpressions (E (HsApp a b)) = [E a, E b] subExpressions (E (HsNegApp a)) = [E a]
src/Language/Haskell/Inspector.hs view
@@ -91,7 +91,7 @@ where f (HsTypeSig _ [hsName] _) = isName binding hsName f _ = False -hasExpression :: (EO -> Bool) -> Inspection+hasExpression :: (Expression -> Bool) -> Inspection hasExpression f binding = has f (expressionsOf binding) hasRhs :: (HsRhs -> Bool)-> Inspection
src/Language/Haskell/Inspector/Smell.hs view
@@ -2,12 +2,15 @@ hasRedundantBooleanComparison, hasRedundantIf, hasRedundantGuards,- hasRedundantLambda) where+ hasRedundantLambda,+ hasRedundantParameter) where import Language.Haskell.Explorer import Language.Haskell.Syntax import Language.Haskell.Inspector ++-- | Inspection that tells whether a binding has expressions like 'x == True' hasRedundantBooleanComparison :: Inspection hasRedundantBooleanComparison = hasExpression f where f (E (HsInfixApp x (HsQVarOp (UnQual (HsSymbol c))) y)) = any isBooleanLiteral [x, y] && isComp c@@ -15,12 +18,16 @@ isComp c = c == "==" || c == "/=" +-- | Inspection that tells whether a binding has an if expression where both branches return+-- boolean literals hasRedundantIf :: Inspection hasRedundantIf = hasExpression f where f (E (HsIf _ x y)) = all isBooleanLiteral [x, y] f _ = False +-- | Inspection that tells whether a binding has guards where both branches return+-- boolean literals hasRedundantGuards :: Inspection hasRedundantGuards = hasRhs f -- TODO not true when condition is a pattern where f (HsGuardedRhss [@@ -28,11 +35,21 @@ HsGuardedRhs _ (HsVar (UnQual (HsIdent "otherwise"))) y]) = all isBooleanLiteral [x, y] f _ = False ++-- | Inspection that tells whether a binding has lambda expressions like '\x -> g x' hasRedundantLambda :: Inspection hasRedundantLambda = hasExpression f where f (E (HsLambda _ [HsPVar (HsIdent x)] (HsApp _ (HsVar (UnQual (HsIdent y)))))) = x == y f _ = False -- TODO consider parenthesis and symbols +-- | Inspection that tells whether a binding has parameters that+-- can be avoided using point-free+hasRedundantParameter :: Inspection+hasRedundantParameter binding = any f . declsOf binding+ where f (HsFunBind [+ HsMatch _ _ params (HsUnGuardedRhs (HsApp _ (HsVar (UnQual arg)))) _ ]) | (HsPVar param) <- last params = param == arg+ f _ = False+--private isBooleanLiteral (HsCon (UnQual (HsIdent "True"))) = True isBooleanLiteral (HsCon (UnQual (HsIdent "False"))) = True isBooleanLiteral _ = False