-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add library call linter * tests * correct lint findings * fix lint results * return last node rather than last element * remove debug code * review comments * add explicit L for integers * indent XPath one more level * add readability * added news --------- Co-authored-by: Nicholas Masel <[email protected]> Co-authored-by: AshesITR <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
- Loading branch information
1 parent
37e9c39
commit 3e6b862
Showing
11 changed files
with
245 additions
and
3 deletions.
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
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,75 @@ | ||
#' Library call linter | ||
#' | ||
#' Force library calls to all be at the top of the script. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = " | ||
#' library(dplyr) | ||
#' print('test') | ||
#' library(tidyr) | ||
#' ", | ||
#' linters = library_call_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = " | ||
#' library(dplyr) | ||
#' print('test') | ||
#' library(tidyr) | ||
#' library(purrr) | ||
#' ", | ||
#' linters = library_call_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = " | ||
#' library(dplyr) | ||
#' print('test') | ||
#' ", | ||
#' linters = library_call_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = " | ||
#' # comment | ||
#' library(dplyr) | ||
#' ", | ||
#' linters = library_call_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("library_call_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
library_call_linter <- function() { | ||
|
||
xpath <- " | ||
(//SYMBOL_FUNCTION_CALL[text() = 'library'])[last()] | ||
/preceding::expr | ||
/SYMBOL_FUNCTION_CALL[text() != 'library'][last()] | ||
/following::expr[SYMBOL_FUNCTION_CALL[text() = 'library']] | ||
" | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "file")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$full_xml_parsed_content | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
if (length(bad_expr) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = "Move all library calls to the top of the script.", | ||
type = "warning" | ||
) | ||
}) | ||
} |
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,99 @@ | ||
test_that("library_call_linter skips allowed usages", { | ||
|
||
expect_lint( | ||
trim_some(" | ||
library(dplyr) | ||
print('test') | ||
"), | ||
NULL, | ||
library_call_linter() | ||
) | ||
|
||
expect_lint("print('test')", | ||
NULL, | ||
library_call_linter() | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
# comment | ||
library(dplyr) | ||
"), | ||
NULL, | ||
library_call_linter() | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
print('test') | ||
# library(dplyr) | ||
"), | ||
NULL, | ||
library_call_linter() | ||
) | ||
}) | ||
|
||
test_that("library_call_linter warns on disallowed usages", { | ||
lint_message <- rex::rex("Move all library calls to the top of the script.") | ||
|
||
expect_lint( | ||
trim_some(" | ||
library(dplyr) | ||
print('test') | ||
library(tidyr) | ||
"), | ||
lint_message, | ||
library_call_linter() | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
library(dplyr) | ||
print('test') | ||
library(tidyr) | ||
library(purrr) | ||
"), | ||
list( | ||
list(lint_message, line_number = 3L, column_number = 1L), | ||
list(lint_message, line_number = 4L, column_number = 1L) | ||
), | ||
library_call_linter() | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
library(dplyr) | ||
print('test') | ||
print('test') | ||
library(tidyr) | ||
"), | ||
lint_message, | ||
library_call_linter() | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
library(dplyr) | ||
print('test') | ||
library(tidyr) | ||
print('test') | ||
"), | ||
lint_message, | ||
library_call_linter() | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
library(dplyr) | ||
print('test') | ||
library(tidyr) | ||
print('test') | ||
library(purrr) | ||
"), | ||
list( | ||
list(lint_message, line_number = 3L, column_number = 1L), | ||
list(lint_message, line_number = 5L, column_number = 1L) | ||
), | ||
library_call_linter() | ||
) | ||
}) |