diff --git a/NEWS.md b/NEWS.md index bc6e195..cf37275 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,11 @@ # devel +* Ensure that closing xml-tags for code expressions that end at the same + position in a file respect start-first-end-last ordering in the produced xml. + Ensures that the new `equal_assign` token in `getParseData()` for R-3.6 is + handled appropriately. #5 @russHyde + # 1.0.2 * Remove control characters `\003`, `\007`, `\010`, `\027`, as they are diff --git a/R/package.R b/R/package.R index 84c9a2c..58b4e78 100644 --- a/R/package.R +++ b/R/package.R @@ -107,7 +107,11 @@ xml_parse_data <- function(x, includeText = NA, pretty = FALSE) { } ## Order the nodes properly - pd <- pd[ order(pd$line1, pd$col1, -pd$line2, -pd$col2, pd$terminal), ] + ## - the terminal nodes from pd2 may be nested inside each other, when + ## this happens they will have the same line1, col1, line2, col2 and + ## terminal status; and 'start' is used to break ties + ord <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, pd$terminal, -pd$start) + pd <- pd[ord, ] if (pretty) { str <- ! pd$terminal diff --git a/tests/testthat/test.R b/tests/testthat/test.R index 9e76762..6533ec1 100644 --- a/tests/testthat/test.R +++ b/tests/testthat/test.R @@ -94,7 +94,6 @@ test_that("data frame input", { }) - test_that("Control-C character", { src <- "# Control-C \003 # Bell \007 @@ -105,3 +104,17 @@ test_that("Control-C character", { expect_is(x, "xml_document") }) + +test_that("equal_assign is handled on R 3.6", { + # `a = 1` is an example of an R statement that gets parsed into nested xml + # nodes that have different token / tagnames (following the introduction of + # the `equal_assign` token to getParseData() in R-3.6), but the same ending + # position in the original code. Tokens/expressions that start before should + # end after any nested subexpressions in the resulting xml: + + xml <- xml_parse_data(parse(text = "a = 1", keep.source = TRUE)) + expect_true(is.character(xml)) + expect_true(length(xml) == 1) + expect_silent(x <- xml2::read_xml(xml)) +}) +