Skip to content

Commit

Permalink
Merge pull request #658 from hashicorp/b-fix-func-fmt
Browse files Browse the repository at this point in the history
hclwrite: Fix formatting of namespaced functions
  • Loading branch information
radeksimko authored Feb 16, 2024
2 parents e28daa3 + 9926eaf commit a7f9ab5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
15 changes: 10 additions & 5 deletions hclwrite/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ func spaceAfterToken(subject, before, after *Token) bool {
// Don't split a function name from open paren in a call
return false

case (subject.Type == hclsyntax.TokenIdent && after.Type == hclsyntax.TokenDoubleColon) ||
(subject.Type == hclsyntax.TokenDoubleColon && after.Type == hclsyntax.TokenIdent):
// Don't split namespace segments in a function call
return false

case subject.Type == hclsyntax.TokenDot || after.Type == hclsyntax.TokenDot:
// Don't use spaces around attribute access dots
return false
Expand Down Expand Up @@ -450,11 +455,11 @@ func tokenBracketChange(tok *Token) int {
// formatLine represents a single line of source code for formatting purposes,
// splitting its tokens into up to three "cells":
//
// lead: always present, representing everything up to one of the others
// assign: if line contains an attribute assignment, represents the tokens
// starting at (and including) the equals symbol
// comment: if line contains any non-comment tokens and ends with a
// single-line comment token, represents the comment.
// - lead: always present, representing everything up to one of the others
// - assign: if line contains an attribute assignment, represents the tokens
// starting at (and including) the equals symbol
// - comment: if line contains any non-comment tokens and ends with a
// single-line comment token, represents the comment.
//
// When formatting, the leading spaces of the first tokens in each of these
// cells is adjusted to align vertically their occurences on consecutive
Expand Down
18 changes: 18 additions & 0 deletions hclwrite/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,24 @@ module "x" {
abcde = "456"
}`,
},
{
`attr = provider::framework::example()`,
`attr = provider::framework::example()`,
},
{
`attr = provider :: framework :: example()`,
`attr = provider::framework::example()`,
},
{
`attr = provider ::framework:: example()`,
`attr = provider::framework::example()`,
},
{
// This is invalid syntax so formatting it with spaces
// does not have any meaning other than to make the fact more visible
`attr = provider::+example()`,
`attr = provider:: + example()`,
},
}

for i, test := range tests {
Expand Down

0 comments on commit a7f9ab5

Please sign in to comment.