From 81d22773002de532651a19acb91b4e1fe2b44cb2 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 23 May 2018 16:56:29 -0700 Subject: [PATCH] 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. --- hclwrite/format.go | 3 +++ hclwrite/format_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/hclwrite/format.go b/hclwrite/format.go index 64f161d..178cb2a 100644 --- a/hclwrite/format.go +++ b/hclwrite/format.go @@ -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 diff --git a/hclwrite/format_test.go b/hclwrite/format_test.go index e89f451..38b1089 100644 --- a/hclwrite/format_test.go +++ b/hclwrite/format_test.go @@ -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)`,