Skip to content

Commit

Permalink
fix duplicate line_length lints. (#682)
Browse files Browse the repository at this point in the history
* fix duplicate line_length lints.

fixes #681

also added a regression test.

* update NEWS.md

* incorporate suggestions
  • Loading branch information
AshesITR authored Dec 6, 2020
1 parent f2d2666 commit 28951bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Lints are now marked with the name of the `linter` that caused them instead of the name of their implementation
function (#664, #673, @AshesITR).
* Fixed `spaces_left_parentheses_linter` sporadically causing warnings (#654, #674, @AshesITR)
* Fixed `line_length_linter` causing duplicate lints for lines containing multiple expressions (#681, #682, @AshesITR)

# lintr 2.0.1

Expand Down
10 changes: 8 additions & 2 deletions R/line_length_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
line_length_linter <- function(length) {
function(source_file) {

lapply(names(source_file$lines)[vapply(source_file$lines, nchar, integer(1)) > length],
# Only go over complete file
if (is.null(source_file$file_lines)) return(list())

oversized_lines <- which(vapply(source_file$file_lines, nchar, integer(1)) > length)

lapply(oversized_lines,
function(line_number) {

col_start <- 1
line <- source_file$lines[as.character(line_number)]
line <- source_file$file_lines[line_number]
col_end <- unname(nchar(line))

Lint(
Expand Down
11 changes: 10 additions & 1 deletion tests/testthat/test-line_length_linter.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("line_length_linter")
test_that("returns the correct linting", {

expect_lint("blah",
Expand Down Expand Up @@ -30,4 +29,14 @@ test_that("returns the correct linting", {
expect_lint("aaaaaaaaaaaaaaaaaaaab",
rex("Lines should not be more than 20 characters"),
line_length_linter(20))

# Don't duplicate lints
expect_length(
lint(
"x <- 2 # ------------\n",
linters = line_length_linter(20),
parse_settings = FALSE
),
1L
)
})

0 comments on commit 28951bc

Please sign in to comment.