configs/configupgrade: preserve in-line comments for lists (#21299)

* configs/configupgrade: preserve in-line comments for lists

The configupgrade tool was not writing `LineComments` for lists. Now it
is!

Fixes #21155
This commit is contained in:
Kristin Laemmert 2019-05-14 16:19:31 -04:00 committed by GitHub
parent abdd680fd2
commit 14d625c850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 4 deletions

View File

@ -0,0 +1,25 @@
variable "list" {
type = "list"
default = [
"foo", # I am a comment
"bar", # I am also a comment
"baz",
]
}
variable "list2" {
type = "list"
default = [
"foo",
"bar",
"baz",
]
}
variable "list_the_third" {
type = "list"
default = ["foo", "bar", "baz"]
}

View File

@ -0,0 +1,25 @@
variable "list" {
type = list(string)
default = [
"foo", # I am a comment
"bar", # I am also a comment
"baz",
]
}
variable "list2" {
type = list(string)
default = [
"foo",
"bar",
"baz",
]
}
variable "list_the_third" {
type = list(string)
default = ["foo", "bar", "baz"]
}

View File

@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}

View File

@ -168,12 +168,19 @@ Value:
src, moreDiags := upgradeExpr(node, filename, interp, an)
diags = diags.Append(moreDiags)
buf.Write(src)
if lit, ok := node.(*hcl1ast.LiteralType); ok && lit.LineComment != nil {
for _, comment := range lit.LineComment.List {
buf.WriteString(", " + comment.Text)
buf.WriteString("\n")
}
} else {
if multiline {
buf.WriteString(",\n")
} else if i < len(tv.List)-1 {
buf.WriteString(", ")
}
}
}
buf.WriteString("]")
case *hcl1ast.ObjectType: