Skip to content

Commit

Permalink
hclwrite: Fix heredocs never close during format
Browse files Browse the repository at this point in the history
 Waiting for TokenCHeredoc never ends since scanTokens() does not
 produce
 TokenNewlines inside heredocs.

 Related Issue: hashicorp/terraform#21434
  • Loading branch information
nozaq authored and apparentlymart committed Jul 25, 2019
1 parent 984d1e8 commit 0c3fe38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
22 changes: 1 addition & 21 deletions hclwrite/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,36 +382,16 @@ func linesForFormat(tokens Tokens) []formatLine {

// Now we'll pick off any trailing comments and attribute assignments
// to shuffle off into the "comment" and "assign" cells.
inHeredoc := false
for i := range lines {
line := &lines[i]

if len(line.lead) == 0 {
// if the line is empty then there's nothing for us to do
// (this should happen only for the final line, because all other
// lines would have a newline token of some kind)
continue
}

if inHeredoc {
for _, tok := range line.lead {
if tok.Type == hclsyntax.TokenCHeredoc {
inHeredoc = false
break
}
}
// Inside a heredoc everything is "lead", even if there's a
// template interpolation embedded in there that might otherwise
// confuse our logic below.
continue
}

for _, tok := range line.lead {
if tok.Type == hclsyntax.TokenOHeredoc {
inHeredoc = true
break
}
}

if len(line.lead) > 1 && line.lead[len(line.lead)-1].Type == hclsyntax.TokenComment {
line.comment = line.lead[len(line.lead)-1:]
line.lead = line.lead[:len(line.lead)-1]
Expand Down
24 changes: 24 additions & 0 deletions hclwrite/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,30 @@ bar {
}
`,
},
{
`
module "foo" {
foo = <<EOF
5
EOF
}
module "x" {
a = "b"
abcde = "456"
}`,
`
module "foo" {
foo = <<EOF
5
EOF
}
module "x" {
a = "b"
abcde = "456"
}`,
},
}

for i, test := range tests {
Expand Down

0 comments on commit 0c3fe38

Please sign in to comment.