Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnecessary_placeholder_linter() covers a few other ops #1681

Merged
merged 5 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/unnecessary_placeholder_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#' passed as the first positional argument; using it can cause confusion
#' and impacts readability.
#'
#' This is true for forward (`%>%`), assignment (`%<>%`), and tee (`%T>%`) operators.
#'
#' @examples
#' # will produce lints
#' lint(
Expand Down Expand Up @@ -33,7 +35,7 @@
unnecessary_placeholder_linter <- function() {
# TODO(michaelchirico): handle R4.2.0 native placeholder _ as well
xpath <- "
//SPECIAL[text() = '%>%']
//SPECIAL[text() = '%>%' or text() = '%T>%' or text() = '%<>%']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note which are covered in the documentation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Done.

We can revise this once the native pipe is also supported.

/following-sibling::expr[
expr/SYMBOL_FUNCTION_CALL
and not(expr[
Expand Down
7 changes: 5 additions & 2 deletions man/sprintf_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/unnecessary_placeholder_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 36 additions & 23 deletions tests/testthat/test-unnecessary_placeholder_linter.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
test_that("unnecessary_placeholder_linter skips allowed usages", {
# . used in position other than first --> ok
expect_lint("x %>% foo(y, .)", NULL, unnecessary_placeholder_linter())
# ditto for nested usage
expect_lint("x %>% foo(y, bar(.))", NULL, unnecessary_placeholder_linter())
# . used twice --> ok
expect_lint("x %>% foo(., .)", NULL, unnecessary_placeholder_linter())
# . used as a keyword argument --> ok
expect_lint("x %>% foo(arg = .)", NULL, unnecessary_placeholder_linter())
})
skip_if_not_installed("patrick")
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
patrick::with_parameters_test_that(
"unnecessary_placeholder_linter skips allowed usages",
{
linter <- unnecessary_placeholder_linter()

test_that("unnecessary_placeholder_linter blocks simple disallowed usages", {
expect_lint(
"x %>% sum(.)",
rex::rex("Don't use the placeholder (`.`) when it's not needed"),
unnecessary_placeholder_linter()
)
# . used in position other than first --> ok
expect_lint(sprintf("x %s foo(y, .)", pipe), NULL, linter)
# ditto for nested usage
expect_lint(sprintf("x %s foo(y, bar(.))", pipe), NULL, linter)
# . used twice --> ok
expect_lint(sprintf("x %s foo(., .)", pipe), NULL, linter)
# . used as a keyword argument --> ok
expect_lint(sprintf("x %s foo(arg = .)", pipe), NULL, linter)
},
.test_name = c("forward", "assignment", "tee"),
pipe = c("%>%", "%<>%", "%T>%")
)

# can come anywhere in the pipeline
expect_lint(
"x %>% y(.) %>% sum()",
rex::rex("Don't use the placeholder (`.`) when it's not needed"),
unnecessary_placeholder_linter()
)
})
patrick::with_parameters_test_that(
"unnecessary_placeholder_linter blocks simple disallowed usages",
{
linter <- unnecessary_placeholder_linter()
expect_lint(
sprintf("x %s sum(.)", pipe),
rex::rex("Don't use the placeholder (`.`) when it's not needed"),
unnecessary_placeholder_linter()
)

expect_lint(
sprintf("x %s y(.) %s sum()", pipe, pipe),
rex::rex("Don't use the placeholder (`.`) when it's not needed"),
unnecessary_placeholder_linter()
)
},
.test_name = c("forward", "assignment", "tee"),
pipe = c("%>%", "%<>%", "%T>%")
)