You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First timer of pkg (which looks really neat) here. Without having looked into the details of the code myself, is the following statement correct:
The code produced by styler will always have the same AST ("parse tree") as the code going in?
If the above is correct, do styler functions validate this - something like all.equal(ast_out, ast_in) - before returning?
The reason why ask, is because when I get a big "hard-to-read" set of R code handed to me, I'd like to run it through a code formatter without having to worry that it might introduce bugs on top of what's already in the code. If I know ast_in and ast_out are guaranteed to be identical (formally - not just "yeah, they should be the same") then I don't have to worry about that part.
However, I wonder if there are other cases like this, or the assignment operators = / <- are unique in this sense. If the latter is the case, I guess I could set up a "styler" that preserves x = 1 as is and then I can validate the correctness. Only then, as a last step, I can do transformation of any x = 1 to x <- 1. I really would like to be able to do re-formatting at scale / automatically without having to worry about correctness.
Only then, as a last step, I can do transformation of any x = 1 to x <- 1.
Exactly. Styler validates if it can validate, which is the case if you set scope = "line_breaks". The corresponding source code is here and here.
Hence, I suggest you to:
Check initial code into git.
Get the brand new styler 1.0.1 from CRAN with some relevant enhancements you can find in the release notes.
First style with scope = "line_breaks" and hope you don't get the error mentioned above (to my best knowledge, no one ever reported that error, so I am confident styler does indeed not change the AST). Also, have a look at the other options of the style guide with ?tidyverse_style such as strict before you style.
Review and commit changes styler made to your code.
Style with scope = "tokens" and manually inspect the changes. Should not be that many I hope.
For reference: Early discussion of so-called roundtrip validation were brought up in #85 and later in #140. Roundtrip validation was introduced in mainly in #287, tests were added in #289.
For completness: Note that there are other transformations apart from = to <- that styler performs with scope = "tokens" that change the AST. Here a few examples:
Adding curly braces to if statements (if there are none) that are multi-line.
Put statements separated with ; on two lines.
In addition, for all levels of scope, comments that are not shebang, knitr chunk headers or roxygen comments are forced to start with at least one space, i.e
styler::style_text("#a", scope="line_breaks")
#> # a
Maybe that should not be the case for scope < "tokens". Note that this does not trigger a validation error because we compare two parsed objects (with styler:::expressions_are_identical()), which drops all comments.
Sorry for the delay and thanks for you detailed description and pointers; I finally got time to test what you describe and I can confirm it works. Below are my observations (in case someone else finds this later).
Yes, I think it would be useful to bring the "roundtrip validation" from in the documentation; it's an important concept. It may even deserve a tiny vignette by itself so it's more visible (e.g. on CRAN pages).
BTW, as seen below, I noticed that styler outputs:
Please review the changes carefully!>
when using scope = "tokens" but not when using scope = "line_breaks" - is that referring this problem where the AST cannot be guaranteed to be preserved?
BTW2, there is a need for a newline after that message.
Roundtrip validation supported by scope = "line_breaks"
I can confirm that using scope = "line_breaks" preserves the AST;
Roundtrip validation not possible with scope = "tokens"
Then, if I style with scope = "tokens" (one level up from "line_breaks" and the most invasive as described in ?tidyverse_style)
>res<-styler::style_file(pathname, scope="tokens")
Styling1files:cstruct1.R ℹ
────────────────────────────────────────
StatusCountLegend
✔ 0Fileunchanged.
ℹ 1Filechanged.
✖ 0Stylingthrewanerror.
────────────────────────────────────────
Pleasereviewthechangescarefully!>>expr2<- parse(file=pathname)
> all.equal(as.list(expr2), as.list(expr0), check.attributes=FALSE)
[1] "Component 2: target, current do not match when deparsed"
[2] "Component 9: target, current do not match when deparsed"
[3] "Component 10: target, current do not match when deparsed"...
[30] "Component 41: target, current do not match when deparsed"
is deliberately only printed for scope = "tokens" and only if the AST was changed. I think we'll add an note in the helpfiles. So I don't understand what your last code example is about? Is there a problem with the changed wholeSrcef?
My code examples are just for others stumbling upon this thread and for my future self - everything works as expected. The last piece using diffobj::diffDeparse() is just to show what those 30 diffs look like (i.e. = updated to <-) plus it's a neat way to view the diff from within R (as an alternative to diff / git diff). (I didn't look into the details how to diffDeparse() how to not compare srcref - if that was you question).
Ok. Well my question was just about the very last line. It seemed spurious that 1070L, and 1038L are flipped. But I concluded from your response that this is not of any relevance at this point, right?
You're saying you'd expect 1038L vs 1070L rather than 1070L vs 1038L? I used "after" vs "before" when diffing. The "tokens" output got more lines (1070) than the "line_breaks" output (1038) because of changes like:
Activity
HenrikBengtsson commentedon Mar 13, 2018
Hmm... maybe this cannot be guaranteed, e.g. since
x = 1is reformatted asx <- 1:we don't get the same AST:
However, I wonder if there are other cases like this, or the assignment operators
=/<-are unique in this sense. If the latter is the case, I guess I could set up a "styler" that preservesx = 1as is and then I can validate the correctness. Only then, as a last step, I can do transformation of anyx = 1tox <- 1. I really would like to be able to do re-formatting at scale / automatically without having to worry about correctness.lorenzwalthert commentedon Mar 13, 2018
Exactly. Styler validates if it can validate, which is the case if you set
scope = "line_breaks". The corresponding source code is here and here.Hence, I suggest you to:
scope = "line_breaks"and hope you don't get the error mentioned above (to my best knowledge, no one ever reported that error, so I am confident styler does indeed not change the AST). Also, have a look at the other options of the style guide with?tidyverse_stylesuch asstrictbefore you style.scope = "tokens"and manually inspect the changes. Should not be that many I hope.lorenzwalthert commentedon Mar 13, 2018
For reference: Early discussion of so-called roundtrip validation were brought up in #85 and later in #140. Roundtrip validation was introduced in mainly in #287, tests were added in #289.
lorenzwalthert commentedon Mar 13, 2018
For completness: Note that there are other transformations apart from
=to<-that styler performs withscope = "tokens"that change the AST. Here a few examples:;on two lines.In addition, for all levels of scope, comments that are not shebang, knitr chunk headers or roxygen comments are forced to start with at least one space, i.e
Created on 2018-03-13 by the reprex package (v0.2.0).
Maybe that should not be the case for
scope < "tokens". Note that this does not trigger a validation error because we compare two parsed objects (withstyler:::expressions_are_identical()), which drops all comments.lorenzwalthert commentedon Mar 23, 2018
Do you @HenrikBengtsson think we should add a note in the documentation about when a verification can be expected?
HenrikBengtsson commentedon Mar 25, 2018
Sorry for the delay and thanks for you detailed description and pointers; I finally got time to test what you describe and I can confirm it works. Below are my observations (in case someone else finds this later).
Yes, I think it would be useful to bring the "roundtrip validation" from in the documentation; it's an important concept. It may even deserve a tiny vignette by itself so it's more visible (e.g. on CRAN pages).
BTW, as seen below, I noticed that styler outputs:
when using
scope = "tokens"but not when usingscope = "line_breaks"- is that referring this problem where the AST cannot be guaranteed to be preserved?BTW2, there is a need for a newline after that message.
Roundtrip validation supported by
scope = "line_breaks"I can confirm that using
scope = "line_breaks"preserves the AST;Roundtrip validation not possible with
scope = "tokens"Then, if I style with
scope = "tokens"(one level up from "line_breaks" and the most invasive as described in?tidyverse_style)and
lorenzwalthert commentedon Mar 25, 2018
The message
is deliberately only printed for
scope = "tokens"and only if the AST was changed. I think we'll add an note in the helpfiles. So I don't understand what your last code example is about? Is there a problem with the changed wholeSrcef?HenrikBengtsson commentedon Mar 25, 2018
Thanks for confirming.
My code examples are just for others stumbling upon this thread and for my future self - everything works as expected. The last piece using
diffobj::diffDeparse()is just to show what those 30 diffs look like (i.e.=updated to<-) plus it's a neat way to view the diff from within R (as an alternative todiff/git diff). (I didn't look into the details how todiffDeparse()how to not comparesrcref- if that was you question).lorenzwalthert commentedon Mar 25, 2018
Ok. Well my question was just about the very last line. It seemed spurious that
1070L, and1038Lare flipped. But I concluded from your response that this is not of any relevance at this point, right?HenrikBengtsson commentedon Mar 25, 2018
You're saying you'd expect
1038Lvs1070Lrather than1070Lvs1038L? I used "after" vs "before" when diffing. The "tokens" output got more lines (1070) than the "line_breaks" output (1038) because of changes like:to
Notes on guarantee of correctness. Closes r-lib#368.
Notes on guarantee of correctness. Closes r-lib#368.
CLEANUP: styler::style_text(code, scope = "line_breaks") [#1]
CLEANUP: styler::style_text(code, scope = "tokens") [#1]
Notes on guarantee of correctness. Closes r-lib#368.
STYLE: Prettified code using the styler package