-
-
Notifications
You must be signed in to change notification settings - Fork 152
Add iterator for ASTNodeArray #8
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
Conversation
|
Great addition! FWIW, I think the most efficient way to implement this will be to use |
Also changing iterator implementation to use nextSibling and nextNamedSibling
|
@maxbrunsfeld, turns out it was a smaller change than I anticipated =]. Additionally, my testing thus far has been manual. Is adding JS tests on the roadmap for this? If so, which test framework (Just in case I make another change before tests are added)? |
maxbrunsfeld
left a comment
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.
Great! Thanks for doing the optimization. Left two minor comments.
tree-sitter.d.ts
Outdated
|
|
||
| export interface AstNodeArray extends ArrayLike<AstNode>, Iterable<AstNode> { | ||
| // Getters | ||
| isNamed: boolean; |
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.
I think we shouldn't document this property; let's just treat it as an implementation detail of node arrays.
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.
I thought that too, I was just concerned about someone seeing it later down the line and thinking "Hey, this isn't documented, must be a bug". If only JS had true private methods =]
| yield node; | ||
| while ((node = getNext(node))) { | ||
| yield node; | ||
| } |
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.
Even though it will mean more duplication, I'd prefer to have one conditional check at the beginning of the loop, rather than a closure with a conditional on each iteration. Something like:
if (this.isNamed) {
while ((node = node.nextNamedSibling)) {
yield node
}
} else {
while ((node = node.nextSibling)) {
yield node
}
}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.
I can do this, but I wanted to mention that the way it's currently implemented doesn't use a closure and the this.isNamed is only evaluated once. The only difference this change would make would be removing the stack frame of the lambda on lines 31/32.
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.
Ah, thanks for pointing that out; I misread your code.
Yeah, the test suite lives in If you're up for adding some tests for this new code in node_test.js after this PR is merged, that'd be 💯. |
No description provided.