Skip to content
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

Merged
merged 7 commits into from
Jul 9, 2024

Conversation

infomiho
Copy link
Contributor

@infomiho infomiho commented Jul 4, 2024

Closes #2148

Introduces AST validation:

  • it runs after the parser
  • there is only one check: if entity declaration is used, throw an error

How it looks like

Screenshot 2024-07-04 at 11 55 53
In the CLI

LS in VS Code:

Screen.Recording.2024-07-04.at.11.55.23.mov

@infomiho infomiho changed the title Adds AST validation Make sure entity declaration is not used in the Wasp file Jul 4, 2024
@infomiho infomiho added the prisma label Jul 4, 2024
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting

@infomiho infomiho requested review from sodic and Martinsos July 4, 2024 12:06
Copy link
Contributor

@sodic sodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All seems well, but let me just make sure I understand what's going on.

Can you explain why entities can't be a ParseError (as one would expect) and rather require their own phase in the compilation pipeline:

  • Is it to provide a better error message?
  • Is it because we hooked into a part of the pipeline in a way that prevents us from changing the parser?

I've read this in the issue description:

We didn't remove the entity declaration to make it easier to switch to using the models from the Prisma file without touching the compiler too much.

But would like some more context if it's easy to explain.

@@ -138,6 +139,7 @@ import qualified Wasp.Psl.Ast.Schema as Psl.Schema
analyze :: Psl.Schema.Schema -> String -> Either [AnalyzeError] [Decl]
analyze prismaSchemaAst =
(left (map ParseError) . parseStatements)
>=> (left ((: []) . ValidationError) . validateAst)
Copy link
Contributor

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 😄

waspc/src/Wasp/Analyzer/Parser/Valid.hs Outdated Show resolved Hide resolved

isEntityDecl :: P.WithCtx P.Stmt -> Bool
isEntityDecl = \case
P.WithCtx _ (P.Decl "entity" _ _) -> True
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted! We should make sure that the entity declaration type name ("entity") is the same in the:

  • definition of the entity declaration type
  • here.

I'll make it happen 👍

Nothing -> Right ast
where
findEntityDecl :: [P.WithCtx P.Stmt] -> Maybe (P.WithCtx P.Stmt)
findEntityDecl = find isEntityDecl
Copy link
Contributor

@sodic sodic Jul 4, 2024

Choose a reason for hiding this comment

The 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 includesEntities :: [P.WithCtx P.Stmt] -> Bool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here is that I need the ctx that contains the SourceRange to produce the error, so I do need the result, not just the Bool answer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted the code a bit 🤷

@infomiho
Copy link
Contributor Author

infomiho commented Jul 8, 2024

@infomiho infomiho requested a review from sodic July 8, 2024 12:13
Signed-off-by: Mihovil Ilakovac <[email protected]>
Copy link
Contributor

@sodic sodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice explanation!

@infomiho infomiho merged commit 8f136fb into main Jul 9, 2024
6 checks passed
@infomiho infomiho deleted the miho-no-entities-in-wasp-file branch July 9, 2024 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disallow using entities in the Wasp file
2 participants