Skip to content

Commit 4270c61

Browse files
New function_brace_linter (#987)
1 parent 309db27 commit 4270c61

File tree

12 files changed

+87
-7
lines changed

12 files changed

+87
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Collate:
7979
'expect_type_linter.R'
8080
'extract.R'
8181
'extraction_operator_linter.R'
82+
'function_brace_linter.R'
8283
'function_left_parentheses.R'
8384
'get_source_expressions.R'
8485
'ids_with_token.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export(expect_s4_class_linter)
4545
export(expect_true_false_linter)
4646
export(expect_type_linter)
4747
export(extraction_operator_linter)
48+
export(function_brace_linter)
4849
export(function_left_parentheses_linter)
4950
export(get_source_expressions)
5051
export(ids_with_token)

R/function_brace_linter.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#' Require multi-line functions to use braces
2+
#'
3+
#' This linter catches function definitions spanning multiple lines of code
4+
#' that aren't wrapped in braces
5+
#'
6+
#' @evalRd rd_tags("function_brace_linter")
7+
#' @seealso [linters] for a complete list of linters available in lintr.
8+
#' @export
9+
function_brace_linter <- function() {
10+
Linter(function(source_file) {
11+
if (length(source_file$xml_parsed_content) == 0L) {
12+
return(list())
13+
}
14+
15+
xml <- source_file$xml_parsed_content
16+
17+
bad_expr_xpath <- "//expr[FUNCTION and @line1 != @line2 and not(expr[OP-LEFT-BRACE])]"
18+
bad_expr <- xml2::xml_find_all(xml, bad_expr_xpath)
19+
20+
return(lapply(
21+
bad_expr,
22+
xml_nodes_to_lint,
23+
source_file = source_file,
24+
lint_message = "Any function spanning multiple lines must use curly braces.",
25+
type = "warning"
26+
))
27+
})
28+
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ default_linters <- with_defaults(
8989
commented_code_linter(),
9090
cyclocomp_linter(),
9191
equals_na_linter(),
92+
function_brace_linter(),
9293
function_left_parentheses_linter(),
9394
if_else_match_braces_linter(),
9495
infix_spaces_linter(),

inst/lintr/linters.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ expect_s4_class_linter,package_development best_practices
2323
expect_true_false_linter,package_development best_practices readability
2424
expect_type_linter,package_development best_practices
2525
extraction_operator_linter,style best_practices
26+
function_brace_linter,default style readability
2627
function_left_parentheses_linter,style readability default
2728
if_else_match_braces_linter,default style readability
2829
implicit_integer_linter,style consistency best_practices

man/default_linters.Rd

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

man/function_brace_linter.Rd

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

man/linters.Rd

Lines changed: 7 additions & 6 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: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_linters.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.

0 commit comments

Comments
 (0)