Merge pull request #101 from mssola/printer-empty-node

printer: don't write a newline on empty objects
This commit is contained in:
Mitchell Hashimoto 2016-03-14 15:48:00 +01:00
commit c84febd4c1
4 changed files with 45 additions and 2 deletions

View File

@ -221,12 +221,12 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
defer un(trace(p, "ObjectType"))
var buf bytes.Buffer
buf.WriteString("{")
buf.WriteByte(newline)
var index int
var nextItem token.Pos
var commented bool
var commented, newlinePrinted bool
for {
// Print stand alone comments
for _, c := range p.standaloneComments {
for _, comment := range c.List {
@ -238,6 +238,13 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
}
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
// If there are standalone comments and the initial newline has not
// been printed yet, do it now.
if !newlinePrinted {
newlinePrinted = true
buf.WriteByte(newline)
}
// add newline if it's between other printed nodes
if index > 0 {
commented = true
@ -258,6 +265,14 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
break
}
// At this point we are sure that it's not a totally empty block: print
// the initial newline if it hasn't been printed yet by the previous
// block about standalone comments.
if !newlinePrinted {
buf.WriteByte(newline)
newlinePrinted = true
}
// check if we have adjacent one liner items. If yes we'll going to align
// the comments.
var aligned []*ast.ObjectItem

View File

@ -29,6 +29,7 @@ var data = []entry{
{"comment.input", "comment.golden"},
{"comment_aligned.input", "comment_aligned.golden"},
{"comment_standalone.input", "comment_standalone.golden"},
{"empty_block.input", "empty_block.golden"},
}
func TestFiles(t *testing.T) {

13
hcl/printer/testdata/empty_block.golden vendored Normal file
View File

@ -0,0 +1,13 @@
variable "foo" {}
variable "foo" {}
variable "foo" {
# Standalone comment should be still here
}
foo {}
foo {
bar = "mssola"
}

14
hcl/printer/testdata/empty_block.input vendored Normal file
View File

@ -0,0 +1,14 @@
variable "foo" {}
variable "foo" {
}
variable "foo" {
# Standalone comment should be still here
}
foo {
}
foo {
bar = "mssola"
}