d2parser: Fix bad import key panic
This commit is contained in:
parent
f819e5395b
commit
10c6d2af71
5 changed files with 53 additions and 1 deletions
|
|
@ -54,10 +54,16 @@ func compileFS(t testing.TB, path string, mfs map[string]string) (*d2ir.Map, err
|
|||
t.Helper()
|
||||
|
||||
ast, err := d2parser.Parse(path, strings.NewReader(mfs[path]), nil)
|
||||
assert.Success(t, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fs, err := mapfs.New(mfs)
|
||||
assert.Success(t, err)
|
||||
t.Cleanup(func() {
|
||||
err = fs.Close()
|
||||
assert.Success(t, err)
|
||||
})
|
||||
m, err := d2ir.Compile(ast, &d2ir.CompileOptions{
|
||||
FS: fs,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -98,6 +98,15 @@ label: meow`,
|
|||
assert.ErrorString(t, err, `index.d2:1:1: failed to import "x.d2": open x.d2: no such file or directory`)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "escape",
|
||||
run: func(t testing.TB) {
|
||||
_, err := compileFS(t, "index.d2", map[string]string{
|
||||
"index.d2": "...@'./../x.d2'",
|
||||
})
|
||||
assert.ErrorString(t, err, `index.d2:1:1: failed to import "../x.d2": stat ../x.d2: invalid argument`)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "absolute",
|
||||
run: func(t testing.TB) {
|
||||
|
|
|
|||
|
|
@ -1710,6 +1710,9 @@ func (p *parser) parseImport(spread bool) *d2ast.Import {
|
|||
}
|
||||
|
||||
k := p.parseKey()
|
||||
if k == nil {
|
||||
return imp
|
||||
}
|
||||
if k.Path[0].UnquotedString != nil && len(k.Path) > 1 && k.Path[1].UnquotedString != nil && k.Path[1].Unbox().ScalarString() == "d2" {
|
||||
k.Path = append(k.Path[:1], k.Path[2:]...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -451,6 +451,18 @@ func testImport(t *testing.T) {
|
|||
assert.Equal(t, "d2", ast.Nodes[0].Import.Path[1].Unbox().ScalarString())
|
||||
},
|
||||
},
|
||||
{
|
||||
text: "...@../file",
|
||||
assert: func(t testing.TB, ast *d2ast.Map, err error) {
|
||||
assert.ErrorString(t, err, "d2/testdata/d2parser/TestParse/import/#06.d2:1:5: unexpected text after import")
|
||||
},
|
||||
},
|
||||
{
|
||||
text: "@file",
|
||||
assert: func(t testing.TB, ast *d2ast.Map, err error) {
|
||||
assert.ErrorString(t, err, "d2/testdata/d2parser/TestParse/import/#06.d2:1:5: unexpected text after import")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runa(t, tca)
|
||||
|
|
|
|||
22
testdata/d2parser/TestParse/import/#06.exp.json
generated
vendored
Normal file
22
testdata/d2parser/TestParse/import/#06.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"ast": {
|
||||
"range": "d2/testdata/d2parser/TestParse/import/#06.d2,0:0:0-0:11:11",
|
||||
"nodes": [
|
||||
{
|
||||
"import": {
|
||||
"range": "d2/testdata/d2parser/TestParse/import/#06.d2,0:0:0-0:4:4",
|
||||
"spread": true,
|
||||
"path": null
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"err": {
|
||||
"errs": [
|
||||
{
|
||||
"range": "d2/testdata/d2parser/TestParse/import/#06.d2,0:4:4-0:11:11",
|
||||
"errmsg": "d2/testdata/d2parser/TestParse/import/#06.d2:1:5: unexpected text after import"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue