node-text: Extract the code corresponding to one or several nodes

node-textR Documentation

Extract the code corresponding to one or several nodes

Description

Those functions extract the code corresponding to the node(s):

  • node_text() applies on a single node, for example the output of node_get_match()

  • node_text_all() applies on a list of nodes, for example the output of node_get_multiple_matches()

Usage

node_text(x)

node_text_all(x)

Arguments

x

A node, either from tree_root() or from another ⁠node_*()⁠ function.

Value

A list with as many elements as there are in the input. Each element is a list itself with the text corresponding to the input.

Examples

src <- "x <- rnorm(100, mean = 2)
    any(duplicated(y))
    plot(mtcars)
    any(duplicated(x))"

root <- src |>
  tree_new() |>
  tree_root()

# node_text() must be applied on single nodes
root |>
  node_find(ast_rule(pattern = "plot($A)")) |>
  node_text()

# node_find_all() returns a list on nodes on which
# we can use node_text_all()
root |>
  node_find_all(ast_rule(pattern = "any(duplicated($A))")) |>
  node_text_all()

astgrepr documentation built on June 8, 2025, 11:05 a.m.