Skip to content

Commit

Permalink
Remove pipe on end of commented code prior to checking parsability (#…
Browse files Browse the repository at this point in the history
…2672)

* fix: remove base pipe from end of line before detecting parsability

* fix: remove magrittr pipe from end of line before detecting parsability

* feat: add test

* chore: add commented code linter ignoring end pipe to news

* fix: seperate tests for magrittr/base pipe to allow for minimum R version

* refac: check for trailing comma/pipes in one fn call

* chore: minor rewording of news

Co-authored-by: AshesITR <[email protected]>

* avoid extra nesting

* further condense test code

---------

Co-authored-by: AshesITR <[email protected]>
Co-authored-by: Michael Chirico <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 1db7cc7 commit 5bc4dbe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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 code that ends with a pipe (#2671, @jcken95)

## Changes to default linters

Expand Down
5 changes: 3 additions & 2 deletions R/commented_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ commented_code_linter <- function() {
all_comments <- xml_text(all_comment_nodes)
code_candidates <- re_matches(all_comments, code_candidate_regex, global = FALSE, locations = TRUE)
extracted_code <- code_candidates[, "code"]
# ignore trailing ',' when testing for parsability
extracted_code <- re_substitutes(extracted_code, rex(",", any_spaces, end), "")
# ignore trailing ',' or pipes ('|>', '%>%') when testing for parsability
extracted_code <- re_substitutes(extracted_code, rex(or(",", "|>", "%>%"), any_spaces, end), "")
extracted_code <- re_substitutes(extracted_code, rex(start, any_spaces, ","), "")

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

lint_list <- xml_nodes_to_lints(
Expand Down
10 changes: 10 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,13 @@ 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 pipes", {
linter <- commented_code_linter()
lint_msg <- rex::rex("Remove commented code.")

expect_lint("# f() %>%", lint_msg, linter)

skip_if_not_r_version("4.1.0")
expect_lint("# f() |>", lint_msg, linter)
})

0 comments on commit 5bc4dbe

Please sign in to comment.