Skip to content

Commit 5bc4dbe

Browse files
jcken95AshesITRMichaelChirico
authored
Remove pipe on end of commented code prior to checking parsability (#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]>
1 parent 1db7cc7 commit 5bc4dbe

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* `.lintr` configs set by option `lintr.linter_file` or environment variable `R_LINTR_LINTER_FILE` can point to subdirectories (#2512, @MichaelChirico).
3030
* `indentation_linter()` returns `ranges[1L]==1L` when the offending line has 0 spaces (#2550, @MichaelChirico).
3131
* `literal_coercion_linter()` doesn't surface a warning about NAs during coercion for code like `as.integer("a")` (#2566, @MichaelChirico).
32+
* `commented_code_linter()` can detect commented code that ends with a pipe (#2671, @jcken95)
3233

3334
## Changes to default linters
3435

R/commented_code_linter.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ commented_code_linter <- function() {
8383
all_comments <- xml_text(all_comment_nodes)
8484
code_candidates <- re_matches(all_comments, code_candidate_regex, global = FALSE, locations = TRUE)
8585
extracted_code <- code_candidates[, "code"]
86-
# ignore trailing ',' when testing for parsability
87-
extracted_code <- re_substitutes(extracted_code, rex(",", any_spaces, end), "")
86+
# ignore trailing ',' or pipes ('|>', '%>%') when testing for parsability
87+
extracted_code <- re_substitutes(extracted_code, rex(or(",", "|>", "%>%"), any_spaces, end), "")
8888
extracted_code <- re_substitutes(extracted_code, rex(start, any_spaces, ","), "")
89+
8990
is_parsable <- which(vapply(extracted_code, parsable, logical(1L)))
9091

9192
lint_list <- xml_nodes_to_lints(

tests/testthat/test-commented_code_linter.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,13 @@ test_that("commented_code_linter can detect operators in comments and lint corre
103103
commented_code_linter()
104104
)
105105
})
106+
107+
test_that("commented_code_linter can detect commented code ending with pipes", {
108+
linter <- commented_code_linter()
109+
lint_msg <- rex::rex("Remove commented code.")
110+
111+
expect_lint("# f() %>%", lint_msg, linter)
112+
113+
skip_if_not_r_version("4.1.0")
114+
expect_lint("# f() |>", lint_msg, linter)
115+
})

0 commit comments

Comments
 (0)