From 3906bea73ef6a4cf8cd4b6456dfebd4bd179b5a5 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 12 Jun 2024 22:09:56 -0700 Subject: [PATCH 1/3] more adaptation to 4.0+ features --- NAMESPACE | 7 +------ R/absolute_path_linter.R | 4 +--- R/lintr-package.R | 7 +------ R/utils.R | 1 - R/zzz.R | 3 +-- man/absolute_path_linter.Rd | 5 +---- man/get_r_string.Rd | 3 +-- 7 files changed, 6 insertions(+), 24 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 767662c585..6b5c469379 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,11 +1,5 @@ # Generated by roxygen2: do not edit by hand - -if (getRversion() >= "4.0.0") { - importFrom(tools, R_user_dir) -} else { - importFrom(backports, R_user_dir) -} S3method("[",lints) S3method(as.data.frame,lints) S3method(format,lint) @@ -183,6 +177,7 @@ importFrom(rex,re_substitutes) importFrom(rex,regex) importFrom(rex,rex) importFrom(stats,na.omit) +importFrom(tools,R_user_dir) importFrom(utils,capture.output) importFrom(utils,getParseData) importFrom(utils,getTxtProgressBar) diff --git a/R/absolute_path_linter.R b/R/absolute_path_linter.R index a27ebc1ea4..38fffd40b5 100644 --- a/R/absolute_path_linter.R +++ b/R/absolute_path_linter.R @@ -8,9 +8,7 @@ #' * contain at least two path elements, with one having at least two characters and #' * contain only alphanumeric chars (including UTF-8), spaces, and win32-allowed punctuation #' -#' @examplesIf getRversion() >= "4.0" -#' # Following examples use raw character constant syntax introduced in R 4.0. -#' +#' @examples #' # will produce lints #' lint( #' text = 'R"--[/blah/file.txt]--"', diff --git a/R/lintr-package.R b/R/lintr-package.R index 27f55bee35..70f8d0d115 100644 --- a/R/lintr-package.R +++ b/R/lintr-package.R @@ -12,16 +12,11 @@ #' @importFrom glue glue glue_collapse #' @importFrom rex rex regex re_matches re_substitutes character_class #' @importFrom stats na.omit +#' @importFrom tools R_user_dir #' @importFrom utils capture.output getParseData getTxtProgressBar globalVariables head relist #' setTxtProgressBar tail txtProgressBar #' @importFrom xml2 as_list #' xml_attr xml_children xml_find_all xml_find_chr xml_find_lgl xml_find_num xml_find_first xml_name xml_text -#' @rawNamespace -#' if (getRversion() >= "4.0.0") { -#' importFrom(tools, R_user_dir) -#' } else { -#' importFrom(backports, R_user_dir) -#' } ## lintr namespace: end NULL diff --git a/R/utils.R b/R/utils.R index f6e7090283..4bbbaad479 100644 --- a/R/utils.R +++ b/R/utils.R @@ -220,7 +220,6 @@ platform_independent_sort <- function(x) x[platform_independent_order(x)] #' unlink(tmp) #' #' # more importantly, extract strings under R>=4 raw strings -#' @examplesIf getRversion() >= "4.0.0" #' tmp4.0 <- tempfile() #' writeLines("c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')", tmp4.0) #' expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content diff --git a/R/zzz.R b/R/zzz.R index 2915392974..b8d317d9fc 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -295,9 +295,8 @@ settings <- new.env(parent = emptyenv()) toset <- !(names(op_lintr) %in% names(op)) if (any(toset)) options(op_lintr[toset]) - # R>=4.0.0: deparse1 # R>=4.1.0: ...names - backports::import(pkgname, c("deparse1", "...names")) + backports::import(pkgname, "...names") utils::assignInMyNamespace("default_settings", list( linters = default_linters, diff --git a/man/absolute_path_linter.Rd b/man/absolute_path_linter.Rd index f8b40a312a..04fa1cf1a3 100644 --- a/man/absolute_path_linter.Rd +++ b/man/absolute_path_linter.Rd @@ -18,9 +18,6 @@ If \code{TRUE}, only lint path strings, which Check that no absolute paths are used (e.g. "/var", "C:\\System", "~/docs"). } \examples{ -\dontshow{if (getRversion() >= "4.0") (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -# Following examples use raw character constant syntax introduced in R 4.0. - # will produce lints lint( text = 'R"--[/blah/file.txt]--"', @@ -32,7 +29,7 @@ lint( text = 'R"(./blah)"', linters = absolute_path_linter() ) -\dontshow{\}) # examplesIf} + } \seealso{ \itemize{ diff --git a/man/get_r_string.Rd b/man/get_r_string.Rd index b1564c6017..1960d9b9f4 100644 --- a/man/get_r_string.Rd +++ b/man/get_r_string.Rd @@ -32,7 +32,6 @@ get_r_string(expr_as_xml, "expr[3]") unlink(tmp) # more importantly, extract strings under R>=4 raw strings -\dontshow{if (getRversion() >= "4.0.0") (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} tmp4.0 <- tempfile() writeLines("c(R'(a\\\\b)', R'--[a\\\\\"\'\"\\\\b]--')", tmp4.0) expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content @@ -40,5 +39,5 @@ writeLines(as.character(expr_as_xml4.0)) get_r_string(expr_as_xml4.0, "expr[2]") get_r_string(expr_as_xml4.0, "expr[3]") unlink(tmp4.0) -\dontshow{\}) # examplesIf} + } From 30a0597fc3ccc89405b2bc2a6a8bbc595d2f4c0d Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 12 Jun 2024 22:53:48 -0700 Subject: [PATCH 2/3] better future-proof docs --- R/utils.R | 23 +++++++++++------------ man/get_r_string.Rd | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/R/utils.R b/R/utils.R index 4bbbaad479..c3ed7f0f10 100644 --- a/R/utils.R +++ b/R/utils.R @@ -198,10 +198,9 @@ platform_independent_sort <- function(x) x[platform_independent_order(x)] #' Extract text from `STR_CONST` nodes #' #' Convert `STR_CONST` `text()` values into R strings. This is useful to account for arbitrary -#' character literals valid since R 4.0, e.g. `R"------[hello]------"`, which is parsed in -#' R as `"hello"`. It is quite cumbersome to write XPaths allowing for strings like this, -#' so whenever your linter logic requires testing a `STR_CONST` node's value, use this -#' function. +#' character literals, e.g. `R"------[hello]------"`, which is parsed in R as `"hello"`. +#' It is quite cumbersome to write XPaths allowing for strings like this, so whenever your +#' linter logic requires testing a `STR_CONST` node's value, use this function. #' NB: this is also properly vectorized on `s`, and accepts a variety of inputs. Empty inputs #' will become `NA` outputs, which helps ensure that `length(get_r_string(s)) == length(s)`. #' @@ -219,14 +218,14 @@ platform_independent_sort <- function(x) x[platform_independent_order(x)] #' get_r_string(expr_as_xml, "expr[3]") #' unlink(tmp) #' -#' # more importantly, extract strings under R>=4 raw strings -#' tmp4.0 <- tempfile() -#' writeLines("c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')", tmp4.0) -#' expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content -#' writeLines(as.character(expr_as_xml4.0)) -#' get_r_string(expr_as_xml4.0, "expr[2]") -#' get_r_string(expr_as_xml4.0, "expr[3]") -#' unlink(tmp4.0) +#' # more importantly, extract raw strings correctly +#' tmp_raw <- tempfile() +#' writeLines("c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')", tmp_raw) +#' expr_as_xml_raw <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content +#' writeLines(as.character(expr_as_xml_raw)) +#' get_r_string(expr_as_xml_raw, "expr[2]") +#' get_r_string(expr_as_xml_raw, "expr[3]") +#' unlink(tmp_raw) #' #' @export get_r_string <- function(s, xpath = NULL) { diff --git a/man/get_r_string.Rd b/man/get_r_string.Rd index 1960d9b9f4..9fe00ba696 100644 --- a/man/get_r_string.Rd +++ b/man/get_r_string.Rd @@ -15,10 +15,9 @@ and \code{xpath} is specified, it is extracted with \code{\link[xml2:xml_find_al } \description{ Convert \code{STR_CONST} \code{text()} values into R strings. This is useful to account for arbitrary -character literals valid since R 4.0, e.g. \code{R"------[hello]------"}, which is parsed in -R as \code{"hello"}. It is quite cumbersome to write XPaths allowing for strings like this, -so whenever your linter logic requires testing a \code{STR_CONST} node's value, use this -function. +character literals, e.g. \code{R"------[hello]------"}, which is parsed in R as \code{"hello"}. +It is quite cumbersome to write XPaths allowing for strings like this, so whenever your +linter logic requires testing a \code{STR_CONST} node's value, use this function. NB: this is also properly vectorized on \code{s}, and accepts a variety of inputs. Empty inputs will become \code{NA} outputs, which helps ensure that \code{length(get_r_string(s)) == length(s)}. } @@ -31,13 +30,13 @@ get_r_string(expr_as_xml, "expr[2]") get_r_string(expr_as_xml, "expr[3]") unlink(tmp) -# more importantly, extract strings under R>=4 raw strings -tmp4.0 <- tempfile() -writeLines("c(R'(a\\\\b)', R'--[a\\\\\"\'\"\\\\b]--')", tmp4.0) -expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content -writeLines(as.character(expr_as_xml4.0)) -get_r_string(expr_as_xml4.0, "expr[2]") -get_r_string(expr_as_xml4.0, "expr[3]") -unlink(tmp4.0) +# more importantly, extract raw strings correctly +tmp_raw <- tempfile() +writeLines("c(R'(a\\\\b)', R'--[a\\\\\"\'\"\\\\b]--')", tmp_raw) +expr_as_xml_raw <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content +writeLines(as.character(expr_as_xml_raw)) +get_r_string(expr_as_xml_raw, "expr[2]") +get_r_string(expr_as_xml_raw, "expr[3]") +unlink(tmp_raw) } From a02335b05676ecf86684f1fabb3401e720c2c627 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 12 Jun 2024 22:55:34 -0700 Subject: [PATCH 3/3] one more var rename --- R/utils.R | 2 +- man/get_r_string.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index c3ed7f0f10..531ac1d89b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -221,7 +221,7 @@ platform_independent_sort <- function(x) x[platform_independent_order(x)] #' # more importantly, extract raw strings correctly #' tmp_raw <- tempfile() #' writeLines("c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')", tmp_raw) -#' expr_as_xml_raw <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content +#' expr_as_xml_raw <- get_source_expressions(tmp_raw)$expressions[[1L]]$xml_parsed_content #' writeLines(as.character(expr_as_xml_raw)) #' get_r_string(expr_as_xml_raw, "expr[2]") #' get_r_string(expr_as_xml_raw, "expr[3]") diff --git a/man/get_r_string.Rd b/man/get_r_string.Rd index 9fe00ba696..418d0f17c0 100644 --- a/man/get_r_string.Rd +++ b/man/get_r_string.Rd @@ -33,7 +33,7 @@ unlink(tmp) # more importantly, extract raw strings correctly tmp_raw <- tempfile() writeLines("c(R'(a\\\\b)', R'--[a\\\\\"\'\"\\\\b]--')", tmp_raw) -expr_as_xml_raw <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content +expr_as_xml_raw <- get_source_expressions(tmp_raw)$expressions[[1L]]$xml_parsed_content writeLines(as.character(expr_as_xml_raw)) get_r_string(expr_as_xml_raw, "expr[2]") get_r_string(expr_as_xml_raw, "expr[3]")