From a69a320d5415ec59b808f39cd97252ae5a765246 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 24 Sep 2024 21:36:54 -0700 Subject: [PATCH] closes #3, #4 --- NEWS.md | 7 +++++++ R/dot.R | 9 ++++++++- tests/testthat/test-dot.R | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 913143b..bda5cba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,13 @@ # dotty 0.2.0 (UNRELEASED) +- Fixed an issue where invocations like `.[a, .., z] <- letters` would + also create a binding called `..`. (#4) + +- `dotty` now requires all arguments within a `.[]` invocation to be + either named or unnamed -- that is, `.[a = a, b] <- c(a = "a", b = "b")` + are now disallowed. This may be relaxed in a future release. (#3) + - `dotty::dotify()` is now exported for use by R packages which would like to use `dotty` internally. (#1) diff --git a/R/dot.R b/R/dot.R index 40c9347..df59820 100644 --- a/R/dot.R +++ b/R/dot.R @@ -77,7 +77,7 @@ dotty <- function(parts, value, envir) { # lhs and rhs parts, and apply on each side # split into left parts, right parts nlhs <- index - 1L - nrhs <- length(parts) - index + 1L + nrhs <- length(parts) - index # evaluate left variables dotty_impl( @@ -99,6 +99,13 @@ dotty <- function(parts, value, envir) { dotty_impl <- function(parts, value, envir) { + nms <- names(parts) + if (is.character(nms)) { + n <- sum(nms == "") + if (!n %in% c(0L, length(nms))) + stop("cannot mix named with unnamed arguments in `.[]` invocations") + } + for (i in seq_along(parts)) { part <- parts[[i]] diff --git a/tests/testthat/test-dot.R b/tests/testthat/test-dot.R index 92d0c9e..1c30531 100644 --- a/tests/testthat/test-dot.R +++ b/tests/testthat/test-dot.R @@ -101,3 +101,21 @@ test_that("dotify helps codetools understand dotty usages", { expect_true(grepl("cherry", messages)) }) + +test_that("dotty doesn't create a variable called '..'", { + + .[a, .., z] <- letters + expect_equal(a, "a") + expect_equal(z, "z") + expect_false(exists("..")) + +}) + +test_that("we require all arguments to be named or unnamed", { + + expect_error({ + data <- list(w = 1, x = 2, y = 3, z = 4) + .[apple = x, banana] <- data + }) + +})