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.
language- TheLanguageobject associated with the querysource- A string containing one or more query patterns
QueryError if the query string is invalid
Example:
Properties
captureNames
The names of the captures used in the query.
captureQuantifiers
The quantifiers of the captures used in the query.
CaptureQuantifier.Zero(0) - Zero occurrencesCaptureQuantifier.ZeroOrOne(1) - Zero or one occurrenceCaptureQuantifier.ZeroOrMore(2) - Zero or more occurrencesCaptureQuantifier.One(3) - Exactly one occurrenceCaptureQuantifier.OneOrMore(4) - One or more occurrences
predicates
User-defined predicates associated with each pattern.
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.
node- The node to execute the query on (typically the root node)options(optional) - Options for query execution
QueryMatch objects
Example:
captures(node, options?)
Execute the query and return all individual captures in the order they appear.
node- The node to execute the query onoptions(optional) - Options for query execution
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
Bothmatches() and captures() accept an optional options parameter:
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.
Disabling Patterns and Captures
disablePattern(patternIndex)
Disable a pattern, preventing it from matching.
disableCapture(captureName)
Disable a capture, preventing it from being returned in results.
Match Limit
didExceedMatchLimit()
Check if the query exceeded its maximum number of in-progress matches on the last execution.
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.
QueryErrorKind.Syntax(1) - Syntax errorQueryErrorKind.NodeName(2) - Invalid node type nameQueryErrorKind.FieldName(3) - Invalid field nameQueryErrorKind.CaptureName(4) - Invalid capture nameQueryErrorKind.PatternStructure(5) - Invalid pattern structure
Query Language
Basic Patterns
Match nodes by type: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:Property Predicates
Assert a property:Quantifiers
Match multiple children:Examples
Find All Function Calls
Find Unused Variables
Extract Function Names and Bodies
Find JSX Components
See Also
- Query syntax documentation
- Node class - Working with captured nodes
- Tree class - Syntax tree operations