From 17f32751bf0ea16bd891a9d1c43452c4d3d8dc44 Mon Sep 17 00:00:00 2001 From: Salim B Date: Sun, 15 Oct 2023 21:31:27 +0200 Subject: [PATCH] Add param `require_multi_line` to `brace_linter()` --- NEWS.md | 1 + R/brace_linter.R | 22 +++++++++++++--------- man/brace_linter.Rd | 6 ++++-- tests/testthat/test-brace_linter.R | 5 +++++ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/NEWS.md b/NEWS.md index 07073e65b6..6515c77c88 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/brace_linter.R b/R/brace_linter.R index 787cd53244..5c71459424 100644 --- a/R/brace_linter.R +++ b/R/brace_linter.R @@ -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 @@ -50,7 +51,8 @@ #' - #' - #' @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)) { @@ -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, diff --git a/man/brace_linter.Rd b/man/brace_linter.Rd index 3fb01e06cd..796d2c5cc2 100644 --- a/man/brace_linter.Rd +++ b/man/brace_linter.Rd @@ -4,10 +4,12 @@ \alias{brace_linter} \title{Brace linter} \usage{ -brace_linter(allow_single_line = FALSE) +brace_linter(allow_single_line = FALSE, require_multi_line = TRUE) } \arguments{ -\item{allow_single_line}{if \code{TRUE}, allow an open and closed curly pair on the same line.} +\item{allow_single_line}{If \code{TRUE}, allow an open and closed curly pair on the same line.} + +\item{require_multi_line}{If \code{TRUE}, require functions spanning multiple lines to be wrapped in curly braces.} } \description{ Perform various style checks related to placement and spacing of curly braces: diff --git a/tests/testthat/test-brace_linter.R b/tests/testthat/test-brace_linter.R index e3adc1a599..60813eb8b6 100644 --- a/tests/testthat/test-brace_linter.R +++ b/tests/testthat/test-brace_linter.R @@ -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", {