hcl/printer: singleline objects followed by non-singleline need blank

Fixes https://github.com/hashicorp/terraform/issues/12017
This commit is contained in:
Mitchell Hashimoto 2017-02-17 08:45:53 -08:00
parent 372e8ddaa1
commit b979c7a8d6
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 25 additions and 3 deletions

View File

@ -176,10 +176,23 @@ func (p *printer) output(n interface{}) []byte {
// Always write a newline to separate us from the next item
buf.WriteByte(newline)
// If the next item is an object that is exactly one line,
// then we don't output another newline.
// Need to determine if we're going to separate the next item
// with a blank line. The logic here is simple, though there
// are a few conditions:
//
// 1. The next object is more than one line away anyways,
// so we need an empty line.
//
// 2. The next object is not a "single line" object, so
// we need an empty line.
//
// 3. This current object is not a single line object,
// so we need an empty line.
current := t.Items[index]
next := t.Items[index+1]
if next.Pos().Line != t.Items[index].Pos().Line+1 || !p.isSingleLineObject(next) {
if next.Pos().Line != t.Items[index].Pos().Line+1 ||
!p.isSingleLineObject(next) ||
!p.isSingleLineObject(current) {
buf.WriteByte(newline)
}
}

View File

@ -13,6 +13,12 @@ variable "foo" {}
# lead comment
variable "bar" {}
variable "foo" {
default = "bar"
}
variable "bar" {}
# Purposeful newline check below:
variable "foo" {}

View File

@ -9,6 +9,9 @@ variable "foo" {}
# lead comment
variable "bar" {}
variable "foo" { default = "bar" }
variable "bar" {}
# Purposeful newline check below:
variable "foo" {}