-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ts_node_parent
can still return wrong parent for a zero-width node
#3271
Comments
doesn't seem to pass anymore after your reverse iteration pr it seems like, correct me if I'm wrong though |
I added tree-sitter/cli/src/tests/node_test.rs Lines 287 to 291 in c19f23f
Updated test.patch: test.patchdiff --git a/cli/src/tests/node_test.rs b/cli/src/tests/node_test.rs
index f226a890..895868ca 100644
--- a/cli/src/tests/node_test.rs
+++ b/cli/src/tests/node_test.rs
@@ -269,7 +269,7 @@ fn test_node_parent_of_child_by_field_name() {
#[test]
fn test_parent_of_zero_width_node() {
- let code = "def dupa(foo):";
+ let code = "def dupa(foo)";
let mut parser = Parser::new();
parser.set_language(&get_language("python")).unwrap();
@@ -277,18 +277,20 @@ fn test_parent_of_zero_width_node() {
let tree = parser.parse(code, None).unwrap();
let root = tree.root_node();
let function_definition = root.child(0).unwrap();
- let block = function_definition.child(4).unwrap();
+ let parameters = function_definition.child(2).unwrap();
+ let block = function_definition.child(3).unwrap();
let block_parent = block.parent().unwrap();
assert_eq!(block.to_string(), "(block)");
- assert_eq!(block_parent.kind(), "function_definition");
- assert_eq!(block_parent.to_string(), "(function_definition name: (identifier) parameters: (parameters (identifier)) body: (block))");
+ assert_eq!(block_parent.kind(), "parameters");
+ assert_eq!(block_parent.to_string(), "(parameters (identifier))");
+ assert_eq!(function_definition.to_string(), "(function_definition name: (identifier) parameters: (parameters (identifier)) body: (block))");
assert_eq!(
root.child_containing_descendant(block).unwrap(),
function_definition
);
- assert_eq!(function_definition.child_containing_descendant(block), None);
+ assert_eq!(function_definition.child_containing_descendant(block).unwrap(), parameters);
}
#[test] |
thanks for updating |
Problem
Related to #3185.
Sorry if I misunderstood something in how tree-sitter works, but it looks like
ts_node_parent()
sometimes returns the previous sibling (or its descendant) of a zero-width node instead of its parent. This can happen when the previous sibling has children, since currentlyts_node_parent()
descends into the first node that has child nodes and can contain the given node:tree-sitter/lib/src/node.c
Line 523 in 4d8b031
I made a modified version of python grammar that could produce such a scenario (python.patch). It allows function definitions without ":" at the end, so that
parameters
node could be a sibling of a zero-widthbody
.Steps to reproduce
test.patch
python.patch
Put both files into the home directory and run this:
All tests pass.
Expected behavior
(modified)
test_parent_of_zero_width_node()
should fail sinceparameters
node is notbody
's immediate parent.Tree-sitter version (tree-sitter --version)
tree-sitter 0.22.2 (cbcb51b)
Operating system/version
Ubuntu 22.04.3 LTS (WSL 2)
The text was updated successfully, but these errors were encountered: