compile reserved keys to lowercase in IR

This commit is contained in:
Alexander Wang 2023-03-05 09:43:42 -08:00
parent 843720e34d
commit 60c912c605
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
6 changed files with 44 additions and 3 deletions

View file

@ -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)

View file

@ -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",

View file

@ -1,6 +1,8 @@
package d2ir
import (
"strings"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2format"
"oss.terrastruct.com/d2/d2parser"
@ -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
}

View file

@ -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"`)
}

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/image_children.d2,3:2:115-3:3:116",
"errmsg": "d2/testdata/d2compiler/TestCompile/image_children.d2:4:3: image shapes cannot have children."
}
]
}
}

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/image_children_Steps.d2,3:2:115-3:7:120",
"errmsg": "d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps is only allowed at a board root"
}
]
}
}