d2ir: prevent illegal non-tail keywords

This commit is contained in:
Alexander Wang 2023-05-31 23:04:09 -07:00
parent 26b41a79bb
commit e998a95b96
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 24 additions and 5 deletions

View file

@ -1722,6 +1722,13 @@ y -> x.style
`,
expErr: `d2/testdata/d2compiler/TestCompile/edge_to_style.d2:2:8: reserved keywords are prohibited in edges`,
},
{
name: "keyword-container",
text: `a.near.b
`,
expErr: `d2/testdata/d2compiler/TestCompile/keyword-container.d2:1:3: "near" must be the last part of the key`,
},
{
name: "escaped_id",

View file

@ -1645,11 +1645,12 @@ var SimpleReservedKeywords = map[string]struct{}{
"classes": {},
}
// ReservedKeywordHolders are reserved keywords that are meaningless on its own and exist solely to hold a set of reserved keywords
// ReservedKeywordHolders are reserved keywords that can hold composites
var ReservedKeywordHolders = map[string]struct{}{
"style": {},
"source-arrowhead": {},
"target-arrowhead": {},
"classes": {},
}
// StyleKeywords are reserved keywords which cannot exist outside of the "style" keyword

View file

@ -669,10 +669,9 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext) (*Field,
if _, ok := d2graph.ReservedKeywords[strings.ToLower(head)]; ok {
head = strings.ToLower(head)
}
if head == "class" && i < len(kp.Path)-1 {
return nil, d2parser.Errorf(kp.Path[i].Unbox(), `"class" must be the last part of the key`)
if _, ok := d2graph.ReservedKeywordHolders[head]; !ok && i < len(kp.Path)-1 {
return nil, d2parser.Errorf(kp.Path[i].Unbox(), fmt.Sprintf(`"%s" must be the last part of the key`, head))
}
}
if head == "_" {

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/keyword-container.d2,0:2:2-0:6:6",
"errmsg": "d2/testdata/d2compiler/TestCompile/keyword-container.d2:1:3: \"near\" must be the last part of the key"
}
]
}
}