Skip to content

Commit f9bf506

Browse files
Use node-level | for efficiency in infix_spaces_linter (#2025)
1 parent c3c10bb commit f9bf506

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

R/infix_spaces_linter.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ infix_spaces_linter <- function(exclude_operators = NULL, allow_multiple_spaces
144144

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

171171
Linter(function(source_expression) {
172172
if (!is_lint_level(source_expression, "expression")) {

tests/testthat/test-infix_spaces_linter.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,14 @@ test_that("native pipe is supported", {
185185
expect_lint("a |> foo()", NULL, linter)
186186
expect_lint("a|>foo()", rex::rex("Put spaces around all infix operators."), linter)
187187
})
188+
189+
test_that("mixed unary & binary operators aren't mis-lint", {
190+
expect_lint(
191+
"-1-1",
192+
list(
193+
message = rex::rex("Put spaces around all infix operators."),
194+
column_number = 3L
195+
),
196+
infix_spaces_linter()
197+
)
198+
})

0 commit comments

Comments
 (0)