non-root err check

This commit is contained in:
Alexander Wang 2023-07-11 15:05:16 -07:00
parent 85cf491d74
commit 4a9327e102
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 15 additions and 0 deletions

View file

@ -283,6 +283,9 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
c.errorf(f.Map().Edges[0].LastRef().AST(), "vars cannot contain an edge")
}
}
if d2ir.NodeBoardKind(d2ir.ParentMap(f)) == "" {
c.errorf(f.LastRef().AST(), "vars must be defined at the root of a board")
}
return
} else if isReserved {
c.compileReserved(&obj.Attributes, f)

View file

@ -3545,6 +3545,18 @@ hi: ${colors}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/map.d2:7:1: cannot reference map variable "colors"`)
},
},
{
name: "non-root",
run: func(t *testing.T) {
assertCompile(t, `
x: {
vars: {
x: hey
}
}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/non-root.d2:3:3: vars must be defined at the root of a board`)
},
},
}
for _, tc := range tca {