Skip to content

Commit 86d018a

Browse files
committed
add option to ignore blank lines to trailing_whitespace_linter()
supersedes #165
1 parent dd6b3eb commit 86d018a

5 files changed

+58
-30
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ function calls. (#850, #851, @renkun-ken)
137137
This prevents false positive lints in the case of long generic names, e.g.
138138
`very_very_very_long_generic_name.short_class` no longer produces a lint (#871, @AshesITR)
139139
* `object_name_linter()` now correctly detects assignment generics (#843, @jonkeane)
140+
* `trailing_whitespace_linter()` now also lints completely blank lines by default. This can be disabled by setting the
141+
new argument `allow_empty_lines = TRUE` (#1033, @AshesITR)
140142

141143
# lintr 2.0.1
142144

R/trailing_whitespace_linter.R

+26-27
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,40 @@
22
#'
33
#' Check that there are no space characters at the end of source lines.
44
#'
5+
#' @param allow_empty_lines Suppress lints for lines that only contain whitespace.
6+
#'
57
#' @evalRd rd_tags("trailing_whitespace_linter")
68
#' @seealso [linters] for a complete list of linters available in lintr.
79
#' @export
8-
trailing_whitespace_linter <- function() {
10+
trailing_whitespace_linter <- function(allow_empty_lines = FALSE) {
911
Linter(function(source_file) {
12+
if (is.null(source_file$file_lines)) return(list())
13+
1014
res <- re_matches(
11-
source_file$lines,
12-
rex(capture(name = "space", some_of(" ", regex("\\t"))), or(newline, end)),
13-
global = TRUE,
15+
source_file$file_lines,
16+
rex(blanks, end),
1417
locations = TRUE
1518
)
1619

17-
lapply(seq_along(source_file$lines), function(itr) {
18-
19-
mapply(
20-
FUN = function(start, end) {
21-
if (is.na(start)) {
22-
return()
23-
}
24-
line_number <- names(source_file$lines)[itr]
25-
Lint(
26-
filename = source_file$filename,
27-
line_number = line_number,
28-
column_number = start,
29-
type = "style",
30-
message = "Trailing whitespace is superfluous.",
31-
line = source_file$lines[as.character(line_number)],
32-
ranges = list(c(start, end))
33-
)
34-
},
35-
start = res[[itr]]$space.start,
36-
end = res[[itr]]$space.end,
37-
SIMPLIFY = FALSE
38-
)
39-
})
20+
if (isTRUE(allow_empty_lines)) {
21+
bad_lines <- which(res$start > 1L)
22+
} else {
23+
bad_lines <- which(!is.na(res$start))
24+
}
4025

26+
lapply(
27+
bad_lines,
28+
function(line) {
29+
Lint(
30+
filename = source_file$filename,
31+
line_number = line,
32+
column_number = res$start[[line]],
33+
type = "style",
34+
message = "Trailing whitespace is superfluous.",
35+
line = source_file$file_lines[[line]],
36+
ranges = list(c(res$start[[line]], res$end[[line]]))
37+
)
38+
}
39+
)
4140
})
4241
}

man/trailing_whitespace_linter.Rd

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

tests/testthat/test-knitr_formats.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ regexes <- list(
22
assign = rex("Use <-, not =, for assignment."),
33
local_var = rex("local variable"),
44
quotes = rex("Only use double-quotes."),
5-
trailing = rex("Trailing blank lines are superfluous.")
5+
trailing = rex("Trailing blank lines are superfluous."),
6+
trailws = rex("Trailing whitespace is superfluous.")
67
)
78

89
test_that("it handles dir", {
@@ -72,7 +73,12 @@ test_that("it handles tex", {
7273
list(regexes[["assign"]], line_number = 11),
7374
list(regexes[["local_var"]], line_number = 23),
7475
list(regexes[["assign"]], line_number = 23),
75-
list(regexes[["trailing"]], line_number = 25)
76+
list(regexes[["trailing"]], line_number = 25),
77+
list(regexes[["trailws"]], line_number = 25)
78+
# FIXME(AshesITR)
79+
# file_lines contains a whitespace on the final line for Rtex, because that is used to mark the Rtex escape char
80+
# "%" as well.
81+
# cf. get_source_expressions("tests/testthat/knitr_formats/test.Rtex")$lines[[25]]
7682
),
7783
default_linters,
7884
parse_settings = FALSE

tests/testthat/test-trailing_whitespace_linter.R

+18
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,22 @@ test_that("returns the correct linting", {
2020
list(message = rex("Trailing whitespace is superfluous."), line_number = 3),
2121
linter
2222
)
23+
24+
expect_lint(
25+
"blah <- 1\n \n'hi'\na <- 2",
26+
list(message = rex("Trailing whitespace is superfluous."), line_number = 2),
27+
linter
28+
)
29+
30+
expect_lint(
31+
"blah <- 1 ",
32+
list(message = rex("Trailing whitespace is superfluous."), column_number = 10),
33+
trailing_whitespace_linter(allow_empty_lines = TRUE)
34+
)
35+
36+
expect_lint(
37+
"blah <- 1\n \n'hi'\na <- 2",
38+
NULL,
39+
trailing_whitespace_linter(allow_empty_lines = TRUE)
40+
)
2341
})

0 commit comments

Comments
 (0)