Skip to contents

node_replace() gives the replacement for a particular node. node_replace_all() does the same but for several nodes (e.g. the output of node_find_all()). The output of those functions can be passed to tree_rewrite() to rewrite the entire input code with those replacements.

Usage

node_replace(x, ...)

node_replace_all(x, ...)

Arguments

x

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

...

Named elements where the name is a rule ID and the value is a character string indicating the replacement to apply to nodes that match this rule. Meta-variables are accepted but the syntax is different: they must be wrapped in ~~, e.g "anyNA(~~VAR~~)".

Examples

src <- "
x <- c(1, 2, 3)
any(duplicated(x), na.rm = TRUE)
any(duplicated(x))
if (any(is.na(x))) {
  TRUE
}
any(is.na(y))"

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


### Only replace the first nodes found by each rule

nodes_to_replace <- root |>
  node_find(
    ast_rule(id = "any_na", pattern = "any(is.na($VAR))"),
    ast_rule(id = "any_dup", pattern = "any(duplicated($VAR))")
  )

nodes_to_replace |>
  node_replace(
    any_na = "anyNA(~~VAR~~)",
    any_dup = "anyDuplicated(~~VAR~~) > 0"
  )
#> [[1]]
#> [[1]][[1]]
#> [1] 73
#> 
#> [[1]][[2]]
#> [1] 86
#> 
#> [[1]][[3]]
#> [1] "anyNA(x)"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 50
#> 
#> [[2]][[2]]
#> [1] 68
#> 
#> [[2]][[3]]
#> [1] "anyDuplicated(x) > 0"
#> 
#> 
#> attr(,"class")
#> [1] "astgrep_replacement" "list"               

### Replace all nodes found by each rule

nodes_to_replace <- root |>
  node_find(
    ast_rule(id = "any_na", pattern = "any(is.na($VAR))"),
    ast_rule(id = "any_dup", pattern = "any(duplicated($VAR))")
  )

nodes_to_replace |>
  node_replace(
    any_na = "anyNA(~~VAR~~)",
    any_dup = "anyDuplicated(~~VAR~~) > 0"
  )
#> [[1]]
#> [[1]][[1]]
#> [1] 73
#> 
#> [[1]][[2]]
#> [1] 86
#> 
#> [[1]][[3]]
#> [1] "anyNA(x)"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 50
#> 
#> [[2]][[2]]
#> [1] 68
#> 
#> [[2]][[3]]
#> [1] "anyDuplicated(x) > 0"
#> 
#> 
#> attr(,"class")
#> [1] "astgrep_replacement" "list"