Skip to content

Allow trailing_whitespace_linter to skip empty lines #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions R/trailing_whitespace_linter.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
#' @describeIn linters check there are no trailing whitespace characters.
#' @export
trailing_whitespace_linter <- function(source_file) {
trailing_whitespace_linter <- function(check_empty_lines = TRUE) {
# for backwards compatibility, check if this was called on the
# check_empty_lines option or directly on a source file
if (is.logical(check_empty_lines)) {
function(source_file) trailws_linter(source_file, check_empty_lines)
} else { # call with old default
source_file <- check_empty_lines
trailws_linter(source_file, TRUE)
}
}

trailws_linter <- function(source_file, check_empty_lines) {
res <- re_matches(source_file$lines,
rex(capture(name = "space", some_of(" ", regex("\\t"))), or(newline, end)),
global = TRUE,
locations = TRUE)

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

mapply(
FUN = function(start, end) {
if (is.na(start)) {
return()
}
line_number <- names(source_file$lines)[itr]
Lint(
filename = source_file$filename,
line_number = line_number,
column_number = start,
type = "style",
message = "Trailing whitespace is superfluous.",
line = source_file$lines[as.character(line_number)],
ranges = list(c(start, end)),
linter = "trailing_whitespace_linter"
)
},
start = res[[itr]]$space.start,
end = res[[itr]]$space.end,
SIMPLIFY = FALSE
)
})
lapply(seq_along(source_file$lines), function(idx) {
start <- res[[idx]]$space.start
end <- res[[idx]]$space.end

skip_empty_line <- !check_empty_lines && start == 1L
if (is.na(start) || skip_empty_line) {
return()
}

Lint(
filename = source_file$filename,
line_number = names(source_file$lines)[[idx]],
column_number = start,
type = "style",
message = "Trailing whitespace is superfluous.",
line = source_file$lines[[idx]],
ranges = list(c(start, end)),
linter = "trailing_whitespace_linter"
)
})
}
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @title linters
#' @param source_file returned by \code{\link{get_source_expressions}}
#' @param length the length cutoff to use for the given linter.
#' @param check_empty_lines if \code{trailing_whitespace_linter} should consider completely empty lines.
NULL

named_list <- function(...) {
Expand Down Expand Up @@ -75,7 +76,7 @@ default_linters <- with_defaults(default = list(),
multiple_dots_linter,
object_length_linter(30),
object_usage_linter,
trailing_whitespace_linter,
trailing_whitespace_linter(),
trailing_blank_lines_linter,
commented_code_linter,

Expand Down
4 changes: 3 additions & 1 deletion man/linters.Rd

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

1 change: 1 addition & 0 deletions tests/testthat/test-assignment_linter.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
context("assignment_linter")
options(encoding = "UTF-8")
Sys.setenv(LANGUAGE = "en") # make sure no translated messages are matched
test_that("returns the correct linting", {
expect_lint("blah", NULL, assignment_linter)

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-error.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
context("error")
Sys.setenv(LANGUAGE = "en") # make sure no translated messages are matched
test_that("returns the correct linting", {
expect_lint("\"\\R\"",
rex("is an unrecognized escape in character string starting")
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-trailing_whitespace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ context("trailing_whitespace_linter")
test_that("returns the correct linting", {
expect_lint("blah",
NULL,
trailing_whitespace_linter)
trailing_whitespace_linter())

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

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

expect_lint("blah <- 1\n'hi'\na <- 2 ",
list(
Expand All @@ -20,5 +20,5 @@ test_that("returns the correct linting", {
line_number = 3
)
),
trailing_whitespace_linter)
trailing_whitespace_linter())
})