Skip to content

Commit 5979267

Browse files
Merge pull request #287 from lorenzwalthert/roundtrip
- Add roundtrip (#287).
2 parents e00f0a7 + 11c0ac5 commit 5979267

File tree

4 files changed

+108
-4
lines changed

4 files changed

+108
-4
lines changed

R/transform-files.R

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,23 @@ parse_transform_serialize <- function(text, transformers) {
9494
return("")
9595
}
9696
transformed_pd <- apply_transformers(pd_nested, transformers)
97-
# TODO verify_roundtrip
9897
flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>%
9998
enrich_terminals(transformers$use_raw_indention) %>%
10099
apply_ref_indention() %>%
101100
set_regex_indention(
102101
pattern = transformers$reindention$regex_pattern,
103102
target_indention = transformers$reindention$indention,
104-
comments_only = transformers$reindention$comments_only)
105-
103+
comments_only = transformers$reindention$comments_only
104+
)
106105
serialized_transformed_text <-
107106
serialize_parse_data_flattened(flattened_pd, start_line = start_line)
107+
108+
if (can_verify_roundtrip(transformers)) {
109+
verify_roundtrip(text, serialized_transformed_text)
110+
}
108111
serialized_transformed_text
109112
}
110113

111-
112114
#' Apply transformers to a parse table
113115
#'
114116
#' The column `multi_line` is updated (after the line break information is
@@ -154,5 +156,47 @@ apply_transformers <- function(pd_nested, transformers) {
154156
outer_indention_refs = NA
155157
)
156158
transformed_absolute_indent
159+
}
160+
161+
157162

163+
#' Check whether a roundtip verification can be carried out
164+
#'
165+
#' If scope was set to "line_breaks" or lower (compare [tidyverse_style()]),
166+
#' we can compare the expression before and after styling and return an error if
167+
#' it is not the same.
168+
#' @param transformers The list of transformer functions used for styling.
169+
#' Needed for reverse engineering the scope.
170+
can_verify_roundtrip <- function(transformers) {
171+
is.null(transformers$token)
172+
}
173+
174+
#' Verify the styling
175+
#'
176+
#' If scope was set to "line_breaks" or lower (compare [tidyverse_style()]),
177+
#' we can compare the expression before and after styling and return an error if
178+
#' it is not the same. Note that this method ignores comments and no
179+
#' verification can be conducted if scope > "line_breaks".
180+
#' @param old_text The initial expression in its character representation.
181+
#' @param new_text The styled expression in its character representation.
182+
#' @examples
183+
#' styler:::verify_roundtrip("a+1", "a + 1")
184+
#' styler:::verify_roundtrip("a+1", "a + 1 # comments are dropped")
185+
#' \dontrun{
186+
#' styler:::verify_roundtrip("a+1", "b - 3")
187+
#' }
188+
verify_roundtrip <- function(old_text, new_text) {
189+
expressions_are_identical <- identical(
190+
parse(text = old_text, keep.source = FALSE),
191+
parse(text = new_text, keep.source = FALSE)
192+
)
193+
if (!expressions_are_identical) {
194+
msg <- paste(
195+
"The expression evaluated before the styling is not the same as the",
196+
"expression after styling. This should not happen. Please file a",
197+
"bug report on GitHub (https://github.com/r-lib/styler/issues)",
198+
"using a reprex."
199+
)
200+
stop(msg, call. = FALSE)
201+
}
158202
}

man/can_verify_roundtrip.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/verify_roundtrip.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-roundtrip.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
context("roundtrip works")
2+
3+
4+
test_that("can_verify_roundtrip works", {
5+
expect_true(can_verify_roundtrip(tidyverse_style(scope = "line_breaks")))
6+
expect_true(can_verify_roundtrip(tidyverse_style(scope = "spaces")))
7+
expect_true(can_verify_roundtrip(tidyverse_style(scope = "indention")))
8+
expect_false(can_verify_roundtrip(tidyverse_style(scope = "tokens")))
9+
})
10+
11+
test_that("correct styling does not give an error", {
12+
expect_error(verify_roundtrip("1+1", "1 + 1"), NA)
13+
})
14+
15+
test_that("corrupt styling does give an error", {
16+
expect_error(verify_roundtrip("1-1", "1 + 1"), "bug")
17+
})

0 commit comments

Comments
 (0)