Skip to contents

Check that infix operators are surrounded by spaces. Enforces the corresponding Tidyverse style guide rule; see https://style.tidyverse.org/syntax.html#infix-operators.

Usage

infix_spaces_linter(exclude_operators = NULL, allow_multiple_spaces = TRUE)

Arguments

exclude_operators

Character vector of operators to exclude from consideration for linting. Default is to include the following "low-precedence" operators: +, -, ~, >, >=, <, <=, ==, !=, &, &&, |, ||, <-, :=, <<-, ->, ->>, =, /, *, and any infix operator (exclude infixes by passing "%%"). Note that "=" here includes three different operators, from the parser's point of view. To lint only some of these, pass the corresponding parse tags (i.e., some of "EQ_ASSIGN", "EQ_SUB", and "EQ_FORMALS"; see utils::getParseData()).

allow_multiple_spaces

Logical, default TRUE. If FALSE, usage like x = 2 will also be linted; excluded by default because such usage can sometimes be used for better code alignment, as is allowed by the style guide.

See also

Examples

# will produce lints
lint(
  text = "x<-1L",
  linters = infix_spaces_linter()
)
#> ::warning file=<text>,line=1,col=2::file=<text>,line=1,col=2,[infix_spaces_linter] Put spaces around all infix operators.

lint(
  text = "1:4 %>%sum()",
  linters = infix_spaces_linter()
)
#> ::warning file=<text>,line=1,col=5::file=<text>,line=1,col=5,[infix_spaces_linter] Put spaces around all infix operators.

# okay
lint(
  text = "x <- 1L",
  linters = infix_spaces_linter()
)

lint(
  text = "1:4 %>% sum()",
  linters = infix_spaces_linter()
)

code_lines <- "
ab     <- 1L
abcdef <- 2L
"
writeLines(code_lines)
#> 
#> ab     <- 1L
#> abcdef <- 2L
#> 
lint(
  text = code_lines,
  linters = infix_spaces_linter(allow_multiple_spaces = TRUE)
)

lint(
  text = "a||b",
  linters = infix_spaces_linter(exclude_operators = "||")
)

lint(
  text = "sum(1:10, na.rm=TRUE)",
  linters = infix_spaces_linter(exclude_operators = "EQ_SUB")
)