File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,7 @@ extract_calls <- function(pd) {
106106 calls <- Filter(function (call ) ! (nrow(call ) == 1 && call $ token == " ';'" ), calls )
107107 calls <- Filter(Negate(is.null ), calls )
108108 calls <- fix_shifted_comments(calls )
109+ calls <- remove_custom_assign(calls , c(" :=" ))
109110 fix_arrows(calls )
110111}
111112
@@ -144,6 +145,23 @@ fix_shifted_comments <- function(calls) {
144145 Filter(nrow , calls )
145146}
146147
148+ # ' Fixes edge case of custom assignments operator being treated as assignment.
149+ # '
150+ # ' @param exclude (`character`) custom assignment operators to be excluded
151+ # ' @keywords internal
152+ # ' @noRd
153+ remove_custom_assign <- function (calls , exclude = NULL ) {
154+ checkmate :: assert_list(calls )
155+ checkmate :: assert_character(exclude , null.ok = TRUE )
156+ lapply(calls , function (call ) {
157+ if (! is.null(exclude )) {
158+ call [! (call $ token == " LEFT_ASSIGN" & call $ text %in% exclude ), ]
159+ } else {
160+ call
161+ }
162+ })
163+ }
164+
147165# ' Fixes edge case of `<-` assignment operator being called as function,
148166# ' which is \code{`<-`(y,x)} instead of traditional `y <- x`.
149167# ' @keywords internal
Original file line number Diff line number Diff line change @@ -105,6 +105,15 @@ testthat::test_that("eval_code accepts calls containing only comments and empty
105105 testthat :: expect_identical(get_code(eval_code(qenv(), code )), code )
106106})
107107
108+ testthat :: test_that(" eval_code does not treat := as an assignment operator" , {
109+ code <- "
110+ x <- 'name'
111+ rlang::list2(!!x := 1)
112+ "
113+ q <- eval_code(qenv(), code )
114+ testthat :: expect_identical(get_code(q ), code )
115+ })
116+
108117# comments ----------
109118testthat :: test_that(" comments fall into proper calls" , {
110119 # If comment is on top, it gets moved to the first call.
You can’t perform that action at this time.
0 commit comments