From a4fb90636774636e72caf69b421f0cd7227a8dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Moravec?= Date: Fri, 23 Feb 2024 13:37:48 +1300 Subject: [PATCH] Fix matched := when using 'allow_equal_assign' --- R/assignment_linter.R | 2 +- tests/testthat/test-assignment_linter.R | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/R/assignment_linter.R b/R/assignment_linter.R index 92d7836be..8d81ac014 100644 --- a/R/assignment_linter.R +++ b/R/assignment_linter.R @@ -91,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) - if (allow_equal_assign) "//LEFT_ASSIGN" else "//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 <<-. diff --git a/tests/testthat/test-assignment_linter.R b/tests/testthat/test-assignment_linter.R index 27a87d816..c18e298d6 100644 --- a/tests/testthat/test-assignment_linter.R +++ b/tests/testthat/test-assignment_linter.R @@ -199,4 +199,7 @@ test_that("= instead of <- can be used 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) })