-
Notifications
You must be signed in to change notification settings - Fork 19
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
fix NAPI cursor
types and expose cursor.depth
#676
Conversation
🦋 Changeset detectedLatest commit: 82b9683 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
3d679ce
to
428e9ce
Compare
Cursor::find_matching()
APIcursor
types and expose cursor.depth
let mut pragmas = vec![]; | ||
while let Some(rule_node) = cursor.find_rule_with_kind(&[RuleKind::VersionPragmaExpression]) { |
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.
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())
?
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.
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.
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.
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?
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.
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.
65dfb5c
to
18164d5
Compare
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.
Left a question but not blocking; thanks!
let mut pragmas = vec![]; | ||
while let Some(rule_node) = cursor.find_rule_with_kind(&[RuleKind::VersionPragmaExpression]) { |
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.
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; |
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.
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() |
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 was gonna mumble about it but on the second thought it really is a lot clearer now, good thinking!
It looks like CI needs fixing. |
@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. |
18164d5
to
82b9683
Compare
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>
cursor.depth
API.cst.Node
union type.index.d.ts
.Cursor::find_*()
APIs withgo_to_*()
alternatives.Fixes #636