Skip to content

Commit

Permalink
Merge pull request #1695 from alixander/vars-panic
Browse files Browse the repository at this point in the history
compiler: vars fixes
  • Loading branch information
alixander authored Nov 1, 2023
2 parents 92ef0b7 + cb001ad commit 65c4849
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Grid cells can now contain nested edges [#1629](https://github.com/terrastruct/d2/pull/1629)
- Edges can now go across constant nears, sequence diagrams, and grids including nested ones. [#1631](https://github.com/terrastruct/d2/pull/1631)
- All vars defined in a scope are accessible everywhere in that scope, i.e., an object can use a var defined after itself. [#1695](https://github.com/terrastruct/d2/pull/1695)

#### Bugfixes ⛑️

Expand All @@ -15,3 +16,4 @@
- Fixes elk growing shapes with width/height set [#1679](https://github.com/terrastruct/d2/pull/1679)
- Adds a compiler error when accidentally using an arrowhead on a shape [#1686](https://github.com/terrastruct/d2/pull/1686)
- Correctly reports errors from invalid values set by globs. [#1691](https://github.com/terrastruct/d2/pull/1691)
- Fixes panic when spread substitution referenced a nonexistant var. [#1695](https://github.com/terrastruct/d2/pull/1695)
24 changes: 24 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4160,6 +4160,30 @@ mybox: {
`, "")
},
},
{
name: "undeclared-var-usage",
run: func(t *testing.T) {
assertCompile(t, `
x: { ...${v} }
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/undeclared-var-usage.d2:2:4: could not resolve variable "v"`)
},
},
{
name: "split-var-usage",
run: func(t *testing.T) {
assertCompile(t, `
x1
vars: {
v: {
style.fill: green
}
}
x1: { ...${v} }
`, ``)
},
},
}

for _, tc := range tca {
Expand Down
9 changes: 9 additions & 0 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (c *compiler) compileSubstitutions(m *Map, varsStack []*Map) {
if f.Name == "vars" && f.Map() != nil {
varsStack = append([]*Map{f.Map()}, varsStack...)
}
}
for _, f := range m.Fields {
if f.Primary() != nil {
c.resolveSubstitutions(varsStack, f)
}
Expand Down Expand Up @@ -459,6 +461,13 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
Value: []d2ast.InterpolationBox{{Substitution: n.Substitution}},
},
},
References: []*FieldReference{{
Context_: &RefContext{
Scope: ast,
ScopeMap: dst,
ScopeAST: scopeAST,
},
}},
}
dst.Fields = append(dst.Fields, f)
case n.Import != nil:
Expand Down
280 changes: 280 additions & 0 deletions testdata/d2compiler/TestCompile2/vars/errors/split-var-usage.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65c4849

Please sign in to comment.