Skip to content

Commit 1b0f5ea

Browse files
reject styling wrongly parsed coder
1 parent b96d188 commit 1b0f5ea

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
* Add vignette on distributing style guide (#846, #861).
3939
* ensure a trailing blank line also if the input is cached (#867).
4040
* Fix argument name `filetype` in Example for `style_dir()` (#855).
41+
* An error is now thrown on styling if input unicode characters can't be
42+
correctly parsed for Windows and R < 4.2 (#883).
4143

4244
**Infrastructure**
4345

R/parse.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,18 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
9797
pd <- as_tibble(
9898
utils::getParseData(parsed, includeText = include_text),
9999
.name_repair = "minimal"
100-
) %>%
100+
)
101+
if (getRversion() < "4.2") {
102+
is_unicode_parsing_error <- grepl("^\"<U\\+[0-9]+>\"$", pd$text)
103+
if (any(is_unicode_parsing_error)) {
104+
rlang::abort(
105+
"Can't parse input due to unicode restriction in base R. Please ",
106+
"upgrade R to style this input. ",
107+
"Context: https://github.com/r-lib/styler/issues/847"
108+
)
109+
}
110+
}
111+
pd <- pd %>%
101112
add_id_and_short()
102113

103114
parser_version_set(parser_version_find(pd))

R/utils.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ even_index <- function(x) {
7070
seq(2L, length(x), by = 2)
7171
}
7272

73+
is_windows <- function() {
74+
identical(.Platform$OS.type, "windows")
75+
}
7376

7477
#' Invoke a system command
7578
#'
@@ -79,7 +82,7 @@ even_index <- function(x) {
7982
#' @param ... Arguments passed to [shell()] or [system()].
8083
#' @keywords internal
8184
calls_sys <- function(sys_call, ...) {
82-
if (Sys.info()[1] == "Windows") {
85+
if (is_windows()) {
8386
error <- shell(sys_call, ...)
8487
} else {
8588
error <- system(sys_call, ...)

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ ubuntu
243243
ui
244244
uncached
245245
unexplainable
246+
unicode
246247
unindent
247248
unindention
248249
unlink

tests/testthat/test-parsing.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ test_that("mixed CRLF / LF EOLs fail", {
6363
"unexpected input"
6464
)
6565
})
66+
67+
test_that("unicode can't be propprely handled on Windows for R < 4.2", {
68+
msg <- ifelse(getRversion() < 4.2 && is_windows(),
69+
"Can't parse input due to unicode restriction in base R\\.",
70+
NA
71+
)
72+
expect_error(style_text('suit <- "♠"'), msg)
73+
})

0 commit comments

Comments
 (0)