-
Notifications
You must be signed in to change notification settings - Fork 188
New if_else_match_braces_linter #983
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 4 commits
a54f349
63cd2a4
c6a900b
4adc275
912d98a
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,46 @@ | ||
#' Require both or neither if/else branches to use curly braces | ||
#' | ||
#' This linter catches `if`/`else` clauses where the `if` branch is wrapped | ||
#' in `{...}` but the `else` branch is not, or vice versa, i.e., it ensures | ||
#' that either both branches use `{...}` or neither does. | ||
#' | ||
#' @evalRd rd_tags("if_else_match_braces_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
if_else_match_braces_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$xml_parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# if (x) { ... } else if (y) { ... } else { ... } is OK; fully exact pairing | ||
# of if/else would require this to be | ||
# if (x) { ... } else { if (y) { ... } else { ... } } since there's no | ||
# elif operator/token in R, which is pretty unseemly | ||
xpath <- " | ||
//IF[ | ||
following-sibling::expr[2][OP-LEFT-BRACE] | ||
and following-sibling::ELSE | ||
/following-sibling::expr[1][not(OP-LEFT-BRACE or IF/following-sibling::expr[2][OP-LEFT-BRACE])] | ||
] | ||
|
||
| | ||
|
||
//ELSE[ | ||
following-sibling::expr[1][OP-LEFT-BRACE] | ||
and preceding-sibling::IF/following-sibling::expr[2][not(OP-LEFT-BRACE)] | ||
] | ||
" | ||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
lint_message = "Either both or neither branch in `if`/`else` should 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.
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,64 @@ | ||
test_that("if_else_match_braces_linter skips allowed usages", { | ||
expect_lint("if (TRUE) 1 else 2", NULL, if_else_match_braces_linter()) | ||
expect_lint("if (TRUE) 1", NULL, if_else_match_braces_linter()) | ||
|
||
lines_brace <- trim_some(" | ||
if (TRUE) { | ||
1 | ||
} else { | ||
2 | ||
} | ||
") | ||
expect_lint(lines_brace, NULL, if_else_match_braces_linter()) | ||
|
||
# such usage is also not allowed by the style guide, but test anyway | ||
lines_unbrace <- trim_some(" | ||
foo <- function(x) { | ||
if (TRUE) | ||
1 | ||
else | ||
2 | ||
} | ||
") | ||
expect_lint(lines_unbrace, NULL, if_else_match_braces_linter()) | ||
|
||
# else if is OK | ||
lines_else_if <- trim_some(" | ||
if (x) { | ||
1 | ||
} else if (y) { | ||
2 | ||
} else { | ||
3 | ||
} | ||
") | ||
expect_lint(lines_else_if, NULL, if_else_match_braces_linter()) | ||
}) | ||
|
||
test_that("if_else_match_braces_linter blocks disallowed usage", { | ||
lines_if <- trim_some(" | ||
foo <- function(x) { | ||
if (x) { | ||
1 | ||
} else 2 | ||
} | ||
") | ||
expect_lint( | ||
lines_if, | ||
rex::rex("Either both or neither branch in `if`/`else` should use curly braces."), | ||
if_else_match_braces_linter() | ||
) | ||
|
||
lines_else <- trim_some(" | ||
foo <- function(x) { | ||
if (x) 1 else { | ||
2 | ||
} | ||
} | ||
") | ||
expect_lint( | ||
lines_else, | ||
rex::rex("Either both or neither branch in `if`/`else` should use curly braces."), | ||
if_else_match_braces_linter() | ||
) | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.