-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for
namespace::function()
calls (#121)
* add namespaced_function_calls to default linters
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: box.linters | ||
Title: Linters for 'box' Modules | ||
Version: 0.9.1.9004 | ||
Version: 0.9.1.9005 | ||
Authors@R: | ||
c( | ||
person("Ricardo Rodrigo", "Basa", role = c("aut", "cre"), email = "[email protected]"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#' Check that `namespace::function()` calls except for `box::*()` are not made. | ||
#' | ||
#' @param allow Character vector of `namespace` or `namespace::function` to allow in the source | ||
#' code. Take not that the `()` are not included. The `box` namespace will always be allowed | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' code <- "box::use(package) | ||
#' tidyr::pivot_longer()" | ||
#' | ||
#' lintr::lint(text = code, linters = namespaced_function_calls()) | ||
#' | ||
#' ## allow `tidyr::pivot_longer()` | ||
#' code <- "box::use(package) | ||
#' tidyr::pivot_longer() | ||
#' tidyr::pivot_wider()" | ||
#' | ||
#' lintr::lint(text = code, linters = namespaced_function_calls(allow = c("tidyr::pivot_longer"))) | ||
#' | ||
#' # okay | ||
#' code <- "box::use(package)" | ||
#' | ||
#' lintr::lint(text = code, linters = namespaced_function_calls()) | ||
#' | ||
#' ## allow all `tidyr` | ||
#' code <- "box::use(package) | ||
#' tidyr::pivot_longer() | ||
#' tidyr::pivot_wider()" | ||
#' | ||
#' lintr::lint(text = code, linters = namespaced_function_calls(allow = c("tidyr"))) | ||
#' | ||
#' ## allow `tidyr::pivot_longer()` | ||
#' code <- "box::use(package) | ||
#' tidyr::pivot_longer()" | ||
#' | ||
#' lintr::lint(text = code, linters = namespaced_function_calls(allow = c("tidyr::pivot_longer"))) | ||
#' | ||
#' @export | ||
namespaced_function_calls <- function(allow = NULL) { | ||
always_allow <- c("box") | ||
|
||
allow <- c(always_allow, allow) | ||
|
||
ts_query <- " | ||
function: (namespace_operator | ||
lhs: (identifier) @pkg_namespace | ||
operator: \"::\" | ||
rhs: (identifier) | ||
) @full_namespace" | ||
|
||
lint_message <- "Explicit `package::function()` calls are not advisible when using `box` modules." | ||
|
||
lintr::Linter(function(source_expression) { | ||
if (!lintr::is_lint_level(source_expression, "file")) { | ||
return(list()) | ||
} | ||
source_text <- paste(source_expression$file_lines, collapse = "\n") | ||
|
||
tree_root <- ts_root(source_text) | ||
|
||
tree_namespaced <- ts_find_all(tree_root, ts_query) | ||
|
||
lapply(tree_namespaced[[1]], function(tree_node) { | ||
full_idx <- match("full_namespace", tree_node$name) | ||
full_text <- treesitter::node_text(tree_node$node[[full_idx]]) | ||
pkg_idx <- match("pkg_namespace", tree_node$name) | ||
pkg_text <- treesitter::node_text(tree_node$node[[pkg_idx]]) | ||
|
||
namespaced_calls <- c(full_text, pkg_text) | ||
|
||
if (all(!namespaced_calls %in% allow)) { | ||
start_where <- treesitter::node_start_point(tree_node$node[[full_idx]]) | ||
line_number <- treesitter::point_row(start_where) + 1 | ||
start_col_number <- treesitter::point_column(start_where) + 1 | ||
end_where <- treesitter::node_end_point(tree_node$node[[full_idx]]) | ||
end_col_number <- treesitter::point_column(end_where) | ||
lintr::Lint( | ||
source_expression$filename, | ||
line_number = line_number, | ||
column_number = start_col_number, | ||
type = "warning", | ||
message = lint_message, | ||
line = source_expression$file_lines[line_number], | ||
ranges = list(c(start_col_number, end_col_number)) | ||
) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
test_that("namespaced_function_calls() skips allowed box::*() calls", { | ||
linter <- namespaced_function_calls() | ||
|
||
code <- "box::use(package) | ||
box::export() | ||
box::file() | ||
box::name()" | ||
|
||
lintr::expect_lint(code, NULL, linter) | ||
}) | ||
|
||
test_that("namespaced_function_calls() blocks non-box namespace calls", { | ||
linter <- namespaced_function_calls() | ||
lint_message <- rex::rex( | ||
"Explicit `package::function()` calls are not advisible when using `box` modules." | ||
) | ||
|
||
code <- "box::use(package) | ||
tidyr::pivot_longer() | ||
" | ||
|
||
lintr::expect_lint(code, list(message = lint_message), linter) | ||
}) | ||
|
||
test_that("namespaced_function_calls() skips excluded namespace calls", { | ||
linter <- namespaced_function_calls(allow = c("tidyr")) | ||
|
||
code <- "box::use(package) | ||
tidyr::pivot_longer() | ||
" | ||
|
||
lintr::expect_lint(code, NULL, linter) | ||
}) | ||
|
||
test_that("namespaced_function_calls() skips excluded namespace::function calls", { | ||
linter <- namespaced_function_calls(allow = c("tidyr::pivot_longer")) | ||
|
||
code <- "box::use(package) | ||
tidyr::pivot_longer() | ||
" | ||
|
||
lintr::expect_lint(code, NULL, linter) | ||
}) |