Skip to content

Commit 0c985fa

Browse files
committed
delete else_same_line_linter and merge it into brace_linter
1 parent a251c1e commit 0c985fa

13 files changed

+64
-101
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ Collate:
6868
'declared_functions.R'
6969
'deprecated.R'
7070
'duplicate_argument_linter.R'
71-
'else_same_line_linter.R'
7271
'equals_na_linter.R'
7372
'exclude.R'
7473
'expect_comparison_linter.R'

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export(default_settings)
3535
export(default_undesirable_functions)
3636
export(default_undesirable_operators)
3737
export(duplicate_argument_linter)
38-
export(else_same_line_linter)
3938
export(equals_na_linter)
4039
export(expect_comparison_linter)
4140
export(expect_identical_linter)

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
* Consistent access to linters through a function call, even for linters without parameters (#245, @fangly, @AshesITR, and @MichaelChirico)
1010
* Removed deprecated functions `absolute_paths_linter`, `camel_case_linter`, `multiple_dots_linter`, `snake_case_linter`, and `trailing_semicolons_linter`. They have been marked as deprecated since v1.0.1, which was released in 2017.
1111
* Rename `semicolon_terminator_linter` to `semicolon_linter` for better consistency. `semicolon_terminator_linter` survives but is marked for deprecation. The new linter also has a new signature, taking arguments `allow_compound` and `allow_trailing` to replace the old single argument `semicolon=`, again for signature consistency with other linters.
12-
* Combined several curly brace related linters into a new `brace_linter`:
12+
* Combined several curly brace related linters into a new `brace_linter` (#1041, @AshesITR):
1313
+ `closed_curly_linter()`
14+
+ Require `else` to come on the same line as the preceding `}`, if present (#884, @michaelchirico)
1415
* The `...` arguments for `lint()`, `lint_dir()`, and `lint_package()` have promoted to an earlier position to better match the [Tidyverse design principal](https://design.tidyverse.org/args-data-details.html) of data->descriptor->details. This change enables passing objects to `...` without needing to specify non-required arguments, e.g. `lint_dir("/path/to/dir", linter())` now works without the need to specify `relative_path`. This affects some code that uses positional arguments. (#935, @michaelchirico)
1516
+ For `lint()`, `...` is now the 3rd argument, where earlier this was `cache=`
1617
+ For `lint_dir()` and `lint_package()`, `...` is now the 2nd argument, where earlier this was `relative_path=`
@@ -130,7 +131,7 @@ function calls. (#850, #851, @renkun-ken)
130131
* `nested_ifelse_linter()` Prevent nested calls to `ifelse()` like `ifelse(A, x, ifelse(B, y, z))`, and similar
131132
* `condition_message_linter` Prevent condition messages from being constructed like `stop(paste(...))` (where just `stop(...)` is preferable)
132133
* `redundant_ifelse_linter()` Prevent usage like `ifelse(A & B, TRUE, FALSE)` or `ifelse(C, 0, 1)` (the latter is `as.numeric(!C)`)
133-
* `else_same_line_linter()` Require `else` to come on the same line as the preceding `}`, if present
134+
* `brace_linter()` Require `else` to come on the same line as the preceding `}`, if present
134135
* `unreachable_code_linter()` Prevent code after `return()` and `stop()` statements that will never be reached (extended for #1051 thanks to early user testing, thanks @bersbersbers!)
135136
* `regex_subset_linter()` Require usage of `grep(ptn, x, value = TRUE)` over `x[grep(ptn, x)]` and similar
136137
* `consecutive_stopifnot_linter()` Require consecutive calls to `stopifnot()` to be unified into one

R/brace_linter.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#' Perform various style checks related to placement and spacing of curly braces:
44
#'
55
#' - Curly braces are on their own line unless they are followed by an `else`.
6+
#' - Closing curly braces in `if` conditions are on the same line as the corresponding `else`.
67
#'
78
#' @param allow_single_line if `TRUE`, allow an open and closed curly pair on the same line.
89
#'
@@ -31,6 +32,7 @@ brace_linter <- function(allow_single_line = FALSE) {
3132
)"
3233
))
3334

35+
# TODO (AshesITR): if c_style_braces is TRUE, skip the not(ELSE) condition
3436
xp_closed_curly <- glue::glue("//OP-RIGHT-BRACE[
3537
{ xp_cond_closed } and (
3638
(@line1 = preceding-sibling::*/@line2) or
@@ -48,6 +50,19 @@ brace_linter <- function(allow_single_line = FALSE) {
4850
)
4951
))
5052

53+
xp_else_closed_curly <- "preceding-sibling::IF/following-sibling::expr[2]/OP-RIGHT-BRACE"
54+
# need to (?) repeat previous_curly_path since != will return true if there is
55+
# no such node. ditto for approach with not(@line1 = ...).
56+
# TODO (AshesITR): if c_style_braces is TRUE, this needs to be @line2 + 1
57+
xp_else_same_line <- glue::glue("//ELSE[{xp_else_closed_curly} and @line1 != {xp_else_closed_curly}/@line2]")
58+
59+
lints <- c(lints, lapply(
60+
xml2::xml_find_all(source_expression$xml_parsed_content, xp_else_same_line),
61+
xml_nodes_to_lint,
62+
source_file = source_expression,
63+
lint_message = "`else` should come on the same line as the previous `}`."
64+
))
65+
5166
lints
5267
})
5368
}

R/else_same_line_linter.R

Lines changed: 0 additions & 31 deletions
This file was deleted.

inst/lintr/linters.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ conjunct_test_linter,package_development best_practices readability
1414
consecutive_stopifnot_linter,style readability consistency
1515
cyclocomp_linter,style readability best_practices default configurable
1616
duplicate_argument_linter,correctness common_mistakes configurable
17-
else_same_line_linter,style readability
1817
equals_na_linter,robustness correctness common_mistakes default
1918
expect_comparison_linter,package_development best_practices
2019
expect_identical_linter,package_development

man/brace_linter.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/else_same_line_linter.Rd

Lines changed: 0 additions & 18 deletions
This file was deleted.

man/linters.Rd

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/readability_linters.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_linters.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-brace_linter.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,46 @@ test_that("brace_linter lints closed braces correctly", {
8080
# }} is allowed
8181
expect_lint("{{ x }}", NULL, linter)
8282
})
83+
84+
test_that("brace_linter lints else correctly", {
85+
linter <- brace_linter()
86+
expect_lint("if (TRUE) 1 else 2", NULL, linter)
87+
expect_lint("if (TRUE) 1", NULL, linter)
88+
89+
lines_brace <- trim_some("
90+
if (TRUE) {
91+
1
92+
} else {
93+
2
94+
}
95+
")
96+
expect_lint(lines_brace, NULL, linter)
97+
98+
# such usage is also not allowed by the style guide, but test anyway
99+
lines_unbrace <- trim_some("
100+
foo <- function(x) {
101+
if (TRUE)
102+
1
103+
else
104+
2
105+
}
106+
")
107+
expect_lint(lines_unbrace, NULL, linter)
108+
109+
lines <- trim_some("
110+
foo <- function(x) {
111+
if (x) {
112+
1
113+
}
114+
else {
115+
2
116+
}
117+
}
118+
")
119+
expect_lint(
120+
lines,
121+
rex::rex("`else` should come on the same line as the previous `}`."),
122+
linter
123+
)
124+
})
125+

tests/testthat/test-else_same_line_linter.R

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)