Skip to content

Commit e8ed7e2

Browse files
Merge branch 'master' into expect_named
2 parents 0c395e0 + 878dacf commit e8ed7e2

11 files changed

+107
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Collate:
6363
'duplicate_argument_linter.R'
6464
'equals_na_linter.R'
6565
'exclude.R'
66+
'expect_length_linter.R'
6667
'expect_lint.R'
6768
'expect_named_linter.R'
6869
'expect_not_linter.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export(default_undesirable_functions)
2929
export(default_undesirable_operators)
3030
export(duplicate_argument_linter)
3131
export(equals_na_linter)
32+
export(expect_length_linter)
3233
export(expect_lint)
3334
export(expect_lint_free)
3435
export(expect_named_linter)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function calls. (#850, #851, @renkun-ken)
9494
+ `expect_not_linter()` Require usage of `expect_false(x)` over `expect_true(!x)`, and _vice versa_.
9595
+ `expect_true_false_linter()` Require usage of `expect_true(x)` over `expect_equal(x, TRUE)` and similar
9696
+ `expect_named_linter()` Require usage of `expect_named(x, n)` over `expect_equal(names(x), n)` and similar
97+
* `expect_length_linter()` Require usage of `expect_length(x, n)` over `expect_equal(length(x), n)` and similar
9798
* `assignment_linter()` now lints right assignment (`->` and `->>`) and gains two arguments. `allow_cascading_assign` (`TRUE` by default) toggles whether to lint `<<-` and `->>`; `allow_right_assign` toggles whether to lint `->` and `->>` (#915, @michaelchirico)
9899

99100
# lintr 2.0.1

R/expect_length_linter.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' Require usage of expect_length(x, n) over expect_equal(length(x), n)
2+
#'
3+
#' [testthat::expect_length()] exists specifically for testing the [length()] of
4+
#' an object. [testthat::expect_equal()] can also be used for such tests,
5+
#' but it is better to use the tailored function instead.
6+
#'
7+
#' @evalRd rd_tags("expect_length_linter")
8+
#' @seealso [linters] for a complete list of linters available in lintr.
9+
#' @export
10+
expect_length_linter <- function() {
11+
Linter(function(source_file) {
12+
if (length(source_file$parsed_content) == 0L) {
13+
return(list())
14+
}
15+
16+
xml <- source_file$xml_parsed_content
17+
18+
# TODO(michaelchirico): also catch expect_true(length(x) == 1)
19+
xpath <- sprintf("//expr[
20+
SYMBOL_FUNCTION_CALL[text() = 'expect_equal' or text() = 'expect_identical']
21+
and following-sibling::expr[
22+
expr[SYMBOL_FUNCTION_CALL[text() = 'length']]
23+
and (position() = 1 or preceding-sibling::expr[NUM_CONST])
24+
]
25+
]")
26+
27+
bad_expr <- xml2::xml_find_all(xml, xpath)
28+
return(lapply(bad_expr, gen_expect_length_lint, source_file))
29+
})
30+
}
31+
32+
gen_expect_length_lint <- function(expr, source_file) {
33+
matched_function <- xml2::xml_text(xml2::xml_find_first(expr, "SYMBOL_FUNCTION_CALL"))
34+
lint_msg <- sprintf("expect_length(x, n) is better than %s(length(x), n)", matched_function)
35+
xml_nodes_to_lint(expr, source_file, lint_msg, type = "warning")
36+
}

inst/lintr/linters.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ commented_code_linter,style readability best_practices default
99
cyclocomp_linter,style readability best_practices default configurable
1010
duplicate_argument_linter,correctness common_mistakes configurable
1111
equals_na_linter,robustness correctness common_mistakes default
12+
expect_length_linter,package_development best_practices readability
1213
expect_named_linter,package_development best_practices readability
1314
expect_not_linter,package_development best_practices readability
1415
expect_null_linter,package_development best_practices

man/best_practices_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/expect_length_linter.Rd

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

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

0 commit comments

Comments
 (0)