-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure entity
declaration is not used in the Wasp file
#2152
Changes from 3 commits
ddb265d
0abfd21
0d18813
96523f9
f1869e7
1886f6a
afd108f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Wasp.Analyzer.Parser.Valid | ||
( validateAst, | ||
) | ||
where | ||
|
||
import Data.List (find) | ||
import qualified Wasp.Analyzer.Parser as P | ||
|
||
validateAst :: P.AST -> Either (String, P.Ctx) P.AST | ||
validateAst = validateNoEntityDeclInWaspFile | ||
|
||
validateNoEntityDeclInWaspFile :: P.AST -> Either (String, P.Ctx) P.AST | ||
validateNoEntityDeclInWaspFile ast@(P.AST stmts) = case findEntityDecl stmts of | ||
sodic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Just (P.WithCtx ctx _) -> Left (entitiesNoLongerSupportedError, ctx) | ||
Nothing -> Right ast | ||
where | ||
findEntityDecl :: [P.WithCtx P.Stmt] -> Maybe (P.WithCtx P.Stmt) | ||
findEntityDecl = find isEntityDecl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can pattern-match the maybe here and return a boolean, no need to propagate it. You can then call the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue here is that I need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've adjusted the code a bit 🤷 |
||
|
||
isEntityDecl :: P.WithCtx P.Stmt -> Bool | ||
isEntityDecl = \case | ||
P.WithCtx _ (P.Decl "entity" _ _) -> True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come we need to hardcode "entity" here? I haven't looked at this code for a long time so it might be necessary, but it seems weird. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well spotted! We should make sure that the entity declaration type name (
I'll make it happen 👍 |
||
_ -> False | ||
|
||
entitiesNoLongerSupportedError :: String | ||
entitiesNoLongerSupportedError = | ||
"Entities can no longer be defined in the .wasp file. You should migrate your entities to the schema.prisma file. Read more: https://wasp-lang.dev/docs/migrate-from-0-13-to-0-14#migrate-to-the-new-schemaprisma-file" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,15 +27,15 @@ data TypeError' | |
-- We use "unify" in the TypeChecker when trying to infer the common type for | ||
-- typed expressions that we know should be of the same type (e.g. for | ||
-- elements in the list). | ||
= UnificationError TypeCoercionError | ||
= UnificationError TypeCoercionError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting |
||
-- | Type coercion error that occurs when trying to use the typed expression | ||
-- of type T1 where T2 is expected. If T2 is a super type of T1 and T1 can be | ||
-- safely coerced to T2, no problem, but if not, we get this error. | ||
| CoercionError TypeCoercionError | ||
| NoDeclarationType TypeName | ||
| UndefinedIdentifier Identifier | ||
| QuoterUnknownTag QuoterTag | ||
| DictDuplicateField DictFieldName | ||
| CoercionError TypeCoercionError | ||
| NoDeclarationType TypeName | ||
| UndefinedIdentifier Identifier | ||
| QuoterUnknownTag QuoterTag | ||
| DictDuplicateField DictFieldName | ||
deriving (Eq, Show) | ||
{- ORMOLU_ENABLE -} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Analyzer.ValidTest where | ||
|
||
import Data.Either (fromRight, isRight) | ||
import Test.Tasty.Hspec | ||
import Wasp.Analyzer.Parser hiding (withCtx) | ||
import qualified Wasp.Analyzer.Parser as P | ||
import Wasp.Analyzer.Parser.Valid (validateAst) | ||
import qualified Wasp.Version as WV | ||
|
||
spec_ValidateAst :: Spec | ||
spec_ValidateAst = do | ||
it "Returns an error when entities are used" $ do | ||
validateAndParseSource (waspSourceLines ++ entityDeclarationLines) | ||
`shouldBe` Left | ||
( "Entities can no longer be defined in the .wasp file. You should migrate your entities to the schema.prisma file. Read more: https://wasp-lang.dev/docs/migrate-from-0-13-to-0-14#migrate-to-the-new-schemaprisma-file", | ||
P.Ctx | ||
( P.SourceRegion | ||
(P.SourcePosition 34 1) | ||
(P.SourcePosition 37 5) | ||
) | ||
) | ||
|
||
it "Returns AST when everything is correct" $ do | ||
isRight (validateAndParseSource waspSourceLines) `shouldBe` True | ||
where | ||
validateAndParseSource = validateAst . parseSource | ||
|
||
parseSource = fromRight (error "Parsing went wrong") . parseStatements . unlines | ||
|
||
waspSourceLines = | ||
[ "app Todo {", | ||
" wasp: {", | ||
" version: \"^" ++ show WV.waspVersion ++ "\",", | ||
" },", | ||
" title: \"Todo App\",", | ||
" head: [\"foo\", \"bar\"],", | ||
" auth: {", | ||
" userEntity: User,", | ||
" methods: {", | ||
" usernameAndPassword: {", | ||
" userSignupFields: import { getUserFields } from \"@src/auth/signup.js\",", | ||
" }", | ||
" },", | ||
" onAuthFailedRedirectTo: \"/\",", | ||
" },", | ||
"}", | ||
"", | ||
"page HomePage {", | ||
" component: import Home from \"@src/pages/Main\"", | ||
"}", | ||
"", | ||
"route HomeRoute { path: \"/\", to: HomePage }", | ||
"", | ||
"query getUsers {", | ||
" fn: import { getAllUsers } from \"@src/foo.js\",", | ||
" entities: [User]", | ||
"}", | ||
"", | ||
"action updateUser {", | ||
" fn: import { updateUser } from \"@src/foo.js\",", | ||
" entities: [User],", | ||
" auth: true", | ||
"}" | ||
] | ||
|
||
entityDeclarationLines = | ||
[ "entity User {=psl", | ||
" id Int @id @default(autoincrement())", | ||
" email String @unique", | ||
"psl=}" | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imagine opening up this PR after reviewing and writing TypeScript all day. And the first thing you see is this line here.
Oh well, time to gaslight myself into thinking Haskell is readable again 😄