Skip to content

Commit

Permalink
Merge pull request #2112 from alixander/import-duplicate
Browse files Browse the repository at this point in the history
d2ir: fix importing scopes
  • Loading branch information
alixander authored Sep 23, 2024
2 parents e3952ca + 24f9150 commit 451aefb
Show file tree
Hide file tree
Showing 14 changed files with 1,149 additions and 43 deletions.
3 changes: 3 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
}
}

parent := obj
obj = obj.EnsureChild(d2graphIDA([]string{f.Name}))
if f.Primary() != nil {
c.compileLabel(&obj.Attributes, f)
Expand All @@ -376,6 +377,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
MapKeyEdgeIndex: fr.Context_.EdgeIndex(),
Scope: fr.Context_.Scope,
ScopeAST: fr.Context_.ScopeAST,
ScopeObj: parent,
}
if fr.Context_.ScopeMap != nil && !d2ir.IsVar(fr.Context_.ScopeMap) {
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(fr.Context_.ScopeMap))
Expand Down Expand Up @@ -777,6 +779,7 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) {
MapKeyEdgeIndex: er.Context_.EdgeIndex(),
Scope: er.Context_.Scope,
ScopeAST: er.Context_.ScopeAST,
ScopeObj: obj,
}
if er.Context_.ScopeMap != nil && !d2ir.IsVar(er.Context_.ScopeMap) {
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(er.Context_.ScopeMap))
Expand Down
34 changes: 32 additions & 2 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,36 @@ x -> y: {
}
},
},
{
name: "nested-scope-1",

text: `...@second
`,
files: map[string]string{
"second.d2": `second: {
...@third
}`,
"third.d2": `third: {
elem
}`,
},
assertions: func(t *testing.T, g *d2graph.Graph) {
assert.Equal(t, 3, len(g.Objects))
},
},
{
name: "nested-scope-2",

text: `...@second
a.style.fill: null
`,
files: map[string]string{
"second.d2": `a.style.fill: red`,
},
assertions: func(t *testing.T, g *d2graph.Graph) {
assert.Equal(t, 1, len(g.Objects))
},
},
{
name: "url_tooltip",
text: `x: {tooltip: https://google.com}`,
Expand Down Expand Up @@ -2998,8 +3028,8 @@ qa: {
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "dev.env", g.Objects[1].AbsID())
tassert.Equal(t, "Dev Environment", g.Objects[1].Label.Value)
tassert.Equal(t, "qa.env", g.Objects[4].AbsID())
tassert.Equal(t, "Qa Environment", g.Objects[4].Label.Value)
tassert.Equal(t, "qa.env", g.Objects[2].AbsID())
tassert.Equal(t, "Qa Environment", g.Objects[2].Label.Value)
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
dst.Fields = append(dst.Fields, f)
case n.Import != nil:
// Spread import
impn, ok := c._import(n.Import)
if !ok {
continue
Expand Down Expand Up @@ -867,6 +868,7 @@ func (c *compiler) _compileField(f *Field, refctx *RefContext) {
c.overlayClasses(f.Map())
}
} else if refctx.Key.Value.Import != nil {
// Non-spread import
n, ok := c._import(refctx.Key.Value.Import)
if !ok {
return
Expand Down
11 changes: 7 additions & 4 deletions d2ir/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,15 @@ func (c *compiler) _import(imp *d2ast.Import) (Node, bool) {
if !ok {
return nil, false
}
nilScopeMap(ir)
if len(imp.IDA()) > 0 {
f := ir.GetField(imp.IDA()...)
if f == nil {
c.errorf(imp, "import key %q doesn't exist inside import", imp.IDA())
return nil, false
}
if f.Map() != nil {
nilScopeMap(f.Map())
}
return f, true
}
nilScopeMap(ir)
return ir, true
}

Expand Down Expand Up @@ -135,9 +132,15 @@ func nilScopeMap(n Node) {
for _, r := range n.References {
r.Context_.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
case *Field:
for _, r := range n.References {
r.Context_.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
}
}
17 changes: 17 additions & 0 deletions d2ir/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ label: meow`,
assert.Success(t, err)
},
},
{
name: "nested-scope",
run: func(t testing.TB) {
m, err := compileFS(t, "index.d2", map[string]string{
"index.d2": `...@second
`,
"second.d2": `elem: {
...@third
}`,
"third.d2": `third: {
elem
}`,
})
assert.Success(t, err)
assertQuery(t, m, 3, 0, nil, "")
},
},
}

runa(t, tca)
Expand Down
24 changes: 24 additions & 0 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ if i'm right: {
a <- b: fix
}
if i'm wrong: {
a <- b: nah, intended
}`)
err := runTestMain(t, ctx, dir, env, "index.d2")
assert.Success(t, err)

assert.TestdataDir(t, filepath.Join(dir, "index"))
},
},
{
name: "sequence-spread-layer",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "index.d2", `k; layers: { seq: {[email protected]} }`)
writeFile(t, dir, "seq.d2", `shape: sequence_diagram
a: me
b: github.com/terrastruct/d2
a -> b: issue about a bug
a."some note about the bug"
if i'm right: {
a <- b: fix
}
if i'm wrong: {
a <- b: nah, intended
}`)
Expand Down
Binary file modified e2etests-cli/testdata/TestCLI_E2E/pptx-theme-overrides.exp.pptx
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 451aefb

Please sign in to comment.