Merge pull request #28202 from hashicorp/alisdair/fmt-multiline-value-expr-unwrap

cli: Fix fmt output for multi-line value exprs
This commit is contained in:
Alisdair McDiarmid 2021-03-26 11:33:14 -04:00 committed by GitHub
commit fd7c4105f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 1 deletions

View File

@ -366,7 +366,41 @@ func (c *FmtCommand) formatValueExpr(tokens hclwrite.Tokens) hclwrite.Tokens {
// "${
// foo
// }"
return c.trimNewlines(inside)
trimmed := c.trimNewlines(inside)
// Finally, we check if the unwrapped expression is on multiple lines. If
// so, we ensure that it is surrounded by parenthesis to make sure that it
// parses correctly after unwrapping. This may be redundant in some cases,
// but is required for at least multi-line ternary expressions.
isMultiLine := false
hasLeadingParen := false
hasTrailingParen := false
for i, token := range trimmed {
switch {
case i == 0 && token.Type == hclsyntax.TokenOParen:
hasLeadingParen = true
case token.Type == hclsyntax.TokenNewline:
isMultiLine = true
case i == len(trimmed)-1 && token.Type == hclsyntax.TokenCParen:
hasTrailingParen = true
}
}
if isMultiLine && !(hasLeadingParen && hasTrailingParen) {
wrapped := make(hclwrite.Tokens, 0, len(trimmed)+2)
wrapped = append(wrapped, &hclwrite.Token{
Type: hclsyntax.TokenOParen,
Bytes: []byte("("),
})
wrapped = append(wrapped, trimmed...)
wrapped = append(wrapped, &hclwrite.Token{
Type: hclsyntax.TokenCParen,
Bytes: []byte(")"),
})
return wrapped
}
return trimmed
}
func (c *FmtCommand) formatTypeExpr(tokens hclwrite.Tokens) hclwrite.Tokens {

View File

@ -42,3 +42,12 @@ resource "foo_instance" /* ... */ "baz" {
provider "" {
}
locals {
name = "${contains(["foo"], var.my_var) ? "${var.my_var}-bar" :
contains(["baz"], var.my_var) ? "baz-${var.my_var}" :
file("ERROR: unsupported type ${var.my_var}")}"
wrapped = "${(var.my_var == null ? 1 :
var.your_var == null ? 2 :
3)}"
}

View File

@ -42,3 +42,12 @@ resource "foo_instance" "baz" {
provider "" {
}
locals {
name = (contains(["foo"], var.my_var) ? "${var.my_var}-bar" :
contains(["baz"], var.my_var) ? "baz-${var.my_var}" :
file("ERROR: unsupported type ${var.my_var}"))
wrapped = (var.my_var == null ? 1 :
var.your_var == null ? 2 :
3)
}