From f84f91327c0525dcf1d137402effec47b8f65eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Hangh=C3=B8j=20Iversen?= Date: Sun, 20 Oct 2019 22:14:50 +0200 Subject: [PATCH] Consider unqualified function calls. --- src/Rubyhs/References.hs | 18 ++++++++++++++++-- test/Main.hs | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Rubyhs/References.hs b/src/Rubyhs/References.hs index 2cf411d..63569c7 100644 --- a/src/Rubyhs/References.hs +++ b/src/Rubyhs/References.hs @@ -13,7 +13,6 @@ module Rubyhs.References ) where import Frelude --- import Data.Language.Ruby hiding (context) import qualified Data.Language.Ruby as Ruby import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HashMap @@ -160,14 +159,29 @@ instance MyMonad (State Env) where application n = do Context c <- getContext let + n' = case n of + -- TODO: Maybe check if there is `a` is a function in the + -- current closure. + (NodeDef (FQN [] a)) -> NodeDef $ FQN (nodeNs c) a + -- Look for the closest enclosing scope that has a reference + -- to `c`. For now we'll just guess that it is a fully + -- qualified reference. + NodeDef{} -> n + -- Ditto as above. + NodeModule{} -> n go env@Env{applications} - = env { applications = HashMap.insertWith mappend c (Set.singleton n) applications } + = env { applications = HashMap.insertWith mappend c (Set.singleton n') applications } modify go getContext = gets Rubyhs.References.context writeContext q = modify go where go env = env { Rubyhs.References.context = q } +nodeNs :: Node -> Namespace +nodeNs = \case + (NodeDef (FQN a _)) -> a + NodeModule n -> n + locally :: MyMonad m => m a -> m a locally act = do old <- getContext diff --git a/test/Main.hs b/test/Main.hs index a859029..5dba7ef 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -32,7 +32,7 @@ tests = go <$> ["mod", "simple"] let (Ruby.Result refs) = Ruby.references begin when (refs /= json) $ assertFailure - $ "Expected " <> convertString (Aeson.encode refs) <> " but got " <> convertString (Aeson.encode json) + $ "Expected " <> convertString (Aeson.encode json) <> " but got " <> convertString (Aeson.encode refs) decodeFile :: FromJSON a => FilePath -> IO a decodeFile p = Aeson.eitherDecodeFileStrict' p >>= \case