From f3829128c792c0776f68c844cb80629985b9358a Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Wed, 4 Dec 2024 19:59:56 +0530 Subject: [PATCH 1/2] Format with devel-`{styler}` --- R/indentation_linter.R | 4 ++-- R/lint.R | 9 ++++++--- R/nested_pipe_linter.R | 5 +++-- R/object_overwrite_linter.R | 5 +++-- R/return_linter.R | 11 ++++++----- R/unnecessary_nesting_linter.R | 21 +++++++++++---------- R/utils.R | 3 +-- man/lint.Rd | 9 ++++++--- 8 files changed, 38 insertions(+), 29 deletions(-) diff --git a/R/indentation_linter.R b/R/indentation_linter.R index a89f3da7c..2e9bf3822 100644 --- a/R/indentation_linter.R +++ b/R/indentation_linter.R @@ -258,8 +258,8 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al # Only lint non-empty lines if the indentation level doesn't match. bad_lines <- which(indent_levels != expected_indent_levels & - nzchar(trimws(source_expression$file_lines)) & - !in_str_const) + nzchar(trimws(source_expression$file_lines)) & + !in_str_const) if (length(bad_lines) > 0L) { # Suppress consecutive lints with the same indentation difference, to not generate an excessive number of lints is_consecutive_lint <- c(FALSE, diff(bad_lines) == 1L) diff --git a/R/lint.R b/R/lint.R index 970b2faf8..36faf36de 100644 --- a/R/lint.R +++ b/R/lint.R @@ -27,11 +27,14 @@ #' @return An object of class `c("lints", "list")`, each element of which is a `"list"` object. #' #' @examples +#' # linting inline-code +#' lint("a = 123\n") +#' lint(text = "a = 123") +#' +#' # linting a file #' f <- tempfile() #' writeLines("a=1", f) -#' lint(f) # linting a file -#' lint("a = 123\n") # linting inline-code -#' lint(text = "a = 123") # linting inline-code +#' lint(f) #' unlink(f) #' #' @export diff --git a/R/nested_pipe_linter.R b/R/nested_pipe_linter.R index fd595b233..adc0d5089 100644 --- a/R/nested_pipe_linter.R +++ b/R/nested_pipe_linter.R @@ -51,8 +51,9 @@ #' @seealso [linters] for a complete list of linters available in lintr. #' @export nested_pipe_linter <- function( - allow_inline = TRUE, - allow_outer_calls = c("try", "tryCatch", "withCallingHandlers")) { + allow_inline = TRUE, + allow_outer_calls = c("try", "tryCatch", "withCallingHandlers") +) { multiline_and <- if (allow_inline) "@line1 != @line2 and" else "" xpath <- glue(" (//PIPE | //SPECIAL[{ xp_text_in_table(magrittr_pipes) }]) diff --git a/R/object_overwrite_linter.R b/R/object_overwrite_linter.R index 099129ed5..acee4c2ea 100644 --- a/R/object_overwrite_linter.R +++ b/R/object_overwrite_linter.R @@ -52,8 +52,9 @@ #' - #' @export object_overwrite_linter <- function( - packages = c("base", "stats", "utils", "tools", "methods", "graphics", "grDevices"), - allow_names = character()) { + packages = c("base", "stats", "utils", "tools", "methods", "graphics", "grDevices"), + allow_names = character() +) { for (package in packages) { if (!requireNamespace(package, quietly = TRUE)) { cli_abort("Package {.pkg {package}} is required, but not available.") diff --git a/R/return_linter.R b/R/return_linter.R index d0bc75d91..bb2383b2d 100644 --- a/R/return_linter.R +++ b/R/return_linter.R @@ -72,11 +72,12 @@ #' - #' @export return_linter <- function( - return_style = c("implicit", "explicit"), - allow_implicit_else = TRUE, - return_functions = NULL, - except = NULL, - except_regex = NULL) { + return_style = c("implicit", "explicit"), + allow_implicit_else = TRUE, + return_functions = NULL, + except = NULL, + except_regex = NULL +) { return_style <- match.arg(return_style) check_except <- !allow_implicit_else || return_style == "explicit" diff --git a/R/unnecessary_nesting_linter.R b/R/unnecessary_nesting_linter.R index 653406881..b198326bf 100644 --- a/R/unnecessary_nesting_linter.R +++ b/R/unnecessary_nesting_linter.R @@ -95,16 +95,17 @@ #' - [linters] for a complete list of linters available in lintr. #' @export unnecessary_nesting_linter <- function( - allow_assignment = TRUE, - allow_functions = c( - "switch", - "try", "tryCatch", "withCallingHandlers", - "quote", "expression", "bquote", "substitute", - "with_parameters_test_that", - "reactive", "observe", "observeEvent", - "renderCachedPlot", "renderDataTable", "renderImage", "renderPlot", - "renderPrint", "renderTable", "renderText", "renderUI" - )) { + allow_assignment = TRUE, + allow_functions = c( + "switch", + "try", "tryCatch", "withCallingHandlers", + "quote", "expression", "bquote", "substitute", + "with_parameters_test_that", + "reactive", "observe", "observeEvent", + "renderCachedPlot", "renderDataTable", "renderImage", "renderPlot", + "renderPrint", "renderTable", "renderText", "renderUI" + ) +) { exit_calls <- c("stop", "return", "abort", "quit", "q") exit_call_expr <- glue(" expr[SYMBOL_FUNCTION_CALL[{xp_text_in_table(exit_calls)}]] diff --git a/R/utils.R b/R/utils.R index 0820c9766..1cdfca15c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -186,8 +186,7 @@ read_lines <- function(file, encoding = settings$encoding, ...) { # nocov start # support for usethis::use_release_issue(). Make sure to use devtools::load_all() beforehand! -release_bullets <- function() { -} +release_bullets <- function() {} # nocov end # see issue #923, PR #2455 -- some locales ignore _ when running sort(), others don't. diff --git a/man/lint.Rd b/man/lint.Rd index a165a9343..da16dafea 100644 --- a/man/lint.Rd +++ b/man/lint.Rd @@ -89,11 +89,14 @@ Note that if files contain unparseable encoding problems, only the encoding prob unintelligible error messages from other linters. } \examples{ +# linting inline-code +lint("a = 123\n") +lint(text = "a = 123") + +# linting a file f <- tempfile() writeLines("a=1", f) -lint(f) # linting a file -lint("a = 123\n") # linting inline-code -lint(text = "a = 123") # linting inline-code +lint(f) unlink(f) if (FALSE) { From 246207e310f5d0b51f8f96bd0406c23ba4c8d9a5 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Thu, 5 Dec 2024 01:26:12 +0530 Subject: [PATCH 2/2] Update indentation_linter.R --- R/indentation_linter.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/indentation_linter.R b/R/indentation_linter.R index 2e9bf3822..de4a7c777 100644 --- a/R/indentation_linter.R +++ b/R/indentation_linter.R @@ -257,9 +257,12 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al } # Only lint non-empty lines if the indentation level doesn't match. + # TODO: remove styler ignore directives once tidyverse/style/issues/197 is resolved + # styler: off bad_lines <- which(indent_levels != expected_indent_levels & - nzchar(trimws(source_expression$file_lines)) & - !in_str_const) + nzchar(trimws(source_expression$file_lines)) & + !in_str_const) + # styler: on if (length(bad_lines) > 0L) { # Suppress consecutive lints with the same indentation difference, to not generate an excessive number of lints is_consecutive_lint <- c(FALSE, diff(bad_lines) == 1L)