-
Notifications
You must be signed in to change notification settings - Fork 195
New function_brace_linter #987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
e310da1
fbba438
f73ed4c
96a64c6
b83a9d0
fb59515
a27a2ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #' Require multi-line functions to use braces | ||
| #' | ||
| #' This linter catches function definitions spanning multiple lines of code | ||
| #' that aren't wrapped in braces | ||
| #' | ||
| #' @evalRd rd_tags("function_brace_linter") | ||
| #' @seealso [linters] for a complete list of linters available in lintr. | ||
| #' @export | ||
| function_brace_linter <- function() { | ||
| Linter(function(source_file) { | ||
| if (length(source_file$parsed_content) == 0L) { | ||
| return(list()) | ||
| } | ||
|
|
||
| xml <- source_file$xml_parsed_content | ||
|
|
||
| bad_expr_xpath <- "//expr[FUNCTION and @line1 != @line2 and not(expr[OP-LEFT-BRACE])]" | ||
| bad_expr <- xml2::xml_find_all(xml, bad_expr_xpath) | ||
|
|
||
| return(lapply( | ||
| bad_expr, | ||
| xml_nodes_to_lint, | ||
| source_file = source_file, | ||
| lint_message = "Any function spanning multiple lines must use curly braces.", | ||
| type = "warning" | ||
| )) | ||
| }) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| test_that("function_brace_linter skips allowed usages", { | ||
| expect_lint("function(x) 4", NULL, function_brace_linter()) | ||
|
|
||
| lines <- trim_some(" | ||
| function(x) { | ||
| x + 4 | ||
| } | ||
| ") | ||
| expect_lint(lines, NULL, function_brace_linter()) | ||
| }) | ||
|
|
||
| test_that("function_brace_linter blocks disallowed usage", { | ||
| lines <- trim_some(" | ||
| function(x) | ||
| x+4 | ||
| ") | ||
| expect_lint( | ||
| lines, | ||
| rex::rex("Any function spanning multiple lines must use curly braces."), | ||
| function_brace_linter() | ||
| ) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.