Skip to content

Commit

Permalink
Remove duplication when parsing ext imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Oct 7, 2024
1 parent 7eab0bc commit 8c8cb4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,34 +155,18 @@ tuple4 eval1 eval2 eval3 eval4 = evaluation $ \(typeDefs, bindings) -> withCtx $
-- | An evaluation that expects an "ExtImport".
extImport :: TypedExprEvaluation AppSpec.ExtImport.ExtImport
extImport = evaluation' . withCtx $ \ctx -> \case
TypedAST.ExtImport name extImportPath ->
TypedAST.ExtImport name extImportPath ->
-- NOTE(martin): This parsing here could instead be done in Parser.
-- I don't have a very good reason for doing it here instead of Parser, except
-- for being somewhat simpler to implement.
-- So we might want to move it to Parser at some point in the future, if we
-- figure out that is better (it sounds/feels like it could be).
case stripImportPrefix extImportPath of
Just relFileFP -> case SP.parseRelFileP relFileFP of
Left err -> mkParseError ctx $ show err
Right relFileSP -> pure $ AppSpec.ExtImport.ExtImport name relFileSP
Nothing ->
mkParseError
ctx
$ "Path in external import must start with \"" ++ extSrcPrefix ++ "\"!"
case AppSpec.ExtImport.parseExtImportPath extImportPath of
Left err -> mkParseError ctx err
Right importPath -> pure $ AppSpec.ExtImport.ExtImport name importPath
expr -> Left $ ER.mkEvaluationError ctx $ ER.ExpectedType T.ExtImportType (TypedAST.exprType expr)
where
mkParseError ctx msg = Left $ ER.mkEvaluationError ctx $ ER.ParseError $ ER.EvaluationParseError msg
stripImportPrefix importPath = stripPrefix extSrcPrefix importPath
-- Filip: We no longer want separation between client and server code
-- todo (filip): Do we still want to know whic is which. We might (because of the reloading).
-- For now, as we'd like (expect):
-- - Nodemon watches all files in the user's source folder (client files
-- included), but tsc only compiles the server files (I think because it
-- knows that the others aren't used). I am not yet sure how it knows this.
-- - Vite also only triggers on client files. I am not sure how it knows
-- about the difference either.
-- todo (filip): investigate
extSrcPrefix = "@src/"

-- | An evaluation that expects a "JSON".
json :: TypedExprEvaluation AppSpec.JSON.JSON
Expand Down
12 changes: 7 additions & 5 deletions waspc/src/Wasp/AppSpec/ExtImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ module Wasp.AppSpec.ExtImport
( ExtImport (..),
ExtImportName (..),
importIdentifier,
parseExtImportPath,
)
where

import Control.Arrow (left)
import Data.Aeson (FromJSON (parseJSON), withObject, (.:))
import Data.Aeson.Types (ToJSON)
import Data.Data (Data)
import Data.List (stripPrefix)
import GHC.Generics (Generic)
import StrongPath (File', Path, Posix, Rel, parseRelFileP)
import StrongPath (File', Path, Posix, Rel)
import qualified StrongPath as SP
import Wasp.AppSpec.ExternalFiles (SourceExternalCodeDir)

Expand Down Expand Up @@ -58,13 +60,13 @@ importIdentifier (ExtImport importName _) = case importName of
ExtImportModule n -> n
ExtImportField n -> n

-- TODO: Remove duplication
parseExtImportPath :: String -> Either String ExtImportPath
parseExtImportPath extImportPath = case stripImportPrefix extImportPath of
Just relFileFP -> case SP.parseRelFileP relFileFP of
Left err -> Left $ "Failed to parse relative posix path to file: " ++ show err
Right path' -> Right path'
Nothing -> Left $ "Path in external import must start with \"" ++ extSrcPrefix ++ "\"!"
Just relFileFP ->
left
(("Failed to parse relative posix path to file: " ++) . show)
$ SP.parseRelFileP relFileFP
where
stripImportPrefix importPath = stripPrefix extSrcPrefix importPath
-- Filip: We no longer want separation between client and server code
Expand Down

0 comments on commit 8c8cb4f

Please sign in to comment.