Skip to content

Conversation

@Xapphire13
Copy link
Contributor

No description provided.

@Xapphire13 Xapphire13 changed the title Add iterator for ASTNodeArray WIP: Add iterator for ASTNodeArray Dec 28, 2017
@maxbrunsfeld
Copy link
Contributor

Great addition! FWIW, I think the most efficient way to implement this will be to use nextSibling repeatedly.

@Xapphire13 Xapphire13 changed the title WIP: Add iterator for ASTNodeArray Add iterator for ASTNodeArray Dec 28, 2017
Also changing iterator implementation to use
nextSibling and nextNamedSibling
@Xapphire13
Copy link
Contributor Author

Xapphire13 commented Dec 28, 2017

@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)?

Copy link
Contributor

@maxbrunsfeld maxbrunsfeld left a 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;
Copy link
Contributor

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.

Copy link
Contributor Author

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;
}
Copy link
Contributor

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
  }
}

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@maxbrunsfeld
Copy link
Contributor

maxbrunsfeld commented Dec 28, 2017

Additionally, my testing thus far has been manual. Is adding JS tests on the roadmap for this?

Yeah, the test suite lives in tree-sitter-cli. It's a bit odd, but I do all of the testing in that repo because that's the repo that allows you to create grammars.

If you're up for adding some tests for this new code in node_test.js after this PR is merged, that'd be 💯.

@maxbrunsfeld maxbrunsfeld merged commit c6c22e3 into tree-sitter:master Dec 28, 2017
@Xapphire13 Xapphire13 deleted the iterable branch December 28, 2017 20:19
verhovsky added a commit to verhovsky/node-tree-sitter that referenced this pull request Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants