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 14 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 = "opensource+kamil@appsilon.com"),
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.2),
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
44 changes: 41 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,22 @@ 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. `box.linters` provides styling functions specific to the `box::use()`
#' calls. These include:
#' * Separate `box::use()` calls for packages and local modules
#' * Alphabetically sorted packages, modules, and functions.
#' * Trailing commas
#'
#'
#' @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 +164,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 some 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)
styler::style_file(path, style = rhino_style)
}
}
Expand Down
16 changes: 14 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
Loading