-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New if_else_match_braces_linter (#983)
- Loading branch information
1 parent
3d22c75
commit eefe67b
Showing
13 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
}) |