From ddff2bcdd793a29a76fc44899c982945bf58833e Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Mar 2018 14:24:21 +0200 Subject: [PATCH 1/2] printer: Add another failing input to TestFormatParsable. --- hcl/printer/printer_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/hcl/printer/printer_test.go b/hcl/printer/printer_test.go index 15a4754c..5ae9ef73 100644 --- a/hcl/printer/printer_test.go +++ b/hcl/printer/printer_test.go @@ -153,6 +153,7 @@ func TestFormatValidOutput(t *testing.T) { cases := []string{ "#\x00", "#\ue123t", + "x=//\n0y=<<_\n_\n", "Y=<<4\n4/\n\n\n/4/@=4/\n\n\n/4000000004\r\r\n00004\n", "x=<<_\n_\r\r\n_\n", } From c2326d41d7d15ca88905ffaec5f83473c589548f Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Mar 2018 17:23:41 +0200 Subject: [PATCH 2/2] Fix handling of line comments in multi-line statements. --- hcl/parser/parser.go | 6 ++++++ hcl/printer/nodes.go | 10 +++++++++- hcl/printer/testdata/comment.golden | 3 +++ hcl/printer/testdata/comment.input | 2 ++ hcl/printer/testdata/comment_crlf.input | 2 ++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/hcl/parser/parser.go b/hcl/parser/parser.go index 098e1bc4..64c83bcf 100644 --- a/hcl/parser/parser.go +++ b/hcl/parser/parser.go @@ -205,6 +205,12 @@ func (p *Parser) objectItem() (*ast.ObjectItem, error) { } } + // key=#comment + // val + if p.lineComment != nil { + o.LineComment, p.lineComment = p.lineComment, nil + } + // do a look-ahead for line comment p.scan() if len(keys) > 0 && o.Val.Pos().Line == keys[0].Pos().Line && p.lineComment != nil { diff --git a/hcl/printer/nodes.go b/hcl/printer/nodes.go index c896d584..f95c6d80 100644 --- a/hcl/printer/nodes.go +++ b/hcl/printer/nodes.go @@ -252,6 +252,14 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte { } } + // If key and val are on different lines, treat line comments like lead comments. + if o.LineComment != nil && o.Val.Pos().Line != o.Keys[0].Pos().Line { + for _, comment := range o.LineComment.List { + buf.WriteString(comment.Text) + buf.WriteByte(newline) + } + } + for i, k := range o.Keys { buf.WriteString(k.Token.Text) buf.WriteByte(blank) @@ -265,7 +273,7 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte { buf.Write(p.output(o.Val)) - if o.Val.Pos().Line == o.Keys[0].Pos().Line && o.LineComment != nil { + if o.LineComment != nil && o.Val.Pos().Line == o.Keys[0].Pos().Line { buf.WriteByte(blank) for _, comment := range o.LineComment.List { buf.WriteString(comment.Text) diff --git a/hcl/printer/testdata/comment.golden b/hcl/printer/testdata/comment.golden index 9d4b072a..192c26aa 100644 --- a/hcl/printer/testdata/comment.golden +++ b/hcl/printer/testdata/comment.golden @@ -34,3 +34,6 @@ variable = { foo { bar = "fatih" // line comment 2 } // line comment 3 + +// comment +multiline = "assignment" diff --git a/hcl/printer/testdata/comment.input b/hcl/printer/testdata/comment.input index 57c37ac1..c4b29de7 100644 --- a/hcl/printer/testdata/comment.input +++ b/hcl/printer/testdata/comment.input @@ -35,3 +35,5 @@ foo { bar = "fatih" // line comment 2 } // line comment 3 +multiline = // comment +"assignment" diff --git a/hcl/printer/testdata/comment_crlf.input b/hcl/printer/testdata/comment_crlf.input index 5d272067..49550864 100644 --- a/hcl/printer/testdata/comment_crlf.input +++ b/hcl/printer/testdata/comment_crlf.input @@ -35,3 +35,5 @@ foo { bar = "fatih" // line comment 2 } // line comment 3 +multiline = // comment +"assignment"