-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
* New which_grepl_linter * correct test to use '|' * examples * new "regex" linter tag * forgot to check this in * wording * fix doc --------- Co-authored-by: Indrajeet Patil <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Require usage of grep over which(grepl(.)) | ||
#' | ||
#' `which(grepl(pattern, x))` is the same as `grep(pattern, x)`, but harder | ||
#' to read and requires two passes over the vector. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "which(grepl('^a', x))", | ||
#' linters = which_grepl_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = "which(grepl('^a', x) | grepl('^b', x))", | ||
#' linters = which_grepl_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("which_grepl_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
which_grepl_linter <- make_linter_from_xpath( | ||
xpath = " | ||
//SYMBOL_FUNCTION_CALL[text() = 'grepl'] | ||
/parent::expr | ||
/parent::expr | ||
/parent::expr[expr/SYMBOL_FUNCTION_CALL[text() = 'which']] | ||
", | ||
lint_message = "grep(pattern, x) is better than which(grepl(pattern, x))." | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
test_that("which_grepl_linter skips allowed usages", { | ||
# this _could_ be combined as p1|p2, but often it's cleaner to read this way | ||
expect_lint("which(grepl(p1, x) | grepl(p2, x))", NULL, which_grepl_linter()) | ||
}) | ||
|
||
test_that("which_grepl_linter blocks simple disallowed usages", { | ||
linter <- which_grepl_linter() | ||
lint_msg <- rex::rex("grep(pattern, x) is better than which(grepl(pattern, x)).") | ||
|
||
expect_lint("which(grepl('^a', x))", lint_msg, linter) | ||
# options also don't matter (grep has more arguments: value, invert) | ||
expect_lint("which(grepl('^a', x, perl = TRUE, fixed = TRUE))", lint_msg, linter) | ||
|
||
expect_lint( | ||
trim_some('{ | ||
which(x) | ||
grepl(y) | ||
which(grepl("pt1", x)) | ||
which(grepl("pt2", y)) | ||
}'), | ||
list( | ||
list(lint_msg, line_number = 4L, column_number = 3L), | ||
list(lint_msg, line_number = 5L, column_number = 3L) | ||
), | ||
linter | ||
) | ||
}) |