@@ -104,9 +104,11 @@ parse_transform_serialize <- function(text, transformers) {
104
104
)
105
105
serialized_transformed_text <-
106
106
serialize_parse_data_flattened(flattened_pd , start_line = start_line )
107
- verify_roundtrip(text , serialized_transformed_text , transformers )
108
- serialized_transformed_text
109
107
108
+ if (can_verify_roundtrip(transformers )) {
109
+ verify_roundtrip(text , serialized_transformed_text )
110
+ }
111
+ serialized_transformed_text
110
112
}
111
113
112
114
# ' Apply transformers to a parse table
@@ -157,40 +159,44 @@ apply_transformers <- function(pd_nested, transformers) {
157
159
}
158
160
159
161
160
- # ' Verify the styling if possible
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
161
175
# '
162
176
# ' If scope was set to "line_breaks" or lower (compare [tidyverse_style()]),
163
177
# ' we can compare the expression before and after styling and return an error if
164
178
# ' it is not the same. Note that this method ignores comments and no
165
179
# ' verification can be conducted if scope > "line_breaks".
166
180
# ' @param old_text The initial expression in its character representation.
167
181
# ' @param new_text The styled expression in its character representation.
168
- # ' @param transformers The list of transformer functions used for styling.
169
- # ' Needed for reverse engineering the scope.
170
182
# ' @examples
171
- # ' styler:::verify_roundtrip("a+1", "a + 1", tidyverse_style(scope = "line_breaks"))
172
- # ' styler:::verify_roundtrip(
173
- # ' "a+1",
174
- # ' "a + 1 # comments are dropped",
175
- # ' tidyverse_style(scope = "line_breaks")
176
- # ' )
183
+ # ' styler:::verify_roundtrip("a+1", "a + 1")
184
+ # ' styler:::verify_roundtrip("a+1", "a + 1 # comments are dropped")
177
185
# ' \dontrun{
178
- # ' styler:::verify_roundtrip("a+1", "b - 3", tidyverse_style(scope = "line_breaks") )
186
+ # ' styler:::verify_roundtrip("a+1", "b - 3")
179
187
# ' }
180
- verify_roundtrip <- function (old_text , new_text , transformers ) {
181
- if (is.null(transformers $ token )) {
182
- expressions_are_identical <- identical(
183
- parse(text = old_text , keep.source = FALSE ),
184
- parse(text = new_text , keep.source = FALSE )
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."
185
199
)
186
- if (! expressions_are_identical ) {
187
- msg <- paste(
188
- " The expression evaluated before the styling is not the same as the" ,
189
- " expression after styling. This should not happen. Please file a" ,
190
- " bug report on GitHub (https://github.com/r-lib/styler/issues)" ,
191
- " using a reprex."
192
- )
193
- stop(msg , call. = FALSE )
194
- }
200
+ stop(msg , call. = FALSE )
195
201
}
196
202
}
0 commit comments