Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract XML-related utilities into a separate file #2201

Merged
merged 6 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Collate:
'with.R'
'with_id.R'
'xml_nodes_to_lints.R'
'xml_utils.R'
'yoda_test_linter.R'
'zzz.R'
Language: en-US
23 changes: 0 additions & 23 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,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)] <- ""

Expand Down Expand Up @@ -257,18 +246,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 (`<expr>` 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) {
Expand Down
7 changes: 0 additions & 7 deletions R/xml_nodes_to_lints.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,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))))
}
32 changes: 32 additions & 0 deletions R/xml_utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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 (`<expr>` 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()
)
}

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))))
}
Loading