diff --git a/DESCRIPTION b/DESCRIPTION index 5e1cd3751..4aa682d9f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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), @@ -29,7 +30,6 @@ Imports: tibble (>= 1.4.2), tools, withr (>= 1.0.0), - xfun (>= 0.1) Suggests: data.tree (>= 0.1.6), digest, diff --git a/NEWS.md b/NEWS.md index ea1b17161..ef6ebddb9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/io.R b/R/io.R index 9caad1a67..0c588500e 100644 --- a/R/io.R +++ b/R/io.R @@ -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 } @@ -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) +} diff --git a/R/parse.R b/R/parse.R index 13a4788e6..a82c60637 100644 --- a/R/parse.R +++ b/R/parse.R @@ -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) @@ -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 #' diff --git a/R/roxygen-examples-find.R b/R/roxygen-examples-find.R index 6b0be661e..941414b14 100644 --- a/R/roxygen-examples-find.R +++ b/R/roxygen-examples-find.R @@ -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) } diff --git a/R/testing.R b/R/testing.R index 9754b9a16..1238b8e6f 100644 --- a/R/testing.R +++ b/R/testing.R @@ -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, diff --git a/R/transform-files.R b/R/transform-files.R index b8cf90f0c..619b134e5 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -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 ) diff --git a/R/ui-styling.R b/R/ui-styling.R index 9117fa3f0..bd035c336 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -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: @@ -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 diff --git a/R/zzz.R b/R/zzz.R index 427fc0083..1464cd2dd 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,5 +1,4 @@ .onLoad <- function(libname, pkgname) { - backports::import(pkgname, "trimws") op <- options() op.styler <- list( styler.addins_style_transformer = "styler::tidyverse_style()", @@ -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() { diff --git a/man/ensure_valid_pd.Rd b/man/ensure_valid_pd.Rd deleted file mode 100644 index c6e1cd8d9..000000000 --- a/man/ensure_valid_pd.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/parse.R -\name{ensure_valid_pd} -\alias{ensure_valid_pd} -\title{Ensure that the parse data is valid} -\usage{ -ensure_valid_pd(pd) -} -\arguments{ -\item{pd}{A parse table.} -} -\description{ -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. -} -\keyword{internal} diff --git a/man/style_file.Rd b/man/style_file.Rd index 3c58e917f..95fec034e 100644 --- a/man/style_file.Rd +++ b/man/style_file.Rd @@ -97,7 +97,7 @@ See section 'Warning' for a good strategy to apply styling safely. \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: @@ -110,7 +110,7 @@ style_file(file, scope = "indention", strict = TRUE) # 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) } \seealso{ diff --git a/man/write_utf8.Rd b/man/write_utf8.Rd new file mode 100644 index 000000000..9955e86e2 --- /dev/null +++ b/man/write_utf8.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/io.R +\name{write_utf8} +\alias{write_utf8} +\title{Drop-in replacement for \code{xfun::write_utf8()}} +\usage{ +write_utf8(text, con, ...) +} +\description{ +Drop-in replacement for \code{xfun::write_utf8()} +} +\keyword{internal} diff --git a/tests/testthat/test-parsing.R b/tests/testthat/test-parsing.R index 08d3d410b..73d0db1cf 100644 --- a/tests/testthat/test-parsing.R +++ b/tests/testthat/test-parsing.R @@ -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 @@ -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."