Skip to content

Commit

Permalink
Merge pull request hashicorp#186 from hashicorp/b-comments
Browse files Browse the repository at this point in the history
hcl/printer: cleaner handling of standalone comments at end of file
  • Loading branch information
mitchellh committed Feb 2, 2017
2 parents 88e9565 + 47562cc commit 372e8dd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions hcl/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
6 changes: 6 additions & 0 deletions hcl/printer/testdata/comment_end_file.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "blah" "blah" {}

//
//
//

5 changes: 5 additions & 0 deletions hcl/printer/testdata/comment_end_file.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "blah" "blah" {}

//
//
//

0 comments on commit 372e8dd

Please sign in to comment.