From 47562ccf19d3299abea5efdde59d844418301d72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 1 Feb 2017 16:03:59 -0800 Subject: [PATCH] hcl/printer: cleaner handling of standalone comments at end of file --- hcl/printer/nodes.go | 4 +++- hcl/printer/printer_test.go | 1 + hcl/printer/testdata/comment_end_file.golden | 6 ++++++ hcl/printer/testdata/comment_end_file.input | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 hcl/printer/testdata/comment_end_file.golden create mode 100644 hcl/printer/testdata/comment_end_file.input diff --git a/hcl/printer/nodes.go b/hcl/printer/nodes.go index a733159..35a46fc 100644 --- a/hcl/printer/nodes.go +++ b/hcl/printer/nodes.go @@ -134,6 +134,7 @@ func (p *printer) output(n interface{}) []byte { // Go through all the comments in the group. The group // should be printed together, not separated by double newlines. printed := false + newlinePrinted := false for _, comment := range c.List { // We only care about comments after the previous item // we've printed so that comments are printed in the @@ -144,8 +145,9 @@ func (p *printer) output(n interface{}) []byte { // we don't do this if prev is invalid which means the // beginning of the file since the first comment should // be at the first line. - if p.prev.IsValid() && index == len(t.Items) { + if !newlinePrinted && p.prev.IsValid() && index == len(t.Items) { buf.Write([]byte{newline, newline}) + newlinePrinted = true } // Write the actual comment. diff --git a/hcl/printer/printer_test.go b/hcl/printer/printer_test.go index 23662db..01ea3b2 100644 --- a/hcl/printer/printer_test.go +++ b/hcl/printer/printer_test.go @@ -35,6 +35,7 @@ var data = []entry{ {"comment.input", "comment.golden"}, {"comment_aligned.input", "comment_aligned.golden"}, {"comment_array.input", "comment_array.golden"}, + {"comment_end_file.input", "comment_end_file.golden"}, {"comment_multiline_indent.input", "comment_multiline_indent.golden"}, {"comment_multiline_no_stanza.input", "comment_multiline_no_stanza.golden"}, {"comment_multiline_stanza.input", "comment_multiline_stanza.golden"}, diff --git a/hcl/printer/testdata/comment_end_file.golden b/hcl/printer/testdata/comment_end_file.golden new file mode 100644 index 0000000..dbeae36 --- /dev/null +++ b/hcl/printer/testdata/comment_end_file.golden @@ -0,0 +1,6 @@ +resource "blah" "blah" {} + +// +// +// + diff --git a/hcl/printer/testdata/comment_end_file.input b/hcl/printer/testdata/comment_end_file.input new file mode 100644 index 0000000..68c4c28 --- /dev/null +++ b/hcl/printer/testdata/comment_end_file.input @@ -0,0 +1,5 @@ +resource "blah" "blah" {} + +// +// +//