Skip to main content
The Query class allows you to search for patterns in syntax trees using a powerful pattern matching language. Queries are written in a Scheme-like S-expression syntax.

Constructor

new Query(language, source)

Create a new query from a string containing one or more S-expression patterns.
Parameters:
  • language - The Language object associated with the query
  • source - A string containing one or more query patterns
Throws: QueryError if the query string is invalid Example:

Properties

captureNames

The names of the captures used in the query.
Example:

captureQuantifiers

The quantifiers of the captures used in the query.
Quantifier values:
  • CaptureQuantifier.Zero (0) - Zero occurrences
  • CaptureQuantifier.ZeroOrOne (1) - Zero or one occurrence
  • CaptureQuantifier.ZeroOrMore (2) - Zero or more occurrences
  • CaptureQuantifier.One (3) - Exactly one occurrence
  • CaptureQuantifier.OneOrMore (4) - One or more occurrences

predicates

User-defined predicates associated with each pattern.
This includes predicates other than the built-in ones (match?, eq?, any-of?, is?, is-not?, set!).

setProperties

Properties set via the #set! directive for each pattern.

assertedProperties

Properties asserted via the #is? directive for each pattern.

refutedProperties

Properties refuted via the #is-not? directive for each pattern.

Methods

matches(node, options?)

Execute the query and return all matches in the order they were found.
Parameters:
  • node - The node to execute the query on (typically the root node)
  • options (optional) - Options for query execution
Returns: An array of QueryMatch objects Example:

captures(node, options?)

Execute the query and return all individual captures in the order they appear.
Parameters:
  • node - The node to execute the query on
  • options (optional) - Options for query execution
Returns: An array of QueryCapture objects Example:
Use captures() when you want a single ordered sequence of all captures, regardless of which pattern matched. Use matches() when you care about which pattern matched and need to see all captures grouped by match.

Query Options

Both matches() and captures() accept an optional options parameter:
Example with range:
Example with timeout:

Pattern Information

patternCount()

Get the number of patterns in the query.

startIndexForPattern(patternIndex)

Get the byte offset where the given pattern starts in the query’s source.

endIndexForPattern(patternIndex)

Get the byte offset where the given pattern ends in the query’s source.

predicatesForPattern(patternIndex)

Get the predicates for a given pattern.

isPatternRooted(patternIndex)

Check if a given pattern has a single root node.

isPatternNonLocal(patternIndex)

Check if a given pattern is non-local (can match at any depth).

isPatternGuaranteedAtStep(byteIndex)

Check if a given step in a query is ‘definite’ (guaranteed to match once reached).

Capture Information

captureIndexForName(captureName)

Get the index for a given capture name.
Returns: The index, or -1 if not found Example:

Disabling Patterns and Captures

disablePattern(patternIndex)

Disable a pattern, preventing it from matching.
Example:

disableCapture(captureName)

Disable a capture, preventing it from being returned in results.
Example:

Match Limit

didExceedMatchLimit()

Check if the query exceeded its maximum number of in-progress matches on the last execution.
Example:

delete()

Delete the query and free its resources.

Types

QueryMatch

A match of a query to a set of nodes.

QueryCapture

A particular node that has been captured with a name.

QueryPredicate

A predicate that contains an operator and operands.

PredicateStep

A step in a predicate (either a capture or a string).

QueryError

Error thrown when parsing a query fails.
Error kinds:
  • QueryErrorKind.Syntax (1) - Syntax error
  • QueryErrorKind.NodeName (2) - Invalid node type name
  • QueryErrorKind.FieldName (3) - Invalid field name
  • QueryErrorKind.CaptureName (4) - Invalid capture name
  • QueryErrorKind.PatternStructure (5) - Invalid pattern structure

Query Language

Basic Patterns

Match nodes by type:
Match with fields:
Match nested structures:

Wildcards

Match any node:

Anonymous Nodes

Match literal tokens:

Alternation

Match multiple patterns:

Negation

Match nodes that are NOT a certain type:

Predicates

Text Predicates

Match against regex:
String equality:
Compare two captures:
Check against multiple values:

Property Predicates

Assert a property:
Refute a property:
Set a property:

Quantifiers

Match multiple children:

Examples

Find All Function Calls

Find Unused Variables

Extract Function Names and Bodies

Find JSX Components

See Also