Skip to content

Commit

Permalink
Merge pull request #609 from lorenzwalthert/issue-608
Browse files Browse the repository at this point in the history
- Don't cache stylerignore sequences (#609).
  • Loading branch information
lorenzwalthert committed Feb 22, 2020
2 parents 0797fc8 + d895999 commit a719528
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
14 changes: 12 additions & 2 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,27 @@ cache_is_activated <- function(cache_name = NULL) {
#' Cache text
#'
#' Splits `text` into expressions and adds these to the cache. Note that
#' comments are **not** cached because caching them is too expensive.
#' comments are **not** cached because caching them is too expensive. Also, we
#' must not cache stylerignore sequence, because we might see the same
#' expression that does not comply with the style guide outside a stylerignore
#' sequence and wrongly think we should leave it as is.
#' @param text A character vector with one or more expressions.
#' @param transformers The transformers.
#' @keywords internal
cache_by_expression <- function(text, transformers) {
expressions <- parse(text = text, keep.source = TRUE) %>%
utils::getParseData(includeText = TRUE)
expressions[expressions$parent == 0 & expressions$token != "COMMENT", "text"] %>%
if (env_current$any_stylerignore) {
expressions <- expressions %>%
add_stylerignore()
} else {
expressions$stylerignore <- rep(FALSE, length(expressions$text))
}
expressions[expressions$parent == 0 & expressions$token != "COMMENT" & !expressions$stylerignore, "text"] %>%
map(~ cache_write(.x, transformers = transformers))
}


cache_write <- function(text, transformers) {
R.cache::generateCache(
key = cache_make_key(text, transformers),
Expand Down
5 changes: 4 additions & 1 deletion man/cache_by_expression.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-interaction-caching-stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,23 @@ test_that("caching works for non-top-level expressions", {
)
})

test_that("does not cache stylerignore sequences", {
on.exit(clear_testthat_cache())
fresh_testthat_cache()
text <- c(
"1+1# styler: off"
)
style_text(text)
expect_false(
is_cached("1+1", tidyverse_style())
)
fresh_testthat_cache()
text <- c(
"# styler: off",
"1+1"
)
style_text(text)
expect_false(
is_cached("1+1", tidyverse_style())
)
})

0 comments on commit a719528

Please sign in to comment.