Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent hidden blocks from causing an error in hcl.Body.JustAttributes #646

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions hclsyntax/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions hclsyntax/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down