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

Query (_ . (name) @firstChild) captures every name node #2869

Closed
RedCMD opened this issue Jan 19, 2024 · 3 comments
Closed

Query (_ . (name) @firstChild) captures every name node #2869

RedCMD opened this issue Jan 19, 2024 · 3 comments
Assignees
Labels
bug query Related to tree-sitter queries executor

Comments

@RedCMD
Copy link

RedCMD commented Jan 19, 2024

The query (_ . (_) @firstChild) will correctly capture the first child of every named node
returns: function_definition and the first identifier

(source_file
  (function_definition
    (identifier)
    (identifier)
    (identifier)))

however if you specify a node name inside of (_), the query will instead capture every single node of that name
regardless of if its the first named node or not
(_ . (identifier) @everyChild)
returns: function_definition and every single identifier

specifying the parent name does work correctly: (function_definition . (identifier) @firstChild)
returns: the first identifier

https://github.com/tree-sitter/tree-sitter/blob/master/docs/section-2-using-parsers.md#anchors
https://github.com/tree-sitter/tree-sitter/blob/master/docs/section-2-using-parsers.md#wildcard-node

@dundargoc dundargoc changed the title [bug] Query (_ . (name) @firstChild) captures every name node Query (_ . (name) @firstChild) captures every name node Feb 6, 2024
@dundargoc dundargoc added the bug label Feb 6, 2024
@amaanq
Copy link
Member

amaanq commented Mar 21, 2024

this isn't actionable without a grammar to go along with your query reproducer

@RedCMD
Copy link
Author

RedCMD commented Mar 23, 2024

go to https://tree-sitter.github.io/tree-sitter/playground
select JavaScript
paste code function name(one, two, three) { }
paste query (formal_parameters . (identifier) @firstChild)
notice how only the first identifier is highlighted ✅

change formal_parameters to _
(_ . (identifier) @firstChild)
notice how every single identifier is now highlighted ❌

@ObserverOfTime ObserverOfTime added the query Related to tree-sitter queries executor label Apr 12, 2024
quentinLeDilavrec added a commit to quentinLeDilavrec/tree-sitter that referenced this issue Jun 27, 2024
fixing tree-sitter#2869 by restricting pattern_map optimization in ts_query_new()
i.e. do not select a second step which is immediate
quentinLeDilavrec added a commit to quentinLeDilavrec/tree-sitter that referenced this issue Jun 27, 2024
fixing tree-sitter#2869 by restricting pattern_map optimization in ts_query_new()
i.e. do not select a second step which is immediate
quentinLeDilavrec added a commit to quentinLeDilavrec/tree-sitter that referenced this issue Jun 30, 2024
@amaanq
Copy link
Member

amaanq commented Jul 5, 2024

Fixed by #3440

@amaanq amaanq closed this as completed Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug query Related to tree-sitter queries executor
Projects
None yet
Development

No branches or pull requests

4 participants
-