Skip to content

Commit

Permalink
Merge pull request hashicorp#181 from hashicorp/b-heredoc-single
Browse files Browse the repository at this point in the history
hcl: print single heredoc elements properly
  • Loading branch information
mitchellh committed Jan 25, 2017
2 parents 8c0b1f0 + 1bed775 commit dfbfb1c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
19 changes: 18 additions & 1 deletion hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ func (p *printer) list(l *ast.ListType) []byte {
insertSpaceBeforeItem := false
lastHadLeadComment := false
for i, item := range l.List {
// Keep track of whether this item is a heredoc since that has
// unique behavior.
heredoc := false
if lit, ok := item.(*ast.LiteralType); ok && lit.Token.Type == token.HEREDOC {
heredoc = true
}

if item.Pos().Line != l.Lbrack.Line {
// multiline list, add newline before we add each item
buf.WriteByte(newline)
Expand Down Expand Up @@ -507,7 +514,7 @@ func (p *printer) list(l *ast.ListType) []byte {
// if this item is a heredoc, then we output the comma on
// the next line. This is the only case this happens.
comma := []byte{','}
if lit, ok := item.(*ast.LiteralType); ok && lit.Token.Type == token.HEREDOC {
if heredoc {
buf.WriteByte(newline)
comma = p.indent(comma)
}
Expand Down Expand Up @@ -541,7 +548,17 @@ func (p *printer) list(l *ast.ListType) []byte {
buf.WriteByte(blank)
insertSpaceBeforeItem = false
}

// Output the item itself
buf.Write(p.output(item))

// If this is a heredoc item we always have to output a newline
// so that it parses properly.
if heredoc {
buf.WriteByte(newline)
}

// If this isn't the last element, write a comma.
if i != len(l.List)-1 {
buf.WriteString(",")
insertSpaceBeforeItem = true
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 @@ -42,6 +42,7 @@ var data = []entry{
{"empty_block.input", "empty_block.golden"},
{"list_of_objects.input", "list_of_objects.golden"},
{"multiline_string.input", "multiline_string.golden"},
{"object_with_heredoc.input", "object_with_heredoc.golden"},
}

func TestFiles(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions hcl/printer/testdata/list.golden
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ EOS
,
]

foo = [
<<EOS
foo = [<<EOS
one
EOS
,
]
3 changes: 1 addition & 2 deletions hcl/printer/testdata/list.input
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ EOS
,
]

foo = [
<<EOS
foo = [<<EOS
one
EOS
]
6 changes: 6 additions & 0 deletions hcl/printer/testdata/object_with_heredoc.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
obj {
foo = [<<EOF
TEXT!
EOF
]
}
6 changes: 6 additions & 0 deletions hcl/printer/testdata/object_with_heredoc.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
obj {
foo = [<<EOF
TEXT!
EOF
]
}

0 comments on commit dfbfb1c

Please sign in to comment.