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

Remove pipe on end of commented code prior to checking parsability #2672

Merged
merged 9 commits into from
Oct 24, 2024
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* `.lintr` configs set by option `lintr.linter_file` or environment variable `R_LINTR_LINTER_FILE` can point to subdirectories (#2512, @MichaelChirico).
* `indentation_linter()` returns `ranges[1L]==1L` when the offending line has 0 spaces (#2550, @MichaelChirico).
* `literal_coercion_linter()` doesn't surface a warning about NAs during coercion for code like `as.integer("a")` (#2566, @MichaelChirico).
* `commented_code_linter()` can detect commented out code ending with a pipe (#2671, @jcken95)
jcken95 marked this conversation as resolved.
Show resolved Hide resolved

## Changes to default linters

Expand Down
4 changes: 4 additions & 0 deletions R/commented_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ commented_code_linter <- function() {
# ignore trailing ',' when testing for parsability
extracted_code <- re_substitutes(extracted_code, rex(",", any_spaces, end), "")
extracted_code <- re_substitutes(extracted_code, rex(start, any_spaces, ","), "")
# ignore a trailing pipe (|> or %>%) when testing parsability
extracted_code <- re_substitutes(extracted_code, rex("|>", any_spaces, end), "")
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
extracted_code <- re_substitutes(extracted_code, rex("%>%", any_spaces, end), "")

is_parsable <- which(vapply(extracted_code, parsable, logical(1L)))

lint_list <- xml_nodes_to_lints(
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-commented_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,19 @@ test_that("commented_code_linter can detect operators in comments and lint corre
commented_code_linter()
)
})

test_that("commented_code_linter can detect commented code ending with a pipe", {
skip_if_not_r_version("4.1.0")

expect_lint(
"# f() |>",
rex::rex("Remove commented code."),
commented_code_linter()
)

expect_lint(
"# f() %>%",
rex::rex("Remove commented code."),
commented_code_linter()
)
})
Loading