d2ir: prevent illegal non-tail keywords
This commit is contained in:
parent
26b41a79bb
commit
e998a95b96
4 changed files with 24 additions and 5 deletions
|
|
@ -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",
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 == "_" {
|
||||
|
|
|
|||
12
testdata/d2compiler/TestCompile/keyword-container.exp.json
generated
vendored
Normal file
12
testdata/d2compiler/TestCompile/keyword-container.exp.json
generated
vendored
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue