Skip to content

Commit

Permalink
closes #3, #4
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Sep 25, 2024
1 parent 56c8a4f commit a69a320
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
9 changes: 8 additions & 1 deletion R/dot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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]]
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-dot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

})

0 comments on commit a69a320

Please sign in to comment.