Skip to content
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

Trailing commas #2103

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions R/commas_linter.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#' Commas linter
#'
#' 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
Expand All @@ -18,6 +21,11 @@
#' text = "x[ ,, drop=TRUE]",
#' linters = commas_linter()
#' )
#'
#' lint(
#' text = "x[1,]",
#' linters = commas_linter()
#' )
#'
#' # okay
#' lint(
Expand All @@ -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) {
# 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
Expand All @@ -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)",
"]"
)

Linter(function(source_expression) {
if (!is_lint_level(source_expression, "expression")) {
Expand Down
55 changes: 54 additions & 1 deletion tests/testthat/test-commas_linter.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("returns the correct linting", {
test_that("returns the correct linting (with default parameters)", {
linter <- commas_linter()
msg_after <- rex::rex("Commas should always have a space after.")
msg_before <- rex::rex("Commas should never have a space before.")
Expand All @@ -13,6 +13,8 @@ test_that("returns the correct linting", {
expect_lint("fun(1\n,1)", msg_after, linter)
expect_lint("fun(1,1)", msg_after, linter)
expect_lint("\nfun(1,1)", msg_after, linter)
expect_lint("a(1,)", msg_after, linter)
expect_lint("a[1,]", msg_after, linter)
expect_lint(
"fun(1 ,1)",
list(
Expand Down Expand Up @@ -46,3 +48,54 @@ test_that("returns the correct linting", {
linter
)
})

test_that("returns the correct linting (with 'allow_trailing_comma' set)", {
linter <- commas_linter(allow_trailing_comma = TRUE)
msg_after <- rex::rex("Commas should always have a space after.")
msg_before <- rex::rex("Commas should never have a space before.")

expect_lint("blah", NULL, linter)
expect_lint("fun(1, 1)", NULL, linter)
expect_lint("fun(1,\n 1)", NULL, linter)
expect_lint("fun(1,\n1)", NULL, linter)
expect_lint("fun(1\n,\n1)", NULL, linter)
expect_lint("fun(1\n ,\n1)", NULL, linter)
expect_lint("a[1,]", NULL, linter)

expect_lint("fun(1\n,1)", msg_after, linter)
expect_lint("fun(1,1)", msg_after, linter)
expect_lint("\nfun(1,1)", msg_after, linter)
expect_lint("a(1,)", msg_after, linter)
expect_lint(
"fun(1 ,1)",
list(
msg_before,
msg_after
),
linter
)

expect_lint("\"fun(1 ,1)\"", NULL, linter)
expect_lint("a[1, , 2]", NULL, linter)
expect_lint("a[1, , 2, , 3]", NULL, linter)

expect_lint("switch(op, x = foo, y = bar)", NULL, linter)
expect_lint("switch(op, x = , y = bar)", NULL, linter)
expect_lint("switch(op, \"x\" = , y = bar)", NULL, linter)
expect_lint("switch(op, x = ,\ny = bar)", NULL, linter)

expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
expect_lint("switch(op , x = foo, y = bar)", msg_before, linter)
expect_lint("switch(op, x = foo, y = bar(a = 4 , b = 5))", msg_before, linter)
expect_lint("fun(op, x = foo , y = switch(bar, a = 4, b = 5))", msg_before, linter)

expect_lint(
"fun(op ,bar)",
list(
list(message = msg_before, column_number = 7L, ranges = list(c(7L, 10L))),
list(message = msg_after, column_number = 12L, ranges = list(c(12L, 12L)))
),
linter
)
})