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 universal import linter #533

Merged
merged 12 commits into from
Jan 26, 2024
6 changes: 4 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.7.0.9000
Version: 1.7.0.9001
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 1.6.0.9001

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means dev_next should be 1.6.0.9000

Authors@R:
c(
person("Kamil", "Żyła", role = c("aut", "cre"), email = "[email protected]"),
Expand Down Expand Up @@ -44,9 +44,11 @@ Suggests:
knitr,
mockery,
rcmdcheck,
rex,
rmarkdown,
shiny.react,
spelling
spelling,
xml2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As xml2 is used in the code, not in tests, it should be Imported instead of being Suggested.

LazyData: true
Config/testthat/edition: 3
Config/testthat/parallel: true
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_universal_import_linter)
export(build_js)
export(build_sass)
export(diagnostics)
Expand Down
55 changes: 55 additions & 0 deletions R/linter_box_universal_import_linter.R
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest having all linters in a single file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' Box library universal import linter
#'
#' Checks that all function imports are explicit. `package[...]` is not used.
#'
#' @examples
#' # will produce lints
#' lintr::lint(
#' text = "box::use(base[...])",
#' linters = box_universal_import_linter()
#' )
#'
#' lintr::lint(
#' text = "box::use(path/to/file[...])",
#' linters = box_universal_import_linter()
#' )
#'
#' # okay
#' lintr::lint(
#' text = "box::use(base[print])",
#' linters = box_universal_import_linter()
#' )
#'
#' lintr::lint(
#' text = "box::use(path/to/file[do_something])",
#' linters = box_universal_import_linter()
#' )
#'
#' @export
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add @return (CRAN requirement).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems r-lib/lintr is not CRAN-compliant.

box_universal_import_linter <- function() {
lint_message <- "Explicitly declare imports rather than universally import with `...`."

xpath <- "
//SYMBOL_PACKAGE[(text() = 'box' and following-sibling::SYMBOL_FUNCTION_CALL[text() = 'use'])]
/parent::expr
/parent::expr
//SYMBOL[text() = '...']
"

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"
)
})
}
1 change: 1 addition & 0 deletions inst/templates/app_structure/dot.lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linters:
linters_with_defaults(
line_length_linter = line_length_linter(100),
box_universal_import_linter = rhino::box_universal_import_linter(),
object_usage_linter = NULL # Does not work with `box::use()`.
)
35 changes: 35 additions & 0 deletions man/box_universal_import_linter.Rd

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

4 changes: 4 additions & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ reference:
- diagnostics
- test_e2e

- title: Linters
contents:
- box_universal_import_linter

- title: Data
contents:
- rhinos
4 changes: 2 additions & 2 deletions tests/e2e/app-files/test-hello.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
box::use(
shiny[testServer],
testthat[...],
testthat[describe, expect_identical, it],
)
box::use(
app/view/hello[...],
app/view/hello[server],
)

describe("hello$server()", {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/app-files/test-say_hello.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
box::use(testthat[...])
box::use(testthat[describe, expect_identical, it], )

box::use(app/logic/say_hello[say_hello])
box::use(app/logic/say_hello[say_hello], )

describe("say_hello()", {
it("should say hello with the correct name", {
Expand Down
71 changes: 71 additions & 0 deletions tests/testthat/test-linter_box_universal_import_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
good_package_imports <- "box::use(
dplyr[select, mutate, ],
stringr[str_sub, str_match, ],
)
"

good_module_imports <- "box::use(
path/to/file1[do_something, do_another, ],
path/to/file2[find_x, find_y, ],
)
"

bad_package_imports <- "box::use(
dplyr[...],
stringr[str_sub, str_match, ],
)
"

bad_module_imports <- "box::use(
path/to/file1[...],
path/to/file2[find_x, find_y, ],
)
"

function_with_three_dots <- "some_function <- function(...) {
sum(...)
}
"

no_lint <- "box::use(
shiny[...], # nolint
)
"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move test cases inside tests - this way they will be easier to read.


lint_msg <- rex::rex("Explicitly declare imports rather than universally import with `...`.")

test_that("box_universal_count_linter skips allowed package import usage", {
linter <- box_universal_import_linter()

lintr::expect_lint(good_package_imports, NULL, linter)
})

test_that("box_universal_count_linter skips allowed module import usage", {
linter <- box_universal_import_linter()

lintr::expect_lint(good_module_imports, NULL, linter)
})

test_that("box_universal_count_linter blocks disallowed package import usage", {
linter <- box_universal_import_linter()

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

test_that("box_universal_count_linter blocks disallowed module import usage", {
linter <- box_universal_import_linter()

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

test_that("box_universal_count_linter skips three dots in function declarations and calls", {
linter <- box_universal_import_linter()

lintr::expect_lint(function_with_three_dots, NULL, linter)
})

test_that("box_universal_count_linter respects #nolint", {
linter <- box_universal_import_linter()

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