Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Imports:
utils
Suggests:
cli (>= 3.4.0),
data.table (>= 1.16.2),
knitr (>= 1.42),
rmarkdown (>= 2.23),
shiny (>= 1.6.0),
Expand All @@ -47,7 +48,7 @@ RdMacros:
lifecycle
Config/Needs/verdepcheck: mllg/checkmate, r-lib/lifecycle, r-lib/rlang,
r-lib/cli, yihui/knitr, rstudio/rmarkdown, rstudio/shiny,
r-lib/testthat, r-lib/withr
r-lib/testthat, r-lib/withr, Rdatatable/data.table
Config/Needs/website: insightsengineering/nesttemplate
Encoding: UTF-8
Language: en-US
Expand Down
17 changes: 17 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 @@ -148,6 +149,22 @@ fix_shifted_comments <- function(calls) {
#' which is \code{`<-`(y,x)} instead of traditional `y <- x`.
#' @keywords internal
#' @noRd
remove_dt_assign <- function(calls) {
checkmate::assert_list(calls)
lapply(calls, function(call) {
dt_assign <-
which(call$token == "LEFT_ASSIGN" & call$text == ":=")
if (length(dt_assign) > 0) {
call[-dt_assign, ]
} else {
call
}
})
}

#' Fixes edge case of `:=` assignment operator being treated as assignemnt.
#' @keywords internal
#' @noRd
fix_arrows <- function(calls) {
checkmate::assert_list(calls)
lapply(calls, function(call) {
Expand Down
10 changes: 10 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,16 @@ 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", {
testthat::skip_if_not_installed("data.table")
code <- "
iris <- data.table::data.table(iris) %>%
.[, NewSpecies := factor(Species)]
"
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