Skip to content

Commit d44696d

Browse files
committed
Add --no-eval flag to parsable-roxygen
1 parent 2a4a2a8 commit d44696d

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

inst/hooks/exported/parsable-roxygen.R

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
#!/usr/bin/env Rscript
2-
files <- commandArgs(trailing = TRUE)
32

4-
out <- lapply(files, function(path) {
3+
"Check whether roxygen comments within files are valid
4+
Usage:
5+
parsable-roxygen [--no-eval] <files>...
6+
7+
Options:
8+
--no-eval Parse, but do not evaluate, file contents - this also suppresses evaluation of `@eval` tags
9+
10+
" -> doc
11+
12+
arguments <- precommit::precommit_docopt(doc)
13+
14+
out <- lapply(arguments$files, function(path) {
515
tryCatch(
616
# Capture any messages from roxygen2:::warn_roxy()
717
msg <- capture.output(
8-
roxygen2::parse_file(path, env = NULL),
18+
roxygen2::parse_file(
19+
path,
20+
env = if (isTRUE(arguments$no_eval))
21+
NULL
22+
else
23+
roxygen2::env_file(path)
24+
),
925
type = "message"
1026
),
1127

tests/testthat/in/parsable-roxygen-success.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
some_function <- function(x) {
1414
x
1515
}
16+
17+
# To check whether code was evaluated
18+
print("A random print statement")
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
1+
# success - code evaluated
12
run_test(
23
"parsable-roxygen",
34
suffix = "-success.R",
4-
std_err = NULL
5+
std_out = "A random print statement",
6+
std_err = NULL,
7+
read_only = TRUE
58
)
69

7-
# failure - roxygen not parsed
10+
# success - code not evaluated
11+
run_test(
12+
"parsable-roxygen",
13+
suffix = "-success.R",
14+
cmd_args = "--no-eval",
15+
std_out = NULL,
16+
std_err = NULL,
17+
read_only = TRUE
18+
)
19+
20+
# failure - roxygen problem
821
run_test(
922
"parsable-roxygen",
1023
suffix = "-fail.R",
1124
std_out = "Roxygen commentary",
12-
std_err = "@description has mismatched braces or quotes"
25+
std_err = "@description has mismatched braces or quotes",
26+
read_only = TRUE
1327
)
1428

15-
# failure - R not parsed
29+
# failure - R problem
1630
run_test(
1731
"parsable-roxygen",
1832
suffix = "-fail2.R",
1933
std_out = "File ",
20-
std_err = "unexpected '}'"
34+
std_err = "unexpected '}'",
35+
read_only = TRUE
36+
)
37+
38+
39+
run_test(
40+
"parsable-roxygen",
41+
suffix = "-fail4.R",
42+
std_out = "File ",
43+
std_err = "unexpected '}'",
44+
read_only = TRUE
2145
)

vignettes/available-hooks.Rmd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ running `roxygen2::parse_file()` on them returns any messages.
146146

147147
This hook does not modify files.
148148

149+
**no eval**
150+
151+
The `--no-eval` flag causes `roxygen2::parse_file()` to be run with`env = NULL`.
152+
This means each file will be parsed, but code will not be evaluated - neither
153+
any explicit code in the file, nor any `@eval` tags within roxygen comments.
154+
This can be useful if your files contain anything other than
155+
"simple object declarations" (e.g. [box modules](https://github.com/klmr/box/)
156+
containing `box::use()` calls).
157+
158+
If `--no-eval` is set, inline code within roxygen comments (i.e. within
159+
backticks) _will_ still be evaluated, but in a new empty environment.
160+
161+
id: parsable-roxygen
162+
args: [--no-eval]
163+
164+
149165
## `no-browser-statement`
150166

151167
Guarantees you that you don't accidentally commit code with a

0 commit comments

Comments
 (0)