Skip to content

Commit

Permalink
spaces_inside_linter() lints spaces after [[ (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Oct 11, 2022
1 parent 02b9682 commit 849c40e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

### New linters

* `unnecessary_lambda_linter()`: detect unnecessary lambdas (anonymous functions), e.g.
Expand Down
7 changes: 5 additions & 2 deletions R/spaces_inside_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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."
)
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-spaces_inside_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 849c40e

Please sign in to comment.