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

Box function import count linter #537

Merged
merged 19 commits into from
Feb 20, 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
2 changes: 1 addition & 1 deletion 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.6.0.9005
Version: 1.6.0.9006
Authors@R:
c(
person("Kamil", "Żyła", role = c("aut", "cre"), email = "[email protected]"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(app)
export(box_func_import_count_linter)
export(box_trailing_commas_linter)
export(box_universal_import_linter)
export(build_js)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
1. Introduce linters for `box::use` statements:
* `box_universal_import_linter` checks if all imports are explicit.
* `box_trailing_commas_linter` checks if statements include trailing commas.
* `box_func_import_count_linter` checks if the number of function imports does not exceed the limit.
2. Major refactor of `rhino::app()`:
* The `request` parameter is now correctly forwarded to the UI function
when using a `legacy_entrypoint` ([#395](https://github.com/Appsilon/rhino/issues/395)).
Expand Down
69 changes: 69 additions & 0 deletions R/box_linters.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,72 @@
#' `box` library function import count linter
#'
#' Checks that function imports do not exceed the defined `max`.
#'
#' @param max Maximum function imports allowed between `[` and `]`. Defaults to 8.
#'
#' @return A custom linter function for use with `r-lib/lintr`.
#'
#' @examples
#' # will produce lints
#' lintr::lint(
#' text = "box::use(package[one, two, three, four, five, six, seven, eight, nine])",
#' linters = box_func_import_count_linter()
#' )
#'
#' lintr::lint(
#' text = "box::use(package[one, two, three, four])",
#' linters = box_func_import_count_linter(3)
#' )
#'
#' # okay
#' lintr::lint(
#' text = "box::use(package[one, two, three, four, five])",
#' linters = box_func_import_count_linter()
#' )
#'
#' lintr::lint(
#' text = "box::use(package[one, two, three])",
#' linters = box_func_import_count_linter(3)
#' )
#'
#' @export
box_func_import_count_linter <- function(max = 8L) {
xpath <- glue::glue("//SYMBOL_PACKAGE[
(text() = 'box' and
following-sibling::SYMBOL_FUNCTION_CALL[text() = 'use'])
]
/parent::expr
/parent::expr
/descendant::OP-LEFT-BRACKET[
count(
following-sibling::expr[
count(. | ..//OP-RIGHT-BRACKET/preceding-sibling::expr) =
count(../OP-RIGHT-BRACKET/preceding-sibling::expr)
]
) > {max}
]
/parent::expr")

lint_message <- glue::glue("Limit the function imports to a max of {max}.")

lintr::Linter(function(source_expression) {
if (!lintr::is_lint_level(source_expression, "file")) {
return(list())
}

xml <- source_expression$full_xml_parsed_content

bad_expr <- xml2::xml_find_all(xml, xpath)

lintr::xml_nodes_to_lints(
bad_expr,
source_expression = source_expression,
lint_message = lint_message,
type = "style"
)
})
}

#' `box` library trailing commas linter
#'
#' Checks that all `box:use` imports have a trailing comma. This applies to
Expand Down
1 change: 1 addition & 0 deletions inst/templates/app_structure/dot.lintr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
linters:
linters_with_defaults(
line_length_linter = line_length_linter(100),
box_func_import_count_linter = rhino::box_func_import_count_linter(),
box_trailing_commas_linter = rhino::box_trailing_commas_linter(),
box_universal_import_linter = rhino::box_universal_import_linter(),
object_usage_linter = NULL # Does not work with `box::use()`.
Expand Down
41 changes: 41 additions & 0 deletions man/box_func_import_count_linter.Rd

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

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ reference:

- title: Linters
contents:
- box_func_import_count_linter
- box_trailing_commas_linter
- box_universal_import_linter

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/app-files/hello.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
box::use(
shiny[
shiny[ # nolint
actionButton,
bootstrapPage,
isolate,
Expand Down
87 changes: 87 additions & 0 deletions tests/testthat/test-linter_box_func_import_count_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
lint_message <- function(max = 5L) {
rex::rex(glue::glue("Limit the function imports to a max of {max}."))
}

test_that("box_func_import_count_linter skips allowed function count.", {
linter <- box_func_import_count_linter(max = 5)

five_function_imports <- "box::use(
dplyr[
arrange,
select,
mutate,
distinct,
filter,
],
)"

five_function_imports_inline <- "box::use(
dplyr[arrange, select, mutate, distinct, filter, ],
)"

lintr::expect_lint(five_function_imports, NULL, linter)
lintr::expect_lint(five_function_imports_inline, NULL, linter)
})

test_that("box_func_import_count_linter skips allowed function count supplied max", {
linter <- box_func_import_count_linter(max = 3)

three_function_imports <- "box::use(
dplyr[
arrange,
select,
mutate,
],
)"

three_function_imports_inline <- "box::use(
dplyr[arrange, select, mutate, ],
)"

lintr::expect_lint(three_function_imports, NULL, linter)
lintr::expect_lint(three_function_imports_inline, NULL, linter)
})

test_that("box_func_import_count_linter blocks function imports more than max", {
max <- 5
linter <- box_func_import_count_linter(max = max)
lint_msg <- lint_message(max)

six_function_imports <- "box::use(
dplyr[
arrange,
select,
mutate,
distinct,
filter,
slice,
],
)"

six_function_imports_inline <- "box::use(
dplyr[arrange, select, mutate, distinct, filter, slice, ],
)"

lintr::expect_lint(six_function_imports, list(message = lint_msg), linter)
lintr::expect_lint(six_function_imports_inline, list(message = lint_msg), linter)
})

test_that("box_func_import_count_linter resepect # nolint", {
linter <- box_func_import_count_linter()

no_lint <- "box::use(
dplyr[ # nolint
arrange,
select,
mutate,
distinct
],
)"

no_lint_inline <- "box::use(
dplyr[arrange, select, mutate, distinct], # nolint
)"

lintr::expect_lint(no_lint, NULL, linter)
lintr::expect_lint(no_lint_inline, NULL, linter)
})
Loading