From dc1536e8fbf26319f8f959581580f8eaa464b69e Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 9 Jun 2023 19:31:57 -0700 Subject: [PATCH] d2format: path.Clean imports --- d2format/format.go | 6 ++++++ d2format/format_test.go | 8 ++++++++ d2parser/parse.go | 4 +--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/d2format/format.go b/d2format/format.go index 84df2e945..b915c2638 100644 --- a/d2format/format.go +++ b/d2format/format.go @@ -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) } diff --git a/d2format/format_test.go b/d2format/format_test.go index 63e185dcf..3c957129d 100644 --- a/d2format/format_test.go +++ b/d2format/format_test.go @@ -649,6 +649,14 @@ x: @./file x: @../file `, exp: `x: @../file +`, + }, + { + name: "import/4", + in: ` +x: @"x/../file" +`, + exp: `x: @file `, }, } diff --git a/d2parser/parse.go b/d2parser/parse.go index 7970f1fa6..fa64531a3 100644 --- a/d2parser/parse.go +++ b/d2parser/parse.go @@ -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 - } + imp.Path = k.Path return imp }