Skip to content
Merged
14 changes: 14 additions & 0 deletions R/utils-get_code_dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ extract_calls <- function(pd) {
calls <- Filter(function(call) !(nrow(call) == 1 && call$token == "';'"), calls)
calls <- Filter(Negate(is.null), calls)
calls <- fix_shifted_comments(calls)
calls <- remove_custom_assign(calls)
fix_arrows(calls)
}

Expand Down Expand Up @@ -144,6 +145,19 @@ fix_shifted_comments <- function(calls) {
Filter(nrow, calls)
}

#' Fixes edge case of custom assignments operator being treated as assignment.
#'
#' @param exclude (`character`) custom assignment operators to be excluded
#' @keywords internal
#' @noRd
remove_custom_assign <- function(calls, exclude = c(":=")) {
checkmate::assert_list(calls)
checkmate::assert_character(exclude)
lapply(calls, function(call) {
call[!(call$token == "LEFT_ASSIGN" & call$text %in% exclude), ]
})
}

#' Fixes edge case of `<-` assignment operator being called as function,
#' which is \code{`<-`(y,x)} instead of traditional `y <- x`.
#' @keywords internal
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-qenv_eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ testthat::test_that("eval_code accepts calls containing only comments and empty
testthat::expect_identical(get_code(eval_code(qenv(), code)), code)
})

testthat::test_that("eval_code does not treat := as an assignment operator", {
code <- "
x <- 'name'
rlang::list2(!!x := 1)
"
q <- eval_code(qenv(), code)
testthat::expect_identical(get_code(q), code)
})

# comments ----------
testthat::test_that("comments fall into proper calls", {
# If comment is on top, it gets moved to the first call.
Expand Down