fix no-primary-composite

This commit is contained in:
Alexander Wang 2023-07-13 14:11:14 -07:00
parent 4847714c3c
commit 29af92310b
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
6 changed files with 13 additions and 22 deletions

View file

@ -281,12 +281,6 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
if f.Map() != nil {
if len(f.Map().Edges) > 0 {
c.errorf(f.Map().Edges[0].LastRef().AST(), "vars cannot contain an edge")
} else {
for _, f := range f.Map().Fields {
if f.Primary() == nil && f.Composite == nil {
c.errorf(f.LastRef().AST(), "invalid var with no value")
}
}
}
}
return

View file

@ -3797,17 +3797,6 @@ hi: ${z}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable "z"`)
},
},
{
name: "bad-var",
run: func(t *testing.T) {
assertCompile(t, `
vars: {
x
}
hi: ${x}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2:3:3: invalid var with no value`)
},
},
{
name: "multi-part-map",
run: func(t *testing.T) {
@ -3818,7 +3807,7 @@ vars: {
}
}
hi: 1 ${x}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute map variable "x" as part of a string`)
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute composite variable "x" as part of a string`)
},
},
{
@ -3938,7 +3927,7 @@ z: {
d: ...${x}
c
}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute map variable "x" as part of a string`)
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute composite variable "x" as part of a string`)
},
},
}

View file

@ -188,8 +188,12 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node Node) {
}
}
if resolvedField.Primary() == nil {
if resolvedField.Composite == nil {
c.errorf(node.LastRef().AST(), `cannot substitute variable without value: "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
if len(s.Value) > 1 {
c.errorf(node.LastRef().AST(), `cannot substitute map variable "%s" as part of a string`, strings.Join(box.Substitution.IDA(), "."))
c.errorf(node.LastRef().AST(), `cannot substitute composite variable "%s" as part of a string`, strings.Join(box.Substitution.IDA(), "."))
return
}
switch n := node.(type) {

View file

@ -5,6 +5,10 @@
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2,2:2:11-2:3:12",
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2:3:3: invalid var with no value"
},
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2,4:4:23-4:5:24",
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2:5:5: invalid var with no value"
}
]
}

View file

@ -4,7 +4,7 @@
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2,6:0:31-6:2:33",
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute map variable \"x\" as part of a string"
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute composite variable \"x\" as part of a string"
}
]
}

View file

@ -4,7 +4,7 @@
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2,7:1:36-7:2:37",
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute map variable \"x\" as part of a string"
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute composite variable \"x\" as part of a string"
}
]
}