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.
This commit is contained in:
Martin Atkins 2018-11-03 19:54:56 +00:00
parent 379d277e2b
commit fd915f557d
2 changed files with 16 additions and 87 deletions

View File

@ -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'},
},
})
}

View File

@ -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) {