Skip to content

Commit

Permalink
Merge pull request #866 from lorenzwalthert/remove-deps
Browse files Browse the repository at this point in the history
- Cut-down on dependencies (#866).
  • Loading branch information
lorenzwalthert authored Nov 21, 2021
2 parents 4310668 + 73a0413 commit 9fa042a
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 71 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Description: Pretty-prints R code without changing the user's formatting
License: MIT + file LICENSE
URL: https://github.com/r-lib/styler, https://styler.r-lib.org
BugReports: https://github.com/r-lib/styler/issues
Depends:
R (>= 3.4.0)
Imports:
backports (>= 1.1.0),
cli (>= 1.1.0),
glue,
magrittr (>= 2.0.0),
Expand All @@ -29,7 +30,6 @@ Imports:
tibble (>= 1.4.2),
tools,
withr (>= 1.0.0),
xfun (>= 0.1)
Suggests:
data.tree (>= 0.1.6),
digest,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
(also used by {precommit}).
* rename default branch to main (#859).
* Fix argument name `filetype` in Example for `style_dir()` (#855).
* Bump minimal R requirement to 3.4 in line with the [tidyverse](https://www.tidyverse.org/blog/2019/04/r-version-support/), which
allowed to remove the dependency at {backports} and some exception handling.
* Remove dependency on {xfun} (#866).

# styler 1.6.2

Expand Down
9 changes: 8 additions & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ transform_utf8_one <- function(path, fun, dry) {
} else if (dry == "on") {
# don't do anything
} else if (dry == "off") {
xfun::write_utf8(new, path)
write_utf8(new, path)
} else {
# not implemented
}
Expand Down Expand Up @@ -114,3 +114,10 @@ read_utf8_bare <- function(con, warn = TRUE) {
invalid_utf8 <- function(x) {
which(!is.na(x) & is.na(iconv(x, "UTF-8", "UTF-8")))
}

#' Drop-in replacement for `xfun::write_utf8()`
#' @keywords internal
write_utf8 <- function(text, con, ...) {
withr::local_options(encoding = "native.enc")
writeLines(enc2utf8(text), con, ..., useBytes = TRUE)
}
25 changes: 0 additions & 25 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ add_id_and_short <- function(pd) {
#' @importFrom magrittr or
#' @keywords internal
ensure_correct_txt <- function(pd, text) {
ensure_valid_pd(pd)
is_problematic_text <- or(
is_insufficiently_parsed_string(pd),
is_insufficiently_parsed_number(pd)
Expand Down Expand Up @@ -173,30 +172,6 @@ ensure_correct_txt <- function(pd, text) {
arrange_pos_id()
}

#' Ensure that the parse data is valid
#'
#' Test whether all non-terminals have at least one child and throw an error
#' otherwise. As this is check is rather expensive, it is only
#' carried out for configurations we have good reasons to expect problems.
#' @param pd A parse table.
#' @importFrom rlang abort
#' @keywords internal
ensure_valid_pd <- function(pd) {
if (getRversion() < "3.2") {
non_terminals <- pd %>%
filter(terminal == FALSE)
valid_pd <- non_terminals$id %>%
map_lgl(~ .x %in% pd$parent) %>%
all()
if (!valid_pd) {
abort(paste(
"The parse data is not valid and the problem is most likely related",
"to the parser in base R. Please install R >= 3.2 and try again."
))
}
}
TRUE
}

#' Identify strings that were not fully parsed
#'
Expand Down
2 changes: 1 addition & 1 deletion R/roxygen-examples-find.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
}

identify_start_to_stop_of_roxygen_examples <- function(path) {
content <- xfun::read_utf8(path)
content <- read_utf8_bare(path)
identify_start_to_stop_of_roxygen_examples_from_text(content)
}

Expand Down
2 changes: 1 addition & 1 deletion R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ transform_and_check <- function(in_item, out_item,
write_tree = NA,
out_tree = "_tree", ...) {
write_tree <- set_arg_write_tree(write_tree)
read_in <- xfun::read_utf8(in_item)
read_in <- read_utf8_bare(in_item)
if (write_tree) {
create_tree(read_in) %>%
write.table(out_tree,
Expand Down
2 changes: 1 addition & 1 deletion R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ parse_transform_serialize_r <- function(text,
return("")
}
transformers <- transformers_drop(
if (getRversion() < 3.4) text else pd_nested$text[!pd_nested$is_cached],
pd_nested$text[!pd_nested$is_cached],
transformers
)

Expand Down
4 changes: 2 additions & 2 deletions R/ui-styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ prettify_any <- function(transformers,
#' @inheritSection style_pkg Round trip validation
#' @examples
#' file <- tempfile("styler", fileext = ".R")
#' xfun::write_utf8("1++1", file)
#' writeLines("1++1", file)
#'
#' # the following is identical (because of ... and defaults),
#' # but the first is most convenient:
Expand All @@ -325,7 +325,7 @@ prettify_any <- function(transformers,
#' # name levels explicitly to not style less invasive levels
#' style_file(file, scope = I(c("tokens", "spaces")), strict = TRUE)
#'
#' xfun::read_utf8(file)
#' readLines(file)
#' unlink(file)
#' @family stylers
#' @export
Expand Down
3 changes: 1 addition & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.onLoad <- function(libname, pkgname) {
backports::import(pkgname, "trimws")
op <- options()
op.styler <- list(
styler.addins_style_transformer = "styler::tidyverse_style()",
Expand Down Expand Up @@ -37,8 +36,8 @@ ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) {
"options(styler.cache_root = \"styler\")\n\nin your `.Rprofile`. This ",
"message will only be displayed once in a while.\n"
))
options(styler.cache_root = "styler")
}
options(styler.cache_root = "styler")
}

remove_old_cache_files <- function() {
Expand Down
17 changes: 0 additions & 17 deletions man/ensure_valid_pd.Rd

This file was deleted.

4 changes: 2 additions & 2 deletions man/style_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/write_utf8.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 2 additions & 17 deletions tests/testthat/test-parsing.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ test_that("repreated parsing solves wrong parent assignment", {
"R -q -e \"styler::cache_deactivate(); styler::style_file(\\\"", path_temp, "\\\")\""
)
calls_sys(sys_call, intern = FALSE, ignore.stdout = TRUE, ignore.stderr = TRUE)
ref <- xfun::read_utf8(testthat_file("parsing", "repeated_parsing-out.R"))
result <- xfun::read_utf8(path_temp)
ref <- read_utf8_bare(testthat_file("parsing", "repeated_parsing-out.R"))
result <- read_utf8_bare(path_temp)
expect_equal(ref, result)
unlink(dir)
})

test_that("long strings are parsed correctly", {
if (getRversion() < "3.2") skip("skip on R < 3.2 because of parsing problems")

expect_warning(
test_collection("parsing", "long_strings", transformer = style_text),
NA
Expand All @@ -47,20 +45,7 @@ test_that("0x number representation is preserved with(out) L", {
})


test_that("issues with parsing long strings on R 3.1 can be detected", {
if (getRversion() >= "3.2") {
skip("skip on R >= 3.2 because parsing probmes don't appear")
}
expect_error(
test_collection("parsing", "long_strings", transformer = style_text),
"install R .* 3.2"
)
})


test_that("CRLF EOLs fail with informative error", {
skip_if(getRversion() < "3.4")

expect_error(
style_text("glück <- 3\r\n glück + 1"),
"Please change the EOL character in your editor to Unix style and try again."
Expand Down

0 comments on commit 9fa042a

Please sign in to comment.