44# NB: this metadata is used elsewhere in lintr, e.g. spaces_left_parentheses_linter.
55# because of that, even though some rows of this table are currently unused, but
66# we keep them around because it's useful to keep this info in one place.
7+
8+ # styler: off
79infix_metadata <- data.frame (stringsAsFactors = FALSE , matrix (byrow = TRUE , ncol = 2L , c(
8- " OP-PLUS" , " +" ,
9- " OP-MINUS" , " -" ,
10- " OP-TILDE" , " ~" ,
11- " GT" , " >" ,
12- " GE" , " >=" ,
13- " LT" , " <" ,
14- " LE" , " <=" ,
15- " EQ" , " ==" ,
16- " NE" , " !=" ,
17- " AND" , " &" ,
18- " OR" , " |" ,
19- " AND2" , " &&" ,
20- " OR2" , " ||" ,
21- " LEFT_ASSIGN" , " <-" , # also includes := and <<-
22- " RIGHT_ASSIGN" , " ->" , # also includes ->>
23- " EQ_ASSIGN" , " =" ,
24- " EQ_SUB" , " = " , # in calls: foo(x = 1)
25- " EQ_FORMALS" , " =" , # in definitions: function(x = 1)
26- " SPECIAL" , " %%" ,
27- " OP-SLASH" , " /" ,
28- " OP-STAR" , " *" ,
29- " OP-COMMA" , " ," ,
30- " OP-CARET" , " ^" , # also includes **
31- " OP-AT" , " @" ,
32- " OP-EXCLAMATION" , " !" ,
33- " OP-COLON" , " :" ,
34- " NS_GET" , " ::" ,
35- " NS_GET_INT" , " :::" ,
36- " OP-LEFT-BRACE" , " {" ,
10+ " OP-PLUS" , " +" ,
11+ " OP-MINUS" , " -" ,
12+ " OP-TILDE" , " ~" ,
13+ " GT" , " >" ,
14+ " GE" , " >=" ,
15+ " LT" , " <" ,
16+ " LE" , " <=" ,
17+ " EQ" , " ==" ,
18+ " NE" , " !=" ,
19+ " AND" , " &" ,
20+ " OR" , " |" ,
21+ " AND2" , " &&" ,
22+ " OR2" , " ||" ,
23+ " LEFT_ASSIGN" , " <-" , # also includes := and <<-
24+ " RIGHT_ASSIGN" , " ->" , # also includes ->>
25+ " EQ_ASSIGN" , " =" ,
26+ " EQ_SUB" , " = " , # in calls: foo(x = 1)
27+ " EQ_FORMALS" , " =" , # in definitions: function(x = 1)
28+ " SPECIAL" , " %%" ,
29+ " OP-SLASH" , " /" ,
30+ " OP-STAR" , " *" ,
31+ " OP-COMMA" , " ," ,
32+ " OP-CARET" , " ^" , # also includes **
33+ " OP-AT" , " @" ,
34+ " OP-EXCLAMATION" , " !" ,
35+ " OP-COLON" , " :" ,
36+ " NS_GET" , " ::" ,
37+ " NS_GET_INT" , " :::" ,
38+ " OP-LEFT-BRACE" , " {" ,
3739 " OP-LEFT-BRACKET" , " [" ,
38- " LBB" , " [[" ,
39- " OP-LEFT-PAREN" , " (" ,
40- " OP-QUESTION" , " ?" ,
41- " OP-DOLLAR" , " $" ,
40+ " LBB" , " [[" ,
41+ " OP-LEFT-PAREN" , " (" ,
42+ " OP-QUESTION" , " ?" ,
43+ " OP-DOLLAR" , " $" ,
4244 NULL
4345)))
46+ # styler: on
47+
4448names(infix_metadata ) <- c(" xml_tag" , " string_value" )
4549# utils::getParseData()'s designation for the tokens wouldn't be valid as XML tags
4650infix_metadata $ parse_tag <- ifelse(
@@ -70,7 +74,7 @@ infix_overload <- data.frame(
7074# ' Check that infix operators are surrounded by spaces. Enforces the corresponding Tidyverse style guide rule;
7175# ' see <https://style.tidyverse.org/syntax.html#infix-operators>.
7276# '
73- # ' @param exclude_operators Character vector of operators to exlude from consideration for linting.
77+ # ' @param exclude_operators Character vector of operators to exclude from consideration for linting.
7478# ' Default is to include the following "low-precedence" operators:
7579# ' `+`, `-`, `~`, `>`, `>=`, `<`, `<=`, `==`, `!=`, `&`, `&&`, `|`, `||`, `<-`, `:=`, `<<-`, `->`, `->>`,
7680# ' `=`, `/`, `*`, and any infix operator (exclude infixes by passing `"%%"`). Note that `<-`, `:=`, and `<<-`
@@ -79,6 +83,45 @@ infix_overload <- data.frame(
7983# ' @param allow_multiple_spaces Logical, default `TRUE`. If `FALSE`, usage like `x = 2` will also be linted;
8084# ' excluded by default because such usage can sometimes be used for better code alignment, as is allowed
8185# ' by the style guide.
86+ # '
87+ # ' @examples
88+ # ' # will produce lints
89+ # ' lint(
90+ # ' text = "x<-1L",
91+ # ' linters = infix_spaces_linter()
92+ # ' )
93+ # '
94+ # ' lint(
95+ # ' text = "1:4 %>%sum()",
96+ # ' linters = infix_spaces_linter()
97+ # ' )
98+ # '
99+ # ' # okay
100+ # ' lint(
101+ # ' text = "x <- 1L",
102+ # ' linters = infix_spaces_linter()
103+ # ' )
104+ # '
105+ # ' lint(
106+ # ' text = "1:4 %>% sum()",
107+ # ' linters = infix_spaces_linter()
108+ # ' )
109+ # '
110+ # ' code_lines <- "
111+ # ' ab <- 1L
112+ # ' abcdef <- 2L
113+ # ' "
114+ # ' cat(code_lines)
115+ # ' lint(
116+ # ' text = code_lines,
117+ # ' linters = infix_spaces_linter(allow_multiple_spaces = TRUE)
118+ # ' )
119+ # '
120+ # ' lint(
121+ # ' text = "a||b",
122+ # ' linters = infix_spaces_linter(exclude_operators = "||")
123+ # ' )
124+ # '
82125# ' @evalRd rd_tags("infix_spaces_linter")
83126# ' @seealso
84127# ' [linters] for a complete list of linters available in lintr. \cr
@@ -132,6 +175,11 @@ infix_spaces_linter <- function(exclude_operators = NULL, allow_multiple_spaces
132175 xml <- source_expression $ xml_parsed_content
133176 bad_expr <- xml2 :: xml_find_all(xml , xpath )
134177
135- xml_nodes_to_lints(bad_expr , source_expression = source_expression , lint_message = lint_message , type = " style" )
178+ xml_nodes_to_lints(
179+ bad_expr ,
180+ source_expression = source_expression ,
181+ lint_message = lint_message ,
182+ type = " style"
183+ )
136184 })
137185}
0 commit comments