-
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 paste_to_string_linter * improve xpath readability * remove dQuote/sQuote * note better alternatives * missing comma * update documenation * roxygenize again
- Loading branch information
1 parent
2309d0e
commit b88ca02
Showing
11 changed files
with
108 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,39 @@ | ||
#' Block usage of paste() with collapse=", " | ||
#' | ||
#' [toString()] is a more concise and expressive wrapper for aggregating string | ||
#' vectors with `paste(., collapse = ", ")`. | ||
#' | ||
#' @evalRd rd_tags("paste_to_string_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
paste_to_string_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$xml_parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# 3 expr: the function call, the argument, and collapse= | ||
# TODO(michaelchirico): catch raw-string equivalents | ||
seps <- c("', '", '", "') | ||
xpath <- glue::glue("//expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'paste' or text() = 'paste0']] | ||
and count(expr) = 3 | ||
and SYMBOL_SUB[text() = 'collapse']/following-sibling::expr[1][STR_CONST[ {xp_text_in_table(seps)} ]] | ||
]") | ||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
lint_message = paste( | ||
'toString(.) is more expressive than paste(., collapse = ", ").', | ||
"Note also glue::glue_collapse() and and::and()", | ||
"for constructing human-readable / translation-friendly lists" | ||
), | ||
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
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,33 @@ | ||
test_that("paste_to_string_linter skips allowed usages", { | ||
expect_lint("paste('a', 'b', 'c')", NULL, paste_to_string_linter()) | ||
expect_lint("paste(x, sep = ', ')", NULL, paste_to_string_linter()) | ||
expect_lint("paste(x, collapse = ',')", NULL, paste_to_string_linter()) | ||
expect_lint("paste(foo(x), collapse = '/')", NULL, paste_to_string_linter()) | ||
# harder to catch statically | ||
expect_lint("collapse <- ', '; paste(x, collapse = collapse)", NULL, paste_to_string_linter()) | ||
|
||
# paste(..., sep=sep, collapse=", ") is not a trivial swap to toString | ||
expect_lint("paste(x, y, sep = '.', collapse = ', ')", NULL, paste_to_string_linter()) | ||
# any call involving ...length() > 1 will implicitly use the default sep | ||
expect_lint("paste(x, y, collapse = ', ')", NULL, paste_to_string_linter()) | ||
expect_lint("paste0(x, y, collapse = ', ')", NULL, paste_to_string_linter()) | ||
|
||
expect_lint("toString(x)", NULL, paste_to_string_linter()) | ||
|
||
# string match of ", " is OK -- lint only _exact_ match | ||
expect_lint('paste(x, collapse = ", \n")', NULL, paste_to_string_linter()) | ||
}) | ||
|
||
test_that("paste_to_string_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"paste(collapse = ', ', x)", | ||
rex::rex('toString(.) is more expressive than paste(., collapse = ", ")'), | ||
paste_to_string_linter() | ||
) | ||
|
||
expect_lint( | ||
"paste0(foo(x), collapse = ', ')", | ||
rex::rex('toString(.) is more expressive than paste(., collapse = ", ")'), | ||
paste_to_string_linter() | ||
) | ||
}) |