diff --git a/NEWS.md b/NEWS.md index 9b984a2e8..92f00a4ee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -39,6 +39,8 @@ * `paste_linter()` also catches usages like `paste(rep("*", 10L), collapse = "")` that can be written more concisely as `strrep("*", 10L)` (#1108, @MichaelChirico) +* `spaces_inside_linter()` produces lints for spaces inside `[[` (#1673, @IndrajeetPatil). + * `sprintf_linter()` also applies to `gettextf()` (#1677, @MichaelChirico) ### New linters diff --git a/R/spaces_inside_linter.R b/R/spaces_inside_linter.R index e07f49720..bce2a0e69 100644 --- a/R/spaces_inside_linter.R +++ b/R/spaces_inside_linter.R @@ -38,7 +38,10 @@ spaces_inside_linter <- function() { and @end != following-sibling::*[1]/@start - 1 and @line1 = following-sibling::*[1]/@line1 " - left_xpath <- glue::glue("//OP-LEFT-BRACKET[{left_xpath_condition}] | //OP-LEFT-PAREN[{left_xpath_condition}]") + left_xpath <- glue::glue(" + //OP-LEFT-BRACKET[{left_xpath_condition}] + | //LBB[{left_xpath_condition}] + | //OP-LEFT-PAREN[{left_xpath_condition}]") right_xpath_condition <- " not(preceding-sibling::*[1][self::OP-COMMA]) @@ -58,7 +61,7 @@ spaces_inside_linter <- function() { left_expr <- xml2::xml_find_all(xml, left_xpath) left_msg <- ifelse( - xml2::xml_text(left_expr) == "[", + xml2::xml_text(left_expr) %in% c("[", "[["), "Do not place spaces after square brackets.", "Do not place spaces after parentheses." ) diff --git a/tests/testthat/test-spaces_inside_linter.R b/tests/testthat/test-spaces_inside_linter.R index 973570665..3cab5d145 100644 --- a/tests/testthat/test-spaces_inside_linter.R +++ b/tests/testthat/test-spaces_inside_linter.R @@ -101,6 +101,17 @@ test_that("spaces_inside_linter blocks diallowed usages", { linter ) + expect_lint( + "a[[ 1]]", + list( + message = "Do not place spaces after square brackets", + line_number = 1L, + column_number = 4L, + type = "style" + ), + linter + ) + expect_lint( "a( 1)", list( @@ -112,6 +123,25 @@ test_that("spaces_inside_linter blocks diallowed usages", { linter ) + expect_lint( + "x[[ 1L ]]", + list( + list( + message = "Do not place spaces after square brackets", + line_number = 1L, + column_number = 4L, + type = "style" + ), + list( + message = "Do not place spaces before square brackets", + line_number = 1L, + column_number = 7L, + type = "style" + ) + ), + linter + ) + expect_lint( "a( 1 )", list(