Skip to content

Commit 7f4d152

Browse files
Add "list" to S3 class of lint(), Lint() output (#1641)
* 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]>
1 parent 7755014 commit 7f4d152

File tree

6 files changed

+33
-23
lines changed

6 files changed

+33
-23
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868

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

71+
* The output of `lint()` and `Lint()` gain S3 class `"list"` to assist with S3 dispatch (#1494, @MichaelChirico)
72+
7173
# lintr 3.0.1
7274

7375
* Skip multi-byte tests in non UTF-8 locales (#1504)

R/lint.R

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#'
2626
#' @aliases lint_file
2727
# TODO(next release after 3.0.0): remove the alias
28-
#' @return A list of lint objects.
28+
#' @return An object of class `c("lints", "list")`, each element of which is a `"list"` object.
2929
#'
3030
#' @examples
3131
#' \dontrun{
@@ -97,7 +97,8 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings =
9797
}
9898

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

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

456457
type <- match.arg(type)
457458

458-
structure(
459-
list(
460-
filename = filename,
461-
line_number = as.integer(line_number),
462-
column_number = as.integer(column_number),
463-
type = type,
464-
message = message,
465-
line = line,
466-
ranges = ranges,
467-
linter = NA_character_
468-
),
469-
class = "lint")
459+
obj <- list(
460+
filename = filename,
461+
line_number = as.integer(line_number),
462+
column_number = as.integer(column_number),
463+
type = type,
464+
message = message,
465+
line = line,
466+
ranges = ranges,
467+
linter = NA_character_
468+
)
469+
class(obj) <- c("lint", "list")
470+
obj
470471
}
471472

472473
rstudio_source_markers <- function(lints) {

man/lint-s3.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/lint.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-lint_file.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# The lints for a given file should be the same regardless of the working
22
# directory
33

4-
# Helper function: run assignment_linter on a given file
5-
lint_assignments <- function(filename) {
6-
lint(filename, linters = list(assignment_linter()))
7-
}
8-
94
test_that("lint() results do not depend on the working directory", {
5+
# Helper function: run assignment_linter on a given file
6+
lint_assignments <- function(filename) {
7+
lint(filename, linters = list(assignment_linter()))
8+
}
9+
1010
# a dummy package for use in the test
1111
pkg_path <- test_path("dummy_packages", "assignmentLinter")
1212

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

6969
# a dummy package for use in the test

tests/testthat/test-methods.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test_that("as.data.frame.lints", {
3131
),
3232
"lint"
3333
)
34+
expect_type(l1, "list")
3435

3536
# A larger lint
3637
expect_s3_class(
@@ -147,3 +148,9 @@ test_that("split.lint works as intended", {
147148
l <- lint(tmp, seq_linter())
148149
expect_true(all(vapply(split(l), inherits, logical(1L), "lints")))
149150
})
151+
152+
test_that("within.list is dispatched", {
153+
l <- lint(text = "a=1\nb=2", linters = infix_spaces_linter())
154+
expect_silent(l <- lapply(l, within, line_number <- line_number + 1L))
155+
expect_identical(vapply(l, `[[`, integer(1L), "line_number"), 2L:3L)
156+
})

0 commit comments

Comments
 (0)