Skip to content

Commit 83cae10

Browse files
Accept default= in modify_defaults (#1339)
1 parent aec989e commit 83cae10

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
It has also been renamed as the argument of the now-private functional versions of many linters, which has no direct
5353
effect on packages importing lintr, but is mentioned in case custom linters imitating `lintr` style have also
5454
adopted the `source_file` naming and want to adapt to keep in sync.
55-
* Deprecated `with_defaults()` in favor of `linters_with_defaults()` (#1029, @AshesITR)
55+
* Deprecated `with_defaults()` in favor of `linters_with_defaults()`, and add `modify_defaults()` which is intended to be used
56+
more generally to modify (i.e., extend, trim, and/or update) a list of defaults. Note that the argument corresponding to
57+
`with_defaults()`'s `default=` is called `defaults=` (i.e., pluralized) in both of these (#1029, #1336, @AshesITR and @michaelchirico)
5658

5759
## Other changes to defaults
5860

R/with.R

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,9 @@ modify_defaults <- function(defaults, ...) {
2424
}
2525
vals <- list(...)
2626
nms <- names2(vals)
27-
missing <- !nzchar(nms, keepNA = TRUE)
28-
if (any(missing)) {
29-
args <- as.character(eval(substitute(alist(...)[missing])))
30-
# foo_linter(x=1) => "foo"
31-
# var[["foo"]] => "foo"
32-
nms[missing] <- re_substitutes(
33-
re_substitutes(
34-
# Very long input might have newlines which are not caught
35-
# by . in a perl regex; see #774
36-
re_substitutes(args, rex("(", anything), "", options = "s"),
37-
rex(start, anything, '["' %or% "::"),
38-
""
39-
),
40-
rex('"]', anything, end),
41-
""
42-
)
27+
missing_index <- !nzchar(nms, keepNA = TRUE)
28+
if (any(missing_index)) {
29+
nms[missing_index] <- guess_names(..., missing_index = missing_index)
4330
}
4431

4532
to_null <- vapply(vals, is.null, logical(1L))
@@ -160,6 +147,22 @@ linters_with_tags <- function(tags, ..., packages = "lintr", exclude_tags = "dep
160147
#' absolute_path_linter()
161148
#' )
162149
linters_with_defaults <- function(..., defaults = default_linters) {
150+
dots <- list(...)
151+
if (missing(defaults) && "default" %in% names(dots)) {
152+
warning(
153+
"'default' is not an argument to linters_with_defaults(). Did you mean 'defaults'? ",
154+
"This warning will be removed when with_defaults() is fully deprecated."
155+
)
156+
defaults <- dots$default
157+
nms <- names2(dots)
158+
missing_index <- !nzchar(nms, keepNA = TRUE)
159+
if (any(missing_index)) {
160+
names(dots)[missing_index] <- guess_names(..., missing_index = missing_index)
161+
}
162+
dots$default <- NULL
163+
dots <- c(dots, list(defaults = defaults))
164+
return(do.call(modify_defaults, dots))
165+
}
163166
modify_defaults(..., defaults = defaults)
164167
}
165168

@@ -181,3 +184,16 @@ call_linter_factory <- function(linter_factory, linter_name, package) {
181184
attr(linter, "name") <- linter_name
182185
linter
183186
}
187+
188+
guess_names <- function(..., missing_index) {
189+
args <- as.character(eval(substitute(alist(...)[missing_index])))
190+
# foo_linter(x=1) => "foo"
191+
# var[["foo"]] => "foo"
192+
# strip call: foo_linter(x=1) --> foo_linter
193+
# NB: Very long input might have newlines which are not caught
194+
# by . in a perl regex; see #774
195+
args <- re_substitutes(args, rex("(", anything), "", options = "s")
196+
# strip extractors: pkg::foo_linter, var[["foo_linter"]] --> foo_linter
197+
args <- re_substitutes(args, rex(start, anything, '["' %or% "::"), "")
198+
re_substitutes(args, rex('"]', anything, end), "")
199+
}

tests/testthat/test-with.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,17 @@ test_that("modify_defaults works", {
6161
# auto-sorts
6262
expect_equal(modify_defaults(defaults = list(b = 2L, a = 1L), c = 3L), my_default)
6363
})
64+
65+
test_that("linters_with_defaults(default = .) is supported with a deprecation warning", {
66+
expect_warning(linters <- linters_with_defaults(default = list(), no_tab_linter()), "'default'")
67+
expect_named(linters, "no_tab_linter")
68+
69+
# the same warning is not triggered in modify_defaults
70+
expect_silent(linters <- modify_defaults(defaults = list(), default = list(), no_tab_linter()))
71+
expect_named(linters, c("default", "no_tab_linter"))
72+
73+
# if default= is explicitly provided alongside defaults=, assume that was intentional
74+
default <- Linter(function(.) list())
75+
expect_silent(linters <- linters_with_defaults(defaults = list(), default = default))
76+
expect_named(linters, "default")
77+
})

0 commit comments

Comments
 (0)