Skip to content

Commit

Permalink
Merge pull request #611 from lorenzwalthert/issue-606
Browse files Browse the repository at this point in the history
- Don't modify token for edge cases styler ignore (#611).
  • Loading branch information
lorenzwalthert committed Feb 22, 2020
2 parents 8372af2 + d7cf178 commit 6e96dac
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 8 deletions.
5 changes: 3 additions & 2 deletions R/rules-other.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ wrap_multiline_curly <- function(pd, indent_by, space_after = 1, key_token) {
pd, which(pd$token == key_token)[1]
)
next_terminal <- next_terminal(pd[to_be_wrapped_expr_with_child, ])$text
requires_braces <- if_for_while_part_requires_braces(pd, key_token)
requires_braces <- if_for_while_part_requires_braces(pd, key_token) && !any(pd$stylerignore)
if (requires_braces | next_terminal == "return") {
closing_brace_ind <- which(pd$token == key_token)[1]
pd$spaces[closing_brace_ind] <- 1L
Expand All @@ -94,7 +94,8 @@ wrap_multiline_curly <- function(pd, indent_by, space_after = 1, key_token) {
wrap_else_multiline_curly <- function(pd, indent_by = 2, space_after = 0) {
if (contains_else_expr(pd) &&
pd_is_multi_line(pd) &&
contains_else_expr_that_needs_braces(pd)) {
contains_else_expr_that_needs_braces(pd) &&
!any(pd$stylerignore)) {
else_idx <- which(pd$token == "ELSE")
pd$spaces[else_idx] <- 1L
all_to_be_wrapped_ind <- seq2(else_idx + 1L, nrow(pd))
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/stylerignore/braces-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
x<- function()
3 # styler: off

x<- function() # styler: off
3


if (x) # styler: off
3 else
4

if (x) {
3
} else # styler: off
4

if (x)
3 else 4 # styler: off

while (x) # styler: off
"x"


while (x)
"x"# styler: off
81 changes: 81 additions & 0 deletions tests/testthat/stylerignore/braces-in_tree

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

25 changes: 25 additions & 0 deletions tests/testthat/stylerignore/braces-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
x <- function()
3 # styler: off

x<- function() # styler: off
3


if (x) # styler: off
3 else
4

if (x) {
3
} else # styler: off
4

if (x)
3 else 4 # styler: off

while (x) # styler: off
"x"


while (x)
"x"# styler: off
14 changes: 9 additions & 5 deletions tests/testthat/test-cache-high-level-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test_that("activated cache brings speedup on style_text() API on character vecto
text, text,
fun = style_text
)
expect_gt(n, 70)
expect_gt(n, 65)
})

test_that("activated cache brings speedup on style_text() API on character scalar", {
Expand All @@ -40,7 +40,7 @@ test_that("activated cache brings speedup on style_text() API on character scala
text2, text2,
fun = style_text
)
expect_gt(n, 70)
expect_gt(n, 65)
})


Expand All @@ -50,7 +50,7 @@ test_that("trailing line breaks are ignored for caching", {
n <- n_times_faster_with_cache(text1, text2, clear = "all but last")
expect_equal(cache_info()$n, 3)
skip_on_cran()
expect_gt(n, 70)
expect_gt(n, 65)
})

test_that("trailing line breaks are ignored for caching in one scalar", {
Expand All @@ -59,7 +59,7 @@ test_that("trailing line breaks are ignored for caching in one scalar", {
n <- n_times_faster_with_cache(text1, text2, clear = "all but last")
expect_equal(cache_info()$n, 3)
skip_on_cran()
expect_gt(n, 70)
expect_gt(n, 65)
})

test_that("trailing line breaks are ignored for caching in one scalar", {
Expand All @@ -71,7 +71,7 @@ test_that("trailing line breaks are ignored for caching in one scalar", {
n <- n_times_faster_with_cache(text1, text2, clear = "all but last")
expect_equal(cache_info()$n, 3)
skip_on_cran()
expect_gt(n, 70)
expect_gt(n, 65)
})

test_that("speedup higher when cached roxygen example code is multiple expressions", {
Expand Down Expand Up @@ -195,3 +195,7 @@ capture.output(test_that("partial caching of multiple expressions on one line wo
final_text <- c("mtcars %>%", " f() #")
expect_equal(as.character(style_text(final_text)), final_text)
}))

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
4 changes: 4 additions & 0 deletions tests/testthat/test-cache-low-level-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ test_that("Individual comment expressions are not cached", {
# expressions now
expect_equal(cache_info$n, 2)
})

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
6 changes: 5 additions & 1 deletion tests/testthat/test-cache-with-r-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test_that("top-level test: Caches top-level expressions efficiently on style_tex
partially_cached_benchmark["elapsed"] * 2.5,
not_cached_benchmark["elapsed"]
)
expect_lt(full_cached_benchmark["elapsed"] * 70, benchmark["elapsed"])
expect_lt(full_cached_benchmark["elapsed"] * 65, benchmark["elapsed"])
})


Expand Down Expand Up @@ -84,6 +84,10 @@ capture.output(test_that("cached expressions are displayed propperly", {
)
}))

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})


test_that("When expressions are cached, number of newlines between them are preserved", {
on.exit(clear_testthat_cache())
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-interaction-caching-comments.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ test_that("Correclty removes comments that are not top-level when making pd shal
text7
)
})

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
4 changes: 4 additions & 0 deletions tests/testthat/test-interaction-caching-stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,7 @@ test_that("indention preserved in stylerignore when caching activated", {
text6
)
})

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
6 changes: 6 additions & 0 deletions tests/testthat/test-stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,9 @@ test_that("token adding or removing works in stylerignore", {
transformer = style_text
), NA)
})

test_that("no token added or removed in complex case", {
expect_warning(test_collection("stylerignore", "braces",
transformer = style_text
), NA)
})

0 comments on commit 6e96dac

Please sign in to comment.