|
10 | 10 | #' @export
|
11 | 11 | open_curly_linter <- function(allow_single_line = FALSE) {
|
12 | 12 | lintr_deprecated("open_curly_linter", new = "brace_linter", version = "2.0.1.9001", type = "Linter")
|
13 |
| - Linter(function(source_expression) { |
14 |
| - lapply( |
15 |
| - ids_with_token(source_expression, "'{'"), |
16 |
| - function(id) { |
17 |
| - |
18 |
| - parsed <- with_id(source_expression, id) |
19 |
| - |
20 |
| - tokens_before <- source_expression$parsed_content$token[ |
21 |
| - source_expression$parsed_content$line1 == parsed$line1 & |
22 |
| - source_expression$parsed_content$col1 < parsed$col1] |
23 |
| - |
24 |
| - tokens_after <- source_expression$parsed_content$token[ |
25 |
| - source_expression$parsed_content$line1 == parsed$line1 & |
26 |
| - source_expression$parsed_content$col1 > parsed$col1 & |
27 |
| - source_expression$parsed_content$token != "COMMENT"] |
28 |
| - |
29 |
| - if (isTRUE(allow_single_line) && |
30 |
| - "'}'" %in% tokens_after) { |
31 |
| - return() |
32 |
| - } |
33 |
| - |
34 |
| - line <- source_expression$lines[as.character(parsed$line1)] |
35 | 13 |
|
36 |
| - # the only tokens should be the { and the start of the expression. |
37 |
| - some_before <- length(tokens_before) %!=% 0L |
38 |
| - some_after <- length(tokens_after) %!=% 0L |
39 |
| - |
40 |
| - content_after <- unname(substr(line, parsed$col1 + 1L, nchar(line))) |
41 |
| - content_before <- unname(substr(line, 1L, parsed$col1 - 1L)) |
42 |
| - |
43 |
| - only_comment <- rex::re_matches(content_after, rex::rex(any_spaces, "#", something, end)) |
| 14 | + xpath_before <- "//OP-LEFT-BRACE[ |
| 15 | + not(following-sibling::expr[1][OP-LEFT-BRACE]) |
| 16 | + and not(parent::expr/preceding-sibling::*[1][OP-LEFT-BRACE]) |
| 17 | + and @line1 != parent::expr/preceding-sibling::*[1][not(self::ELSE)]/@line2 |
| 18 | + ]" |
| 19 | + if (allow_single_line) { |
| 20 | + xpath_after <- "//OP-LEFT-BRACE[ |
| 21 | + not(following-sibling::expr[1][OP-LEFT-BRACE]) |
| 22 | + and not(parent::expr/preceding-sibling::OP-LEFT-BRACE) |
| 23 | + and not(@line2 = following-sibling::OP-RIGHT-BRACE/@line1) |
| 24 | + and @line2 = following-sibling::expr[position() = 1 and not(OP-LEFT-BRACE)]/@line1 |
| 25 | + ]" |
| 26 | + message_after <- paste( |
| 27 | + "Opening curly braces should always be followed by a new line", |
| 28 | + "unless the paired closing brace is on the same line." |
| 29 | + ) |
| 30 | + } else { |
| 31 | + xpath_after <- "//OP-LEFT-BRACE[ |
| 32 | + not(following-sibling::expr[1][OP-LEFT-BRACE]) |
| 33 | + and not(parent::expr/preceding-sibling::OP-LEFT-BRACE) |
| 34 | + and @line2 = following-sibling::expr[1]/@line1 |
| 35 | + ]" |
| 36 | + message_after <- "Opening curly braces should always be followed by a new line." |
| 37 | + } |
44 | 38 |
|
45 |
| - double_curly <- rex::re_matches(content_after, rex::rex(start, "{")) || |
46 |
| - rex::re_matches(content_before, rex::rex("{", end)) |
| 39 | + Linter(function(source_expression) { |
| 40 | + if (!is_lint_level(source_expression, "expression")) { |
| 41 | + return(list()) |
| 42 | + } |
47 | 43 |
|
48 |
| - if (double_curly) { |
49 |
| - return() |
50 |
| - } |
| 44 | + xml <- source_expression$xml_parsed_content |
51 | 45 |
|
52 |
| - whitespace_after <- |
53 |
| - unname(substr(line, parsed$col1 + 1L, parsed$col1 + 1L)) %!=% "" |
| 46 | + expr_before <- xml2::xml_find_all(xml, xpath_before) |
| 47 | + lints_before <- xml_nodes_to_lints( |
| 48 | + expr_before, |
| 49 | + source_expression = source_expression, |
| 50 | + lint_message = "Opening curly braces should never go on their own line.", |
| 51 | + type = "style" |
| 52 | + ) |
54 | 53 |
|
55 |
| - if (!some_before || |
56 |
| - some_after || |
57 |
| - (whitespace_after && !only_comment)) { |
58 |
| - Lint( |
59 |
| - filename = source_expression$filename, |
60 |
| - line_number = parsed$line1, |
61 |
| - column_number = parsed$col1, |
62 |
| - type = "style", |
63 |
| - message = paste( |
64 |
| - "Opening curly braces should never go on their own line and", |
65 |
| - "should always be followed by a new line." |
66 |
| - ), |
67 |
| - line = line |
68 |
| - ) |
69 |
| - } |
70 |
| - } |
| 54 | + expr_after <- xml2::xml_find_all(xml, xpath_after) |
| 55 | + lints_after <- xml_nodes_to_lints( |
| 56 | + expr_after, |
| 57 | + source_expression = source_expression, |
| 58 | + lint_message = message_after, |
| 59 | + type = "style" |
71 | 60 | )
|
| 61 | + |
| 62 | + return(c(lints_before, lints_after)) |
72 | 63 | })
|
73 | 64 | }
|
0 commit comments