Skip to content

Commit

Permalink
Add pipe_consistency_linter
Browse files Browse the repository at this point in the history
  • Loading branch information
bairdj committed Jul 24, 2023
1 parent 25d24c0 commit f0fc3a2
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 2 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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 @@ -104,6 +104,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
56 changes: 56 additions & 0 deletions R/pipe_consistency_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Pipe consistency linter
#'
#' Check that all pipes are consistent (either all %>% or all |>).
#'
#' Checks consistency across an entire file, not just a single
#' expression.
#'
#' @examples
#' # will produce lints
#' 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() {
xpath_magrittir <- "//SPECIAL[text() = '%>%']"
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 <- xml2::xml_find_all(xml, xpath_magrittir)
match_native <- xml2::xml_find_all(xml, xpath_native)

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

if (n_magrittr == 0L || n_native == 0L) {
return(list())
}

xml_nodes_to_lints(
xml = c(match_magrittr, match_native),
source_expression = source_expression,
lint_message = "Use consistent pipe operators (either all %>% or all |>).",
type = "style"
)
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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
pipe_continuation_linter,style readability default
quotes_linter,style consistency readability default configurable
redundant_equals_linter,best_practices readability efficiency common_mistakes
Expand Down
5 changes: 3 additions & 2 deletions man/linters.Rd

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

39 changes: 39 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.

0 comments on commit f0fc3a2

Please sign in to comment.