Skip to content

Commit

Permalink
Merge pull request hashicorp#410 from hashicorp/fix-decode-empty-object
Browse files Browse the repository at this point in the history
Return an undefined Pos for an empty ObjectList
  • Loading branch information
sgmiller committed Oct 15, 2020
2 parents d580177 + f6b4bdc commit beb03ea
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions hcl/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (ObjectType) node() {}
func (LiteralType) node() {}
func (ListType) node() {}

var unknownPos token.Pos

// File represents a single HCL file
type File struct {
Node Node // usually a *ObjectList
Expand Down Expand Up @@ -108,7 +110,12 @@ func (o *ObjectList) Elem() *ObjectList {
}

func (o *ObjectList) Pos() token.Pos {
// always returns the uninitiliazed position
// If an Object has no members, it won't have a first item
// to use as position
if len(o.Items) == 0 {
return unknownPos
}
// Return the uninitialized position
return o.Items[0].Pos()
}

Expand All @@ -133,10 +140,10 @@ type ObjectItem struct {
}

func (o *ObjectItem) Pos() token.Pos {
// I'm not entirely sure what causes this, but removing this causes
// a test failure. We should investigate at some point.
// If a parsed object has no keys, there is no position
// for its first element.
if len(o.Keys) == 0 {
return token.Pos{}
return unknownPos
}

return o.Keys[0].Pos()
Expand Down

0 comments on commit beb03ea

Please sign in to comment.