From e83976c008bc41a75f61c7f7b97913727d6ecc3f Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 1 Dec 2018 10:12:35 -0800 Subject: [PATCH] configs/configupgrade: Print trailing comments inside blocks Previously we were erroneously moving these out of their original block into the surrounding body. Now we'll make sure to collect up any remaining ad-hoc comments inside a nested block body before closing it. --- configs/configupgrade/upgrade_body.go | 4 +-- configs/configupgrade/upgrade_native.go | 35 ++++++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/configs/configupgrade/upgrade_body.go b/configs/configupgrade/upgrade_body.go index 2b14887798..b5195a93d2 100644 --- a/configs/configupgrade/upgrade_body.go +++ b/configs/configupgrade/upgrade_body.go @@ -173,7 +173,7 @@ func nestedBlockRule(filename string, nestedRules bodyContentRules, an *analysis printBlockOpen(buf, blockType, labels, item.LineComment) bodyDiags := upgradeBlockBody( filename, fmt.Sprintf("%s.%s", blockAddr, blockType), buf, - blockItem.List.Items, nestedRules, adhocComments, + blockItem.List.Items, blockItem.Rbrace, nestedRules, adhocComments, ) diags = diags.Append(bodyDiags) buf.WriteString("}\n") @@ -264,7 +264,7 @@ func nestedBlockRuleWithDynamic(filename string, nestedRules bodyContentRules, n printBlockOpen(buf, blockType, labels, item.LineComment) bodyDiags := upgradeBlockBody( filename, fmt.Sprintf("%s.%s", blockAddr, blockType), buf, - ti.List.Items, nestedRules, adhocComments, + ti.List.Items, ti.Rbrace, nestedRules, adhocComments, ) diags = diags.Append(bodyDiags) buf.WriteString("}\n") diff --git a/configs/configupgrade/upgrade_native.go b/configs/configupgrade/upgrade_native.go index 952bcf8716..b225bc5d1c 100644 --- a/configs/configupgrade/upgrade_native.go +++ b/configs/configupgrade/upgrade_native.go @@ -162,7 +162,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal }), } log.Printf("[TRACE] configupgrade: Upgrading var.%s at %s", labels[0], declRange) - bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("var.%s", labels[0]), &buf, body.List.Items, rules, adhocComments) + bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("var.%s", labels[0]), &buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") @@ -187,7 +187,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal "depends_on": dependsOnAttributeRule(filename, an), } log.Printf("[TRACE] configupgrade: Upgrading output.%s at %s", labels[0], declRange) - bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("output.%s", labels[0]), &buf, body.List.Items, rules, adhocComments) + bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("output.%s", labels[0]), &buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") @@ -312,7 +312,7 @@ func (u *Upgrader) upgradeNativeSyntaxResource(filename string, buf *bytes.Buffe printComments(buf, item.LeadComment) printBlockOpen(buf, blockType, labels, item.LineComment) - bodyDiags := upgradeBlockBody(filename, addr.String(), buf, body.List.Items, rules, adhocComments) + bodyDiags := upgradeBlockBody(filename, addr.String(), buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") @@ -335,7 +335,7 @@ func (u *Upgrader) upgradeNativeSyntaxProvider(filename string, buf *bytes.Buffe printComments(buf, item.LeadComment) printBlockOpen(buf, "provider", []string{typeName}, item.LineComment) - bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("provider.%s", typeName), buf, body.List.Items, rules, adhocComments) + bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("provider.%s", typeName), buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") @@ -382,7 +382,7 @@ func (u *Upgrader) upgradeNativeSyntaxTerraformBlock(filename string, buf *bytes printComments(buf, item.LeadComment) printBlockOpen(buf, "backend", []string{typeName}, item.LineComment) - bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("terraform.backend.%s", typeName), buf, body.List.Items, rules, adhocComments) + bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("terraform.backend.%s", typeName), buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n") @@ -392,14 +392,14 @@ func (u *Upgrader) upgradeNativeSyntaxTerraformBlock(filename string, buf *bytes printComments(buf, item.LeadComment) printBlockOpen(buf, "terraform", nil, item.LineComment) - bodyDiags := upgradeBlockBody(filename, "terraform", buf, body.List.Items, rules, adhocComments) + bodyDiags := upgradeBlockBody(filename, "terraform", buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") return diags } -func upgradeBlockBody(filename string, blockAddr string, buf *bytes.Buffer, args []*hcl1ast.ObjectItem, rules bodyContentRules, adhocComments *commentQueue) tfdiags.Diagnostics { +func upgradeBlockBody(filename string, blockAddr string, buf *bytes.Buffer, args []*hcl1ast.ObjectItem, end hcl1token.Pos, rules bodyContentRules, adhocComments *commentQueue) tfdiags.Diagnostics { var diags tfdiags.Diagnostics for i, arg := range args { @@ -412,7 +412,6 @@ func upgradeBlockBody(filename string, blockAddr string, buf *bytes.Buffer, args printComments(buf, arg.LeadComment) name := arg.Keys[0].Token.Value().(string) - //labelKeys := arg.Keys[1:] rule, expected := rules[name] if !expected { @@ -450,6 +449,16 @@ func upgradeBlockBody(filename string, blockAddr string, buf *bytes.Buffer, args } } + // Before we return, we must also print any remaining adhocComments that + // appear between our last item and the closing brace. + comments := adhocComments.TakeBeforePos(end) + for i, group := range comments { + printComments(buf, group) + if i < len(comments)-1 { + buf.WriteByte('\n') // Extra separator after each group + } + } + return diags } @@ -636,8 +645,16 @@ func collectAdhocComments(f *hcl1ast.File) *commentQueue { type commentQueue []*hcl1ast.CommentGroup +func (q *commentQueue) TakeBeforeToken(token hcl1token.Token) []*hcl1ast.CommentGroup { + return q.TakeBeforePos(token.Pos) +} + func (q *commentQueue) TakeBefore(node hcl1ast.Node) []*hcl1ast.CommentGroup { - toPos := node.Pos() + return q.TakeBeforePos(node.Pos()) +} + +func (q *commentQueue) TakeBeforePos(pos hcl1token.Pos) []*hcl1ast.CommentGroup { + toPos := pos var i int for i = 0; i < len(*q); i++ { if (*q)[i].Pos().After(toPos) {