From d5b67c06135c6b8d3b4568367723a5bc1e940a03 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Mon, 23 Apr 2018 13:18:52 -0700 Subject: [PATCH] Use a single NotFoundError instead of language specific ResolutionErrors --- src/Analysis/Abstract/BadModuleResolutions.hs | 3 +-- src/Data/Abstract/Evaluatable.hs | 14 ++++++-------- src/Language/Ruby/Syntax.hs | 5 +++-- src/Language/TypeScript/Syntax.hs | 5 +++-- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Analysis/Abstract/BadModuleResolutions.hs b/src/Analysis/Abstract/BadModuleResolutions.hs index 6e888ffec..0286d9eb9 100644 --- a/src/Analysis/Abstract/BadModuleResolutions.hs +++ b/src/Analysis/Abstract/BadModuleResolutions.hs @@ -29,7 +29,6 @@ instance ( Effectful m \yield error -> do traceM ("ResolutionError:" <> show error) case error of - (RubyError nameToResolve) -> yield nameToResolve - (TypeScriptError nameToResolve) -> yield nameToResolve) + (NotFoundError nameToResolve _) -> yield nameToResolve) analyzeModule = liftAnalyze analyzeModule diff --git a/src/Data/Abstract/Evaluatable.hs b/src/Data/Abstract/Evaluatable.hs index bc2dc3679..efec9dd74 100644 --- a/src/Data/Abstract/Evaluatable.hs +++ b/src/Data/Abstract/Evaluatable.hs @@ -35,6 +35,7 @@ import Data.Abstract.Module import Data.Abstract.ModuleTable as ModuleTable import Data.Abstract.Origin (SomeOrigin, packageOrigin) import Data.Abstract.Package as Package +import Data.Language import Data.Scientific (Scientific) import Data.Semigroup.App import Data.Semigroup.Foldable @@ -69,17 +70,14 @@ deriving instance Eq value => Eq (ControlThrow value) -- | An error thrown when we can't resolve a module from a qualified name. data ResolutionError value resume where - RubyError :: String -> ResolutionError value ModulePath - TypeScriptError :: String -> ResolutionError value ModulePath + NotFoundError :: String -- ^ The path that was not found + -> Language -- ^ Language + -> ResolutionError value ModulePath deriving instance Eq (ResolutionError a b) deriving instance Show (ResolutionError a b) -instance Show1 (ResolutionError value) where - liftShowsPrec _ _ = showsPrec -instance Eq1 (ResolutionError value) where - liftEq _ (RubyError a) (RubyError b) = a == b - liftEq _ (TypeScriptError a) (TypeScriptError b) = a == b - liftEq _ _ _ = False +instance Show1 (ResolutionError value) where liftShowsPrec _ _ = showsPrec +instance Eq1 (ResolutionError value) where liftEq _ (NotFoundError a l1) (NotFoundError b l2) = a == b && l1 == l2 -- | An error thrown when loading a module from the list of provided modules. Indicates we weren't able to find a module with the given name. data LoadError term value resume where diff --git a/src/Language/Ruby/Syntax.hs b/src/Language/Ruby/Syntax.hs index cea8d57a9..be3c03bd8 100644 --- a/src/Language/Ruby/Syntax.hs +++ b/src/Language/Ruby/Syntax.hs @@ -7,6 +7,7 @@ import Data.Abstract.Module (ModulePath) import Data.Abstract.ModuleTable as ModuleTable import Data.Abstract.Path import qualified Data.ByteString.Char8 as BC +import qualified Data.Language as Language import Diffing.Algorithm import Prelude hiding (fail) import Prologue @@ -20,14 +21,14 @@ resolveRubyName :: forall value term location m. MonadEvaluatable location term resolveRubyName name = do let name' = cleanNameOrPath name modulePath <- resolve [name' <.> "rb"] - maybe (throwResumable @(ResolutionError value) $ RubyError name') pure modulePath + maybe (throwResumable @(ResolutionError value) $ NotFoundError name' Language.Ruby) pure modulePath -- load "/root/src/file.rb" resolveRubyPath :: forall value term location m. MonadEvaluatable location term value m => ByteString -> m ModulePath resolveRubyPath path = do let name' = cleanNameOrPath path modulePath <- resolve [name'] - maybe (throwResumable @(ResolutionError value) $ RubyError name') pure modulePath + maybe (throwResumable @(ResolutionError value) $ NotFoundError name' Language.Ruby) pure modulePath cleanNameOrPath :: ByteString -> String cleanNameOrPath = BC.unpack . dropRelativePrefix . stripQuotes diff --git a/src/Language/TypeScript/Syntax.hs b/src/Language/TypeScript/Syntax.hs index 7596f8d98..ff6a6dd2e 100644 --- a/src/Language/TypeScript/Syntax.hs +++ b/src/Language/TypeScript/Syntax.hs @@ -8,6 +8,7 @@ import Data.Abstract.Path import qualified Data.ByteString.Char8 as BC import qualified Data.ByteString as B import Data.Abstract.Module (ModulePath, ModuleInfo(..)) +import qualified Data.Language as Language import Diffing.Algorithm import Prelude import Prologue @@ -49,7 +50,7 @@ resolveRelativePath relImportPath exts = do let path = joinPaths relRootDir relImportPath resolveTSModule path exts >>= either notFound (\x -> traceResolve relImportPath x (pure x)) where - notFound _ = throwResumable @(ResolutionError value) $ TypeScriptError relImportPath + notFound _ = throwResumable @(ResolutionError value) $ NotFoundError relImportPath Language.TypeScript -- | Resolve a non-relative TypeScript import to a known 'ModuleName' or fail. -- @@ -74,7 +75,7 @@ resolveNonRelativePath name exts = do Left xs | parentDir <- takeDirectory path , root /= parentDir -> go root parentDir (searched <> xs) | otherwise -> notFound (searched <> xs) Right m -> traceResolve name m $ pure m - notFound _ = throwResumable @(ResolutionError value) $ TypeScriptError name + notFound _ = throwResumable @(ResolutionError value) $ NotFoundError name Language.TypeScript resolveTSModule :: MonadEvaluatable location term value m => FilePath -> [String] -> m (Either [FilePath] ModulePath) resolveTSModule path exts = maybe (Left searchPaths) Right <$> resolve searchPaths