Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code parser for := operator #234

Merged
merged 11 commits into from
Nov 27, 2024
11 changes: 11 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_dt_assign(calls)
fix_arrows(calls)
}

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

#' Fixes edge case of `:=` assignment operator being treated as assignment.
#' @keywords internal
#' @noRd
remove_dt_assign <- function(calls) {
m7pr marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_list(calls)
lapply(calls, function(call) {
call <- subset(call, !(token == "LEFT_ASSIGN" & text == ":="))
m7pr marked this conversation as resolved.
Show resolved Hide resolved
m7pr marked this conversation as resolved.
Show resolved Hide resolved
})
}

#' 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)
m7pr marked this conversation as resolved.
Show resolved Hide resolved
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
Loading