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) {
106
106
calls <- Filter(function (call ) ! (nrow(call ) == 1 && call $ token == " ';'" ), calls )
107
107
calls <- Filter(Negate(is.null ), calls )
108
108
calls <- fix_shifted_comments(calls )
109
+ calls <- remove_custom_assign(calls , c(" :=" ))
109
110
fix_arrows(calls )
110
111
}
111
112
@@ -144,6 +145,23 @@ fix_shifted_comments <- function(calls) {
144
145
Filter(nrow , calls )
145
146
}
146
147
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
+
147
165
# ' Fixes edge case of `<-` assignment operator being called as function,
148
166
# ' which is \code{`<-`(y,x)} instead of traditional `y <- x`.
149
167
# ' @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
105
105
testthat :: expect_identical(get_code(eval_code(qenv(), code )), code )
106
106
})
107
107
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
+
108
117
# comments ----------
109
118
testthat :: test_that(" comments fall into proper calls" , {
110
119
# If comment is on top, it gets moved to the first call.
You can’t perform that action at this time.
0 commit comments