Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.Rhistory
.Rproj.user
.Ruserdata
.Rprofile
docs/
inst/doc
scratch/
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
description: check if a .R file is parsable
entry: Rscript inst/hooks/exported/parsable-R.R
language: r
files: '\.[rR]$'
files: '\.[rR](md)?$'
minimum_pre_commit_version: "2.13.0"
- id: readme-rmd-rendered
name: readme-rmd-rendered
Expand Down
10 changes: 10 additions & 0 deletions inst/hooks/exported/parsable-R.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
files <- commandArgs(trailing = TRUE)

out <- lapply(files, function(path) {
is_rmd <- grepl("\\.[rR]md$", path)
if (is_rmd) {
path <- knitr::purl(
input = path,
output = tempfile(fileext = ".R"),
quiet = TRUE,
documentation = FALSE
)
}

tryCatch(
parse(path),
error = function(x) stop("File ", path, " is not parsable", call. = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion inst/pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
description: check if a .R file is parsable
entry: Rscript inst/hooks/exported/parsable-R.R
language: r
files: '\.[rR]$'
files: '\.[rR](md)?$'
minimum_pre_commit_version: "2.13.0"
- id: readme-rmd-rendered
name: readme-rmd-rendered
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/in/parsable-R-fail.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Parsable Rmd Fail"
output: rmarkdown::html_document
---

```{r}
1 1j1j öj1
```
8 changes: 8 additions & 0 deletions tests/testthat/in/parsable-R-success.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Parsable Rmd Success"
output: rmarkdown::html_document
---

```{r}
call()
```
6 changes: 6 additions & 0 deletions tests/testthat/test-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,15 @@ run_test("parsable-R",
error_msg = NULL
)

run_test("parsable-R",
suffix = "-success.Rmd",
error_msg = NULL
)

# failure
run_test("parsable-R", suffix = "-fail.R", error_msg = "not parsable")

run_test("parsable-R", suffix = "-fail.Rmd", error_msg = "not parsable")

### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
### spell-check ####
Expand Down
5 changes: 3 additions & 2 deletions vignettes/available-hooks.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ This hook does not modify files.

## `parsable-R`

Checks if your `.R` files are "valid" R code by checking if running `parse()` on
them returns an error.
Checks if your `.R` and `.Rmd` files are "valid" R code by checking if running
`parse()` on them (or their `knitr::purl()`ed output for `.Rmd`) returns an
error.

This hook does not modify files.

Expand Down