d2format: path.Clean imports

This commit is contained in:
Anmol Sethi 2023-06-09 19:31:57 -07:00
parent f561b831d0
commit dc1536e8fb
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
3 changed files with 15 additions and 3 deletions

View file

@ -216,6 +216,12 @@ func (p *printer) _import(i *d2ast.Import) {
p.sb.WriteString(pre)
p.sb.WriteRune('/')
}
if len(i.Path) > 0 {
i2 := *i
i2.Path = append([]*d2ast.StringBox{}, i.Path...)
i2.Path[0] = d2ast.RawStringBox(path.Clean(i.Path[0].Unbox().ScalarString()), true)
i = &i2
}
p.path(i.Path)
}

View file

@ -649,6 +649,14 @@ x: @./file
x: @../file
`,
exp: `x: @../file
`,
},
{
name: "import/4",
in: `
x: @"x/../file"
`,
exp: `x: @file
`,
},
}

View file

@ -1742,9 +1742,7 @@ func (p *parser) parseImport(spread bool) *d2ast.Import {
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:]...)
}
if k != nil {
imp.Path = k.Path
}
return imp
}