Skip to content

Commit 873df8c

Browse files
committed
Add --load_package to lintr
1 parent ba21d8b commit 873df8c

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

inst/hooks/exported/lintr.R

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
"Run lintr on R files during a precommit.
55
Usage:
6-
lintr [--warn_only] <files>...
6+
lintr [options] <files>...
7+
78
Options:
8-
--warn_only Print lint warnings instead of blocking the commit. Should be
9-
used with `verbose: True` in `.pre-commit-config.yaml`.
10-
Otherwise, lints will never be shown to the user.
9+
--warn_only Print lint warnings instead of blocking the commit. Should be
10+
used with `verbose: True` in `.pre-commit-config.yaml`.
11+
Otherwise, lints will never be shown to the user.
12+
--load_package Use `pkgload::load_all()` to load subject package prior to
13+
running lintr.
14+
1115
" -> doc
1216

1317
arguments <- precommit::precommit_docopt(doc)
@@ -23,6 +27,11 @@ if (any(lintr_staged)) {
2327
)
2428
}
2529

30+
if (arguments$load_package) {
31+
cat("Attempting to load package\n")
32+
pkgload::load_all()
33+
}
34+
2635
for (path in arguments$files) {
2736
lints <- lintr::lint(path)
2837
if (length(lints) > 0) {

tests/testthat/test-docopt.R

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ test_that("custom docopt interface parses as expected", {
77
c("Another file with spaces.R", "Yet another file with spaces (YAFWS).R", "--warn_only"),
88
c("--warn_only", "Another file with spaces.R"),
99
c("--warn_only", "Another file with spaces.R", "Yet another file with spaces (YAFWS).R"),
10-
c("Another file with spaces.R", "--warn_only", "Yet another file with spaces (YAFWS).R")
10+
c("Another file with spaces.R", "--warn_only", "Yet another file with spaces (YAFWS).R"),
11+
c("--load_package", "Another file with spaces.R", "Yet another file with spaces (YAFWS).R")
1112
)
1213

1314
"Run lintr on R files during a precommit.
1415
Usage:
15-
cmdtest [--warn_only] <files>...
16+
cmdtest [options] <files>...
1617
Options:
17-
--warn_only Placeholder for test.
18+
--warn_only Placeholder for test.
19+
--load_package Placeholder for test.
1820
" -> doc
1921

2022
for (args in args_variants) {
@@ -35,5 +37,12 @@ Options:
3537
expect_false(new_args$warn_only)
3638
expect_false(new_args$`--warn_only`)
3739
}
40+
if ("--load_package" %in% args) {
41+
expect_true(new_args$load_package)
42+
expect_true(new_args$`--load_package`)
43+
} else {
44+
expect_false(new_args$load_package)
45+
expect_false(new_args$`--load_package`)
46+
}
3847
}
3948
})

vignettes/available-hooks.Rmd

+7
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ in a `.lintr` config file in Debian Control Field Format as specified in
347347
the [`.lintr`
348348
documentation](https://github.com/r-lib/lintr#project-configuration).
349349

350+
When linting a package, `load_package` lintr uses `pkgload::load_all()`
351+
to load the package before running `lintr::lint()`.
352+
353+
id: lintr
354+
args: [--load_package]
355+
verbose: true
356+
350357
This hook does not modify any file.
351358

352359
## `codemeta-description-updated`

0 commit comments

Comments
 (0)