Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spec/SmellSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ spec = do
it "is False when not does type test" $ do
doesTypeTest (js "function x(m) { return 1 }") `shouldBe` False

describe "hasUnusedParameters" $ do
it "is False for methods of all kinds" $ do
hasUnusedParameters (js "var o = {x: function(m) { return 4; }}") `shouldBe` False
hasUnusedParameters (js "var o = {x: function(m) { return m; }}") `shouldBe` False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense!


it "is True when has unused parameter" $ do
hasUnusedParameters (js "function x(m) { return 4; } ") `shouldBe` True

it "is False when uses its single parameter" $ do
hasUnusedParameters (js "function x(m) { return m } ") `shouldBe` False

it "is False when uses all its parameters in different places" $ do
hasUnusedParameters (js "function x(m2, m2) { g(m1); return m2 } ") `shouldBe` False

it "is False when uses all its parameters in return" $ do
hasUnusedParameters (js "function x(m2, m2) { return m1 + m2 } ") `shouldBe` False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe parameters in these two tests should be x(m1, m2)


describe "hasRedundantLambda" $ do
it "is True whn etha-conversion applies, hs" $ do
hasRedundantLambda (hs "g = map (\\m -> f m)") `shouldBe` True
Expand Down
13 changes: 12 additions & 1 deletion src/Language/Mulang/Inspector/Generic/Smell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Language.Mulang.Inspector.Generic.Smell (
doesTypeTest,
hasAssignmentReturn,
hasEmptyIfBranches,
hasUnusedParameters,
hasEmptyRepeat,
hasLongParameterList,
hasRedundantBooleanComparison,
Expand All @@ -26,7 +27,7 @@ module Language.Mulang.Inspector.Generic.Smell (

import Language.Mulang.Ast
import qualified Language.Mulang.Ast.Operator as O
import Language.Mulang.Generator (identifierReferences)
import Language.Mulang.Generator (identifierReferences, expressions)
import Language.Mulang.Inspector.Primitive

-- | Inspection that tells whether an expression has expressions like 'x == True'
Expand Down Expand Up @@ -172,6 +173,16 @@ shouldInvertIfCondition = containsExpression f
where f (If _ None elseBranch) = elseBranch /= None
f _ = False

hasUnusedParameters :: Inspection
hasUnusedParameters = containsExpression f
where f (SimpleFunction _ params body) = not . all (`elem` (expressions body)) . variables $ params
f _ = False

variables ps = do
VariablePattern v <- ps
return (Reference v)


hasEmptyIfBranches :: Inspection
hasEmptyIfBranches = containsExpression f
where f (If _ None None) = True
Expand Down