Skip to content

Commit 8669f8c

Browse files
committed
Allow trailing_whitespace_linter to skip empty lines
1 parent 80c8972 commit 8669f8c

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

R/trailing_whitespace_linter.R

+34-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
#' @describeIn linters check there are no trailing whitespace characters.
22
#' @export
3-
trailing_whitespace_linter <- function(source_file) {
3+
trailing_whitespace_linter <- function(check_empty_lines = TRUE) {
4+
# for backwards compatibility, check if this was called on the
5+
# check_empty_lines option or directly on a source file
6+
if (is.logical(check_empty_lines)) {
7+
function(source_file) trailws_linter(source_file, check_empty_lines)
8+
} else { # call with old default
9+
source_file <- check_empty_lines
10+
trailws_linter(source_file, TRUE)
11+
}
12+
}
13+
14+
trailws_linter <- function(source_file, check_empty_lines) {
415
res <- re_matches(source_file$lines,
516
rex(capture(name = "space", some_of(" ", regex("\\t"))), or(newline, end)),
617
global = TRUE,
718
locations = TRUE)
819

9-
lapply(seq_along(source_file$lines), function(itr) {
20+
if (any(lapply(res, nrow) != 1L)) {
21+
stop("invalid data: text after '\\n' found")
22+
}
1023

11-
mapply(
12-
FUN = function(start, end) {
13-
if (is.na(start)) {
14-
return()
15-
}
16-
line_number <- names(source_file$lines)[itr]
17-
Lint(
18-
filename = source_file$filename,
19-
line_number = line_number,
20-
column_number = start,
21-
type = "style",
22-
message = "Trailing whitespace is superfluous.",
23-
line = source_file$lines[as.character(line_number)],
24-
ranges = list(c(start, end)),
25-
linter = "trailing_whitespace_linter"
26-
)
27-
},
28-
start = res[[itr]]$space.start,
29-
end = res[[itr]]$space.end,
30-
SIMPLIFY = FALSE
31-
)
32-
})
24+
lapply(seq_along(source_file$lines), function(idx) {
25+
start <- res[[idx]]$space.start
26+
end <- res[[idx]]$space.end
3327

28+
skip_empty_line <- !check_empty_lines && start == 1L
29+
if (is.na(start) || skip_empty_line) {
30+
return()
31+
}
32+
33+
Lint(
34+
filename = source_file$filename,
35+
line_number = names(source_file$lines)[[idx]],
36+
column_number = start,
37+
type = "style",
38+
message = "Trailing whitespace is superfluous.",
39+
line = source_file$lines[[idx]],
40+
ranges = list(c(start, end)),
41+
linter = "trailing_whitespace_linter"
42+
)
43+
})
3444
}

R/zzz.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#' @title linters
55
#' @param source_file returned by \code{\link{get_source_expressions}}
66
#' @param length the length cutoff to use for the given linter.
7+
#' @param check_empty_lines if \code{trailing_whitespace_linter} should consider completely empty lines.
78
NULL
89

910
named_list <- function(...) {
@@ -75,7 +76,7 @@ default_linters <- with_defaults(default = list(),
7576
multiple_dots_linter,
7677
object_length_linter(30),
7778
object_usage_linter,
78-
trailing_whitespace_linter,
79+
trailing_whitespace_linter(),
7980
trailing_blank_lines_linter,
8081
commented_code_linter,
8182

man/linters.Rd

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-trailing_whitespace_linter.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ context("trailing_whitespace_linter")
22
test_that("returns the correct linting", {
33
expect_lint("blah",
44
NULL,
5-
trailing_whitespace_linter)
5+
trailing_whitespace_linter())
66

77
expect_lint("blah <- 1 ",
88
c(message = rex("Trailing whitespace is superfluous."),
99
column_number = 10),
10-
trailing_whitespace_linter)
10+
trailing_whitespace_linter())
1111

1212
expect_lint("blah <- 1 \n'hi'",
1313
rex("Trailing whitespace is superfluous."),
14-
trailing_whitespace_linter)
14+
trailing_whitespace_linter())
1515

1616
expect_lint("blah <- 1\n'hi'\na <- 2 ",
1717
list(
@@ -20,5 +20,5 @@ test_that("returns the correct linting", {
2020
line_number = 3
2121
)
2222
),
23-
trailing_whitespace_linter)
23+
trailing_whitespace_linter())
2424
})

0 commit comments

Comments
 (0)