Skip to content

Commit

Permalink
compiler: Fix compiler errors of keys in globs
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Oct 31, 2023
1 parent d09024e commit 5c6a94a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
- Fixes use of `null` in `sql_table` constraints (ty @landmaj) [#1660](https://github.com/terrastruct/d2/pull/1660)
- Fixes elk growing shapes with width/height set [#1679](https://github.com/terrastruct/d2/pull/1679)
- Adds a compiler error when accidentally using an arrowhead on a shape [#1686](https://github.com/terrastruct/d2/pull/1686)
- Correctly reports errors from invalid values set by globs. [#1691](https://github.com/terrastruct/d2/pull/1691)
9 changes: 8 additions & 1 deletion d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@ type compiler struct {
}

func (c *compiler) errorf(n d2ast.Node, f string, v ...interface{}) {
c.err.Errors = append(c.err.Errors, d2parser.Errorf(n, f, v...).(d2ast.Error))
err := d2parser.Errorf(n, f, v...).(d2ast.Error)
if c.err.ErrorsLookup == nil {
c.err.ErrorsLookup = make(map[d2ast.Error]struct{})
}
if _, ok := c.err.ErrorsLookup[err]; !ok {
c.err.Errors = append(c.err.Errors, err)
c.err.ErrorsLookup[err] = struct{}{}
}
}

func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) {
Expand Down
32 changes: 32 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4241,6 +4241,38 @@ class: {
`, "")
},
},
{
name: "double-glob-err-val",
run: func(t *testing.T) {
assertCompile(t, `
**: {
label: hi
label.near: center
}
x: {
a -> b
}
`, `d2/testdata/d2compiler/TestCompile2/globs/double-glob-err-val.d2:4:3: invalid "near" field`)
},
},
{
name: "double-glob-override-err-val",
run: func(t *testing.T) {
assertCompile(t, `
(** -> **)[*]: {
label.near: top-center
}
(** -> **)[*]: {
label.near: invalid
}
x: {
a -> b
}
`, `d2/testdata/d2compiler/TestCompile2/globs/double-glob-override-err-val.d2:6:2: invalid "near" field`)
},
},
}

for _, tc := range tca {
Expand Down
3 changes: 2 additions & 1 deletion d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,8 @@ func (c *compiler) _compileField(f *Field, refctx *RefContext) {
// if already set by a non glob key.
func (c *compiler) ignoreLazyGlob(n Node) bool {
if c.lazyGlobBeingApplied && n.Primary() != nil {
if n.LastPrimaryRef() != nil {
lastPrimaryRef := n.LastPrimaryRef()
if lastPrimaryRef != nil && !lastPrimaryRef.DueToLazyGlob() {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (f *Field) Copy(newParent Node) Node {

func (f *Field) LastPrimaryRef() Reference {
for i := len(f.References) - 1; i >= 0; i-- {
if f.References[i].Primary() && !f.References[i].DueToLazyGlob() {
if f.References[i].Primary() {
return f.References[i]
}
}
Expand Down
4 changes: 3 additions & 1 deletion d2parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ type parser struct {

// TODO: rename to Error and make existing Error a private type errorWithRange
type ParseError struct {
Errors []d2ast.Error `json:"errs"`
// Errors from globs need to be deduplicated
ErrorsLookup map[d2ast.Error]struct{} `json:"-"`
Errors []d2ast.Error `json:"errs"`
}

func Errorf(n d2ast.Node, f string, v ...interface{}) error {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5c6a94a

Please sign in to comment.