Skip to content

Commit

Permalink
fix: only type-check if there are no parser errors
Browse files Browse the repository at this point in the history
Type checks are not particularly useful on partially parsed input.
  • Loading branch information
hperl committed Oct 12, 2022
1 parent c4d84f6 commit b4bef07
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ loop:
}
}

p.typeCheck()
if len(p.errors) == 0 {
p.typeCheck()
}

return p.namespaces, p.errors
}
Expand Down
38 changes: 35 additions & 3 deletions internal/schema/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<Group, "members">)[]
owners: (User | SubjectSet<Group, "members">)[]
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 {
Expand Down Expand Up @@ -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)
})
}
})
Expand Down

0 comments on commit b4bef07

Please # to comment.