Skip to content

Commit 95d32c6

Browse files
Notes on guarantee of correctness. Closes r-lib#368.
1 parent bcfb36d commit 95d32c6

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

R/ui.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ NULL
2727
#' This function overwrites files (if styling results in a change of the
2828
#' code to be formatted). It is strongly suggested to only style files
2929
#' that are under version control or to create a backup copy.
30+
#'
31+
#' We suggest to first style with `scope < "tokens"` and inspect and commit
32+
#' changes, because these changes are guaranteed to leave the abstract syntax
33+
#' tree (AST) unchanged. See section 'Roundtrip Validation' for details.
34+
#'
35+
#' Then, we suggest to style with `scope = "tokens"` (if desired) and carefully
36+
#' inspect the changes to make sure the AST is not changed in an unexpected way
37+
#' that invalidates code.
38+
#' @section Roundtrip Validation:
39+
#' The following section describes when and how styling is guaranteed to
40+
#' yield correct code.
41+
#'
42+
#' If the style guide has `scope < "tokens"`, no tokens are changed and the
43+
#' abstract syntax tree (AST) should not change.
44+
#' Hence, it is possible to validate the styling by comparing whether the parsed
45+
#' expression before and after styling have the same AST.
46+
#' This comparison omits comments. styler compares
47+
#' error if the AST has changed through styling.
48+
#'
49+
#' Note that with `scope = "tokens"` such a comparison is not conducted because
50+
#' the AST might well change and such a change is intended. There is no way
51+
#' styler can validate styling, that is why we inform the user to carefully
52+
#' inspect the changes.
53+
#'
54+
#' See section 'Warning' for a good strategy to apply styling safely.
3055
#' @inheritSection transform_files Value
3156
#' @family stylers
3257
#' @examples
@@ -112,6 +137,7 @@ style_text <- function(text,
112137
#' @inheritParams style_pkg
113138
#' @inheritSection transform_files Value
114139
#' @inheritSection style_pkg Warning
140+
#' @inheritSection style_pkg Roundtrip Validation
115141
#' @family stylers
116142
#' @examples
117143
#' \dontrun{
@@ -154,6 +180,7 @@ prettify_any <- function(transformers, filetype, recursive, exclude_files) {
154180
#' @inheritParams style_pkg
155181
#' @inheritSection transform_files Value
156182
#' @inheritSection style_pkg Warning
183+
#' @inheritSection style_pkg Roundtrip Validation
157184
#' @examples
158185
#' # the following is identical but the former is more convenient:
159186
#' file <- tempfile("styler", fileext = ".R")

man/style_dir.Rd

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

man/style_file.Rd

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

man/style_pkg.Rd

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

0 commit comments

Comments
 (0)