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

fix NAPI cursor types and expose cursor.depth #676

Merged

Conversation

OmarTawfik
Copy link
Collaborator

@OmarTawfik OmarTawfik commented Nov 27, 2023

  • expose cursor.depth API.
  • add cst.Node union type.
  • type check NAPI index.d.ts.
  • replace Cursor::find_*() APIs with go_to_*() alternatives.

Fixes #636

@OmarTawfik OmarTawfik requested a review from a team as a code owner November 27, 2023 23:36
Copy link

changeset-bot bot commented Nov 27, 2023

🦋 Changeset detected

Latest commit: 82b9683

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nomicfoundation/slang Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@OmarTawfik OmarTawfik force-pushed the fix-cursor-matching-api branch 2 times, most recently from 3d679ce to 428e9ce Compare December 1, 2023 12:01
@OmarTawfik OmarTawfik changed the title fix Cursor::find_matching() API fix NAPI cursor types and expose cursor.depth Dec 1, 2023
let mut pragmas = vec![];
while let Some(rule_node) = cursor.find_rule_with_kind(&[RuleKind::VersionPragmaExpression]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we adapt the old code to directly use the new while cursor.go_to_next_rule_with_kinds(..) followed by extract_pragma(cursor.node())?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is recursive, which for something like X || Y would match all three of X || Y, X, and Y. We need to skip children when found.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is DFS pre-order, I imagine the parent should be matched first and in the loop body we already call cursor.go_to_next_non_descendent(), which should skip the subtree elements (i.e. individual versions) in a subsequent while find_rule_with_kind loop call. Otherwise, how the previous code handled it? Or was it wrong/matched everything recursively?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would miss two consecutive versions like X Y which is also valid syntax. go_to_next_non_descendent() would go to Y, and then find_rule_with_kind() would skip it before we get to it.

Copy link
Contributor

@Xanewok Xanewok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a question but not blocking; thanks!

let mut pragmas = vec![];
while let Some(rule_node) = cursor.find_rule_with_kind(&[RuleKind::VersionPragmaExpression]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is DFS pre-order, I imagine the parent should be matched first and in the loop body we already call cursor.go_to_next_non_descendent(), which should skip the subtree elements (i.e. individual versions) in a subsequent while find_rule_with_kind loop call. Otherwise, how the previous code handled it? Or was it wrong/matched everything recursively?

@@ -344,7 +341,7 @@ impl Cursor {

if self.current.child_number > 0 {
if let Some(parent_path_element) = self.path.last() {
let new_child_number = self.current.child_number + 1;
let new_child_number = self.current.child_number - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, good catch! It'd be great to add some simple tests to catch stuff like this at some point.

}

true
self.go_to_first_child() || self.go_to_next_non_descendent()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was gonna mumble about it but on the second thought it really is a lot clearer now, good thinking!

@Xanewok
Copy link
Contributor

Xanewok commented Dec 2, 2023

It looks like CI needs fixing.

@Xanewok
Copy link
Contributor

Xanewok commented Dec 2, 2023

@OmarTawfik I've edited the OP to close the #636 when merged, hope you don't mind!

I think that further improvements not strictly related to exposing the existing Rust logic to TS (like tracking the line/column from #683) are probably better tracked in #334.

@OmarTawfik OmarTawfik added this pull request to the merge queue Dec 3, 2023
Merged via the queue into NomicFoundation:main with commit b496d36 Dec 3, 2023
1 check passed
@OmarTawfik OmarTawfik deleted the fix-cursor-matching-api branch December 3, 2023 13:59
@github-actions github-actions bot mentioned this pull request Dec 3, 2023
github-merge-queue bot pushed a commit that referenced this pull request Dec 14, 2023
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and publish to npm
yourself or [setup this action to publish
automatically](https://github.com/changesets/action#with-publishing). If
you're not ready to do a release yet, that's fine, whenever you add more
changesets to main, this PR will be updated.


# Releases
## @nomicfoundation/[email protected]

### Minor Changes

- [#699](#699)
[`ddfebfe9`](ddfebfe)
Thanks [@Xanewok](https://github.com/Xanewok)! - Remove `ProductionKind`
in favor of `RuleKind`

- [#699](#699)
[`ddfebfe9`](ddfebfe)
Thanks [@Xanewok](https://github.com/Xanewok)! - Allow parsing
individual precedence expressions, like `ShiftExpression`

- [#665](#665)
[`4b5f8b46`](4b5f8b4)
Thanks [@Xanewok](https://github.com/Xanewok)! - Remove the CST Visitor
API in favor of the Cursor API

- [#666](#666)
[`0434b68c`](0434b68)
Thanks [@Xanewok](https://github.com/Xanewok)! - Add `Node::unparse()`
that allows to reconstruct the source code from the CST node

- [#675](#675)
[`daea4b7f`](daea4b7)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - rename `Cursor`'s
`pathRuleNodes()` to `ancestors()` in the NodeJS API.

- [#676](#676)
[`b496d361`](b496d36)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - Fix NAPI `cursor`
types and expose `cursor.depth`.

### Patch Changes

- [#685](#685)
[`b5fca94a`](b5fca94)
Thanks [@Xanewok](https://github.com/Xanewok)! - `bytes` is now properly
recognized as a reserved word

- [#660](#660)
[`97028991`](9702899)
Thanks [@Xanewok](https://github.com/Xanewok)! - Drop List suffix from
collection grammar rule names

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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.

Expose Cursor in TypeScript
2 participants