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

Return an undefined Pos for an empty ObjectList #410

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Changes from 1 commit
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
Next Next commit
Return an undefined Pos when an object list's Items are empty
  • Loading branch information
sgmiller committed Oct 15, 2020
commit 4025ab0d88624f3fc0ce7e0c7a1c9de273a8acd0
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