Skip to content

Commit

Permalink
tweak AST traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
qoelet committed Sep 15, 2015
1 parent 70c5bae commit 6c9dfc0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Control.Monad
import Data.Data
import Data.Generics.Uniplate.Data
import Data.List
import Data.Maybe
import qualified GHC
import GHC hiding (Module, moduleName)
import GHC.Paths (libdir)
Expand Down Expand Up @@ -210,4 +211,8 @@ getClassMethodUsedNames ast =

-- | extracts all used names from ASTs
usedNames :: [Name] -> HsBindLR Name Name -> [Name]
usedNames ids = filter (`notElem` ids) . universeBi
usedNames _ids = catMaybes . map extractHsVar . (universeBi :: HsBindLR Name Name -> [HsExpr Name])
where
extractHsVar :: HsExpr Name -> Maybe Name
extractHsVar (HsVar n) = Just n
extractHsVar _ = Nothing
13 changes: 11 additions & 2 deletions test/AstSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ spec = do
|])
withModules [a, b] $ do
parseStringGraph ["A.hs", "B.hs"] `shouldReturn`
Graph [("A.foo", []), ("B.bar", [])] []
Graph [("A.foo", ["A.foo"]), ("B.bar", ["B.bar"])] []

it "does not create any files" $ do
withFooHeader [i|
Expand Down Expand Up @@ -158,11 +158,20 @@ spec = do
(Just foo) = let x = x in x
|] $ do
parseStringGraph ["Foo.hs"] `shouldReturn`
Graph [("Foo.foo", ["GHC.Base.Just"])] []
Graph [("Foo.foo", [])] []

it "can parse tuple pattern binding" $ do
withFooHeader [i|
(a, b) = let x = x in x
|] $ do
parseStringGraph ["Foo.hs"] `shouldReturn`
Graph [("Foo.a", []), ("Foo.b", [])] []

context "local variables" $ do
it "recognizes recursive definitions" $ do
withFooHeader [i|
foo = foo
bar = ()
|] $ do
parseStringGraph ["Foo.hs"] `shouldReturn`
Graph [("Foo.bar", ["GHC.Tuple.()"]), ("Foo.foo", ["Foo.foo"])] []
2 changes: 1 addition & 1 deletion test/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ withFoo code =

withFooHeader :: String -> IO () -> IO ()
withFooHeader code =
withFoo ("module Foo where\n" ++ code)
withFoo ("module Foo where\n" ++ unindent code)

withModules :: [(String, String)] -> IO () -> IO ()
withModules modules action = do
Expand Down

0 comments on commit 6c9dfc0

Please sign in to comment.