|
1 | 1 | #' @describeIn linters check there are no trailing whitespace characters.
|
2 | 2 | #' @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) { |
4 | 15 | res <- re_matches(source_file$lines,
|
5 | 16 | rex(capture(name = "space", some_of(" ", regex("\\t"))), or(newline, end)),
|
6 | 17 | global = TRUE,
|
7 | 18 | locations = TRUE)
|
8 | 19 |
|
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 | + } |
10 | 23 |
|
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 |
33 | 27 |
|
| 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 | + }) |
34 | 44 | }
|
0 commit comments