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

(WIP) Allows the use of = instead of <- #2521

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions R/assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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 <<-.
Expand All @@ -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."),
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
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 == "%<>%"] <-
Expand Down
6 changes: 5 additions & 1 deletion man/assignment_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add one more test for multiple lints. See L178-194 just above for an idea -- we're trying to insure that the new logic doesn't interact poorly with other options.

})
Loading