Skip to content

Commit

Permalink
Merge branch 'main' into test_brace_implicit_assign
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Dec 29, 2022
2 parents a9a83aa + 802db6f commit dbcdfae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/implicit_assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ implicit_assignment_linter <- function(except = c(
//SYMBOL_FUNCTION_CALL"
}

# The walrus operator `:=` is also `LEFT_ASSIGN`, but is not a relevant operator
# to be considered for the present linter.
assignments <- c(
"LEFT_ASSIGN", # e.g. mean(x <- 1:4)
"LEFT_ASSIGN[text() != ':=']", # e.g. mean(x <- 1:4)
"RIGHT_ASSIGN" # e.g. mean(1:4 -> x)
)

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-implicit_assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ test_that("implicit_assignment_linter skips allowed usages", {

expect_lint("x <- 1L", NULL, linter)
expect_lint("1L -> x", NULL, linter)
expect_lint("x <<- 1L", NULL, linter)
expect_lint("1L ->> x", NULL, linter)
expect_lint("y <- if (is.null(x)) z else x", NULL, linter)
expect_lint("for (x in 1:10) x <- x + 1", NULL, linter)

Expand Down Expand Up @@ -262,6 +264,8 @@ test_that("implicit_assignment_linter blocks disallowed usages in simple conditi

expect_lint("if (x <- 1L) TRUE", lint_message, linter)
expect_lint("if (1L -> x) TRUE", lint_message, linter)
expect_lint("if (x <<- 1L) TRUE", lint_message, linter)
expect_lint("if (1L ->> x) TRUE", lint_message, linter)
expect_lint("while (x <- 0L) FALSE", lint_message, linter)
expect_lint("while (0L -> x) FALSE", lint_message, linter)
expect_lint("for (x in y <- 1:10) print(x)", lint_message, linter)
Expand Down Expand Up @@ -327,3 +331,17 @@ test_that("implicit_assignment_linter blocks disallowed usages in function calls
linter
)
})

test_that("implicit_assignment_linter works as expected with pipes and walrus operator", {
linter <- implicit_assignment_linter()

expect_lint("data %>% mutate(a := b)", NULL, linter)
expect_lint("dt %>% .[, z := x + y]", NULL, linter)
expect_lint("data %<>% mutate(a := b)", NULL, linter)

expect_lint("DT[i, x := i]", NULL, linter)

skip_if_not_r_version("4.1.0")

expect_lint("data |> mutate(a := b)", NULL, linter)
})

0 comments on commit dbcdfae

Please sign in to comment.