Skip to content

Commit 8d310a7

Browse files
fix paste(collapse='') lints inside pipes (#2572)
* fix paste(collapse='') lints inside pipes * pipes() is the test helper, not namespace object * longer line * parameterize test
1 parent 79d1059 commit 8d310a7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

R/paste_linter.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ paste_linter <- function(allow_empty_sep = FALSE,
167167
empty_paste_note <-
168168
'Note that paste() converts empty inputs to "", whereas file.path() leaves it empty.'
169169

170-
paste0_collapse_xpath <- "
170+
paste0_collapse_xpath <- glue::glue("
171171
parent::expr
172172
/parent::expr[
173173
SYMBOL_SUB[text() = 'collapse']
174-
and count(expr) = 3
174+
and count(expr) =
175+
3 - count(preceding-sibling::*[self::PIPE or self::SPECIAL[{ xp_text_in_table(magrittr_pipes) }]])
175176
and not(expr/SYMBOL[text() = '...'])
176177
]
177-
"
178+
")
178179

179180
Linter(linter_level = "expression", function(source_expression) {
180181
paste_calls <- source_expression$xml_find_function_calls("paste")

tests/testthat/test-paste_linter.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,22 @@ test_that("paste0(collapse=...) is caught", {
249249
expect_lint("paste0(foo(x, y, z), collapse = '')", lint_msg, linter)
250250
})
251251

252+
local({
253+
linter <- paste_linter()
254+
lint_msg <- rex::rex("Use paste(), not paste0(), to collapse a character vector when sep= is not used.")
255+
pipes <- pipes()
256+
257+
patrick::with_parameters_test_that(
258+
"paste0(collapse=...) is caught in pipes",
259+
{
260+
expect_lint(sprintf('x %s paste0(y, collapse = "")', pipe), NULL, linter)
261+
expect_lint(sprintf('x %s paste0(collapse = "")', pipe), lint_msg, linter)
262+
},
263+
pipe = pipes,
264+
.test_name = pipes
265+
)
266+
})
267+
252268
test_that("paste0(collapse=...) cases interacting with other rules are handled", {
253269
linter <- paste_linter()
254270
lint_msg <- rex::rex("Use paste(), not paste0(), to collapse a character vector when sep= is not used.")

0 commit comments

Comments
 (0)