Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't cache stylerignore sequences #609

Merged
merged 1 commit into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
)
})