Skip to content

Commit

Permalink
return last node rather than last element
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-masel committed Aug 1, 2023
1 parent 25f76af commit 6a56619
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
20 changes: 12 additions & 8 deletions R/library_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#' linters = library_call_linter()
#' )
#'
#' lint(
#' text = c("library(dplyr)", "print('test')", "library(tidyr)", "library(purrr)"),
#' linters = library_call_linter()
#' )
#'
#' # okay
#' lint(
#' text = c("library(dplyr)", "print('test')"),
Expand All @@ -26,30 +31,29 @@
library_call_linter <- function() {

xpath <- "
//SYMBOL_FUNCTION_CALL[text() = 'library'][last()]
(//SYMBOL_FUNCTION_CALL[text() = 'library'])[last()]
//preceding::expr
/SYMBOL_FUNCTION_CALL[text() != 'library'][last()]
//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

writeLines(as.character(xml))

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

if (length(bad_expr) == 0L) {
return(list())
}

bad_expr_library <- xml2::xml_find_all(
xml,
"//SYMBOL_FUNCTION_CALL[text() = 'library'][last()]"
)

xml_nodes_to_lints(
bad_expr_library,
bad_expr,
source_expression = source_expression,
lint_message = "Move all library calls to the top of the script.",
type = "warning"
Expand Down
5 changes: 5 additions & 0 deletions man/library_call_linter.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-library_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test_that("library_call_linter warns on disallowed usages", {
)

expect_lint(c("library(dplyr)", "print('test')", "library(tidyr)", "library(purrr)"),
lint_message,
list(lint_message, lint_message),
library_call_linter()
)

Expand All @@ -45,7 +45,7 @@ test_that("library_call_linter warns on disallowed usages", {
)

expect_lint(c("library(dplyr)", "print('test')", "library(tidyr)", "print('test')", "library(tidyr)"),
lint_message,
list(lint_message, lint_message),
library_call_linter()
)
})

0 comments on commit 6a56619

Please sign in to comment.