From fd915f557d5147090803a405f860e0bfcaa1afe6 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 3 Nov 2018 19:54:56 +0000 Subject: [PATCH] hclwrite: Rename Body.AppendBlock to AppendNewBlock Since this function implicitly creates a new body, this name is more appropriate and leaves the name "AppendBlock" open for a later method to append an _existing_ block, such as when moving a block from one file to another. --- hclwrite/ast_body.go | 33 +++++++++--------- hclwrite/ast_body_test.go | 70 +-------------------------------------- 2 files changed, 16 insertions(+), 87 deletions(-) diff --git a/hclwrite/ast_body.go b/hclwrite/ast_body.go index ded3761..3eda787 100644 --- a/hclwrite/ast_body.go +++ b/hclwrite/ast_body.go @@ -85,26 +85,23 @@ func (b *Body) SetAttributeTraversal(name string, traversal hcl.Traversal) *Attr panic("Body.SetAttributeTraversal not yet implemented") } -// AppendBlock appends a new nested block to the end of the receiving body. -// -// If blankLine is set, an additional empty line is added before the block -// for separation. Usual HCL style suggests that we group together blocks of -// the same type without intervening blank lines and then put blank lines -// between blocks of different types. In some languages, some different block -// types may be conceptually related and so may still be grouped together. -// It is the caller's responsibility to respect the usual conventions of the -// language being generated. -func (b *Body) AppendBlock(typeName string, labels []string, blankLine bool) *Block { +// AppendNewBlock appends a new nested block to the end of the receiving body +// with the given type name and labels. +func (b *Body) AppendNewBlock(typeName string, labels []string) *Block { block := newBlock() block.init(typeName, labels) - if blankLine { - b.AppendUnstructuredTokens(Tokens{ - { - Type: hclsyntax.TokenNewline, - Bytes: []byte{'\n'}, - }, - }) - } b.appendItem(block) return block } + +// AppendNewline appends a newline token to th end of the receiving body, +// which generally serves as a separator between different sets of body +// contents. +func (b *Body) AppendNewline() { + b.AppendUnstructuredTokens(Tokens{ + { + Type: hclsyntax.TokenNewline, + Bytes: []byte{'\n'}, + }, + }) +} diff --git a/hclwrite/ast_body_test.go b/hclwrite/ast_body_test.go index bc0aabd..916beb6 100644 --- a/hclwrite/ast_body_test.go +++ b/hclwrite/ast_body_test.go @@ -419,14 +419,12 @@ func TestBodyAppendBlock(t *testing.T) { src string blockType string labels []string - blank bool want Tokens }{ { "", "foo", nil, - false, Tokens{ { Type: hclsyntax.TokenIdent, @@ -464,7 +462,6 @@ func TestBodyAppendBlock(t *testing.T) { "", "foo", []string{"bar"}, - false, Tokens{ { Type: hclsyntax.TokenIdent, @@ -517,7 +514,6 @@ func TestBodyAppendBlock(t *testing.T) { "", "foo", []string{"bar", "baz"}, - false, Tokens{ { Type: hclsyntax.TokenIdent, @@ -585,7 +581,6 @@ func TestBodyAppendBlock(t *testing.T) { "bar {}\n", "foo", nil, - false, Tokens{ { Type: hclsyntax.TokenIdent, @@ -639,69 +634,6 @@ func TestBodyAppendBlock(t *testing.T) { }, }, }, - { - "bar_blank_after {}\n", - "foo", - nil, - true, - Tokens{ - { - Type: hclsyntax.TokenIdent, - Bytes: []byte(`bar_blank_after`), - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenOBrace, - Bytes: []byte{'{'}, - SpacesBefore: 1, - }, - { - Type: hclsyntax.TokenCBrace, - Bytes: []byte{'}'}, - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenNewline, - Bytes: []byte{'\n'}, - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenNewline, - Bytes: []byte{'\n'}, - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenIdent, - Bytes: []byte(`foo`), - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenOBrace, - Bytes: []byte{'{'}, - SpacesBefore: 1, - }, - { - Type: hclsyntax.TokenNewline, - Bytes: []byte{'\n'}, - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenCBrace, - Bytes: []byte{'}'}, - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenNewline, - Bytes: []byte{'\n'}, - SpacesBefore: 0, - }, - { - Type: hclsyntax.TokenEOF, - Bytes: []byte{}, - SpacesBefore: 0, - }, - }, - }, } for _, test := range tests { @@ -714,7 +646,7 @@ func TestBodyAppendBlock(t *testing.T) { t.Fatalf("unexpected diagnostics") } - f.Body().AppendBlock(test.blockType, test.labels, test.blank) + f.Body().AppendNewBlock(test.blockType, test.labels) got := f.BuildTokens(nil) format(got) if !reflect.DeepEqual(got, test.want) {