Skip to content

Commit

Permalink
Merge pull request #978 from alixander/img-shape-children
Browse files Browse the repository at this point in the history
case insensitive reserved keyword matching
  • Loading branch information
alixander authored Mar 7, 2023
2 parents 843720e + 60c912c commit e981e19
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#### Improvements 🧹

#### Bugfixes ⛑️

- Accept absolute paths again on the CLI. [#979](https://github.com/terrastruct/d2/pull/979)
- Fixes some rare undefined behavior using capitalized reserved keywords [#978](https://github.com/terrastruct/d2/pull/978)
11 changes: 11 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ containers: {
`,
expErr: `d2/testdata/d2compiler/TestCompile/image_non_style.d2:4:3: image shapes cannot have children.`,
},
{
name: "image_children_Steps",

text: `x: {
icon: https://icons.terrastruct.com/aws/_Group%20Icons/EC2-instance-container_light-bg.svg
shape: image
Steps
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps is only allowed at a board root`,
},
{
name: "stroke-width",

Expand Down
6 changes: 4 additions & 2 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package d2ir

import (
"strings"

"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2format"
"oss.terrastruct.com/d2/d2parser"
Expand Down Expand Up @@ -163,13 +165,13 @@ func (c *compiler) compileLink(refctx *RefContext) {
}

// If it doesn't start with one of these reserved words, the link is definitely not a board link.
if linkIDA[0] != "layers" && linkIDA[0] != "scenarios" && linkIDA[0] != "steps" && linkIDA[0] != "_" {
if !strings.EqualFold(linkIDA[0], "layers") && !strings.EqualFold(linkIDA[0], "scenarios") && !strings.EqualFold(linkIDA[0], "steps") && linkIDA[0] != "_" {
return
}

// Chop off the non-board portion of the scope, like if this is being defined on a nested object (e.g. `x.y.z`)
for i := len(scopeIDA) - 1; i > 0; i-- {
if scopeIDA[i-1] == "layers" || scopeIDA[i-1] == "scenarios" || scopeIDA[i-1] == "steps" {
if strings.EqualFold(scopeIDA[i-1], "layers") || strings.EqualFold(scopeIDA[i-1], "scenarios") || strings.EqualFold(scopeIDA[i-1], "steps") {
scopeIDA = scopeIDA[:i+1]
break
}
Expand Down
4 changes: 4 additions & 0 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,10 @@ func (m *Map) EnsureField(kp *d2ast.KeyPath, refctx *RefContext) (*Field, error)
func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext) (*Field, error) {
head := kp.Path[i].Unbox().ScalarString()

if _, ok := d2graph.ReservedKeywords[strings.ToLower(head)]; ok {
head = strings.ToLower(head)
}

if head == "_" {
return nil, d2parser.Errorf(kp.Path[i].Unbox(), `parent "_" can only be used in the beginning of paths, e.g. "_.x"`)
}
Expand Down
12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/image_children.exp.json

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

12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/image_children_Steps.exp.json

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

0 comments on commit e981e19

Please sign in to comment.