Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add box.linters styling functions. add check for treesitter dependencies #607

Merged
merged 19 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 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 @@ -19,12 +19,12 @@ BugReports: https://github.com/Appsilon/rhino/issues
License: LGPL-3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Depends:
R (>= 2.10)
Imports:
box (>= 1.1.3),
box.linters (>= 0.9.1),
box.linters (>= 0.10.4),
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)

* Integrated {box.linters} styling functions to style `box::use()` calls according to the Rhino style guide.
* Added 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
3 changes: 3 additions & 0 deletions R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ read_dependencies <- function() {
}

write_dependencies <- function(deps) {
if (as.numeric(R.Version()$major) >= 4 && as.numeric(R.Version()$minor) >= 3.0) {
deps <- c(deps, "treesitter", "treesitter.r")
}
deps <- sort(unique(c("rhino", deps))) # Rhino is always needed as a dependency.
deps <- purrr::map_chr(deps, function(name) glue::glue("library({name})"))
deps <- c(
Expand Down
52 changes: 49 additions & 3 deletions 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,12 +137,30 @@ 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. As with
#' `styler`, carefully examine the results after running this function.
#'
#' 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.
#'
#' If available, `box::use()` calls are reformatted by styling functions provided by
#' `{box.linters}`. These include:
#'
#' * Separating `box::use()` calls for packages and local modules
#' * Alphabetically sorting packages, modules, and functions.
#' * Adding trailing commas
#'
#' `box.linters::style_*` functions require the `treesitter` and `treesitter.r` packages. These, in
#' turn, require R >= 4.3.0. `format_r()` will continue to operate without these but will not
#' perform `box::use()` call styling.
#'
#' For more information on `box::use()` call styling please refer to the `{box.linters}` styling
#' functions
#' [documentation](https://appsilon.github.io/box.linters/reference/style_box_use_text.html).
#'
#' @param paths Character vector of files and directories to format.
#' @param exclude_files Character vector with regular expressions of files that should be excluded
#' from styling.
#' @return None. This function is called for side effects.
#'
#' @examples
Expand All @@ -139,11 +172,24 @@ rhino_style <- function() {
#' format_r("app/view")
#' }
#' @export
format_r <- function(paths) {
format_r <- function(paths, exclude_files = NULL) {
style_box_use <- box.linters::style_box_use_dir
if (!box.linters::is_treesitter_installed()) {
style_box_use <- function(path, exclude_files) { }
cli::cli_warn(
c(
"x" = "The packages {{treesitter}} and {{treesitter.r}} are required by `box::use()` styling features of `format_r()`.", #nolint
"i" = "These package require R version >= 4.3.0 to install."
)
)
}

for (path in paths) {
if (fs::is_dir(path)) {
styler::style_dir(path, style = rhino_style)
style_box_use(path, exclude_files = exclude_files)
styler::style_dir(path, style = rhino_style, exclude_files = exclude_files)
} else {
style_box_use(path, exclude_files = exclude_files)
jakubnowicki marked this conversation as resolved.
Show resolved Hide resolved
styler::style_file(path, style = rhino_style)
}
}
Expand Down
24 changes: 22 additions & 2 deletions 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.

2 changes: 2 additions & 0 deletions tests/e2e/test-lint-r.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
install.packages(c("treesitter", "treesitter.r"))

rhino::lint_r()
# Create bad scripts and test if formatting returns the expected result
test_file_path <- fs::path("app", "logic", "bad-style.R")
Expand Down
39 changes: 39 additions & 0 deletions vignettes/explanation/rhino-style-guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,42 @@ box::use(
shiny[div, fluidPage, navbarPage, sidebarPanel, sidebarLayout, mainPanel, tabPanel, tabsetPanel, titlePanel],
)
```

# Automated Styling of `box::use()` calls

As of Rhino version 1.10.0, `format_r()` includes styling for `box::use()` calls. This is provided by [`{box.linters}`](https://appsilon.github.io/box.linters/) version >= 0.10.4. As with [`{styler}`](https://styler.r-lib.org/), carefully examine the styling results after performing automated styling on your code.

Automated styling covers some of the topics described above such as:

* Separating `box::use()` calls for packages and local modules
* Alphabetically sorting packages, modules, and functions.
* Adding trailing commas

```r
# Original
box::use(stringr[str_trim, str_pad], dplyr, app/logic/table)

# Styled
box::use(
dplyr,
stringr[str_pad, str_trim],
)

box::use(
app/logic/table
)
```

## Requirements

* R version >= 4.3.0
* `box.linters` version >= 0.10.4
* `treesitter` package
* `treesitter.r` package

## How to use

1. `box::use()` call styling is included when running `format_r()`.
2. It can also be separately executed by running one of the [`box.linters::style_*`](https://appsilon.github.io/box.linters/reference/index.html#box-styling) functions.

For more information on the abilities of `{{box.linters}}` styling functions refer to the styling functions [documentation](https://appsilon.github.io/box.linters/reference/style_box_use_text.html).
Loading