-
Notifications
You must be signed in to change notification settings - Fork 73
Guarantee of correctness? #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hmm... maybe this cannot be guaranteed, e.g. since > styler::style_text("x = 1")
x <- 1 we don't get the same AST: > str(as.list(parse(text = "x = 1")[[1]]))
List of 3
$ : symbol =
$ : symbol x
$ : num 1
> str(as.list(parse(text = "x <- 1")[[1]]))
List of 3
$ : symbol <-
$ : symbol x
$ : num 1 However, I wonder if there are other cases like this, or the assignment operators |
Exactly. Styler validates if it can validate, which is the case if you set Hence, I suggest you to:
|
For completness: Note that there are other transformations apart from
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 Created on 2018-03-13 by the reprex package (v0.2.0). Maybe that should not be the case for |
Do you @HenrikBengtsson think we should add a note in the documentation about when a verification can be expected? |
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 BTW2, there is a need for a newline after that message. Roundtrip validation supported by
|
The message
is deliberately only printed for |
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 |
Ok. Well my question was just about the very last line. It seemed spurious that |
You're saying you'd expect if(rmoutlier){Po=fcorrect(pbin,Po)} to if (rmoutlier) {
Po <- fcorrect(pbin, Po)
} |
This is guaranteed to preserve the AST, i.e. identical to before and after (r-lib/styler#368). Validated using: stopifnot(styler:::expressions_are_identical(expr_after, expr_before))
ASTs are not identical before or after because '=' are replaced with '<-' (r-lib/styler#368) Validation: Visual inspection
> styler::style_file(c("GAM-perm-slice-principal-comp.R", "mFISH_HiC_3D_compare.R"), scope = "line_breaks") Styling 2 files: GAM-perm-slice-principal-comp.R ℹ mFISH_HiC_3D_compare.R ℹ ────────────────────────────────────── Status Count Legend ✔ 0 File unchanged. ℹ 2 File changed. ✖ 0 Styling threw an error. ────────────────────────────────────── This is guaranteed to give the exact same abstract syntax tree (AST), cf. r-lib/styler#368 (comment)
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 likeall.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
andast_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.Thanks
The text was updated successfully, but these errors were encountered: