Search code examples
abstract-syntax-treetreesitter

How to use `or` logic in tree-sitter query syntax?


How to use or logic in tree-sitter query?

I want to query out a class's properties and methods:

I tried to use |:

    (class_declaration 
      name: (type_identifier) @class-name
      body: (class_body
        ((public_field_definition 
            name: (property_identifier) @property-name)
        | (method_definition 
            name: (property_identifier) @method-name))))

enter image description here but this is not work.

and I checked all the query syntax document, didn't find how to implement it.


Solution

  • Use [ ... ], and then no |.

    In the docs you referenced, it's called "Alternations".

    body: (class_body
      [(public_field_definition 
          name: (property_identifier) @property-name)
       (method_definition 
          name: (property_identifier) @method-name)])