Skip to content
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

Add utils to check for lint and error classes #2681

Merged
merged 3 commits into from
Nov 18, 2024
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
2 changes: 1 addition & 1 deletion R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extract_r_source <- function(filename, lines, error = identity) {
output <- rep.int(NA_character_, length(lines))

chunks <- tryCatch(get_chunk_positions(pattern = pattern, lines = lines), error = error)
if (inherits(chunks, "error") || inherits(chunks, "lint")) {
if (is_error(chunks) || is_lint(chunks)) {
assign("e", chunks, envir = parent.frame())
# error, so return empty code
return(output)
Expand Down
6 changes: 3 additions & 3 deletions R/get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ get_source_expressions <- function(filename, lines = NULL) {
source_expression$content <- get_content(source_expression$lines)
parsed_content <- get_source_expression(source_expression, error = function(e) lint_parse_error(e, source_expression))

if (inherits(e, "lint") && (is.na(e$line) || !nzchar(e$line) || e$message == "unexpected end of input")) {
if (is_lint(e) && (is.na(e$line) || !nzchar(e$line) || e$message == "unexpected end of input")) {
# Don't create expression list if it's unreliable (invalid encoding or unhandled parse error)
expressions <- list()
} else {
Expand Down Expand Up @@ -502,7 +502,7 @@ get_source_expression <- function(source_expression, error = identity) {
error = error
)

if (inherits(parsed_content, c("error", "lint"))) {
if (is_error(parsed_content) || is_lint(parsed_content)) {
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
assign("e", parsed_content, envir = parent.frame())
parse_error <- TRUE
}
Expand All @@ -513,7 +513,7 @@ get_source_expression <- function(source_expression, error = identity) {
error = error
)

if (inherits(parsed_content, c("error", "lint"))) {
if (is_error(parsed_content) || is_lint(parsed_content)) {
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
# Let parse errors take precedence over encoding problems
if (!parse_error) assign("e", parsed_content, envir = parent.frame())
return() # parsed_content is unreliable if encoding is invalid
Expand Down
2 changes: 1 addition & 1 deletion R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ maybe_report_progress <- function(pb) {
}

maybe_append_error_lint <- function(lints, error, lint_cache, filename) {
if (inherits(error, "lint")) {
if (is_lint(error)) {
error$linter <- "error"
lints[[length(lints) + 1L]] <- error

Expand Down
5 changes: 4 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ get_r_string <- function(s, xpath = NULL) {
}

is_linter <- function(x) inherits(x, "linter")
is_lint <- function(x) inherits(x, "lint")

is_error <- function(x) inherits(x, "error")

is_tainted <- function(lines) {
inherits(tryCatch(nchar(lines), error = identity), "error")
is_error(tryCatch(nchar(lines), error = identity))
}

#' Check that the entries in ... are valid
Expand Down
Loading