null
This commit is contained in:
parent
301bc8ddfb
commit
ef11d47fc8
3 changed files with 39 additions and 2 deletions
|
|
@ -3696,6 +3696,22 @@ scenarios: {
|
||||||
assert.Equal(t, "im replaced x var", g.Scenarios[0].Objects[0].Label.Value)
|
assert.Equal(t, "im replaced x var", g.Scenarios[0].Objects[0].Label.Value)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "null",
|
||||||
|
run: func(t *testing.T) {
|
||||||
|
assertCompile(t, `
|
||||||
|
vars: {
|
||||||
|
surname: Smith
|
||||||
|
}
|
||||||
|
a: {
|
||||||
|
vars: {
|
||||||
|
surname: null
|
||||||
|
}
|
||||||
|
hi: John ${surname}
|
||||||
|
}
|
||||||
|
`, `d2/testdata/d2compiler/TestCompile2/vars/boards/null.d2:9:3: could not resolve variable "surname"`)
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tca {
|
for _, tc := range tca {
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,11 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node Node) {
|
||||||
for _, vars := range varsStack {
|
for _, vars := range varsStack {
|
||||||
resolvedField = c.resolveSubstitution(vars, box.Substitution)
|
resolvedField = c.resolveSubstitution(vars, box.Substitution)
|
||||||
if resolvedField != nil {
|
if resolvedField != nil {
|
||||||
|
if resolvedField.Primary() != nil {
|
||||||
|
if _, ok := resolvedField.Primary().Value.(*d2ast.Null); ok {
|
||||||
|
resolvedField = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -257,6 +262,7 @@ func (c *compiler) resolveSubstitution(vars *Map, substitution *d2ast.Substituti
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if i == len(substitution.Path)-1 {
|
if i == len(substitution.Path)-1 {
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
@ -351,9 +357,13 @@ func (c *compiler) compileKey(refctx *RefContext) {
|
||||||
|
|
||||||
func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) {
|
func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) {
|
||||||
if refctx.Key != nil && len(refctx.Key.Edges) == 0 && refctx.Key.Value.Null != nil {
|
if refctx.Key != nil && len(refctx.Key.Edges) == 0 && refctx.Key.Value.Null != nil {
|
||||||
|
// For vars, if we delete the field, it may just resolve to an outer scope var of the same name
|
||||||
|
// Instead we keep it around, so that resolveSubstitutions can find it
|
||||||
|
if ParentField(dst) == nil || ParentField(dst).Name != "vars" {
|
||||||
dst.DeleteField(kp.IDA()...)
|
dst.DeleteField(kp.IDA()...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
f, err := dst.EnsureField(kp, refctx)
|
f, err := dst.EnsureField(kp, refctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
|
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
|
||||||
|
|
|
||||||
11
testdata/d2compiler/TestCompile2/vars/boards/null.exp.json
generated
vendored
Normal file
11
testdata/d2compiler/TestCompile2/vars/boards/null.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"graph": null,
|
||||||
|
"err": {
|
||||||
|
"errs": [
|
||||||
|
{
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/null.d2,8:2:64-8:4:66",
|
||||||
|
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/boards/null.d2:9:3: could not resolve variable \"surname\""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue