1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 12:23:08 +03:00

Use a single NotFoundError instead of language specific ResolutionErrors

This commit is contained in:
Timothy Clem 2018-04-23 13:18:52 -07:00
parent bfaac4d78f
commit d5b67c0613
4 changed files with 13 additions and 14 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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