Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-masel committed Aug 2, 2023
1 parent fb19002 commit 33abc2f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 51 deletions.
59 changes: 37 additions & 22 deletions R/library_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,53 @@
#' Force library calls to all be at the top of the script.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = c("library(dplyr)", "print('test')", "library(tidyr)"),
#' linters = library_call_linter()
#' )
#' # will produce lints
#' lint(
#' text = "
#' library(dplyr)
#' print('test')
#' library(tidyr)
#' ",
#' linters = library_call_linter()
#' )
#'
#' lint(
#' text = c("library(dplyr)", "print('test')", "library(tidyr)", "library(purrr)"),
#' linters = library_call_linter()
#' )
#' lint(
#' text = "
#' library(dplyr)
#' print('test')
#' library(tidyr)
#' library(purrr)
#' ",
#' linters = library_call_linter()
#' )
#'
#' # okay
#' lint(
#' text = c("library(dplyr)", "print('test')"),
#' linters = library_call_linter()
#' )
#' # okay
#' lint(
#' text = "
#' library(dplyr)
#' print('test')
#' ",
#' linters = library_call_linter()
#' )
#'
#' lint(
#' text = c("# comment", "library(dplyr)"),
#' 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']]
(//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) {
Expand Down
51 changes: 33 additions & 18 deletions man/library_call_linter.Rd

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

70 changes: 59 additions & 11 deletions tests/testthat/test-library_call_linter.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
test_that("library_call_linter skips allowed usages", {

expect_lint(c("library(dplyr)", "print('test')"),
expect_lint(
trim_some("
library(dplyr)
print('test')
"),
NULL,
library_call_linter()
)
Expand All @@ -10,12 +14,20 @@ test_that("library_call_linter skips allowed usages", {
library_call_linter()
)

expect_lint(c("# comment", "library(dplyr)"),
expect_lint(
trim_some("
# comment
library(dplyr)
"),
NULL,
library_call_linter()
)

expect_lint(c("print('test')", "# library(dplyr)"),
expect_lint(
trim_some("
print('test')
# library(dplyr)
"),
NULL,
library_call_linter()
)
Expand All @@ -24,28 +36,64 @@ test_that("library_call_linter skips allowed usages", {
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(c("library(dplyr)", "print('test')", "library(tidyr)"),
expect_lint(
trim_some("
library(dplyr)
print('test')
library(tidyr)
"),
lint_message,
library_call_linter()
)

expect_lint(c("library(dplyr)", "print('test')", "library(tidyr)", "library(purrr)"),
list(lint_message, lint_message),
expect_lint(
trim_some("
library(dplyr)
print('test')
library(tidyr)
library(purrr)
"),
list(
list(lint_message, line_number = 3, column_number = 1),

Check warning on line 57 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=57,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 57 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=57,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 57 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=57,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 57 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=57,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.
list(lint_message, line_number = 4, column_number = 1)

Check warning on line 58 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=58,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 58 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=58,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 58 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=58,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 58 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=58,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.
),
library_call_linter()
)

expect_lint(c("library(dplyr)", "print('test')", "print('test')", "library(tidyr)"),
rex("Move all library calls to the top of the script."),
expect_lint(
trim_some("
library(dplyr)
print('test')
print('test')
library(tidyr)
"),
lint_message,
library_call_linter()
)

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

expect_lint(c("library(dplyr)", "print('test')", "library(tidyr)", "print('test')", "library(tidyr)"),
list(lint_message, lint_message),
expect_lint(
trim_some("
library(dplyr)
print('test')
library(tidyr)
print('test')
library(purrr)
"),
list(
list(lint_message, line_number = 3, column_number = 1),

Check warning on line 94 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=94,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 94 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=94,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 94 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=94,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 94 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=94,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.
list(lint_message, line_number = 5, column_number = 1)

Check warning on line 95 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=95,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 95 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-library_call_linter.R,line=95,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 95 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=95,col=41,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

Check warning on line 95 in tests/testthat/test-library_call_linter.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-library_call_linter.R,line=95,col=60,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.
),
library_call_linter()
)
})

0 comments on commit 33abc2f

Please sign in to comment.