-
Notifications
You must be signed in to change notification settings - Fork 184
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
Make scalar_in_linter()
configurable
#2574
Changes from 8 commits
60869ce
b38f100
39a9c02
ae58e20
7bd8324
b756379
3f5eb7c
04881ef
49c453e
1aa6491
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
#' Block usage like x %in% "a" | ||
#' | ||
#' `vector %in% set` is appropriate for matching a vector to a set, but if | ||
#' that set has size 1, `==` is more appropriate. `%chin%` from `{data.table}` | ||
#' is matched as well. | ||
#' that set has size 1, `==` is more appropriate. | ||
#' | ||
#' `scalar %in% vector` is OK, because the alternative (`any(vector == scalar)`) | ||
#' is more circuitous & potentially less clear. | ||
#' | ||
#' @param in_operators Character vector of additional infix operators that behave like the `%in%` operator | ||
F-Noelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
|
@@ -16,7 +17,7 @@ | |
#' | ||
#' lint( | ||
#' text = "x %chin% 'a'", | ||
#' linters = scalar_in_linter() | ||
#' linters = scalar_in_linter(in_operators = "%chin%") | ||
#' ) | ||
#' | ||
#' # okay | ||
|
@@ -28,22 +29,24 @@ | |
#' @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(in_operators = NULL) { | ||
# 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%'] | ||
xpath <- glue(" | ||
//SPECIAL[{xp_text_in_table(c('%in%', {in_operators}))}] | ||
/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 | ||
|
||
bad_expr <- xml_find_all(xml, xpath) | ||
in_op <- xml_find_chr(bad_expr, "string(SPECIAL)") | ||
lint_msg <- | ||
paste0("Use == to match length-1 scalars, not ", in_op, ". Note that == preserves NA where ", in_op, " does not.") | ||
lint_msg <- glue( | ||
"Use comparison operators (e.g. ==, !=, etc.) to match length-1 scalars instead of {in_op} ", | ||
"as these operators preserve NA where {in_op} does not." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'm afraid we've over-corrected 😄 "as" implies that preserving NA is the reason for the lint. But that's not true... actually this is a somewhat ambiguous thing. Some authors might prefer this behavior and thus disfavor this linter -- hence we're highlighting this aspect as a tradeoff users should be aware of. So let's go with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
) | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, i'm having trouble making edits myself since this is made in your
main
branch, I think you need to create a dedicated branch for me to make edits.all this to say -- could you please move this item to the "breaking changes" section at the top :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved