From a6125c46ec7f8a8e4ab5f12ce818c91f5983337e Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 11 Jul 2023 14:01:35 -0700 Subject: [PATCH] cleanup --- d2compiler/compile_test.go | 13 +++++++++++++ d2ir/compile.go | 10 +++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 1912a9258..e9a59d6cc 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3473,6 +3473,19 @@ hi: ${z} `, `d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable "z"`) }, }, + { + name: "nested-missing", + run: func(t *testing.T) { + assertCompile(t, ` +vars: { + x: { + y: hey + } +} +hi: ${x.z} +`, `d2/testdata/d2compiler/TestCompile2/vars/errors/nested-missing.d2:7:1: could not resolve variable "x.z"`) + }, + }, { name: "edge", run: func(t *testing.T) { diff --git a/d2ir/compile.go b/d2ir/compile.go index 90c21d060..3fa8f3edb 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -98,11 +98,12 @@ func (c *compiler) overlayClasses(m *Map) { } func (c *compiler) resolveSubstitutions(refctx *RefContext) { - varsMap := &Map{} boardScope := refctx.ScopeMap if NodeBoardKind(refctx.ScopeMap) == "" { boardScope = ParentBoard(refctx.ScopeMap).Map() } + + varsMap := &Map{} vars := boardScope.GetField("vars") if vars != nil { varsMap = vars.Map() @@ -154,13 +155,12 @@ func (c *compiler) resolveSubstitution(vars *Map, mk *d2ast.Key, substitution *d vars = r.Map() resolved = r } + if resolved == nil { c.errorf(mk, `could not resolve variable "%s"`, strings.Join(substitution.IDA(), ".")) + } else if resolved.Composite != nil { + c.errorf(mk, `cannot reference map variable "%s"`, strings.Join(substitution.IDA(), ".")) } else { - if resolved.Composite != nil { - c.errorf(mk, `cannot reference map variable "%s"`, strings.Join(substitution.IDA(), ".")) - return nil - } return resolved } return nil