-
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
1 parent
d6bacad
commit 5da20e0
Showing
10 changed files
with
83 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,33 @@ | ||
#' Block usage of paste() with sep="" | ||
#' | ||
#' [paste0()] is a faster, more concise alternative to using `paste(sep = "")`. | ||
#' | ||
#' @evalRd rd_tags("paste_sep_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
paste_sep_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$xml_parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# NB: string-length()=2 means '' or "" (*not* 'ab' or 'cd' which have length 4) | ||
# TODO: adapt this for R>4.0 raw strings | ||
xpath <- "//expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'paste']] | ||
and SYMBOL_SUB[text() = 'sep']/following-sibling::expr[1][STR_CONST[string-length(text()) = 2]] | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
lint_message = 'paste0(...) is better than paste(..., sep = "").', | ||
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,24 @@ | ||
test_that("paste_sep_linter skips allowed usages", { | ||
expect_lint("paste('a', 'b', 'c')", NULL, paste_sep_linter()) | ||
expect_lint("paste('a', 'b', 'c', sep = ',')", NULL, paste_sep_linter()) | ||
expect_lint("paste('a', 'b', collapse = '')", NULL, paste_sep_linter()) | ||
expect_lint("cat(paste('a', 'b'), sep = '')", NULL, paste_sep_linter()) | ||
expect_lint("sep <- ''; paste('a', sep)", NULL, paste_sep_linter()) | ||
expect_lint("paste(sep = ',', '', 'a')", NULL, paste_sep_linter()) | ||
|
||
expect_lint("paste0('a', 'b', 'c')", NULL, paste_sep_linter()) | ||
}) | ||
|
||
test_that("paste_sep_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"paste(sep = '', 'a', 'b')", | ||
rex::rex('paste0(...) is better than paste(..., sep = "").'), | ||
paste_sep_linter() | ||
) | ||
|
||
expect_lint( | ||
"paste('a', 'b', sep = '')", | ||
rex::rex('paste0(...) is better than paste(..., sep = "").'), | ||
paste_sep_linter() | ||
) | ||
}) |