Skip to content

Commit

Permalink
more messages in lint
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Apr 1, 2024
1 parent 9889933 commit 04c45d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
20 changes: 13 additions & 7 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,20 @@ Lint <- function(filename, line_number = 1L, column_number = 1L, # nolint: objec
}

if (length(line) != 1L || !is.character(line)) {
stop("`line` must be a string.", call. = FALSE)
cli_abort("{.arg line} must be a string.", call. = FALSE)
}
max_col <- max(nchar(line) + 1L, 1L, na.rm = TRUE)
if (!is_number(column_number) || column_number < 0L || column_number > max_col) {
stop(sprintf(
"`column_number` must be an integer between 0 and nchar(line) + 1 (%d). It was %s.",
max_col, column_number
), call. = FALSE)
cli_abort(c(
i = "{.arg column_number} must be an integer between {.val {0}} and {.code nchar(line) + 1} ({.val {max_col}})",
x = "Instead, it was {.val {column_number}}."
))
}
if (!is_number(line_number) || line_number < 1L) {
stop(sprintf("`line_number` must be a positive integer. It was %s.", line_number), call. = FALSE)
cli_abort(c(
i = "{.arg line_number} must be a positive integer.",
x = "Instead, it was {.val {line_number}}."
))
}
check_ranges(ranges, max_col)

Expand Down Expand Up @@ -448,7 +451,10 @@ check_ranges <- function(ranges, max_col) {
return()
}
if (!is.list(ranges)) {
stop("`ranges` must be NULL or a list.", call. = FALSE)
cli_abort(c(
i = "{.arg ranges} must be {.code NULL} or a {.cls list}.",
x = "Instead, it was {.cls {class(ranges)}}."
))
}

for (range in ranges) {
Expand Down
4 changes: 2 additions & 2 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ call_linter_factory <- function(linter_factory, linter_name, package) {
linter <- tryCatch(
linter_factory(),
error = function(e) {
fun_name <- paste0(package, "::", linter_name) # nolint: object_name_linter
pkg_fn <- paste0(package, "::", linter_name) # nolint: object_name_linter
cli_abort(c(
i = "Could not create linter with {.fun fun_name}.",
i = "Could not create linter with {.fun pkg_fn}.",
x = conditionMessage(e)
))
}
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-Lint-builder.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ test_that("Lint() errors on invalid input", {
dummy_line <- "abc"
expect_error(
Lint("dummy.R", line = dummy_line, column_number = NA_integer_),
rex::rex("`column_number` must be an integer between 0 and nchar(line) + 1 (4). It was NA.")
"`column_number` must be an integer between 0 and `nchar(line) + 1` (4)",
fixed = TRUE
)
expect_error(
Lint("dummy.R", line = dummy_line, line_number = 0L),
rex::rex("`line_number` must be a positive integer. It was 0.")
"`line_number` must be a positive integer",
fixed = TRUE
)
expect_error(
Lint("dummy.R", ranges = c(1L, 3L)),
rex::rex("`ranges` must be NULL or a list.")
"`ranges` must be `NULL` or a <list>",
fixed = TRUE
)
expect_error(
Lint("dummy.R", ranges = list(1L)),
Expand Down

0 comments on commit 04c45d4

Please sign in to comment.