Skip to content

Commit

Permalink
Add param require_multi_line to brace_linter()
Browse files Browse the repository at this point in the history
  • Loading branch information
salim-b committed Oct 15, 2023
1 parent 4a8b931 commit 17f3275
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* `object_name_linter()` no longer attempts to lint strings in function calls on the LHS of assignments (#1466, @MichaelChirico).
* `infix_spaces_linter()` allows finer control for linting `=` in different scenarios using parse tags `EQ_ASSIGN`, `EQ_SUB`, and `EQ_FORMALS` (#1977, @MichaelChirico).
* `equals_na_linter()` checks for `x %in% NA`, which is a more convoluted form of `is.na(x)` (#2088, @MichaelChirico).
* `brace_linter()` does not require functions spanning multiple lines to be wrapped in curly braces anymore if `require_multi_line = FALSE` (#1807, #?, @salim-b).

## New and improved features

Expand Down
22 changes: 13 additions & 9 deletions R/brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#' does.
#' - Functions spanning multiple lines use curly braces.
#'
#' @param allow_single_line if `TRUE`, allow an open and closed curly pair on the same line.
#' @param allow_single_line If `TRUE`, allow an open and closed curly pair on the same line.
#' @param require_multi_line If `TRUE`, require functions spanning multiple lines to be wrapped in curly braces.
#'
#' @examples
#' # will produce lints
Expand Down Expand Up @@ -50,7 +51,8 @@
#' - <https://style.tidyverse.org/syntax.html#indenting>
#' - <https://style.tidyverse.org/syntax.html#if-statements>
#' @export
brace_linter <- function(allow_single_line = FALSE) {
brace_linter <- function(allow_single_line = FALSE,
require_multi_line = TRUE) {
xp_cond_open <- xp_and(c(
# matching } is on same line
if (isTRUE(allow_single_line)) {
Expand Down Expand Up @@ -192,14 +194,16 @@ brace_linter <- function(allow_single_line = FALSE) {
)
)

lints <- c(
lints,
xml_nodes_to_lints(
xml_find_all(xml, xp_function_brace),
source_expression = source_expression,
lint_message = "Any function spanning multiple lines should use curly braces."
if (isTRUE(require_multi_line)) {
lints <- c(
lints,
xml_nodes_to_lints(
xml_find_all(xml, xp_function_brace),
source_expression = source_expression,
lint_message = "Any function spanning multiple lines should use curly braces."
)
)
)
}

lints <- c(
lints,
Expand Down
6 changes: 4 additions & 2 deletions man/brace_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/test-brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ test_that("brace_linter lints function expressions correctly", {
rex::rex("Any function spanning multiple lines should use curly braces."),
linter
)
expect_lint(
lines,
NULL,
brace_linter(require_multi_line = FALSE)
)
})

test_that("brace_linter lints if/else matching braces correctly", {
Expand Down

0 comments on commit 17f3275

Please sign in to comment.