hclwrite: Fix heredocs never close during format

Waiting for TokenCHeredoc never ends since scanTokens() does not
 produce
 TokenNewlines inside heredocs.

 Related Issue: hashicorp/terraform#21434
This commit is contained in:
nozaq 2019-07-25 09:18:11 +09:00 committed by Martin Atkins
parent 984d1e8201
commit 0c3fe388e4
2 changed files with 25 additions and 21 deletions

View File

@ -382,9 +382,9 @@ 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
@ -392,26 +392,6 @@ func linesForFormat(tokens Tokens) []formatLine {
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]

View File

@ -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 {