From 1b0f5eaf025dd3786c22fa6da605202d66ae7d2e Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 23 Dec 2021 13:46:08 +0100 Subject: [PATCH 1/3] reject styling wrongly parsed coder --- NEWS.md | 2 ++ R/parse.R | 13 ++++++++++++- R/utils.R | 5 ++++- inst/WORDLIST | 1 + tests/testthat/test-parsing.R | 8 ++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 818e2f258..1717499b1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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** diff --git a/R/parse.R b/R/parse.R index a82c60637..f7173ff2a 100644 --- a/R/parse.R +++ b/R/parse.R @@ -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("^\"\"$", pd$text) + if (any(is_unicode_parsing_error)) { + rlang::abort( + "Can't parse input due to unicode restriction in base R. Please ", + "upgrade R 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)) diff --git a/R/utils.R b/R/utils.R index abc867000..af6260055 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 #' @@ -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, ...) diff --git a/inst/WORDLIST b/inst/WORDLIST index e01a1a2db..042f8a762 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -243,6 +243,7 @@ ubuntu ui uncached unexplainable +unicode unindent unindention unlink diff --git a/tests/testthat/test-parsing.R b/tests/testthat/test-parsing.R index 73d0db1cf..2b4707d3b 100644 --- a/tests/testthat/test-parsing.R +++ b/tests/testthat/test-parsing.R @@ -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) +}) From 6614d3ac5e1d41675cdf0374cfdade8b40c87105 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 23 Dec 2021 14:15:59 +0100 Subject: [PATCH 2/3] add more concise advice --- R/parse.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/parse.R b/R/parse.R index f7173ff2a..46aa1d05b 100644 --- a/R/parse.R +++ b/R/parse.R @@ -103,7 +103,7 @@ get_parse_data <- function(text, include_text = TRUE, ...) { if (any(is_unicode_parsing_error)) { rlang::abort( "Can't parse input due to unicode restriction in base R. Please ", - "upgrade R to style this input. ", + "upgrade R to >= 4.2 to style this input. ", "Context: https://github.com/r-lib/styler/issues/847" ) } From ce351222bdf41a0e7f9704a75b7250e33a458ab6 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 23 Dec 2021 14:31:37 +0100 Subject: [PATCH 3/3] fix error --- R/parse.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/parse.R b/R/parse.R index 46aa1d05b..f728f4583 100644 --- a/R/parse.R +++ b/R/parse.R @@ -101,11 +101,11 @@ get_parse_data <- function(text, include_text = TRUE, ...) { if (getRversion() < "4.2") { is_unicode_parsing_error <- grepl("^\"\"$", pd$text) if (any(is_unicode_parsing_error)) { - rlang::abort( + 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 %>%