From 0c3fe388e4503d9717d39c315e8c7ee2433f752a Mon Sep 17 00:00:00 2001 From: nozaq Date: Thu, 25 Jul 2019 09:18:11 +0900 Subject: [PATCH] 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 --- hclwrite/format.go | 22 +--------------------- hclwrite/format_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/hclwrite/format.go b/hclwrite/format.go index 0cb0bac..ded7fb4 100644 --- a/hclwrite/format.go +++ b/hclwrite/format.go @@ -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] diff --git a/hclwrite/format_test.go b/hclwrite/format_test.go index 5445494..a918290 100644 --- a/hclwrite/format_test.go +++ b/hclwrite/format_test.go @@ -580,6 +580,30 @@ bar { } `, }, + { + ` +module "foo" { +foo = <