Skip to content

Commit a2c0e68

Browse files
committed
Discover and check for rogue print statements
1 parent b5f2610 commit a2c0e68

11 files changed

+60
-0
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ repos:
7373
(?x)^(
7474
tests/testthat/in/.*|
7575
)$
76+
- id: no-print-statement
77+
exclude: >
78+
(?x)^(
79+
tests/testthat/in/.*|
80+
)$
7681
- id: no-debug-statement
7782
exclude: >
7883
(?x)^(

.pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
language: r
2929
files: '\.[rR]$'
3030
minimum_pre_commit_version: "2.13.0"
31+
- id: no-print-statement
32+
name: no-print-statement
33+
description: check if a .R file contains a `print()` statement
34+
entry: Rscript inst/hooks/exported/no-print-statement.R
35+
language: r
36+
files: '\.[rR]$'
37+
minimum_pre_commit_version: "2.13.0"
3138
- id: no-debug-statement
3239
name: no-debug-statement
3340
description: check if a .R file contains a `debug()` or `debugonce()` statement
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env Rscript
2+
3+
files <- commandArgs(trailing = TRUE)
4+
no_print_statement <- function(path) {
5+
pd <- getParseData(parse(path, keep.source = TRUE))
6+
if (any(pd$text[pd$token == "SYMBOL_FUNCTION_CALL"] == "print")) {
7+
stop("File `", path, "` contains a `print()` statement.", call. = FALSE)
8+
}
9+
}
10+
11+
for (file in files) {
12+
temp <- no_print_statement(file)
13+
}

inst/pre-commit-config-pkg.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ repos:
4444
- id: readme-rmd-rendered
4545
- id: parsable-R
4646
- id: no-browser-statement
47+
- id: no-print-statement
4748
- id: no-debug-statement
4849
- id: deps-in-desc
4950
- id: pkgdown

inst/pre-commit-config-proj.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ repos:
4040
- id: readme-rmd-rendered
4141
- id: parsable-R
4242
- id: no-browser-statement
43+
- id: no-print-statement
4344
- id: no-debug-statement
4445
- repo: https://github.com/pre-commit/pre-commit-hooks
4546
rev: v1.2.3

inst/pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
language: r
2929
files: '\.[rR]$'
3030
minimum_pre_commit_version: "2.13.0"
31+
- id: no-print-statement
32+
name: no-print-statement
33+
description: check if a .R file contains a `print()` statement
34+
entry: Rscript inst/hooks/exported/no-print-statement.R
35+
language: r
36+
files: '\.[rR]$'
37+
minimum_pre_commit_version: "2.13.0"
3138
- id: no-debug-statement
3239
name: no-debug-statement
3340
description: check if a .R file contains a `debug()` or `debugonce()` statement
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"print()"

tests/testthat/test-hooks.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,22 @@ run_test(
157157
std_err = "contains a `browser()` statement."
158158
)
159159

160+
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
161+
### no-print-statement ####
162+
# success
163+
run_test(
164+
"no-print-statement",
165+
suffix = "-success.R",
166+
std_err = NULL
167+
)
168+
169+
# failure
170+
run_test(
171+
"no-print-statement",
172+
suffix = "-fail.R",
173+
std_err = "contains a `print()` statement."
174+
)
175+
160176
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
161177
### no-debug-statement ####
162178
# success

vignettes/available-hooks.Rmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ Guarantees you that you don't accidentally commit code with a
146146

147147
This hook does not modify files.
148148

149+
## `no-print-statement`
150+
151+
Guarantees you that you don't accidentally commit code with a
152+
`print()` statement in it.
153+
154+
This hook does not modify files.
155+
149156
## `no-debug-statement`
150157

151158
Guarantees you that you don't accidentally commit code with a `debug()`

0 commit comments

Comments
 (0)