R/mutator-boolean.R

Defines functions boolean_literal

Documented in boolean_literal

#' Mutate a boolean literal
#'
#' Replaces a boolean constant (`TRUE`, `FALSE`, `T`, or `F`) with another one.
#'
#' @param from The literal to be replaced. One of `"TRUE"`, `"FALSE"`, `"T"`, `"F"`.
#' @param to The literal to replace with.
#'
#' @examples
#' boolean_literal("TRUE", "FALSE")
#' boolean_literal("FALSE", "TRUE")
#' boolean_literal("T", "F")
#' boolean_literal("F", "T")
#'
#' @export
boolean_literal <- function(from, to) {
  allowed <- c("TRUE", "FALSE", "T", "F")
  checkmate::assert_choice(from, allowed)
  checkmate::assert_choice(to, allowed)
  # TRUE/FALSE are dedicated AST nodes; T/F are plain identifiers in tree-sitter-r
  query <- if (from %in% c("TRUE", "FALSE")) {
    node_type <- if (from == "TRUE") "true" else "false"
    sprintf("(%s) @value", node_type)
  } else {
    sprintf("(identifier) @value (#eq? @value \"%s\")", from)
  }
  Mutator$new(from = from, to = to, query = query)
}

Try the muttest package in your browser

Any scripts or data that you put into this service are public.

muttest documentation built on May 14, 2026, 5:10 p.m.