Skip to content

Add pipe consistency linter #2018

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

Merged
merged 26 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f0fc3a2
Add pipe_consistency_linter
bairdj Jul 24, 2023
07f57dd
Add pipe_consistency tests
bairdj Jul 24, 2023
6e56008
Skip tests pre-native pipe support
bairdj Jul 24, 2023
7132a18
Fix typo
bairdj Jul 28, 2023
648ae96
Add pipe argument
bairdj Jul 28, 2023
e9b15f5
Add pipe_consistency_linter
bairdj Jul 24, 2023
496cdd8
Add pipe_consistency tests
bairdj Jul 24, 2023
3707d99
Skip tests pre-native pipe support
bairdj Jul 24, 2023
2279132
Merge branch 'pipe-consistency' of https://github.com/bairdj/lintr in…
bairdj Jul 28, 2023
9b42407
Fix unnecessary concatenation
bairdj Jul 28, 2023
c5d74ce
Merge branch 'main' into pipe-consistency
bairdj Aug 2, 2023
de9d356
Tweak documentation
bairdj Aug 2, 2023
a27486d
Show number of inconsistent instances
bairdj Aug 2, 2023
bbcb9fb
Make lint messages more concise
bairdj Aug 2, 2023
6c3da23
Update R/pipe_consistency_linter.R
bairdj Aug 16, 2023
eaff8a2
Update R/pipe_consistency_linter.R
bairdj Aug 16, 2023
483a8e4
Merge branch 'main' into pipe-consistency
bairdj Aug 22, 2023
c702ee4
Remove superfluous imports
bairdj Aug 22, 2023
d93d9a3
Support other magrittr pipes
bairdj Aug 22, 2023
c3a7fdb
Replace sprintf with glue
bairdj Aug 22, 2023
f02a4b4
Skip on old R
bairdj Aug 22, 2023
8a9666b
Fix line length
bairdj Aug 22, 2023
02ea7e2
Merge remote-tracking branch 'origin/main' into pipe-consistency
bairdj Sep 15, 2023
1e08abd
nit: prefer trailing space in glue()
MichaelChirico Sep 15, 2023
1752df1
clean-up tests consistency with other files
MichaelChirico Sep 15, 2023
b21e686
delint
MichaelChirico Sep 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Collate:
'paste_linter.R'
'path_utils.R'
'pipe_call_linter.R'
'pipe_consistency_linter.R'
'pipe_continuation_linter.R'
'quotes_linter.R'
'redundant_equals_linter.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export(paren_body_linter)
export(paren_brace_linter)
export(paste_linter)
export(pipe_call_linter)
export(pipe_consistency_linter)
export(pipe_continuation_linter)
export(quotes_linter)
export(redundant_equals_linter)
Expand Down
82 changes: 82 additions & 0 deletions R/pipe_consistency_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#' Pipe consistency linter
#'
#' Check that pipe operators are used consistently by file, or optionally
#' specify one valid pipe operator.
#'
#' @param pipe Which pipe operator is valid (either `"%>%"` or `"|>"`). By default
#' (`"auto"`), the linter has no preference but will check that each file uses
#' only one type of pipe operator.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "1:3 |> mean() %>% as.character()",
#' linters = pipe_consistency_linter()
#' )
#'
#' lint(
#' text = "1:3 %>% mean() %>% as.character()",
#' linters = pipe_consistency_linter("|>")
#' )
#'
#' # okay
#' lint(
#' text = "1:3 %>% mean() %>% as.character()",
#' linters = pipe_consistency_linter()
#' )
#'
#' lint(
#' text = "1:3 |> mean() |> as.character()",
#' linters = pipe_consistency_linter()
#' )
#' @evalRd rd_tags("pipe_consistency_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
pipe_consistency_linter <- function(pipe = c("auto", "%>%", "|>")) {
pipe <- match.arg(pipe)

xpath_magrittr <- glue("//SPECIAL[{ xp_text_in_table(magrittr_pipes) }]")
xpath_native <- "//PIPE"

Linter(function(source_expression) {
if (!is_lint_level(source_expression, "file")) {
return(list())
}

xml <- source_expression$full_xml_parsed_content

match_magrittr <- xml_find_all(xml, xpath_magrittr)
match_native <- xml_find_all(xml, xpath_native)

n_magrittr <- length(match_magrittr)
n_native <- length(match_native)

if (pipe == "auto" && n_magrittr > 0L && n_native > 0L) {
xml_nodes_to_lints(
xml = c(match_magrittr, match_native),
source_expression = source_expression,
lint_message = glue(
"Found {n_magrittr} instances of %>% and {n_native} instances of |>. ",
"Stick to one pipe operator."
),
type = "style"
)
} else if (pipe == "%>%" && n_native > 0L) {
xml_nodes_to_lints(
xml = match_native,
source_expression = source_expression,
lint_message = "Use the %>% pipe operator instead of the |> pipe operator.",
type = "style"
)
} else if (pipe == "|>" && n_magrittr > 0L) {
xml_nodes_to_lints(
xml = match_magrittr,
source_expression = source_expression,
lint_message = "Use the |> pipe operator instead of the %>% pipe operator.",
type = "style"
)
} else {
list()
}
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ paren_body_linter,style readability default
paren_brace_linter,style readability deprecated
paste_linter,best_practices consistency configurable
pipe_call_linter,style readability
pipe_consistency_linter,style readability configurable
pipe_continuation_linter,style readability default
quotes_linter,style consistency readability default configurable
redundant_equals_linter,best_practices readability efficiency common_mistakes
Expand Down
1 change: 1 addition & 0 deletions man/configurable_linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions man/pipe_consistency_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/readability_linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/style_linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions tests/testthat/test-pipe-consistency-linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
test_that("pipe_consistency skips allowed usage", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter()

expect_lint("1:3 %>% mean() %>% as.character()", NULL, linter)
expect_lint("1:3 |> mean() |> as.character()", NULL, linter)
# With no pipes
expect_lint("x <- 1:5", NULL, linter)
# Across multiple lines
expect_lint(
trim_some("
1:3 %>%
mean() %>%
as.character()
"),
NULL,
linter
)
})

test_that("pipe_consistency lints inconsistent usage", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter()
expected_msg <- rex("Found 1 instances of %>% and 1 instances of |>. Stick to one pipe operator.")

expect_lint(
"1:3 |> mean() %>% as.character()",
list(
list(message = expected_msg, line_number = 1L, column_number = 5L),
list(message = expected_msg, line_number = 1L, column_number = 15L)
),
linter
)

expect_lint(
"1:3 %>% mean() |> as.character()",
list(
list(message = expected_msg, line_number = 1L, column_number = 5L),
list(message = expected_msg, line_number = 1L, column_number = 16L)
),
linter
)

expect_lint(
trim_some("
1:3 %>%
mean() |>
as.character()
"),
list(
list(message = expected_msg, line_number = 1L, column_number = 5L),
list(message = expected_msg, line_number = 2L, column_number = 10L)
),
linter
)

expected_msg_multi <- rex("Found 1 instances of %>% and 2 instances of |>. Stick to one pipe operator.")
expect_lint(
"1:3 |> sort() |> mean() %>% as.character()",
list(
list(message = expected_msg_multi, line_number = 1L, column_number = 5L),
list(message = expected_msg_multi, line_number = 1L, column_number = 15L),
list(message = expected_msg_multi, line_number = 1L, column_number = 25L)
),
linter
)
})


test_that("pipe_consistency_linter works with |> argument", {
skip_if_not_r_version("4.1.0")

linter <- pipe_consistency_linter(pipe = "|>")
expected_message <- rex("Use the |> pipe operator instead of the %>% pipe operator.")

expect_lint(
trim_some("
1:3 %>%
mean() %>%
as.character()
"),
list(
list(message = expected_message, line_number = 1L, column_number = 5L),
list(message = expected_message, line_number = 2L, column_number = 10L)
),
linter
)

expect_lint(
trim_some("
1:3 |>
mean() %>%
as.character()
"),
list(message = expected_message, line_number = 2L, column_number = 10L),
linter
)

expect_lint(
"1:3 |> mean() |> as.character()",
NULL,
linter
)

expect_lint(
trim_some("
1:3 |>
mean() %>%
as.character()
"),
list(message = expected_message, line_number = 2L, column_number = 10L),
linter
)
})

test_that("pipe_consistency_linter works with %>% argument", {
skip_if_not_r_version("4.1.0")

linter <- pipe_consistency_linter(pipe = "%>%")
expected_message <- rex("Use the %>% pipe operator instead of the |> pipe operator.")

expect_lint(
"1:3 |> mean() |> as.character()",
list(
list(message = expected_message, line_number = 1L, column_number = 5L),
list(message = expected_message, line_number = 1L, column_number = 15L)
),
linter
)

expect_lint(
"1:3 %>% mean() |> as.character()",
list(message = expected_message, line_number = 1L, column_number = 16L),
linter
)

expect_lint(
"1:3 %>% mean() %>% as.character()",
NULL,
linter
)

expect_lint(
trim_some("
1:3 %>%
mean() |>
as.character()
"),
list(message = expected_message, line_number = 2L, column_number = 10L),
linter
)
})

test_that("pipe_consistency_linter works with other magrittr pipes", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter()
expected_message <- rex("Found 1 instances of %>% and 1 instances of |>. Stick to one pipe operator.")

expect_lint("1:3 %>% mean() %T% print()", NULL, linter)
expect_lint(
"1:3 |> mean() %T>% print()",
list(
list(message = expected_message, line_number = 1L, column_number = 5L),
list(message = expected_message, line_number = 1L, column_number = 15L)
),
linter
)
})