Skip to content

Commit

Permalink
integrate new linter
Browse files Browse the repository at this point in the history
  • Loading branch information
radbasa committed Jan 26, 2024
1 parent 86c413b commit 367103f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 75 deletions.
53 changes: 53 additions & 0 deletions R/box_linters.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
#' Box library separate packages and module imports linter
#'
#' Checks that packages and modules are imported in separate `box::use()` statements.
#'
#' @examples
#' # will produce lints
#' lintr::lint(
#' text = "box::use(package, path/to/file)",
#' linters = box_separate_calls_linter()
#' )
#'
#' lintr::lint(
#' text = "box::use(path/to/file, package)",
#' linters = box_separate_calls_linter()
#' )
#'
#' # okay
#' lintr::lint(
#' text = "box::use(package1, package2)
#' box::use(path/to/file1, path/to/file2)",
#' linters = box_separate_calls_linter()
#' )
#'
#' @export
box_separate_calls_linter <- function() {
xpath <- "
//SYMBOL_PACKAGE[(text() = 'box' and following-sibling::SYMBOL_FUNCTION_CALL[text() = 'use'])]
/parent::expr
/parent::expr[
./child::expr[child::SYMBOL] and
./child::expr[child::expr[child::OP-SLASH]]
]
"
lint_message <- "Separate packages and modules in their respective box::use() calls."

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 universal import linter
#'
#' Checks that all function imports are explicit. `package[...]` is not used.
Expand Down
52 changes: 0 additions & 52 deletions R/linter_box_separate_packages_modules.R
Original file line number Diff line number Diff line change
@@ -1,52 +0,0 @@
#' Box library separate packages and module imports linter
#'
#' Checks that packages and modules are imported in separate `box::use()` statements.
#'
#' @examples
#' # will produce lints
#' lintr::lint(
#' text = "box::use(package, path/to/file)",
#' linters = box_separate_calls_linter()
#' )
#'
#' lintr::lint(
#' text = "box::use(path/to/file, package)",
#' linters = box_separate_calls_linter()
#' )
#'
#' # okay
#' lintr::lint(
#' text = "box::use(package1, package2)
#' box::use(path/to/file1, path/to/file2)",
#' linters = box_separate_calls_linter()
#' )
#'
#' @export
box_separate_calls_linter <- function() {
xpath <- "
//SYMBOL_PACKAGE[(text() = 'box' and following-sibling::SYMBOL_FUNCTION_CALL[text() = 'use'])]
/parent::expr
/parent::expr[
./child::expr[child::SYMBOL] and
./child::expr[child::expr[child::OP-SLASH]]
]
"
lint_message <- "Separate packages and modules in their respective box::use() calls."

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"
)
})
}
2 changes: 1 addition & 1 deletion man/box_separate_calls_linter.Rd

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

Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
good_box_calls <- "box::use(
dplyr,
shiny,
)
box::use(
path/to/file1,
path/to/file2,
)"

bad_box_call_1 <- "box::use(
dplyr,
path/to/file,
)"

bad_box_call_2 <- "box::use(
path/to/file,
dplyr,
)"

lint_message <- rex::rex("Separate packages and modules in their respective box::use() calls.")

test_that("box_separate_calls_linter skips allowed box::use() calls", {
linter <- box_separate_calls_linter()

good_box_calls <- "box::use(
dplyr,
shiny,
)
box::use(
path/to/file1,
path/to/file2,
)"

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

test_that("box_separate_calls_linter blocks packages and modules in same box::use() call", {
linter <- box_separate_calls_linter()

bad_box_call_1 <- "box::use(
dplyr,
path/to/file,
)"

bad_box_call_2 <- "box::use(
path/to/file,
dplyr,
)"

lint_message <- rex::rex("Separate packages and modules in their respective box::use() calls.")

lintr::expect_lint(bad_box_call_1, list(message = lint_message), linter)
lintr::expect_lint(bad_box_call_2, list(message = lint_message), linter)
})

0 comments on commit 367103f

Please sign in to comment.