From b4bef07ca91b7585a52d536ee8c0c38ec1d66b9d Mon Sep 17 00:00:00 2001 From: hperl <34397+hperl@users.noreply.github.com> Date: Mon, 10 Oct 2022 13:20:48 +0200 Subject: [PATCH] fix: only type-check if there are no parser errors Type checks are not particularly useful on partially parsed input. --- internal/schema/parser.go | 4 +++- internal/schema/parser_test.go | 38 +++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/internal/schema/parser.go b/internal/schema/parser.go index 59b383a50..70ee92768 100644 --- a/internal/schema/parser.go +++ b/internal/schema/parser.go @@ -62,7 +62,9 @@ loop: } } - p.typeCheck() + if len(p.errors) == 0 { + p.typeCheck() + } return p.namespaces, p.errors } diff --git a/internal/schema/parser_test.go b/internal/schema/parser_test.go index f08d74685..3b232929c 100644 --- a/internal/schema/parser_test.go +++ b/internal/schema/parser_test.go @@ -11,6 +11,40 @@ import ( var parserErrorTestCases = []struct{ name, input string }{ {"lexer error", "/* unclosed comment"}, + {"syntax and type errors", + ` + class File implements Namespace { + related: { + parents: (File | Folder)[] + viewers: (User | SubjectSet)[] + owners: (User | SubjectSet)[] + siblings: File[] + } + + SYNTAX ERROR + + // Some comment + permits = { + view: (ctx: Context): boolean => + ( + this.related.parents.traverse((p) => + p.related.viewers.includes(ctx.subject), + ) && + this.related.parents.traverse(p => p.permits.view(ctx)) ) || + (this.related.viewers.includes(ctx.subject) || + this.related.viewers.includes(ctx.subject) || + this.related.viewers.includes(ctx.subject) ) || + this.related.owners.includes(ctx.subject), + + edit: (ctx: Context) => this.related.owners.includes(ctx.subject), + + not: (ctx: Context) => !this.related.owners.includes(ctx.subject), + + rename: (ctx: Context) => + this.related.siblings.traverse(s => s.permits.edit(ctx)), + } + } +`}, } var parserTestCases = []struct { @@ -140,9 +174,7 @@ func TestParser(t *testing.T) { for _, tc := range parserErrorTestCases { t.Run(tc.name, func(t *testing.T) { _, errs := Parse(tc.input) - if len(errs) == 0 { - t.Error("expected error, but got none") - } + assert.Len(t, errs, 1) }) } })