Skip to content

Commit

Permalink
add box.linters styling functions. add check for treesitter dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
radbasa committed Aug 5, 2024
1 parent 5f54d0c commit ea5f235
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"),
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)_
Expand Down
31 changes: 30 additions & 1 deletion R/tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand All @@ -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")
}
Expand Down Expand Up @@ -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.
#'
Expand All @@ -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)
}
}
Expand Down
5 changes: 4 additions & 1 deletion man/format_r.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/lint_r.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea5f235

Please sign in to comment.