diff --git a/hclsyntax/structure.go b/hclsyntax/structure.go index ff272631..a0b9b21f 100644 --- a/hclsyntax/structure.go +++ b/hclsyntax/structure.go @@ -249,14 +249,17 @@ func (b *Body) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { attrs := make(hcl.Attributes) var diags hcl.Diagnostics - if len(b.Blocks) > 0 { - example := b.Blocks[0] + for _, block := range b.Blocks { + if _, hidden := b.hiddenBlocks[block.Type]; hidden { + continue + } diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, - Summary: fmt.Sprintf("Unexpected %q block", example.Type), + Summary: fmt.Sprintf("Unexpected %q block", block.Type), Detail: "Blocks are not allowed here.", - Subject: &example.TypeRange, + Subject: &block.TypeRange, }) + break // we will continue processing anyway, and return the attributes // we are able to find so that certain analyses can still be done // in the face of errors. diff --git a/hclsyntax/structure_test.go b/hclsyntax/structure_test.go index c86dc328..25f30171 100644 --- a/hclsyntax/structure_test.go +++ b/hclsyntax/structure_test.go @@ -488,6 +488,33 @@ func TestBodyJustAttributes(t *testing.T) { }, 1, // blocks are not allowed here }, + { + &Body{ + Attributes: Attributes{ + "foo": &Attribute{ + Name: "foo", + Expr: &LiteralValueExpr{ + Val: cty.StringVal("bar"), + }, + }, + }, + Blocks: Blocks{ + { + Type: "foo", + }, + }, + hiddenBlocks: map[string]struct{}{"foo": {}}, + }, + hcl.Attributes{ + "foo": &hcl.Attribute{ + Name: "foo", + Expr: &LiteralValueExpr{ + Val: cty.StringVal("bar"), + }, + }, + }, + 0, // hidden blocks are ignored, so no error + }, { &Body{ Attributes: Attributes{