diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 9c27c1459..c1800e571 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -282,19 +282,6 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { if len(f.Map().Edges) > 0 { c.errorf(f.Map().Edges[0].LastRef().AST(), "vars cannot contain an edge") } - // for _, varField := range f.Map().Fields { - // if varField.Map() != nil { - // c.errorf(varField.LastRef().AST(), "vars must be simple") - // } - // for _, cf := range classesField.Map().Fields { - // if _, ok := d2graph.ReservedKeywords[cf.Name]; !ok { - // c.errorf(cf.LastRef().AST(), "%s is an invalid class field, must be reserved keyword", cf.Name) - // } - // if cf.Name == "class" { - // c.errorf(cf.LastRef().AST(), `"class" cannot appear within "classes"`) - // } - // } - // } } return } else if isReserved { diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 22373fac5..1912a9258 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3470,7 +3470,31 @@ vars: { x: hey } hi: ${z} -`, "d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable z") +`, `d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable "z"`) + }, + }, + { + name: "edge", + run: func(t *testing.T) { + assertCompile(t, ` +vars: { + x -> a +} +hi +`, "d2/testdata/d2compiler/TestCompile2/vars/errors/edge.d2:3:3: vars cannot contain an edge") + }, + }, + { + name: "map", + run: func(t *testing.T) { + assertCompile(t, ` +vars: { + colors: { + button: red + } +} +hi: ${colors} +`, `d2/testdata/d2compiler/TestCompile2/vars/errors/map.d2:7:1: cannot reference map variable "colors"`) }, }, } diff --git a/d2ir/compile.go b/d2ir/compile.go index 8e69091de..4a7ef23c0 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -153,9 +153,12 @@ func (c *compiler) resolveSubstitution(vars *Map, mk *d2ast.Key, substitution *d resolved = r } if resolved == nil { - c.errorf(mk, "could not resolve variable %s", strings.Join(substitution.IDA(), ".")) + c.errorf(mk, `could not resolve variable "%s"`, strings.Join(substitution.IDA(), ".")) } else { - // TODO maps + if resolved.Composite != nil { + c.errorf(mk, `cannot reference map variable "%s"`, strings.Join(substitution.IDA(), ".")) + return nil + } return resolved } return nil @@ -211,9 +214,6 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) { } for _, n := range ast.Nodes { switch { - case n.Substitution != nil: - println("\033[1;31m--- DEBUG:", "=======================", "\033[m") - c.errorf(n.Substitution, "only values can use substitutions") case n.MapKey != nil: c.compileKey(&RefContext{ Key: n.MapKey, @@ -350,15 +350,6 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) c.overlayClasses(f.Map()) } } - // } else if refctx.Key.Value.Substitution != nil { - // vars := ParentBoard(f).Map().GetField("vars") - // resolved := c.resolveSubstitution(vars.Map(), refctx.Key, refctx.Key.Value.Substitution) - // if resolved != nil { - // f.Primary_ = &Scalar{ - // parent: f, - // Value: resolved, - // } - // } } else if refctx.Key.Value.ScalarBox().Unbox() != nil { // If the link is a board, we need to transform it into an absolute path. if f.Name == "link" { @@ -368,8 +359,6 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) parent: f, Value: refctx.Key.Value.ScalarBox().Unbox(), } - - // InterpolationBox within any of these values can be substitutions too } } @@ -538,15 +527,6 @@ func (c *compiler) compileEdges(refctx *RefContext) { } } c.compileMap(e.Map_, refctx.Key.Value.Map, refctx.ScopeAST) - // } else if refctx.Key.Value.Substitution != nil { - // vars := ParentBoard(e).Map().GetField("vars") - // resolved := c.resolveSubstitution(vars.Map(), refctx.Key, refctx.Key.Value.Substitution) - // if resolved != nil { - // e.Primary_ = &Scalar{ - // parent: e, - // Value: resolved, - // } - // } } else if refctx.Key.Value.ScalarBox().Unbox() != nil { e.Primary_ = &Scalar{ parent: e, diff --git a/testdata/d2compiler/TestCompile2/vars/errors/edge.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/edge.exp.json new file mode 100644 index 000000000..b83fda7d8 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/vars/errors/edge.exp.json @@ -0,0 +1,11 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/edge.d2,2:2:11-2:8:17", + "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/edge.d2:3:3: vars cannot contain an edge" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile2/vars/errors/map.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/map.exp.json new file mode 100644 index 000000000..0d8156660 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/vars/errors/map.exp.json @@ -0,0 +1,11 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/map.d2,6:0:43-6:13:56", + "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/map.d2:7:1: cannot reference map variable \"colors\"" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile2/vars/errors/missing.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/missing.exp.json index 64d495784..bf2e45fed 100644 --- a/testdata/d2compiler/TestCompile2/vars/errors/missing.exp.json +++ b/testdata/d2compiler/TestCompile2/vars/errors/missing.exp.json @@ -4,7 +4,7 @@ "errs": [ { "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2,4:0:20-4:8:28", - "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable z" + "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable \"z\"" } ] }