This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Add few node helpers #363
Merged
Merged
Add few node helpers #363
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,7 +182,13 @@ func AsPosition(m nodes.Object) *Position { | |
} | ||
|
||
// PositionsOf returns a complete positions map for the given UAST node. | ||
func PositionsOf(m nodes.Object) Positions { | ||
// The function will return nil for non-object nodes like arrays and values. To get | ||
// positions for these nodes, PositionsOf should be called on their parent node. | ||
func PositionsOf(n nodes.Node) Positions { | ||
m, ok := n.(nodes.Object) | ||
if !ok { | ||
return nil | ||
} | ||
o, _ := m[KeyPos].(nodes.Object) | ||
if len(o) == 0 { | ||
return nil | ||
|
@@ -216,7 +222,12 @@ func RoleList(roles ...role.Role) nodes.Array { | |
} | ||
|
||
// RolesOf is a helper for getting node UAST roles (see KeyRoles). | ||
func RolesOf(m nodes.Object) role.Roles { | ||
// The function will returns nil roles array for non-object nodes like arrays and values. | ||
func RolesOf(n nodes.Node) role.Roles { | ||
m, ok := n.(nodes.Object) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As 185. Is there some reason for this relaxation more generally? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason to relax this requirement is that all XPath query and any manual node manipulations will usually return a |
||
if !ok { | ||
return nil | ||
} | ||
arr, ok := m[KeyRoles].(nodes.Array) | ||
if !ok || len(arr) == 0 { | ||
if tp := TypeOf(m); tp == "" || strings.HasPrefix(tp, NS+":") { | ||
|
@@ -234,15 +245,19 @@ func RolesOf(m nodes.Object) role.Roles { | |
} | ||
|
||
// TokenOf is a helper for getting node token (see KeyToken). | ||
func TokenOf(m nodes.Object) string { | ||
t := m[KeyToken] | ||
s, ok := t.(nodes.String) | ||
if ok { | ||
return string(s) | ||
} | ||
v, _ := t.(nodes.Value) | ||
if v != nil { | ||
return fmt.Sprint(v) | ||
// It returns an empty string if the node is not an object, or there is no token. | ||
func TokenOf(n nodes.Node) string { | ||
switch n := n.(type) { | ||
case nodes.String: | ||
return string(n) | ||
case nodes.Value: | ||
return fmt.Sprint(n) | ||
case nodes.Object: | ||
t := n[KeyToken] | ||
if t == nil { | ||
return "" | ||
} | ||
return TokenOf(t) | ||
} | ||
return "" | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the comment to document that this is empty for a non-Object. Previously it was clear from the type signature, but no longer—and intuitively it's not obvious that an array wouldn't also have positions (e.g., the start and end of its span). Not saying it should, but if I asked for the position of an arbitrary node I would be surprised that it should't work for an Array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.