Skip to content

add PIPE to infix_metadata #1793

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 4 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@

* `implicit_integer_linter()` gains parameter `allow_colon` to skip lints on expressions like `1:10` (#1155, @MichaelChirico)

* `infix_spaces_linter()` supports the native R pipe `|>` (#1793, @AshesITR)

* `unneeded_concatenation_linter()` no longer lints on `c(...)` (i.e., passing `...` in a function call)
when `allow_single_expression = FALSE` (#1696, @MichaelChirico)

Expand Down Expand Up @@ -101,7 +103,7 @@

* `routine_registration_linter()` for identifying native routines that don't use registration (`useDynLib` in the `NAMESPACE`; @MichaelChirico)

* `indentation_linter()` for checking that the indentation conforms to 2-space Tidyverse-style (@AshesITR and @dgkf, #1411).
* `indentation_linter()` for checking that the indentation conforms to 2-space Tidyverse-style (@AshesITR and @dgkf, #1411, #1792).


## Notes
Expand Down
3 changes: 2 additions & 1 deletion R/infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ infix_metadata <- data.frame(stringsAsFactors = FALSE, matrix(byrow = TRUE, ncol
"EQ_ASSIGN", "=",
"EQ_SUB", "=", # in calls: foo(x = 1)
"EQ_FORMALS", "=", # in definitions: function(x = 1)
"PIPE", "|>",
"SPECIAL", "%%",
"OP-SLASH", "/",
"OP-STAR", "*",
Expand Down Expand Up @@ -57,7 +58,7 @@ infix_metadata$unary <- infix_metadata$xml_tag %in% c("OP-PLUS", "OP-MINUS", "OP
# high-precedence operators are ignored by this linter; see
# https://style.tidyverse.org/syntax.html#infix-operators
infix_metadata$low_precedence <- infix_metadata$string_value %in% c(
"+", "-", "~", ">", ">=", "<", "<=", "==", "!=", "&", "&&", "|", "||", "<-", "->", "=", "%%", "/", "*"
"+", "-", "~", ">", ">=", "<", "<=", "==", "!=", "&", "&&", "|", "||", "<-", "->", "=", "%%", "/", "*", "|>"
)
# comparators come up in several lints
infix_metadata$comparator <- infix_metadata$string_value %in% c("<", "<=", ">", ">=", "==", "!=")
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-indentation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,26 @@ test_that("consecutive same-level lints are suppressed", {
indentation_linter()
)
})

test_that("native pipe is supported", {
skip_if_not_r_version("4.1")
linter <- indentation_linter()

expect_lint(
trim_some("
a |>
foo()
"),
NULL,
linter
)

expect_lint(
trim_some("
b <- a |>
foo()
"),
NULL,
linter
)
})
8 changes: 8 additions & 0 deletions tests/testthat/test-infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ test_that("Rules around missing arguments are respected", {
expect_lint("switch(a =, b = 2)", lint_msg, linter)
expect_lint("alist(missing_arg =)", lint_msg, linter)
})

test_that("native pipe is supported", {
skip_if_not_r_version("4.1")
linter <- infix_spaces_linter()

expect_lint("a |> foo()", NULL, linter)
expect_lint("a|>foo()", rex::rex("Put spaces around all infix operators."), linter)
})