-
Notifications
You must be signed in to change notification settings - Fork 186
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
Commas trailing #2104
Commas trailing #2104
Changes from 7 commits
b6fd3b2
983edd2
a83523c
67230a0
cb88d3f
176ecdc
69ffa1e
6c14159
4038152
2838a6a
b71d22e
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 |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
#' | ||
#' Check that all commas are followed by spaces, but do not have spaces before them. | ||
#' | ||
#' @param allow_trailing_comma If `TRUE`, the linter allows a comma to be followed | ||
#' directly by a closing bracket without a space. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
|
@@ -19,6 +22,11 @@ | |
#' linters = commas_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "x[1,]", | ||
#' linters = commas_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = "switch(op, x = foo, y = bar)", | ||
|
@@ -40,12 +48,17 @@ | |
#' linters = commas_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "x[1,]", | ||
#' linters = commas_linter(allow_trailing_comma = TRUE) | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("commas_linter") | ||
#' @seealso | ||
#' - [linters] for a complete list of linters available in lintr. | ||
#' - <https://style.tidyverse.org/syntax.html#commas> | ||
#' @export | ||
commas_linter <- function() { | ||
commas_linter <- function(allow_trailing_comma = FALSE) { | ||
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.
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. it's also more consistent with missing_argument_linter() |
||
# conditions are in carefully-chosen order for performance -- | ||
# an expression like c(a,b,c,....) with many elements can have | ||
# a huge number of preceding-siblings and the performance of | ||
|
@@ -58,7 +71,11 @@ commas_linter <- function() { | |
@line1 = preceding-sibling::*[1]/@line1 and | ||
not(preceding-sibling::*[1][self::OP-COMMA or self::EQ_SUB]) | ||
]" | ||
xpath_after <- "//OP-COMMA[@line1 = following-sibling::*[1]/@line1 and @col1 = following-sibling::*[1]/@col1 - 1]" | ||
xpath_after <- paste0( | ||
"//OP-COMMA[@line1 = following-sibling::*[1]/@line1 and @col1 = following-sibling::*[1]/@col1 - 1", | ||
if (allow_trailing_comma) " and not(following-sibling::*[1]/self::OP-RIGHT-BRACKET)", | ||
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. Maybe also allow |
||
"]" | ||
) | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "expression")) { | ||
|
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.
I'd suggest to use the wording "gains an option", see the bullet regarding
fixed_regex_linter()
.And please reference the issue number and give yourself credit for it.