@@ -94,21 +94,23 @@ parse_transform_serialize <- function(text, transformers) {
94
94
return (" " )
95
95
}
96
96
transformed_pd <- apply_transformers(pd_nested , transformers )
97
- # TODO verify_roundtrip
98
97
flattened_pd <- post_visit(transformed_pd , list (extract_terminals )) %> %
99
98
enrich_terminals(transformers $ use_raw_indention ) %> %
100
99
apply_ref_indention() %> %
101
100
set_regex_indention(
102
101
pattern = transformers $ reindention $ regex_pattern ,
103
102
target_indention = transformers $ reindention $ indention ,
104
- comments_only = transformers $ reindention $ comments_only )
105
-
103
+ comments_only = transformers $ reindention $ comments_only
104
+ )
106
105
serialized_transformed_text <-
107
106
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
+ }
108
111
serialized_transformed_text
109
112
}
110
113
111
-
112
114
# ' Apply transformers to a parse table
113
115
# '
114
116
# ' The column `multi_line` is updated (after the line break information is
@@ -154,5 +156,47 @@ apply_transformers <- function(pd_nested, transformers) {
154
156
outer_indention_refs = NA
155
157
)
156
158
transformed_absolute_indent
159
+ }
160
+
161
+
157
162
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
+ }
158
202
}
0 commit comments