Skip to content

skip evaluation that hangs on bad input #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
* `get_source_expressions()` no longer fails on R files that match a knitr pattern (#743, #879, #1406, @AshesITR).
* Parse error lints now appear with the linter name `"error"` instead of `NA` (#1405, @AshesITR).
Also, linting no longer runs if the `source_expressions` contain invalid string data that would cause error messages
in other linters.
in other linters.
+ Also precludes hanging on Rmd files with some syntax errors (#1443, @MichaelChirico).
* `get_source_expressions()` no longer omits trailing non-code lines from knitr files (#1400, #1415, @AshesITR).
This fixes the location information for `trailing_blank_lines_linter()` in RMarkdown documents without terminal
newlines.
Expand Down
4 changes: 2 additions & 2 deletions R/get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ get_source_expressions <- function(filename, lines = NULL) {
names(source_expression$lines) <- seq_along(source_expression$lines)
source_expression$content <- get_content(source_expression$lines)
parsed_content <- get_source_expression(source_expression, error = function(e) lint_parse_error(e, source_expression))
top_level_map <- generate_top_level_map(parsed_content)

if (inherits(e, "lint") && !nzchar(e$line)) {
if (inherits(e, "lint") && (is.na(e$line) || !nzchar(e$line))) {
# Don't create expression list if it's unreliable (invalid encoding or unhandled parse error)
expressions <- list()
} else {
top_level_map <- generate_top_level_map(parsed_content)
xml_parsed_content <- safe_parse_to_xml(parsed_content)

expressions <- lapply(
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,19 @@ test_that("#743, #879, #1406: get_source_expressions works on R files matching a
expect_null(source_expressions$error)
})

test_that("Syntax errors in Rmd don't choke lintr", {
tmp <- withr::local_tempfile(lines = c(
"```{r}",
"if (TRUE) {",
" 1",
# missing `}` here
"if (TRUE) {",
"}",
"```"
))
expect_silent(get_source_expressions(tmp))
})

skip_if_not_installed("patrick")
# NB: this is just a cursory test for linters not to
# fail on files where the XML content is xml_missing;
Expand Down