-
Notifications
You must be signed in to change notification settings - Fork 192
feat: argument to disable linting of (
for auto-printing
#2919
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
#' @param allow_scoped Logical, default `FALSE`. If `TRUE`, "scoped assignments", | ||
#' where the object is assigned in the statement beginning a branch and used only | ||
#' within that branch, are skipped. | ||
#' @param allow_print Logical, default `FALSE`. If `TRUE`, using `(` for auto-printing | ||
#' at the top-level is not linted. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
|
@@ -22,6 +24,12 @@ | |
#' linters = implicit_assignment_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "(x <- 1)", | ||
#' linters = implicit_assignment_linter() | ||
#' ) | ||
#' | ||
#' | ||
#' # okay | ||
#' lines <- "x <- 1L\nif (x) TRUE" | ||
#' writeLines(lines) | ||
|
@@ -53,6 +61,11 @@ | |
#' linters = implicit_assignment_linter(allow_scoped = TRUE) | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "(x <- 1)", | ||
#' linters = implicit_assignment_linter(allow_print = TRUE) | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("implicit_assignment_linter") | ||
#' @seealso | ||
#' - [linters] for a complete list of linters available in lintr. | ||
|
@@ -61,7 +74,8 @@ | |
#' @export | ||
implicit_assignment_linter <- function(except = c("bquote", "expression", "expr", "quo", "quos", "quote"), | ||
allow_lazy = FALSE, | ||
allow_scoped = FALSE) { | ||
allow_scoped = FALSE, | ||
allow_print = FALSE) { | ||
stopifnot(is.null(except) || is.character(except)) | ||
|
||
if (length(except) > 0L) { | ||
|
@@ -116,6 +130,9 @@ implicit_assignment_linter <- function(except = c("bquote", "expression", "expr" | |
bad_expr <- xml_find_all(xml, xpath) | ||
|
||
print_only <- !is.na(xml_find_first(bad_expr, "parent::expr[parent::exprlist and *[1][self::OP-LEFT-PAREN]]")) | ||
if (allow_print) { | ||
bad_expr <- bad_expr[!print_only] | ||
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. This works, I think it will be ideal to work this in to the up-front XPath logic, rather than including those hits in the search only to drop them. Have a try and if you get stuck we can mark it as a follow-up issue. 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. I thought that initially but then I realised you need to do the |
||
} | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
|
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.
I think
allow_implicit_print
may be better, WDYT? My only hesitation is doubling "implicit" in the linter & argument names...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.
allow_bracket_print
?I'm already wary of
implicit
as a description as it already doesn't reflect how I think of my usage of this lint ... basically "stop people accidentally using<-
in function calls". I'd prefer not to use the term more if possible.