Skip to content

Commit

Permalink
Strip srcrefs from exprs (#189)
Browse files Browse the repository at this point in the history
This makes the output type simpler and makes testing a little easier.
  • Loading branch information
hadley authored Jul 2, 2024
1 parent e8f788d commit f82a9e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
4 changes: 3 additions & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' expression contains only whitespace and/or comments; 1 if the top-level
#' expression is a single scalar (like `TRUE`, `1`, or `"x"`), name, or call;
#' or 2 if the top-level expression uses `;` to put multiple expressions on
#' one line.
#' one line. The expressions have their srcrefs removed.
#'
#' If there are syntax errors in `x` and `allow_error = TRUE`, the data
#' frame will have an attribute `PARSE_ERROR` that stores the error object.
Expand Down Expand Up @@ -104,6 +104,8 @@ parse_all.character <- function(x, filename = NULL, allow_error = FALSE) {
nl <- c(rep("\n", nrow(res) - 1), if (trailing_nl) "\n" else "")
res$src <- paste0(res$src, nl)

res$expr <- lapply(res$expr, removeSource)

rownames(res) <- NULL
res
}
Expand Down
2 changes: 1 addition & 1 deletion man/parse_all.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 9 additions & 30 deletions tests/testthat/test-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,11 @@ test_that("a character vector is equivalent to a multi-line string", {
})

test_that("recombines multi-expression TLEs", {
expect_equal(
parse_all("1;2;3")$expr[[1]],
expression(1, 2, 3),
ignore_attr = "srcref"
)
expect_equal(
parse_all("1+\n2;3")$expr[[1]],
expression(1 + 2, 3),
ignore_attr = "srcref"
)
expect_equal(parse_all("1;2;3")$expr[[1]], expression(1, 2, 3))
expect_equal(parse_all("1+\n2;3")$expr[[1]], expression(1 + 2, 3))
expect_equal(
parse_all("1+\n2;3+\n4; 5")$expr[[1]],
expression(1 + 2, 3 + 4, 5),
ignore_attr = "srcref"
expression(1 + 2, 3 + 4, 5)
)
})

Expand All @@ -63,8 +54,8 @@ test_that("re-integrates lines without expressions", {

test_that("expr is always an expression", {
expect_equal(parse_all("#")$expr[[1]], expression())
expect_equal(parse_all("1")$expr[[1]], expression(1), ignore_attr = "srcref")
expect_equal(parse_all("1;2")$expr[[1]], expression(1, 2), ignore_attr = "srcref")
expect_equal(parse_all("1")$expr[[1]], expression(1))
expect_equal(parse_all("1;2")$expr[[1]], expression(1, 2))

parsed <- parse_all("#\n1\n1;2")
expect_equal(lengths(parsed$expr), c(0, 1, 2))
Expand All @@ -84,7 +75,7 @@ test_that("double quotes in Chinese characters not destroyed", {

out <- parse_all(c('1+1', '"你好"'))
expect_equal(out$src[[2]], '"你好"')
expect_equal(out$expr[[2]], expression("你好"), ignore_attr = "srcref")
expect_equal(out$expr[[2]], expression("你好"))
})

test_that("multibyte characters are parsed correctly", {
Expand All @@ -100,11 +91,7 @@ test_that("multibyte characters are parsed correctly", {
test_that("can parse a call", {
out <- parse_all(quote(f(a, b, c)))
expect_equal(out$src, "f(a, b, c)")
expect_equal(
out$expr,
I(list(expression(f(a, b, c)))),
ignore_attr = "srcref"
)
expect_equal(out$expr, list(expression(f(a, b, c))))
})

test_that("can parse a connection", {
Expand All @@ -115,11 +102,7 @@ test_that("can parse a connection", {
out <- parse_all(con)

expect_equal(out$src, c("# 1\n", "1 + 1"))
expect_equal(
out$expr,
I(list(expression(), expression(1 + 1))),
ignore_attr = "srcref"
)
expect_equal(out$expr, list(expression(), expression(1 + 1)))

# Doesn't leave any connections around
expect_equal(getAllConnections(), cur_cons)
Expand All @@ -131,11 +114,7 @@ test_that("can parse a function", {
1 + 1
})
expect_equal(out$src, c("# Hi\n", "1 + 1"))
expect_equal(
out$expr,
I(list(expression(), expression(1 + 1))),
ignore_attr = "srcref"
)
expect_equal(out$expr, list(expression(), expression(1 + 1)))
})

# find_function_body -----------------------------------------------------------
Expand Down

0 comments on commit f82a9e3

Please sign in to comment.