From ea5f235b886ef2cf0051ea508e0b803cef035f54 Mon Sep 17 00:00:00 2001 From: Rodrigo Basa Date: Mon, 5 Aug 2024 14:40:02 +0800 Subject: [PATCH] add box.linters styling functions. add check for treesitter dependencies --- DESCRIPTION | 4 ++-- NEWS.md | 3 +++ R/tools.R | 31 ++++++++++++++++++++++++++++++- man/format_r.Rd | 5 ++++- man/lint_r.Rd | 4 ++++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index efafee12..cae78045 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rhino Title: A Framework for Enterprise Shiny Applications -Version: 1.9.0.9000 +Version: 1.9.0.9001 Authors@R: c( person("Kamil", "Żyła", role = c("aut", "cre"), email = "opensource+kamil@appsilon.com"), @@ -24,7 +24,7 @@ Depends: R (>= 2.10) Imports: box (>= 1.1.3), - box.linters (>= 0.9.1), + box.linters (>= 10.1.0), cli, config, fs, diff --git a/NEWS.md b/NEWS.md index b108da0e..9fe3a22a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # rhino (development version) +* integrate {box.linters} styling functions +* add compatibility check for `treesitter` and `treesitter.r` dependencies + # [rhino 1.9.0](https://github.com/Appsilon/rhino/releases/tag/v1.9.0) See _[How-to: Rhino 1.9 Migration Guide](https://appsilon.github.io/rhino/articles/how-to/migrate-1-9.html)_ diff --git a/R/tools.R b/R/tools.R index 3d5b5646..2bf23321 100644 --- a/R/tools.R +++ b/R/tools.R @@ -79,6 +79,10 @@ check_paths <- function(paths) { #' with the `legacy_max_lint_r_errors` option in `rhino.yml`. #' This can be useful when inheriting legacy code with multiple styling issues. #' +#' The [box.linters::namespaced_function_calls()] linter requires the `{treesitter}` and +#' `{treesitter.r}` packages. These require R >= 4.3.0. `lint_r()` will continue to run and skip +#' `namespaced_function_calls()` if its dependencies are not available. +#' #' @param paths Character vector of directories and files to lint. #' When `NULL` (the default), check `app` and `tests/testthat` directories. #' @@ -87,6 +91,17 @@ check_paths <- function(paths) { #' @export # nolint end lint_r <- function(paths = NULL) { + if (!box.linters::is_treesitter_installed()) { + cli::cli_warn( + c( + "!" = paste( + "`box.linters::namespaced_function_calls()` requires {{treesitter}} and {{treesitter.r}}", + "to be installed." + ), + "i" = "`lintr_r()` will continue to run using the other linter functions." + ) + ) + } if (is.null(paths)) { paths <- c("app", "tests/testthat") } @@ -122,11 +137,14 @@ rhino_style <- function() { #' Format R #' -#' Uses the `{styler}` package to automatically format R sources. +#' Uses the `{styler}` and `{box.linters}` packages to automatically format R sources. #' #' The code is formatted according to the `styler::tidyverse_style` guide with one adjustment: #' spacing around math operators is not modified to avoid conflicts with `box::use()` statements. #' +#' `box.linters::style_*` functions require the `treesitter` and `treesitter.r` packages. These, in +#' turn, require R >= 4.3.0. +#' #' @param paths Character vector of files and directories to format. #' @return None. This function is called for side effects. #' @@ -140,10 +158,21 @@ rhino_style <- function() { #' } #' @export format_r <- function(paths) { + if (!box.linters::is_treesitter_installed()) { + cli::cli_abort( + c( + "x" = "The packages {{treesitter}} and {{treesitter.r}} are required by `format_r()`", + "i" = "These package require R version >= 4.3.0 to install." + ) + ) + } + for (path in paths) { if (fs::is_dir(path)) { + box.linters::style_box_use_dir(path) styler::style_dir(path, style = rhino_style) } else { + box.linters::style_box_use_file(path) styler::style_file(path, style = rhino_style) } } diff --git a/man/format_r.Rd b/man/format_r.Rd index c4d00340..7edb4f7c 100644 --- a/man/format_r.Rd +++ b/man/format_r.Rd @@ -13,11 +13,14 @@ format_r(paths) None. This function is called for side effects. } \description{ -Uses the \code{{styler}} package to automatically format R sources. +Uses the \code{{styler}} and \code{{box.linters}} packages to automatically format R sources. } \details{ The code is formatted according to the \code{styler::tidyverse_style} guide with one adjustment: spacing around math operators is not modified to avoid conflicts with \code{box::use()} statements. + +\verb{box.linters::style_*} functions require the \code{treesitter} and \code{treesitter.r} packages. These, in +turn, require R >= 4.3.0. } \examples{ if (interactive()) { diff --git a/man/lint_r.Rd b/man/lint_r.Rd index 5a348725..9c6336a9 100644 --- a/man/lint_r.Rd +++ b/man/lint_r.Rd @@ -24,4 +24,8 @@ in the \code{.lintr} file. You can set the maximum number of accepted style errors with the \code{legacy_max_lint_r_errors} option in \code{rhino.yml}. This can be useful when inheriting legacy code with multiple styling issues. + +The \code{\link[box.linters:namespaced_function_calls]{box.linters::namespaced_function_calls()}} linter requires the \code{{treesitter}} and +\code{{treesitter.r}} packages. These require R >= 4.3.0. \code{lint_r()} will continue to run and skip +\code{namespaced_function_calls()} if its dependencies are not available. }