Skip to content
Open
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Deprecations & breaking changes

* Six linters fully deprecated in the previous release are now removed: `consecutive_stopifnot_linter()`, `extraction_operator_linter()`, `no_tab_linter()`, `single_quotes_linter()`, `unnecessary_nested_if_linter()`, and `unneeded_concatenation_linter()`.
* `assignment_linter()` only allows `<-` by default, i.e., `<<-` is now excluded (consistent with the defaults for `undesirable_operator_linter()`; #2717, @MichaelChirico).
* Arguments `allow_cascading_assign=`, `allow_right_assign=`, and `allow_pipe_assign=` to `assignment_linter()` are now removed. Use `operator=` instead.
* Argument `interpret_glue` to `object_usage_linter()`, marked deprecated in the previous release, is now defunct. Use `interpret_extensions=` instead; see the 3.3.0-1 release notes and `?object_usage_linter` for more.

Expand Down
12 changes: 6 additions & 6 deletions R/assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#'
#' Check that the specified operator is used for assignment.
#'
#' @param operator Character vector of valid assignment operators. Defaults to allowing `<-` and `<<-`; other valid
#' options are `=`, `->`, `->>`, `%<>%`; use `"any"` to denote "allow all operators", in which case this linter only
#' considers `allow_trailing` for generating lints.
#' @param operator Character vector of valid assignment operators. Defaults to allowing only `<-`; other valid
#' options are `=`, `<<-`, `->`, `->>`, `%<>%`; use `"any"` to denote "allow all operators", in which case
# this linter only considers `allow_trailing` for generating lints.
#' @param allow_trailing Logical, default `TRUE`. If `FALSE` then assignments aren't allowed at end of lines.
#'
#' @examples
Expand All @@ -14,7 +14,7 @@
#' linters = assignment_linter()
#' )
#'
#' code_lines <- "1 -> x\n2 ->> y"
#' code_lines <- "1 -> x\n2 ->> y\nz <<- 3"
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
Expand All @@ -37,7 +37,7 @@
#' linters = assignment_linter()
#' )
#'
#' code_lines <- "x <- 1\ny <<- 2"
#' code_lines <- "x <- 1\ny <- 2\nz <- 3"
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
Expand Down Expand Up @@ -79,7 +79,7 @@
#' - <https://style.tidyverse.org/syntax.html#assignment-1>
#' - <https://style.tidyverse.org/pipes.html#assignment-2>
#' @export
assignment_linter <- function(operator = c("<-", "<<-"),
assignment_linter <- function(operator = "<-",
allow_trailing = TRUE) {
all_operators <- c("<-", "=", "->", "<<-", "->>", "%<>%")
if ("any" %in% operator) {
Expand Down
11 changes: 5 additions & 6 deletions man/assignment_linter.Rd

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

20 changes: 10 additions & 10 deletions tests/testthat/test-assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that("assignment_linter skips allowed usages", {

test_that("assignment_linter blocks disallowed usages", {
linter <- assignment_linter()
lint_msg <- rex::rex("Use one of <-, <<- for assignment, not =.")
lint_msg <- rex::rex("Use <- for assignment, not =.")

expect_lint("blah=1", lint_msg, linter)
expect_lint("blah = 1", lint_msg, linter)
Expand All @@ -32,16 +32,16 @@ test_that("arguments handle <<- and ->/->> correctly", {
linter_yes_right <- assignment_linter(operator = c("->", "->>"))
lint_msg_right <- rex::rex("Replace ->> by assigning to a specific environment")

expect_lint("1 -> blah", rex::rex("Use one of <-, <<- for assignment, not ->."), linter)
expect_lint("1 -> blah", rex::rex("Use <- for assignment, not ->."), linter)
expect_lint("1 ->> blah", lint_msg_right, assignment_linter(operator = "<-"))

# <<- is only blocked optionally
expect_no_lint("1 <<- blah", linter)
# <<- can be unblocked optionally
expect_lint(
"1 <<- blah",
rex::rex("Replace <<- by assigning to a specific environment"),
assignment_linter(operator = "<-")
linter
)
expect_no_lint("1 <<- blah", assignment_linter(operator = "<<-"))

# blocking -> can be disabled
expect_no_lint("1 -> blah", linter_yes_right)
Expand Down Expand Up @@ -85,10 +85,10 @@ test_that("arguments handle trailing assignment operators correctly", {

expect_lint(
trim_some("
x <<-
x <-
y
"),
rex::rex("<<- should not be trailing"),
rex::rex("<- should not be trailing"),
linter_no_trailing
)
expect_lint(
Expand Down Expand Up @@ -127,11 +127,11 @@ test_that("arguments handle trailing assignment operators correctly", {
arrange(-value) ->
is_long
")
expect_lint(pipe_right_string, rex::rex("Use one of <-, <<- for assignment, not ->"), linter_default)
expect_lint(pipe_right_string, rex::rex("Use <- for assignment, not ->"), linter_default)
expect_lint(
pipe_right_string,
list(
rex::rex("Use one of <-, <<- for assignment, not ->"),
rex::rex("Use <- for assignment, not ->"),
rex::rex("Assignment -> should not be trailing")
),
linter_no_trailing
Expand All @@ -150,7 +150,7 @@ test_that("arguments handle trailing assignment operators correctly", {
54
"),
list(
list(message = "Use one of <-, <<- for assignment, not =.", line_number = 1L, column_number = 6L),
list(message = "Use <- for assignment, not =.", line_number = 1L, column_number = 6L),
list(message = "Assignment = should not be trailing at the end of a line", line_number = 1L, column_number = 6L),
list(message = "Assignment <- should not be trailing at the end of a line", line_number = 3L, column_number = 6L)
),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-exclusions.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ test_that("next-line exclusion works", { # nofuzz
# NLN: line_length_linter.
x = 1
"),
rex::rex("Use one of <-, <<- for assignment, not =."),
rex::rex("Use <- for assignment, not =."),
list(linter, line_length_linter())
)

Expand All @@ -216,7 +216,7 @@ test_that("next-line exclusion works", { # nofuzz
x = 1 # NLN: assignment_linter.
x = 2
"),
list(rex::rex("Use one of <-, <<- for assignment, not =."), line_number = 1L),
list(rex::rex("Use <- for assignment, not =."), line_number = 1L),
linter
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# fuzzer disable: assignment
linter <- assignment_linter()
lint_msg <- "Use one of <-, <<- for assignment, not ="
lint_msg <- "Use <- for assignment, not ="

test_that("no checks", {
expect_success(expect_no_lint("a", linter))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-knitr_extended_formats.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("marginfigure engine from tufte package doesn't cause problems", {

expect_lint(
file = test_path("knitr_extended_formats", "tufte.Rmd"),
checks = list(rex::rex("Use one of <-, <<- for assignment, not =."), line_number = 11L),
checks = list(rex::rex("Use <- for assignment, not =."), line_number = 11L),
default_linters,
parse_settings = FALSE
)
Expand All @@ -16,7 +16,7 @@ test_that("engines from bookdown package cause no problems", {

expect_lint(
file = test_path("knitr_extended_formats", "bookdown.Rmd"),
checks = list(rex::rex("Use one of <-, <<- for assignment, not =."), line_number = 14L),
checks = list(rex::rex("Use <- for assignment, not =."), line_number = 14L),
default_linters,
parse_settings = FALSE
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-knitr_formats.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
regexes <- list(
assign = rex::rex("Use one of <-, <<- for assignment, not =."),
assign = rex::rex("Use <- for assignment, not =."),
local_var = rex::rex("local variable"),
quotes = rex::rex("Only use double-quotes."),
trailing = rex::rex("Remove trailing blank lines."),
Expand Down
Loading