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

dispatch cases where \ has different meaning. #553

Closed
wants to merge 6 commits into from
Closed
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
51 changes: 51 additions & 0 deletions R/rules-replacement.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,54 @@ resolve_semicolon <- function(pd) {
pd <- pd[!is_semicolon, ]
pd
}


#' Replace single quotes with double quotes
#'
#' @details
#' Depending on whether the backslash represents a character on its own or
#' whether it is used to create a special character, we either use:
#'
#' - `deparse(parse_text(...))`
#' - `paste("\"", parse_text(...), "\""))`
#'
#' To make sure esaping is done correctly.
#' @examples
#' charToRaw("\n") # one character
#' charToRaw("\\n") # two characters
#' charToRaw("\\\n") # two characters
#' # put these into a file and style with style_file(),
#' # style_text() escapes backslash.
#' 'here
#' is a string
#' '
#' 'here\nis a string
#' '
#' 'here\\nis a string'
#' @importFrom purrr map map_chr
#' @param pd_flat A flat parse table.
#' @keywords internal
fix_quotes <- function(pd_flat) {
str_const <- pd_flat$token == "STR_CONST"
str_const_change <- grepl("^'([^\"]*)'$", pd_flat$text[str_const])
pd_flat$text[str_const][str_const_change] <- map_chr(
pd_flat$text[str_const][str_const_change], fix_quotes_one
)
pd_flat
}

fix_quotes_one <- function(x) {
one <- has_one_backslash(x)
multiple <- has_multiple_backslashes(x)

if (one && multiple) {
x
} else if (one) {
x %>%
parse_text() %>%
paste0("\"", ., "\"")
} else {
parse_text(x) %>%
deparse()
}
}
21 changes: 0 additions & 21 deletions R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,6 @@ remove_space_after_unary_pm_nested <- function(pd) {
pd
}

#' Replace single quotes with double quotes
#'
#' We do not use `deparse()` as in previous implementations but `paste0()` since
#' the former approach escapes the reverse backslash in the line break character
#' `\\n` whereas the solution with `paste0()` does not.
#' @examples
#' style_text("'here
#' is a string
#' '")
#' @importFrom purrr map map_chr
#' @param pd_flat A flat parse table.
#' @keywords internal
fix_quotes <- function(pd_flat) {
str_const <- pd_flat$token == "STR_CONST"
str_const_change <- grepl("^'([^\"]*)'$", pd_flat$text[str_const])
pd_flat$text[str_const][str_const_change] <-
map(pd_flat$text[str_const][str_const_change], parse_text) %>%
map_chr(~ paste0("\"", .x, "\""))
pd_flat
}

remove_space_before_opening_paren <- function(pd_flat) {
paren_after <- pd_flat$token == "'('"
if (!any(paren_after)) {
Expand Down
18 changes: 18 additions & 0 deletions R/utils-strings.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ add_newlines <- function(n) {
add_spaces <- function(n) {
rep_char(" ", n)
}


has_one_backslash <- function(x) {
x <- char2raw_as_char(x)
any(substr(x, 1, 1) == "0")
}

has_multiple_backslashes <- function(x) {
x <- char2raw_as_char(x)
any(x == "5c")
}

char2raw_as_char <- function(x) {
x <- charToRaw(x)
class(x) <- "character"
x
}

28 changes: 22 additions & 6 deletions man/fix_quotes.Rd

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

15 changes: 14 additions & 1 deletion tests/testthat/escaping/basic-escape-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ NULL
#' things
#'
#' @examples
#' call('\n') # FIXME when single quotes are used, the newline is evaluated
#' call('\n') # fixed: when single quotes are used, the newline is evaluated
#' ano("\\.", further = X)
NULL

'single quotes with
embedded and \n not embedded line breaks will not be replaced
with \\n double quotes. Too hard sorry.' #FIXME


x <- ' 2' # there is a tab emebbed (created with writeLines("x <- '\t2'"))

x <- '\001'
'\x01'

# FIXME: "\01" gives an error when not in a comment
# FIXME: this too: '\01'
24 changes: 22 additions & 2 deletions tests/testthat/escaping/basic-escape-in_tree

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

16 changes: 14 additions & 2 deletions tests/testthat/escaping/basic-escape-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ NULL
#' things
#'
#' @examples
#' call("
#' ") # FIXME when single quotes are used, the newline is evaluated
#' call("\n") # fixed: when single quotes are used, the newline is evaluated
#' ano("\\.", further = X)
NULL

'single quotes with
embedded and \n not embedded line breaks will not be replaced
with \\n double quotes. Too hard sorry.' # FIXME


x <- " 2" # there is a tab emebbed (created with writeLines("x <- '\t2'"))

x <- "\001"
"\001"

# FIXME: "\01" gives an error when not in a comment
# '\01'
2 changes: 1 addition & 1 deletion tests/testthat/strict/non_strict-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test <- function() {
single quotes are used
.'

'strings with embeded\nline breaks are unfortunately split'
'strings with embeded\nline breaks are no longer split'

# Comments are always preserved

Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/strict/non_strict-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ test <- function() {
single quotes are used
."

"strings with embeded
line breaks are unfortunately split"
"strings with embeded\nline breaks are no longer split"

# Comments are always preserved

Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ test_that("files with and without blank EOF line are read correctly", {
test_path("reference-objects/return-read-utf8-non-missing-EOF")
)
})


test_that("escape characters are properly recognized", {
one <- "jlkaf\n"
multiple <- "\\n3"
expect_true(has_one_backslash(one))
expect_false(has_one_backslash(multiple))
expect_false(has_multiple_backslashes(one))
expect_true(has_multiple_backslashes(multiple))

})