Skip to content

Commit

Permalink
Update scalar_in_linter.R (#1)
Browse files Browse the repository at this point in the history
Make scalar_in_linter configurable to allow projects to define additional %in% style functions like %notin%.
  • Loading branch information
F-Noelle authored May 10, 2024
1 parent cbd0619 commit 60869ce
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
12 changes: 8 additions & 4 deletions R/scalar_in_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' `scalar %in% vector` is OK, because the alternative (`any(vector == scalar)`)
#' is more circuitous & potentially less clear.
#'
#' @param add_in_operators Character vector of additional functions that behave like the %in% operator
#'
#' @examples
#' # will produce lints
#' lint(
Expand All @@ -28,14 +30,16 @@
#' @evalRd rd_tags("scalar_in_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
scalar_in_linter <- function() {
scalar_in_linter <- function(add_in_operators = "%chin%") {
# TODO(#2085): Extend to include other cases where the RHS is clearly a scalar
# NB: all of logical, integer, double, hex, complex are parsed as NUM_CONST
xpath <- "
//SPECIAL[text() = '%in%' or text() = '%chin%']
special <- paste(paste0("text() = '", c("%in%", add_in_operators), "'"), collapse = " or ")

xpath <- paste0("
//SPECIAL[", special, "]
/following-sibling::expr[NUM_CONST[not(starts-with(text(), 'NA'))] or STR_CONST]
/parent::expr
"
")

Linter(linter_level = "expression", function(source_expression) {
xml <- source_expression$xml_parsed_content
Expand Down
2 changes: 1 addition & 1 deletion inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ repeat_linter,style readability
return_linter,style configurable default
routine_registration_linter,best_practices efficiency robustness
sample_int_linter,efficiency readability robustness
scalar_in_linter,readability consistency best_practices efficiency
scalar_in_linter,readability consistency best_practices efficiency configurable
semicolon_linter,style readability default configurable
semicolon_terminator_linter,defunct
seq_linter,robustness efficiency consistency best_practices default
Expand Down
1 change: 1 addition & 0 deletions man/configurable_linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/scalar_in_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 60869ce

Please sign in to comment.