Skip to content

Commit

Permalink
Merge branch 'main' into sprintf-gettext
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Oct 11, 2022
2 parents 4ab6b67 + 849c40e commit 09d3461
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).

* `sprintf_linter()` also applies to `gettextf()` (#1677, @MichaelChirico)

### New linters
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 09d3461

Please sign in to comment.