Merge pull request #186 from hashicorp/b-comments

hcl/printer: cleaner handling of standalone comments at end of file
This commit is contained in:
Mitchell Hashimoto 2017-02-01 16:05:34 -08:00 committed by GitHub
commit 372e8ddaa1
4 changed files with 15 additions and 1 deletions

View File

@ -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.

View File

@ -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"},

View File

@ -0,0 +1,6 @@
resource "blah" "blah" {}
//
//
//

View File

@ -0,0 +1,5 @@
resource "blah" "blah" {}
//
//
//