-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New terminal_close_linter * typo * metadata tests * commit to single lint for multiple close()
- Loading branch information
1 parent
a1e2f1f
commit 984a399
Showing
10 changed files
with
126 additions
and
2 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,25 @@ | ||
#' Prohibit close() from terminating a function definition | ||
#' | ||
#' Functions that end in `close(x)` are almost always better written by using | ||
#' `on.exit(close(x))` close to where `x` is defined and/or opened. | ||
#' | ||
#' @evalRd rd_tags("terminal_close_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
terminal_close_linter <- make_linter_from_xpath( | ||
xpath = " | ||
//FUNCTION | ||
/following-sibling::expr | ||
/expr[last()][ | ||
expr/SYMBOL_FUNCTION_CALL[text() = 'close'] | ||
or expr[ | ||
SYMBOL_FUNCTION_CALL[text() = 'return'] | ||
and following-sibling::expr/expr/SYMBOL_FUNCTION_CALL[text() = 'close'] | ||
] | ||
] | ||
", | ||
lint_message = paste( | ||
"Use on.exit(close(x)) to close connections instead of", | ||
"running it as the last call in a function." | ||
) | ||
) |
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.
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,74 @@ | ||
test_that("terminal_close_linter skips allowed cases", { | ||
linter <- terminal_close_linter() | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
tmp <- tempfile() | ||
on.exit(close(tmp)) | ||
writeLines(bar, tmp) | ||
return(invisible()) | ||
} | ||
") | ||
expect_lint(lines, NULL, linter) | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
close <- bar + 1 | ||
return(close) | ||
} | ||
") | ||
expect_lint(lines, NULL, linter) | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
close <- bar + 1 | ||
close | ||
} | ||
") | ||
expect_lint(lines, NULL, linter) | ||
}) | ||
|
||
test_that("terminal_close_linter blocks simple cases", { | ||
linter <- terminal_close_linter() | ||
lint_msg <- rex::rex("Use on.exit(close(x)) to close connections") | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function(bar) { | ||
tmp <- tempfile() | ||
writeLines(bar, tmp) | ||
return(close(tmp)) | ||
} | ||
"), | ||
list(lint_msg, line_number = 4L, column_number = 3L), | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function(bar) { | ||
tmp <- tempfile() | ||
writeLines(bar, tmp) | ||
close(tmp) | ||
} | ||
"), | ||
list(lint_msg, line_number = 4L, column_number = 3L), | ||
linter | ||
) | ||
|
||
# When multiple terminations happen, only lint the one | ||
expect_lint( | ||
trim_some(" | ||
foo <- function(bar) { | ||
tmp1 <- tempfile() | ||
tmp2 <- tempfile() | ||
writeLines(bar, tmp1) | ||
writeLines(bar, tmp2) | ||
close(tmp1) | ||
close(tmp2) | ||
} | ||
"), | ||
list(lint_msg, line_number = 7L, column_number = 3L), | ||
linter | ||
) | ||
}) |