Skip to content

Commit

Permalink
Return an undefined Pos when an object list's Items are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sgmiller committed Oct 15, 2020
1 parent d580177 commit 4025ab0
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 4025ab0

Please sign in to comment.