query
local query = require("@std/syntax/query")WARNING
These APIs are still open to future evolution. In new major versions, they may change in backwards incompatible ways.
Summary
| Entry | Description |
|---|---|
| filter | Retains only the nodes for which pred returns true. Mutates and returns the query. |
| findAll | Recursively visits every descendant of each node in the query and collects those for which fn returns a non-nil value. Mutates and returns the query. |
| findAllFromRoot | Creates a new Query by visiting every node in cst and collecting those for which fn returns a non-nil value. |
| flatMap | Replaces each node with the array of values returned by fn, flattening one level. Mutates and returns the query. |
| forEach | Calls callback on each node. Returns the query unchanged for chaining. |
| map | Replaces the query's nodes with the non-nil values returned by fn for each node. Mutates and returns the query. |
| mapToArray | Returns a plain array of the non-nil values produced by calling fn on each node. Does not mutate the query. |
| replace | Calls repl on each node and collects the non-nil results into a Replacements table for use with the printer. |
Functions and Properties
query.filter
Retains only the nodes for which pred returns true. Mutates and returns the query.
(self: Query<T>, pred: (T) -> boolean) -> Query<T>query.findAll
Recursively visits every descendant of each node in the query and collects those for which fn returns a non-nil value. Mutates and returns the query.
(self: Query<T>, fn: (Node) -> U?) -> Query<U>query.findAllFromRoot
Creates a new Query by visiting every node in cst and collecting those for which fn returns a non-nil value.
(cst: types.ParseResult | Node, fn: (Node) -> T?) -> Query<T>query.flatMap
Replaces each node with the array of values returned by fn, flattening one level. Mutates and returns the query.
(self: Query<T>, fn: (T) -> { U }) -> Query<U>query.forEach
Calls callback on each node. Returns the query unchanged for chaining.
(self: Query<T>, callback: (T) -> ()) -> Query<T>query.map
Replaces the query's nodes with the non-nil values returned by fn for each node. Mutates and returns the query.
(self: Query<T>, fn: (T) -> U?) -> Query<U>query.mapToArray
Returns a plain array of the non-nil values produced by calling fn on each node. Does not mutate the query.
(self: Query<T>, fn: (T) -> U?) -> { U }query.replace
Calls repl on each node and collects the non-nil results into a Replacements table for use with the printer.
(self: Query<T>, repl: (T) -> types.Replacement?) -> types.Replacements