Skip to content

Commit

Permalink
Add "list" to S3 class of lint(), Lint() output (#1641)
Browse files Browse the repository at this point in the history
* add "list" to S3 class of lint() and Lint() output

* NEWS

* docs

* test that within.list is dispatched

* Update tests/testthat/test-methods.R

Co-authored-by: Indrajeet Patil <[email protected]>
  • Loading branch information
MichaelChirico and IndrajeetPatil authored Oct 8, 2022
1 parent 7755014 commit 7f4d152
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@

Thanks to Yihui and other developers for their helpful discussions around this issue (#797, @IndrajeetPatil).

* The output of `lint()` and `Lint()` gain S3 class `"list"` to assist with S3 dispatch (#1494, @MichaelChirico)

# lintr 3.0.1

* Skip multi-byte tests in non UTF-8 locales (#1504)
Expand Down
31 changes: 16 additions & 15 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#'
#' @aliases lint_file
# TODO(next release after 3.0.0): remove the alias
#' @return A list of lint objects.
#' @return An object of class `c("lints", "list")`, each element of which is a `"list"` object.
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -97,7 +97,8 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings =
}

lints <- maybe_append_error_lint(lints, source_expressions$error, lint_cache, filename)
lints <- structure(reorder_lints(flatten_lints(lints)), class = "lints")
lints <- reorder_lints(flatten_lints(lints))
class(lints) <- c("lints", "list")

cache_file(lint_cache, filename, linters, lints)
save_cache(lint_cache, filename, cache_path)
Expand Down Expand Up @@ -439,7 +440,7 @@ pkg_name <- function(path = find_package()) {
#' @param line code source where the lint occurred
#' @param ranges a list of ranges on the line that should be emphasized.
#' @param linter deprecated. No longer used.
#' @return an object of class 'lint'.
#' @return an object of class `c("lint", "list")`.
#' @name lint-s3
#' @export
Lint <- function(filename, line_number = 1L, column_number = 1L, # nolint: object_name.
Expand All @@ -455,18 +456,18 @@ Lint <- function(filename, line_number = 1L, column_number = 1L, # nolint: objec

type <- match.arg(type)

structure(
list(
filename = filename,
line_number = as.integer(line_number),
column_number = as.integer(column_number),
type = type,
message = message,
line = line,
ranges = ranges,
linter = NA_character_
),
class = "lint")
obj <- list(
filename = filename,
line_number = as.integer(line_number),
column_number = as.integer(column_number),
type = type,
message = message,
line = line,
ranges = ranges,
linter = NA_character_
)
class(obj) <- c("lint", "list")
obj
}

rstudio_source_markers <- function(lints) {
Expand Down
2 changes: 1 addition & 1 deletion man/lint-s3.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/lint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions tests/testthat/test-lint_file.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# The lints for a given file should be the same regardless of the working
# directory

# Helper function: run assignment_linter on a given file
lint_assignments <- function(filename) {
lint(filename, linters = list(assignment_linter()))
}

test_that("lint() results do not depend on the working directory", {
# Helper function: run assignment_linter on a given file
lint_assignments <- function(filename) {
lint(filename, linters = list(assignment_linter()))
}

# a dummy package for use in the test
pkg_path <- test_path("dummy_packages", "assignmentLinter")

Expand Down Expand Up @@ -63,7 +63,7 @@ test_that("lint() results do not depend on the position of the .lintr", {
lint_with_config <- function(config_path, config_string, filename) {
cat(config_string, file = config_path)
on.exit(unlink(config_path))
lint_assignments(filename)
lint(filename, linters = assignment_linter())
}

# a dummy package for use in the test
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test_that("as.data.frame.lints", {
),
"lint"
)
expect_type(l1, "list")

# A larger lint
expect_s3_class(
Expand Down Expand Up @@ -147,3 +148,9 @@ test_that("split.lint works as intended", {
l <- lint(tmp, seq_linter())
expect_true(all(vapply(split(l), inherits, logical(1L), "lints")))
})

test_that("within.list is dispatched", {
l <- lint(text = "a=1\nb=2", linters = infix_spaces_linter())
expect_silent(l <- lapply(l, within, line_number <- line_number + 1L))
expect_identical(vapply(l, `[[`, integer(1L), "line_number"), 2L:3L)
})

0 comments on commit 7f4d152

Please sign in to comment.