@@ -29,10 +29,15 @@ brace_linter <- function(allow_single_line = FALSE) {
29
29
(@line1 = following-sibling::expr/OP-LEFT-BRACE/@line1)
30
30
)" ,
31
31
# allow `(`, `,` and `%>%` on preceding line
32
+ #
33
+ # note that '{' is not supported in RHS call of base-R's native pipe (`|>`),
34
+ # so no exception needs to be made for this operator
32
35
" not(
33
- @line1 = parent::expr/preceding-sibling::*[1][
34
- self::OP-LEFT-PAREN or self::OP-COMMA or (self::SPECIAL and text() = '%>%')
35
- ]/@line2 + 1
36
+ @line1 > parent::expr/preceding-sibling::*[not(self::COMMENT)][1][
37
+ self::OP-LEFT-PAREN or
38
+ self::OP-COMMA or
39
+ (self::SPECIAL and text() = '%>%')
40
+ ]/@line2
36
41
)"
37
42
))
38
43
@@ -110,41 +115,42 @@ brace_linter <- function(allow_single_line = FALSE) {
110
115
return (list ())
111
116
}
112
117
118
+ xml <- source_expression $ xml_parsed_content
113
119
lints <- list ()
114
120
115
121
lints <- c(lints , xml_nodes_to_lints(
116
- xml2 :: xml_find_all(source_expression $ xml_parsed_content , xp_open_curly ),
122
+ xml2 :: xml_find_all(xml , xp_open_curly ),
117
123
source_expression = source_expression ,
118
124
lint_message =
119
125
" Opening curly braces should never go on their own line and should always be followed by a new line."
120
126
))
121
127
122
128
lints <- c(lints , xml_nodes_to_lints(
123
- xml2 :: xml_find_all(source_expression $ xml_parsed_content , xp_paren_brace ),
129
+ xml2 :: xml_find_all(xml , xp_paren_brace ),
124
130
source_expression = source_expression ,
125
131
lint_message = " There should be a space before an opening curly brace."
126
132
))
127
133
128
134
lints <- c(lints , xml_nodes_to_lints(
129
- xml2 :: xml_find_all(source_expression $ xml_parsed_content , xp_closed_curly ),
135
+ xml2 :: xml_find_all(xml , xp_closed_curly ),
130
136
source_expression = source_expression ,
131
137
lint_message = " Closing curly-braces should always be on their own line, unless they are followed by an else."
132
138
))
133
139
134
140
lints <- c(lints , xml_nodes_to_lints(
135
- xml2 :: xml_find_all(source_expression $ xml_parsed_content , xp_else_same_line ),
141
+ xml2 :: xml_find_all(xml , xp_else_same_line ),
136
142
source_expression = source_expression ,
137
143
lint_message = " `else` should come on the same line as the previous `}`."
138
144
))
139
145
140
146
lints <- c(lints , xml_nodes_to_lints(
141
- xml2 :: xml_find_all(source_expression $ xml_parsed_content , xp_function_brace ),
147
+ xml2 :: xml_find_all(xml , xp_function_brace ),
142
148
source_expression = source_expression ,
143
149
lint_message = " Any function spanning multiple lines should use curly braces."
144
150
))
145
151
146
152
lints <- c(lints , xml_nodes_to_lints(
147
- xml2 :: xml_find_all(source_expression $ xml_parsed_content , xp_if_else_match_brace ),
153
+ xml2 :: xml_find_all(xml , xp_if_else_match_brace ),
148
154
source_expression = source_expression ,
149
155
lint_message = " Either both or neither branch in `if`/`else` should use curly braces."
150
156
))
0 commit comments