Skip to content

Commit

Permalink
Merge pull request #1401 from alixander/fix-underscore
Browse files Browse the repository at this point in the history
fix idval edge case
  • Loading branch information
alixander authored Jun 12, 2023
2 parents 34dfae6 + 192a76a commit 5004123
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 36 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
- Display version on CLI help invocation [#1400](https://github.com/terrastruct/d2/pull/1400)

#### Bugfixes ⛑️

- Fixes edge case in compiler using dots in quotes [#1401](https://github.com/terrastruct/d2/pull/1401)
8 changes: 4 additions & 4 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
Scope: fr.Context.Scope,
}
if fr.Context.ScopeMap != nil {
scopeObjIDA := d2ir.BoardIDA(fr.Context.ScopeMap)
r.ScopeObj = obj.Graph.Root.EnsureChildIDVal(scopeObjIDA)
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(fr.Context.ScopeMap))
r.ScopeObj = obj.Graph.Root.EnsureChild(scopeObjIDA)
}
obj.References = append(obj.References, r)
}
Expand Down Expand Up @@ -635,8 +635,8 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) {
Scope: er.Context.Scope,
}
if er.Context.ScopeMap != nil {
scopeObjIDA := d2ir.BoardIDA(er.Context.ScopeMap)
r.ScopeObj = edge.Src.Graph.Root.EnsureChildIDVal(scopeObjIDA)
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(er.Context.ScopeMap))
r.ScopeObj = edge.Src.Graph.Root.EnsureChild(scopeObjIDA)
}
edge.References = append(edge.References, r)
}
Expand Down
14 changes: 14 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ containers: {
`,
expErr: `d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps is only allowed at a board root`,
},
{
name: "name-with-dot-underscore",
text: `A: {
_.C
}
"D.E": {
_.C
}
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, 3, len(g.Objects))
},
},
{
name: "stroke-width",

Expand Down
32 changes: 0 additions & 32 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,38 +671,6 @@ func (obj *Object) HasChild(ids []string) (*Object, bool) {
return child, true
}

// Keep in sync with EnsureChild.
func (obj *Object) EnsureChildIDVal(ids []string) *Object {
if len(ids) == 0 {
return obj
}
if len(ids) == 1 && ids[0] != "style" {
_, ok := ReservedKeywords[ids[0]]
if ok {
return obj
}
}

id := ids[0]
ids = ids[1:]

var child *Object
for _, ch2 := range obj.ChildrenArray {
if ch2.IDVal == id {
child = ch2
break
}
}
if child == nil {
child = obj.newObject(id)
}

if len(ids) >= 1 {
return child.EnsureChildIDVal(ids)
}
return child
}

func (obj *Object) HasEdge(mk *d2ast.Key) (*Edge, bool) {
ea, ok := obj.FindEdges(mk)
if !ok {
Expand Down
Loading

0 comments on commit 5004123

Please sign in to comment.