diff --git a/.gitignore b/.gitignore index e0392f267..5240de782 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .Rhistory .Rproj.user .Ruserdata +.Rprofile docs/ inst/doc scratch/ diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 7040f9b99..2df6fc02b 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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 diff --git a/inst/hooks/exported/parsable-R.R b/inst/hooks/exported/parsable-R.R index ae2c1a997..ad8c4a4d5 100755 --- a/inst/hooks/exported/parsable-R.R +++ b/inst/hooks/exported/parsable-R.R @@ -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) diff --git a/inst/pre-commit-hooks.yaml b/inst/pre-commit-hooks.yaml index 7040f9b99..2df6fc02b 100644 --- a/inst/pre-commit-hooks.yaml +++ b/inst/pre-commit-hooks.yaml @@ -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 diff --git a/tests/testthat/in/parsable-R-fail.Rmd b/tests/testthat/in/parsable-R-fail.Rmd new file mode 100644 index 000000000..ffcbbee33 --- /dev/null +++ b/tests/testthat/in/parsable-R-fail.Rmd @@ -0,0 +1,8 @@ +--- +title: "Parsable Rmd Fail" +output: rmarkdown::html_document +--- + +```{r} +1 1j1j öj1 +``` diff --git a/tests/testthat/in/parsable-R-success.Rmd b/tests/testthat/in/parsable-R-success.Rmd new file mode 100644 index 000000000..70ce092a7 --- /dev/null +++ b/tests/testthat/in/parsable-R-success.Rmd @@ -0,0 +1,8 @@ +--- +title: "Parsable Rmd Success" +output: rmarkdown::html_document +--- + +```{r} +call() +``` diff --git a/tests/testthat/test-hooks.R b/tests/testthat/test-hooks.R index a9768d3ad..c7feeaea7 100644 --- a/tests/testthat/test-hooks.R +++ b/tests/testthat/test-hooks.R @@ -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 #### diff --git a/vignettes/available-hooks.Rmd b/vignettes/available-hooks.Rmd index a57758237..beab4c81f 100644 --- a/vignettes/available-hooks.Rmd +++ b/vignettes/available-hooks.Rmd @@ -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.