diff --git a/README.md b/README.md index bd8ae890b..91fb732cb 100644 --- a/README.md +++ b/README.md @@ -454,6 +454,7 @@ The power of Mulang is grounded on more than 70 different kind of inspections: | `usesIf` | any | is an `if` control structure used? | `usesInheritance` | object oriented | is any superclass explicitly declared? | `usesLambda` | +| `usesLoop` | procedural | are any of: repeat / for loop / foreach / while used? | `usesMixins` | object oriented | is any mixins explicitly included? | `usesNot` | | `usesObjectComposition` | object oriented | is there a class that declares an attributes and sends a message to it? diff --git a/spec/ExpectationsCompilerSpec.hs b/spec/ExpectationsCompilerSpec.hs index 057053c91..df4463820 100644 --- a/spec/ExpectationsCompilerSpec.hs +++ b/spec/ExpectationsCompilerSpec.hs @@ -96,6 +96,9 @@ spec = do it "works with UsesRepeat" $ do run (hs "f x = 2") "*" "UsesRepeat" `shouldBe` False + it "works with UsesLoop" $ do + run (js "function a() { a() }") "*" "UsesLoop" `shouldBe` False + it "works with UsesPatternMatching" $ do run (hs "f x = 2") "*" "UsesPatternMatching" `shouldBe` False run (hs "f [] = 2\nf _ = 3") "*" "UsesPatternMatching" `shouldBe` True diff --git a/spec/InspectorSpec.hs b/spec/InspectorSpec.hs index e5cb85b8f..155b79ab7 100644 --- a/spec/InspectorSpec.hs +++ b/spec/InspectorSpec.hs @@ -73,6 +73,32 @@ spec = do usesRepeat code `shouldBe` False + describe "usesLoop" $ do + it "is True when repeat is present" $ do + let code = SimpleFunction "f" [] (Sequence [Repeat (MuNumber 2) None, Return (MuNumber 2)]) + + usesLoop code `shouldBe` True + + it "is True when foreach is present" $ do + let code = SimpleFunction "f" [] (Sequence [For [] None, Return (MuNumber 2)]) + + usesLoop code `shouldBe` True + + it "is True when for is present" $ do + let code = js "function f() { for(;;); }" + + usesLoop code `shouldBe` True + + it "is True when while is present" $ do + let code = js "function f() { while(true); }" + + usesLoop code `shouldBe` True + + it "is False when none of the aforementioned are present" $ do + let code = js "function f(x){return 1;}" + + usesLoop code `shouldBe` False + describe "declaresVariable" $ do it "is True when declare a variable" $ do let code = js "function f(){ var x = 2}" diff --git a/src/Language/Mulang/Analyzer/ExpectationsCompiler.hs b/src/Language/Mulang/Analyzer/ExpectationsCompiler.hs index 5ea3b8202..46a48f582 100644 --- a/src/Language/Mulang/Analyzer/ExpectationsCompiler.hs +++ b/src/Language/Mulang/Analyzer/ExpectationsCompiler.hs @@ -108,6 +108,7 @@ compileInspectionPrimitive = f f "UsesIf" = simple usesIf f "UsesInheritance" = simple usesInheritance f "UsesLambda" = simple usesLambda + f "UsesLoop" = simple usesLoop f "UsesMixins" = simple usesMixins f "UsesNot" = simple usesNot f "UsesObjectComposition" = simple usesObjectComposition diff --git a/src/Language/Mulang/Inspector/Procedural.hs b/src/Language/Mulang/Inspector/Procedural.hs index 098fecb10..d8ea9a806 100644 --- a/src/Language/Mulang/Inspector/Procedural.hs +++ b/src/Language/Mulang/Inspector/Procedural.hs @@ -4,6 +4,7 @@ module Language.Mulang.Inspector.Procedural ( usesSwitch, usesForEach, usesForLoop, + usesLoop, declaresProcedure) where import Language.Mulang.Ast @@ -45,3 +46,6 @@ usesForLoop :: Inspection usesForLoop = containsExpression f where f (ForLoop _ _ _ _) = True f _ = False + +usesLoop :: Inspection +usesLoop e = usesRepeat e || usesWhile e || usesForLoop e || usesForEach e