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 b551ad6
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 5 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
15 changes: 15 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4241,6 +4241,21 @@ 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`)
},
},
}

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
12 changes: 10 additions & 2 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,19 @@ func (f *Field) Copy(newParent Node) Node {
}

func (f *Field) LastPrimaryRef() Reference {
var lastGlobSet Reference
for i := len(f.References) - 1; i >= 0; i-- {
if f.References[i].Primary() && !f.References[i].DueToLazyGlob() {
return f.References[i]
if f.References[i].Primary() {
if f.References[i].DueToLazyGlob() {
lastGlobSet = f.References[i]
} else {
return f.References[i]
}
}
}
if lastGlobSet != nil {
return lastGlobSet
}
return nil
}

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
14 changes: 14 additions & 0 deletions d2parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,20 @@ c-
assert.Equal(t, "x -> y\n", d2format.Format(ast))
},
},
{
name: "double-glob",
text: `**: {
label: hi
label.near: center
}
x: {
a -> b
}`,
assert: func(t testing.TB, ast *d2ast.Map, err error) {
assert.Success(t, err)
},
},
}

t.Run("import", testImport)
Expand Down

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

194 changes: 194 additions & 0 deletions testdata/d2parser/TestParse/double-glob.exp.json

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

0 comments on commit b551ad6

Please sign in to comment.