Skip to content

Skip linting eval=FALSE chunks - #3072

Merged
MichaelChirico merged 24 commits into
mainfrom
rmd-eval-false
Jul 14, 2026
Merged

MichaelChirico merged 24 commits into
mainfrom
rmd-eval-false

Conversation

@MichaelChirico

Copy link
Copy Markdown
Collaborator

Closes #1964. Initial pass authored by Gemini (I'll try and keep LLM edits pristine and marked as such in the commit log in the PR before ultimately squashing), though you can see it was a bit off on the code quality mark.

@codecov

codecov Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.26%. Comparing base (262580d) to head (1b35daa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3072   +/-   ##
=======================================
  Coverage   99.25%   99.26%           
=======================================
  Files         128      128           
  Lines        7417     7436   +19     
=======================================
+ Hits         7362     7381   +19     
  Misses         55       55           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Gemini review:


Summary of Changes

The PR aims to skip linting of R Markdown/Quarto/Sweave chunks that are marked as eval=FALSE (or eval=F). It updates R/extract.R to parse chunk options and filter out these chunks, and adds corresponding tests.

Review Findings

1. Robustness: Parsing Transient Code (Critical)

During background linting in IDEs, files are often in a transient, syntactically malformed state.
If xfun::csv_options() (for header options) or xfun::divide_chunk() (for body options) encounter invalid syntax, they will throw errors and halt execution. This can crash the background linter process.

Recommendation: Wrap both option parsing calls in tryCatch to handle errors gracefully, defaulting to treating the chunk as evaluated (eval=TRUE) if parsing fails.

is_eval_chunk <- function(start, end, lines, pattern) {
  header <- lines[start]
  params_src <- trimws(gsub(pattern$chunk.begin, "\\1", header))
  header_params <- tryCatch(
    suppressWarnings(xfun::csv_options(params_src)),
    error = function(e) NULL
  )

  code <- lines[(start + 1L):(end - 1L)]
  body_params <- tryCatch(
    suppressWarnings(xfun::divide_chunk("r", code)$options),
    error = function(e) NULL
  )

  eval_value <- body_params$eval %||% header_params$eval
  if (identical(eval_value, quote(F))) {
    return(FALSE)
  }
  !isFALSE(eval_value)
}

2. Design: Avoid Double Negation

The current implementation uses non_eval_chunk which returns TRUE if the chunk is not evaluated. This leads to double negation in the caller:
eval_keep <- !as.logical(Map(...))

Recommendation:

  1. Rename non_eval_chunk to is_eval_chunk (returning TRUE if it should be evaluated).
  2. Use vapply instead of Map for type safety and performance.

Suggested refactoring for R/extract.R:

@@ -73,8 +73,12 @@
   starts <- starts[nonempty_keep]
   ends <- ends[nonempty_keep]
 
-  eval_keep <- !as.logical(Map(\(start, end) non_eval_chunk(start, end, lines, pattern), starts, ends))
-  starts <- starts[eval_keep]
-  ends <- ends[eval_keep]
+  eval_keep <- vapply(
+    seq_along(starts),
+    function(i) is_eval_chunk(starts[i], ends[i], lines, pattern),
+    FUN.VALUE = logical(1)
+  )
+  starts <- starts[eval_keep]
+  ends <- ends[eval_keep]

3. Tests

The added tests in tests/testthat/test-knitr_formats.R are excellent. They cover:

  • .Rmd, .qmd, and .Rnw formats.
  • Header options (eval=FALSE, eval=F) and body options (#| eval: false).
  • Verifying that eval=TRUE chunks are still linted.
  • Zero-chunk files.

@MichaelChirico
MichaelChirico merged commit 468f960 into main Jul 14, 2026
21 checks passed
@MichaelChirico
MichaelChirico deleted the rmd-eval-false branch July 14, 2026 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skip eval=FALSE chunks

1 participant