Skip to content

Commit 44624ec

Browse files
committed
Introduce smell for for..in
1 parent 3aecf22 commit 44624ec

File tree

3 files changed

+65
-45
lines changed

3 files changed

+65
-45
lines changed

spec/SmellSpec.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,5 +503,17 @@ spec = do
503503
describe "usesVarInsteadOfLet" $ do
504504
it "is True when there is a var" $ do
505505
usesVarInsteadOfLet (js "var x = 1; x++") `shouldBe` True
506+
507+
it "is False when there is no var" $ do
506508
usesVarInsteadOfLet (js "let x = 1; x++") `shouldBe` False
507509
usesVarInsteadOfLet (js "const x = 1; x++") `shouldBe` False
510+
511+
describe "usesForInInsteadOfForOf" $ do
512+
it "is True when there is a for..in" $ do
513+
usesForInInsteadOfForOf (js "for (let x in []) {}") `shouldBe` True
514+
usesForInInsteadOfForOf (js "for (var x in []) {}") `shouldBe` True
515+
516+
it "is False when there is no for..in" $ do
517+
usesForInInsteadOfForOf (js "for (var x of []) {}") `shouldBe` False
518+
usesForInInsteadOfForOf (js "for (let x of []) {}") `shouldBe` False
519+
usesForInInsteadOfForOf (js "for (const x of []) {}") `shouldBe` False

src/Language/Mulang/Analyzer/SmellsAnalyzer.hs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ allSmells = [
8787
"UsesFail",
8888
"UsesNamedSelfReference",
8989
"UsesUnificationOperator",
90-
"JavaScript#UsesVarInsteadOfLet" ]
90+
"JavaScript#UsesVarInsteadOfLet",
91+
"JavaScript#UsesForInInsteadOfForOf" ]
9192

9293
---
9394
--- Instantiation
@@ -131,49 +132,50 @@ evalSmellInstance :: SmellsContext -> Expression -> SmellInstance -> [Expectatio
131132
evalSmellInstance context expression smellInstance = map (expectationFor smellInstance) . detectionFor smellInstance context $ expression
132133

133134
detectionFor :: SmellInstance -> Detection
134-
detectionFor ("DiscardsExceptions", Nothing) = simple discardsExceptions
135-
detectionFor ("DoesConsolePrint", Nothing) = simple doesConsolePrint
136-
detectionFor ("DoesNilTest", Nothing) = simple doesNilTest
137-
detectionFor ("DoesNullTest", Nothing) = simple doesNilTest
138-
detectionFor ("DoesTypeTest", Nothing) = simple doesTypeTest
139-
detectionFor ("HasAssignmentCondition", Nothing) = simple hasAssignmentCondition
140-
detectionFor ("HasAssignmentReturn", Nothing) = simple hasAssignmentReturn
141-
detectionFor ("HasCodeDuplication", Nothing) = unsupported
142-
detectionFor ("HasDeclarationTypos", Just target) = raw (detectDeclarationTypos target)
143-
detectionFor ("HasEmptyIfBranches", Nothing) = simple hasEmptyIfBranches
144-
detectionFor ("HasEmptyRepeat", Nothing) = simple hasEmptyRepeat
145-
detectionFor ("HasEqualIfBranches", Nothing) = simple hasEqualIfBranches
146-
detectionFor ("HasLongParameterList", Nothing) = simple hasLongParameterList
147-
detectionFor ("HasMisspelledBindings", Nothing) = withLanguage hasMisspelledIdentifiers
148-
detectionFor ("HasMisspelledIdentifiers", Nothing) = withLanguage hasMisspelledIdentifiers
149-
detectionFor ("HasRedundantBooleanComparison", Nothing) = simple hasRedundantBooleanComparison
150-
detectionFor ("HasRedundantGuards", Nothing) = simple hasRedundantGuards
151-
detectionFor ("HasRedundantIf", Nothing) = simple hasRedundantIf
152-
detectionFor ("HasRedundantLambda", Nothing) = simple hasRedundantLambda
153-
detectionFor ("HasRedundantLocalVariableReturn", Nothing) = simple hasRedundantLocalVariableReturn
154-
detectionFor ("HasRedundantParameter", Nothing) = simple hasRedundantParameter
155-
detectionFor ("HasRedundantReduction", Nothing) = simple hasRedundantReduction
156-
detectionFor ("HasRedundantRepeat", Nothing) = simple hasRedundantRepeat
157-
detectionFor ("HasTooManyMethods", Nothing) = simple hasTooManyMethods
158-
detectionFor ("HasTooShortBindings", Nothing) = withLanguage hasTooShortIdentifiers
159-
detectionFor ("HasTooShortIdentifiers", Nothing) = withLanguage hasTooShortIdentifiers
160-
detectionFor ("HasUnreachableCode", Nothing) = simple hasUnreachableCode
161-
detectionFor ("HasUsageTypos", Just target) = raw (detectUsageTypos target)
162-
detectionFor ("HasWrongCaseBinding", Nothing) = withLanguage hasWrongCaseIdentifiers
163-
detectionFor ("HasWrongCaseIdentifiers", Nothing) = withLanguage hasWrongCaseIdentifiers
164-
detectionFor ("IsLongCode", Nothing) = unsupported
165-
detectionFor ("OverridesEqualOrHashButNotBoth", Nothing) = simple overridesEqualOrHashButNotBoth
166-
detectionFor ("ReturnsNil", Nothing) = simple returnsNil
167-
detectionFor ("ReturnsNull", Nothing) = simple returnsNil
168-
detectionFor ("ShouldInvertIfCondition", Nothing) = simple shouldInvertIfCondition
169-
detectionFor ("ShouldUseOtherwise", Nothing) = simple shouldUseOtherwise
170-
detectionFor ("ShouldUseStrictComparators", Nothing) = simple shouldUseStrictComparators
171-
detectionFor ("UsesCut", Nothing) = simple usesCut
172-
detectionFor ("UsesFail", Nothing) = simple usesFail
173-
detectionFor ("UsesNamedSelfReference", Nothing) = simple usesNamedSelfReference
174-
detectionFor ("UsesUnificationOperator", Nothing) = simple usesUnificationOperator
175-
detectionFor ("JavaScript#UsesVarInsteadOfLet", Nothing) = simple usesVarInsteadOfLet
176-
detectionFor _ = unsupported
135+
detectionFor ("DiscardsExceptions", Nothing) = simple discardsExceptions
136+
detectionFor ("DoesConsolePrint", Nothing) = simple doesConsolePrint
137+
detectionFor ("DoesNilTest", Nothing) = simple doesNilTest
138+
detectionFor ("DoesNullTest", Nothing) = simple doesNilTest
139+
detectionFor ("DoesTypeTest", Nothing) = simple doesTypeTest
140+
detectionFor ("HasAssignmentCondition", Nothing) = simple hasAssignmentCondition
141+
detectionFor ("HasAssignmentReturn", Nothing) = simple hasAssignmentReturn
142+
detectionFor ("HasCodeDuplication", Nothing) = unsupported
143+
detectionFor ("HasDeclarationTypos", Just target) = raw (detectDeclarationTypos target)
144+
detectionFor ("HasEmptyIfBranches", Nothing) = simple hasEmptyIfBranches
145+
detectionFor ("HasEmptyRepeat", Nothing) = simple hasEmptyRepeat
146+
detectionFor ("HasEqualIfBranches", Nothing) = simple hasEqualIfBranches
147+
detectionFor ("HasLongParameterList", Nothing) = simple hasLongParameterList
148+
detectionFor ("HasMisspelledBindings", Nothing) = withLanguage hasMisspelledIdentifiers
149+
detectionFor ("HasMisspelledIdentifiers", Nothing) = withLanguage hasMisspelledIdentifiers
150+
detectionFor ("HasRedundantBooleanComparison", Nothing) = simple hasRedundantBooleanComparison
151+
detectionFor ("HasRedundantGuards", Nothing) = simple hasRedundantGuards
152+
detectionFor ("HasRedundantIf", Nothing) = simple hasRedundantIf
153+
detectionFor ("HasRedundantLambda", Nothing) = simple hasRedundantLambda
154+
detectionFor ("HasRedundantLocalVariableReturn", Nothing) = simple hasRedundantLocalVariableReturn
155+
detectionFor ("HasRedundantParameter", Nothing) = simple hasRedundantParameter
156+
detectionFor ("HasRedundantReduction", Nothing) = simple hasRedundantReduction
157+
detectionFor ("HasRedundantRepeat", Nothing) = simple hasRedundantRepeat
158+
detectionFor ("HasTooManyMethods", Nothing) = simple hasTooManyMethods
159+
detectionFor ("HasTooShortBindings", Nothing) = withLanguage hasTooShortIdentifiers
160+
detectionFor ("HasTooShortIdentifiers", Nothing) = withLanguage hasTooShortIdentifiers
161+
detectionFor ("HasUnreachableCode", Nothing) = simple hasUnreachableCode
162+
detectionFor ("HasUsageTypos", Just target) = raw (detectUsageTypos target)
163+
detectionFor ("HasWrongCaseBinding", Nothing) = withLanguage hasWrongCaseIdentifiers
164+
detectionFor ("HasWrongCaseIdentifiers", Nothing) = withLanguage hasWrongCaseIdentifiers
165+
detectionFor ("IsLongCode", Nothing) = unsupported
166+
detectionFor ("OverridesEqualOrHashButNotBoth", Nothing) = simple overridesEqualOrHashButNotBoth
167+
detectionFor ("ReturnsNil", Nothing) = simple returnsNil
168+
detectionFor ("ReturnsNull", Nothing) = simple returnsNil
169+
detectionFor ("ShouldInvertIfCondition", Nothing) = simple shouldInvertIfCondition
170+
detectionFor ("ShouldUseOtherwise", Nothing) = simple shouldUseOtherwise
171+
detectionFor ("ShouldUseStrictComparators", Nothing) = simple shouldUseStrictComparators
172+
detectionFor ("UsesCut", Nothing) = simple usesCut
173+
detectionFor ("UsesFail", Nothing) = simple usesFail
174+
detectionFor ("UsesNamedSelfReference", Nothing) = simple usesNamedSelfReference
175+
detectionFor ("UsesUnificationOperator", Nothing) = simple usesUnificationOperator
176+
detectionFor ("JavaScript#UsesVarInsteadOfLet", Nothing) = simple usesVarInsteadOfLet
177+
detectionFor ("JavaScript#UsesForInInsteadOfForOf", Nothing) = simple usesForInInsteadOfForOf
178+
detectionFor _ = unsupported
177179

178180
unsupported :: Detection
179181
unsupported _ _ = []

src/Language/Mulang/Inspector/Smell/JavaScript.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Language.Mulang.Inspector.Smell.JavaScript (
2-
usesVarInsteadOfLet) where
2+
usesVarInsteadOfLet,
3+
usesForInInsteadOfForOf) where
34

45
import Language.Mulang.Ast
56
import Language.Mulang.Inspector.Primitive (Inspection, containsExpression)
@@ -10,3 +11,8 @@ usesVarInsteadOfLet = containsExpression f
1011
where f (Other (Just "JSVar") _) = True
1112
f (For [Generator (OtherPattern (Just "JSVar") _) _] _) = True
1213
f _ = False
14+
15+
usesForInInsteadOfForOf :: Inspection
16+
usesForInInsteadOfForOf = containsExpression f
17+
where f (Other (Just "JSForIn") _) = True
18+
f _ = False

0 commit comments

Comments
 (0)