Skip to content

Commit bda3739

Browse files
handle alist(kwd = ) false positive (#1643)
Co-authored-by: Indrajeet Patil <[email protected]>
1 parent 7f4d152 commit bda3739

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* `object_usage_linter()` gains `skip_with` argument to skip code in `with()` expressions.
1616
To be consistent with `R CMD check`, it defaults to `TRUE` (#941, #1458, @IndrajeetPatil).
1717

18+
* `spaces_inside_linter()` allows terminal missing keyword arguments (e.g. `alist(arg = )`; #540, @MichaelChirico)
19+
1820
## New and improved features
1921

2022
* New `get_r_string()` helper to get the R-equivalent value of a string, especially useful for R-4-style raw strings.

R/spaces_inside_linter.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ spaces_inside_linter <- function() {
2222
and @start != preceding-sibling::*[1]/@end + 1
2323
and @line1 = preceding-sibling::*[1]/@line2
2424
"
25-
right_xpath <- glue::glue("//OP-RIGHT-BRACKET[{right_xpath_condition}] | //OP-RIGHT-PAREN[{right_xpath_condition}]")
25+
right_xpath <- glue::glue("
26+
//OP-RIGHT-BRACKET[{right_xpath_condition}]
27+
| //OP-RIGHT-PAREN[{right_xpath_condition} and not(preceding-sibling::*[1][self::EQ_SUB])]")
2628

2729
Linter(function(source_expression) {
2830
if (!is_lint_level(source_expression, "file")) {

tests/testthat/test-infix_spaces_linter.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ test_that("multi-line, multi-expression case is caught", {
166166
infix_spaces_linter()
167167
)
168168
})
169+
170+
test_that("Rules around missing arguments are respected", {
171+
linter <- infix_spaces_linter()
172+
lint_msg <- rex::rex("Put spaces around all infix operators.")
173+
174+
expect_lint("switch(a = , b = 2)", NULL, linter)
175+
expect_lint("alist(missing_arg = )", NULL, linter)
176+
177+
expect_lint("switch(a =, b = 2)", lint_msg, linter)
178+
expect_lint("alist(missing_arg =)", lint_msg, linter)
179+
})

tests/testthat/test-spaces_inside_linter.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,7 @@ test_that("mutli-line expressions have good markers", {
110110
spaces_inside_linter()
111111
)
112112
})
113+
114+
test_that("terminal missing keyword arguments are OK", {
115+
expect_lint("alist(missing_arg = )", NULL, spaces_inside_linter())
116+
})

0 commit comments

Comments
 (0)