This commit is contained in:
Alexander Wang 2023-07-11 14:01:35 -07:00
parent 78e9e4565e
commit a6125c46ec
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 18 additions and 5 deletions

View file

@ -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) {

View file

@ -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 {
} else if resolved.Composite != nil {
c.errorf(mk, `cannot reference map variable "%s"`, strings.Join(substitution.IDA(), "."))
return nil
}
} else {
return resolved
}
return nil