-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
length_levels_linter() for length(levels(x)) --> nlevels(x) (#2051)
- Loading branch information
1 parent
6fc5570
commit 3f22b76
Showing
11 changed files
with
87 additions
and
3 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,45 @@ | ||
#' Require usage of nlevels over length(levels(.)) | ||
#' | ||
#' `length(levels(x))` is the same as `nlevels(x)`, but harder to read. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "length(levels(x))", | ||
#' linters = length_levels_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = "length(c(levels(x), levels(y)))", | ||
#' linters = length_levels_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("length_levels_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
length_levels_linter <- function() { | ||
xpath <- " | ||
//SYMBOL_FUNCTION_CALL[text() = 'levels'] | ||
/parent::expr | ||
/parent::expr | ||
/parent::expr[expr/SYMBOL_FUNCTION_CALL[text() = 'length']] | ||
" | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "expression")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$xml_parsed_content | ||
|
||
bad_expr <- xml_find_all(xml, xpath) | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = "nlevels(x) is better than length(levels(x)).", | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
test_that("length_levels_linter skips allowed usages", { | ||
expect_lint("length(c(levels(x), 'a'))", NULL, length_levels_linter()) | ||
}) | ||
|
||
test_that("length_levels_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"2:length(levels(x))", | ||
rex::rex("nlevels(x) is better than length(levels(x))."), | ||
length_levels_linter() | ||
) | ||
}) |