Skip to content

Commit ffe7b48

Browse files
handle when re_matches returns a data.frame from capture groups (#2287)
1 parent 8f096fe commit ffe7b48

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# lintr (development version)
22

3+
## Bug fixes
4+
5+
* `object_name_linter()` no longer errors when user-supplied `regexes=` have capture groups (#2188, @MichaelChirico).
6+
37
## New and improved features
48

59
* More helpful errors for invalid configs (#2253, @MichaelChirico).

R/object_name_linter.R

+7-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,13 @@ object_name_linter <- function(styles = c("snake_case", "symbols"), regexes = ch
152152
check_style <- function(nms, style, generics = character()) {
153153
conforming <- re_matches(nms, style)
154154

155-
# mark empty names and NA names as conforming
156-
conforming[!nzchar(nms) | is.na(conforming)] <- TRUE
155+
# style has capture group(s)
156+
if (is.data.frame(conforming)) {
157+
# if any group is missing, all groups are missing, so just check the first column
158+
conforming <- !is.na(conforming[[1L]])
159+
}
160+
# mark empty or NA names as conforming
161+
conforming <- is.na(nms) | !nzchar(nms) | conforming
157162

158163
if (!all(conforming)) {
159164
possible_s3 <- re_matches(

tests/testthat/test-object_name_linter.R

+5
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,8 @@ test_that("function shorthand also lints", {
273273

274274
expect_lint("aBc <- \\() NULL", "function name style", object_name_linter())
275275
})
276+
277+
test_that("capture groups in style are fine", {
278+
expect_lint("a <- 1\nab <- 2", NULL, object_name_linter(regexes = c(capture = "^(a)")))
279+
expect_lint("ab <- 1\nabc <- 2", NULL, object_name_linter(regexes = c(capture = "^(a)(b)")))
280+
})

0 commit comments

Comments
 (0)