From af0077a2a43bbaa21831cd159328354b6ac887d9 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Wed, 20 Sep 2023 15:59:20 +0200 Subject: [PATCH 1/2] Extract XML-related utilities into a separate file Since the `utils.R` is getting quite large. Similar to `xp_utils.R`. --- DESCRIPTION | 1 + R/utils.R | 23 ----------------------- R/xml_utils.R | 25 +++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 R/xml_utils.R diff --git a/DESCRIPTION b/DESCRIPTION index 10e5d5984..e785dc18e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -185,6 +185,7 @@ Collate: 'with.R' 'with_id.R' 'xml_nodes_to_lints.R' + 'xml_utils.R' 'yoda_test_linter.R' 'zzz.R' Language: en-US diff --git a/R/utils.R b/R/utils.R index a0d6460c0..a88a0bb47 100644 --- a/R/utils.R +++ b/R/utils.R @@ -87,17 +87,6 @@ names2 <- function(x) { names(x) %||% rep("", length(x)) } -safe_parse_to_xml <- function(parsed_content) { - if (is.null(parsed_content)) { - return(xml2::xml_missing()) - } - tryCatch( - xml2::read_xml(xmlparsedata::xml_parse_data(parsed_content)), - # use xml_missing so that code doesn't always need to condition on XML existing - error = function(e) xml2::xml_missing() - ) -} - get_content <- function(lines, info) { lines[is.na(lines)] <- "" @@ -248,18 +237,6 @@ get_r_string <- function(s, xpath = NULL) { out } -#' str2lang, but for xml children. -#' -#' [xml2::xml_text()] is deceptively close to obviating this helper, but it collapses -#' text across lines. R is _mostly_ whitespace-agnostic, so this only matters in some edge cases, -#' in particular when there are comments within an expression (`` node). See #1919. -#' -#' @noRd -xml2lang <- function(x) { - x_strip_comments <- xml_find_all(x, ".//*[not(self::COMMENT or self::expr)]") - str2lang(paste(xml_text(x_strip_comments), collapse = " ")) -} - is_linter <- function(x) inherits(x, "linter") is_tainted <- function(lines) { diff --git a/R/xml_utils.R b/R/xml_utils.R new file mode 100644 index 000000000..ddcf65a6a --- /dev/null +++ b/R/xml_utils.R @@ -0,0 +1,25 @@ +# utils for working with XML + +#' str2lang, but for xml children. +#' +#' [xml2::xml_text()] is deceptively close to obviating this helper, but it collapses +#' text across lines. R is _mostly_ whitespace-agnostic, so this only matters in some edge cases, +#' in particular when there are comments within an expression (`` node). See #1919. +#' +#' @noRd +xml2lang <- function(x) { + x_strip_comments <- xml_find_all(x, ".//*[not(self::COMMENT or self::expr)]") + str2lang(paste(xml_text(x_strip_comments), collapse = " ")) +} + + +safe_parse_to_xml <- function(parsed_content) { + if (is.null(parsed_content)) { + return(xml2::xml_missing()) + } + tryCatch( + xml2::read_xml(xmlparsedata::xml_parse_data(parsed_content)), + # use xml_missing so that code doesn't always need to condition on XML existing + error = function(e) xml2::xml_missing() + ) +} From e2643879384bd820a7605e07b4c361b1aa7db109 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Fri, 22 Sep 2023 19:01:32 +0200 Subject: [PATCH 2/2] move node utils --- R/xml_nodes_to_lints.R | 7 ------- R/xml_utils.R | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/xml_nodes_to_lints.R b/R/xml_nodes_to_lints.R index 1207121d0..1381ed147 100644 --- a/R/xml_nodes_to_lints.R +++ b/R/xml_nodes_to_lints.R @@ -95,10 +95,3 @@ xml_nodes_to_lints <- function(xml, source_expression, lint_message, ranges = list(c(col1, col2)) ) } - -is_node <- function(xml) inherits(xml, "xml_node") -is_nodeset <- function(xml) inherits(xml, "xml_nodeset") -is_nodeset_like <- function(xml) { - is_nodeset(xml) || - (is.list(xml) && all(vapply(xml, is_node, logical(1L)))) -} diff --git a/R/xml_utils.R b/R/xml_utils.R index ddcf65a6a..3b0546da6 100644 --- a/R/xml_utils.R +++ b/R/xml_utils.R @@ -23,3 +23,10 @@ safe_parse_to_xml <- function(parsed_content) { error = function(e) xml2::xml_missing() ) } + +is_node <- function(xml) inherits(xml, "xml_node") +is_nodeset <- function(xml) inherits(xml, "xml_nodeset") +is_nodeset_like <- function(xml) { + is_nodeset(xml) || + (is.list(xml) && all(vapply(xml, is_node, logical(1L)))) +}