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 6 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.

77 changes: 77 additions & 0 deletions tests/testthat/test-assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,80 @@ test_that("multiple lints throw correct messages", {
assignment_linter(allow_cascading_assign = FALSE)
)
})

# TODO
# test multiple lints (as in above)
test_that("= can be used for top-level assignment in addition to <-", {
linter <- assignment_linter(allow_explicit_equal_assign = "allowed")

expect_lint("blah = 1", NULL, linter)
expect_lint("blah <- 1", NULL, linter)

# implicit <- assignmnet is unaffected
expect_lint("if (x <- 1L) TRUE", NULL, linter)
expect_lint("while (x <- 0L) FALSE", NULL, linter)
expect_lint("for (x in y <- 1:10) print(x)", NULL, linter)

# data.table's left assign := needs to be silent
expect_lint("dt[, x := 42]", NULL, linter)
})


test_that("multiple lints throw correct messages when both = and <- is allowed", {
expect_lint(
trim_some("{
x <<- 1
y ->> 2
z -> 3
x %<>% as.character()
foo <- 1
bar = 2
}"),
list(
list(message = "Replace <<- by assigning to a specific environment", line_number = 2L),
list(message = "Replace ->> by assigning to a specific environment", line_number = 3L),
list(message = "Use <-, not ->", line_number = 4L),
list(message = "Avoid the assignment pipe %<>%", line_number = 5L)
),
assignment_linter(allow_cascading_assign = FALSE, allow_explicit_equal_assign = "allowed")
)
})


test_that("= must be used for top-level assignment instead of <-", {
linter <- assignment_linter(allow_explicit_equal_assign = "required")
lint_msg <- rex::rex("Use =, not <-, for top-level assignment.")

expect_lint("blah = 1", NULL, linter)
expect_lint("blah <- 1", lint_msg, linter)

# implicit <- assignmnet is unaffected
expect_lint("if (x <- 1L) TRUE", NULL, linter)
expect_lint("while (x <- 0L) FALSE", NULL, linter)
expect_lint("for (x in y <- 1:10) print(x)", NULL, linter)

# data.table's left assign := needs to be silent
expect_lint("dt[, x := 42]", NULL, linter)
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
})


test_that("multiple lints throw correct messages when = is required", {
expect_lint(
trim_some("{
x <<- 1
y ->> 2
z -> 3
x %<>% as.character()
foo <- 1
bar = 2
}"),
list(
list(message = "Replace <<- by assigning to a specific environment", line_number = 2L),
list(message = "Replace ->> by assigning to a specific environment", line_number = 3L),
list(message = "Use <-, not ->", line_number = 4L),
list(message = "Avoid the assignment pipe %<>%", line_number = 5L),
list(message = "Use =, not <-, for top-level assignment.", line_number = 6L)
),
assignment_linter(allow_cascading_assign = FALSE, allow_explicit_equal_assign = "required")
)
})
Loading