hclwrite: Format shouldn't introduce spaces before index brackets

This is another heuristic because the "[" syntax is also the tuple
constructor start marker, but this takes care of the common cases of
indexing keywords and bracketed expressions.

This fixes #29.
This commit is contained in:
Martin Atkins 2018-05-23 16:56:29 -07:00
parent 524cf10f48
commit 81d2277300
2 changed files with 15 additions and 0 deletions

View File

@ -243,6 +243,9 @@ func spaceAfterToken(subject, before, after *Token) bool {
// No extra spaces within templates
return false
case after.Type == hclsyntax.TokenOBrack && (subject.Type == hclsyntax.TokenIdent || subject.Type == hclsyntax.TokenNumberLit || tokenBracketChange(subject) < 0):
return false
case subject.Type == hclsyntax.TokenMinus:
// Since a minus can either be subtraction or negation, and the latter
// should _not_ have a space after it, we need to use some heuristics

View File

@ -27,6 +27,18 @@ func TestFormat(t *testing.T) {
`a=b.c`,
`a = b.c`,
},
{
`a=b[c]`,
`a = b[c]`,
},
{
`a=b()[c]`,
`a = b()[c]`,
},
{
`a=["hello"][0]`,
`a = ["hello"][0]`,
},
{
`( a+2 )`,
`(a + 2)`,