Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix idval edge case #1401

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
#### Improvements 🧹

#### 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