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..531ac1d89b 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,15 +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 -#' @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 -#' 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(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]") +#' unlink(tmp_raw) #' #' @export get_r_string <- function(s, xpath = NULL) { 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..418d0f17c0 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,14 +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 -\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 -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} +# 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(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]") +unlink(tmp_raw) + }