diff --git a/R/assignment_linter.R b/R/assignment_linter.R index da42b5119..8d81ac014 100644 --- a/R/assignment_linter.R +++ b/R/assignment_linter.R @@ -7,6 +7,8 @@ #' @param allow_right_assign Logical, default `FALSE`. If `TRUE`, `->` and `->>` are allowed. #' @param allow_trailing Logical, default `TRUE`. If `FALSE` then assignments aren't allowed at end of lines. #' @param allow_pipe_assign Logical, default `FALSE`. If `TRUE`, magrittr's `%<>%` assignment is allowed. +#' @param allow_equal_assign Logical, default `FALSE`. +#' If `TRUE`, `=` instead of `<-` is used for assignment. #' #' @examples #' # will produce lints @@ -73,7 +75,8 @@ assignment_linter <- function(allow_cascading_assign = TRUE, allow_right_assign = FALSE, allow_trailing = TRUE, - allow_pipe_assign = FALSE) { + allow_pipe_assign = FALSE, + allow_equal_assign = FALSE) { trailing_assign_xpath <- paste( collapse = " | ", c( @@ -88,7 +91,7 @@ assignment_linter <- function(allow_cascading_assign = TRUE, xpath <- paste(collapse = " | ", c( # always block = (NB: the parser differentiates EQ_ASSIGN, EQ_SUB, and EQ_FORMALS) - "//EQ_ASSIGN", + if (allow_equal_assign) "//LEFT_ASSIGN[text() = '<-']" else "//EQ_ASSIGN", # -> and ->> are both 'RIGHT_ASSIGN' if (!allow_right_assign) "//RIGHT_ASSIGN" else if (!allow_cascading_assign) "//RIGHT_ASSIGN[text() = '->>']", # <-, :=, and <<- are all 'LEFT_ASSIGN'; check the text if blocking <<-. @@ -108,7 +111,12 @@ assignment_linter <- function(allow_cascading_assign = TRUE, } operator <- xml_text(bad_expr) - lint_message_fmt <- rep("Use <-, not %s, for assignment.", length(operator)) + lint_message_fmt <- rep( + paste0("Use ", + if (allow_equal_assign) "=" else "<-", + ", not %s, for assignment."), + length(operator) + ) lint_message_fmt[operator %in% c("<<-", "->>")] <- "Replace %s by assigning to a specific environment (with assign() or <-) to avoid hard-to-predict behavior." lint_message_fmt[operator == "%<>%"] <- diff --git a/man/assignment_linter.Rd b/man/assignment_linter.Rd index 291343fb2..592f3c990 100644 --- a/man/assignment_linter.Rd +++ b/man/assignment_linter.Rd @@ -8,7 +8,8 @@ assignment_linter( allow_cascading_assign = TRUE, allow_right_assign = FALSE, allow_trailing = TRUE, - allow_pipe_assign = FALSE + allow_pipe_assign = FALSE, + allow_equal_assign = FALSE ) } \arguments{ @@ -20,6 +21,9 @@ If \code{FALSE}, \code{\link[base:assignOps]{<<-}} and \verb{->>} are not allowe \item{allow_trailing}{Logical, default \code{TRUE}. If \code{FALSE} then assignments aren't allowed at end of lines.} \item{allow_pipe_assign}{Logical, default \code{FALSE}. If \code{TRUE}, magrittr's \verb{\%<>\%} assignment is allowed.} + +\item{allow_equal_assign}{Logical, default \code{FALSE}. +If \code{TRUE}, \code{=} instead of \verb{<-} is used for assignment.} } \description{ Check that \verb{<-} is always used for assignment. diff --git a/tests/testthat/test-assignment_linter.R b/tests/testthat/test-assignment_linter.R index bae8a048e..c18e298d6 100644 --- a/tests/testthat/test-assignment_linter.R +++ b/tests/testthat/test-assignment_linter.R @@ -192,3 +192,14 @@ test_that("multiple lints throw correct messages", { assignment_linter(allow_cascading_assign = FALSE) ) }) + +test_that("= instead of <- can be used for assignment", { + linter <- assignment_linter(allow_equal_assign = TRUE) + lint_msg <- rex::rex("Use =, not <-, for assignment.") + + expect_lint("blah = 1", NULL, linter) + expect_lint("blah <- 1", lint_msg, linter) + + # data.table's left assign := needs to be silent + expect_lint("dt[, x := 42]", NULL, linter) +})