-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
139 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,56 @@ | ||
#' Block usage of paste() and paste0() with messaging functions using ... | ||
#' | ||
#' `stop(paste0(...))` is strictly redundant -- `stop(...)` is equivalent. | ||
#' `stop(...)` is also preferable to `stop(paste(...))`. The same applies to | ||
#' all default condition functions, i.e., [stop()], [warning()], [message()], | ||
#' and [packageStartupMessage()]. | ||
#' | ||
#' @evalRd rd_tags("condition_message_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
condition_message_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$xml_parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
translators <- c("packageStartupMessage", "message", "warning", "stop") | ||
# TODO: refactor to work for raw-string equivalents | ||
xpath <- glue::glue("//expr[ | ||
expr[SYMBOL_FUNCTION_CALL[ {xp_text_in_table(translators)} ]] | ||
and expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'paste' or text() = 'paste0']] | ||
and not(SYMBOL_SUB[text() = 'collapse']) | ||
and ( | ||
not(SYMBOL_SUB[text() = 'sep']) | ||
or SYMBOL_SUB[ | ||
text() = 'sep' | ||
and following-sibling::expr[1]/STR_CONST[text() = '\"\"' or text() = '\" \"'] | ||
] | ||
) | ||
] | ||
]") | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
lint_message = function(expr) { | ||
outer_call <- xml2::xml_text(xml2::xml_find_first(expr, "expr/SYMBOL_FUNCTION_CALL")) | ||
inner_call <- xml2::xml_text(xml2::xml_find_first(expr, "expr/expr/SYMBOL_FUNCTION_CALL")) | ||
|
||
message <- sprintf("Don't use %s to build %s strings.", inner_call, outer_call) | ||
paste( | ||
message, | ||
"Instead use the fact that these functions build condition message strings from their input", | ||
'(using "" as a separator). For translateable strings, prefer using gettextf().' | ||
) | ||
}, | ||
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.
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,54 @@ | ||
test_that("condition_message_linter skips allowed usages", { | ||
expect_lint("stop('a string', 'another')", NULL, condition_message_linter()) | ||
expect_lint("warning('a string', 'another')", NULL, condition_message_linter()) | ||
expect_lint("message('a string', 'another')", NULL, condition_message_linter()) | ||
|
||
# paste/paste0 allowed when using other seps and/or collapse | ||
expect_lint("stop(paste(x, collapse = ''))", NULL, condition_message_linter()) | ||
expect_lint("message(paste(x, sep = '-'))", NULL, condition_message_linter()) | ||
|
||
# sprintf is OK (really should be gettextf but offering translations | ||
# at google internally is not likely to happen any time soon) | ||
expect_lint("stop(sprintf('A %s!', 'string'))", NULL, condition_message_linter()) | ||
}) | ||
|
||
test_that("condition_message_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"stop(paste('a string', 'another'))", | ||
rex::rex("Don't use paste to build stop strings."), | ||
condition_message_linter() | ||
) | ||
|
||
expect_lint( | ||
"warning(paste0('a string ', 'another'))", | ||
rex::rex("Don't use paste0 to build warning strings."), | ||
condition_message_linter() | ||
) | ||
|
||
# not thrown off by named arguments | ||
expect_lint( | ||
"stop(paste('a', 'b'), call. = FALSE)", | ||
rex::rex("Don't use paste to build stop strings."), | ||
condition_message_linter() | ||
) | ||
|
||
expect_lint( | ||
"warning(paste0('a', 'b'), immediate. = TRUE)", | ||
rex::rex("Don't use paste0 to build warning strings."), | ||
condition_message_linter() | ||
) | ||
}) | ||
|
||
test_that("packageStartupMessage usages are also matched", { | ||
expect_lint( | ||
"packageStartupMessage(paste('a string', 'another'))", | ||
rex::rex("Don't use paste to build packageStartupMessage strings."), | ||
condition_message_linter() | ||
) | ||
|
||
expect_lint( | ||
"packageStartupMessage(paste0('a string ', 'another'))", | ||
rex::rex("Don't use paste0 to build packageStartupMessage strings."), | ||
condition_message_linter() | ||
) | ||
}) |