compiler: Fix compiler errors of keys in globs
This commit is contained in:
parent
d09024e22d
commit
5c6a94a6ca
8 changed files with 69 additions and 4 deletions
|
|
@ -14,3 +14,4 @@
|
||||||
- Fixes use of `null` in `sql_table` constraints (ty @landmaj) [#1660](https://github.com/terrastruct/d2/pull/1660)
|
- 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)
|
- 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)
|
- 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)
|
||||||
|
|
|
||||||
|
|
@ -177,7 +177,14 @@ type compiler struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *compiler) errorf(n d2ast.Node, f string, v ...interface{}) {
|
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) {
|
func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) {
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
for _, tc := range tca {
|
||||||
|
|
|
||||||
|
|
@ -804,7 +804,8 @@ func (c *compiler) _compileField(f *Field, refctx *RefContext) {
|
||||||
// if already set by a non glob key.
|
// if already set by a non glob key.
|
||||||
func (c *compiler) ignoreLazyGlob(n Node) bool {
|
func (c *compiler) ignoreLazyGlob(n Node) bool {
|
||||||
if c.lazyGlobBeingApplied && n.Primary() != nil {
|
if c.lazyGlobBeingApplied && n.Primary() != nil {
|
||||||
if n.LastPrimaryRef() != nil {
|
lastPrimaryRef := n.LastPrimaryRef()
|
||||||
|
if lastPrimaryRef != nil && !lastPrimaryRef.DueToLazyGlob() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -319,7 +319,7 @@ func (f *Field) Copy(newParent Node) Node {
|
||||||
|
|
||||||
func (f *Field) LastPrimaryRef() Reference {
|
func (f *Field) LastPrimaryRef() Reference {
|
||||||
for i := len(f.References) - 1; i >= 0; i-- {
|
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]
|
return f.References[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -161,6 +161,8 @@ type parser struct {
|
||||||
|
|
||||||
// TODO: rename to Error and make existing Error a private type errorWithRange
|
// TODO: rename to Error and make existing Error a private type errorWithRange
|
||||||
type ParseError struct {
|
type ParseError struct {
|
||||||
|
// Errors from globs need to be deduplicated
|
||||||
|
ErrorsLookup map[d2ast.Error]struct{} `json:"-"`
|
||||||
Errors []d2ast.Error `json:"errs"`
|
Errors []d2ast.Error `json:"errs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
11
testdata/d2compiler/TestCompile2/globs/double-glob-err-val.exp.json
generated
vendored
Normal file
11
testdata/d2compiler/TestCompile2/globs/double-glob-err-val.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"graph": null,
|
||||||
|
"err": {
|
||||||
|
"errs": [
|
||||||
|
{
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile2/globs/double-glob-err-val.d2,3:2:21-3:20:39",
|
||||||
|
"errmsg": "d2/testdata/d2compiler/TestCompile2/globs/double-glob-err-val.d2:4:3: invalid \"near\" field"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
11
testdata/d2compiler/TestCompile2/globs/double-glob-override-err-val.exp.json
generated
vendored
Normal file
11
testdata/d2compiler/TestCompile2/globs/double-glob-override-err-val.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"graph": null,
|
||||||
|
"err": {
|
||||||
|
"errs": [
|
||||||
|
{
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile2/globs/double-glob-override-err-val.d2,5:1:62-5:20:81",
|
||||||
|
"errmsg": "d2/testdata/d2compiler/TestCompile2/globs/double-glob-override-err-val.d2:6:2: invalid \"near\" field"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue