infix_spaces_linter: Infix spaces linter

View source: R/infix_spaces_linter.R

infix_spaces_linterR Documentation

Infix spaces linter

Description

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.

Tags

configurable, default, readability, style

See Also

Examples

# will produce lints
lint(
  text = "x<-1L",
  linters = infix_spaces_linter()
)

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

# 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)
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")
)


lintr documentation built on Nov. 7, 2023, 5:07 p.m.