Skip to content
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
* Add vignette on distributing style guide (#846, #861).
* ensure a trailing blank line also if the input is cached (#867).
* Fix argument name `filetype` in Example for `style_dir()` (#855).
* An error is now thrown on styling if input unicode characters can't be
correctly parsed for Windows and R < 4.2 (#883).

**Infrastructure**

Expand Down
13 changes: 12 additions & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,18 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
pd <- as_tibble(
utils::getParseData(parsed, includeText = include_text),
.name_repair = "minimal"
) %>%
)
if (getRversion() < "4.2") {
is_unicode_parsing_error <- grepl("^\"<U\\+[0-9]+>\"$", pd$text)
if (any(is_unicode_parsing_error)) {
rlang::abort(paste0(
"Can't parse input due to unicode restriction in base R. Please ",
"upgrade R to >= 4.2 to style this input. ",
"Context: https://github.com/r-lib/styler/issues/847"
))
}
}
pd <- pd %>%
add_id_and_short()

parser_version_set(parser_version_find(pd))
Expand Down
5 changes: 4 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ even_index <- function(x) {
seq(2L, length(x), by = 2)
}

is_windows <- function() {
identical(.Platform$OS.type, "windows")
}

#' Invoke a system command
#'
Expand All @@ -79,7 +82,7 @@ even_index <- function(x) {
#' @param ... Arguments passed to [shell()] or [system()].
#' @keywords internal
calls_sys <- function(sys_call, ...) {
if (Sys.info()[1] == "Windows") {
if (is_windows()) {
error <- shell(sys_call, ...)
} else {
error <- system(sys_call, ...)
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ ubuntu
ui
uncached
unexplainable
unicode
unindent
unindention
unlink
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-parsing.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ test_that("mixed CRLF / LF EOLs fail", {
"unexpected input"
)
})

test_that("unicode can't be propprely handled on Windows for R < 4.2", {
msg <- ifelse(getRversion() < 4.2 && is_windows(),
"Can't parse input due to unicode restriction in base R\\.",
NA
)
expect_error(style_text('suit <- "♠"'), msg)
})