From 5bc4dbe4396e558725542cdca7dc9599a2dd1d2b Mon Sep 17 00:00:00 2001 From: Jack Kennedy <57638616+jcken95@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:42:48 +0100 Subject: [PATCH] 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 * avoid extra nesting * further condense test code --------- Co-authored-by: AshesITR Co-authored-by: Michael Chirico --- NEWS.md | 1 + R/commented_code_linter.R | 5 +++-- tests/testthat/test-commented_code_linter.R | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index d017c606a..b827f1e7a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/commented_code_linter.R b/R/commented_code_linter.R index fcc4af9ef..97f9c8d8e 100644 --- a/R/commented_code_linter.R +++ b/R/commented_code_linter.R @@ -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( diff --git a/tests/testthat/test-commented_code_linter.R b/tests/testthat/test-commented_code_linter.R index fadc98d5e..5154a5a94 100644 --- a/tests/testthat/test-commented_code_linter.R +++ b/tests/testthat/test-commented_code_linter.R @@ -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) +})