Skip to content
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

Use node-level | for efficiency in infix_spaces_linter #2025

Merged
merged 10 commits into from
Aug 2, 2023
10 changes: 5 additions & 5 deletions R/infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ infix_spaces_linter <- function(exclude_operators = NULL, allow_multiple_spaces

# NB: preceding-sibling::* and not preceding-sibling::expr because
# of the foo(a=1) case, where the tree is <SYMBOL_SUB><EQ_SUB><expr>
# NB: position() > 1 for the unary case, e.g. x[-1]
# NB: parent::*[count(expr | SYMBOL_SUB)) > 1] for the unary case, e.g. x[-1]
# SYMBOL_SUB for case with missing argument like alist(a =)
# NB: the last not() disables lints inside box::use() declarations
xpath <- glue::glue("//*[
({xp_or(paste0('self::', infix_tokens))})
and position() > 1
xpath <- paste(collapse = "|", glue::glue("//{infix_tokens}[
parent::*[count(expr | SYMBOL_SUB) > 1]
and (
(
@line1 = preceding-sibling::*[1]/@line2
Expand All @@ -166,7 +166,7 @@ infix_spaces_linter <- function(exclude_operators = NULL, allow_multiple_spaces
]
]
)
]")
]"))

Linter(function(source_expression) {
if (!is_lint_level(source_expression, "expression")) {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,14 @@ test_that("native pipe is supported", {
expect_lint("a |> foo()", NULL, linter)
expect_lint("a|>foo()", rex::rex("Put spaces around all infix operators."), linter)
})

test_that("mixed unary & binary operators aren't mis-lint", {
expect_lint(
"-1-1",
list(
message = rex::rex("Put spaces around all infix operators."),
column_number = 3L
),
infix_spaces_linter()
)
})
Loading