Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
98ebda0
ignore string contents optionally
MichaelChirico Feb 27, 2025
1478a4c
expect_no_lint
MichaelChirico Feb 27, 2025
75052f4
test with no string input
MichaelChirico Feb 27, 2025
cdcadd8
revert as.integer()
MichaelChirico Mar 3, 2025
08856b1
Merge branch 'main' into line-length-str-body
MichaelChirico Mar 3, 2025
b44c55a
more tests against off-by-one issue
MichaelChirico Mar 3, 2025
39627ce
Merge branch 'main' into line-length-str-body
MichaelChirico Mar 3, 2025
e9018ec
Merge branch 'main' into line-length-str-body
MichaelChirico May 8, 2025
314dd8b
Merge branch 'main' into line-length-str-body
MichaelChirico May 8, 2025
ff20158
Merge branch 'main' into line-length-str-body
MichaelChirico Jun 5, 2025
744f09e
Merge branch 'main' into line-length-str-body
MichaelChirico Jun 24, 2025
844c4e7
Merge branch 'main' into line-length-str-body
MichaelChirico Jul 29, 2025
2ac86a8
Merge branch 'main' into line-length-str-body
MichaelChirico Jul 29, 2025
4c0a210
typo: with->width
MichaelChirico Jul 29, 2025
eaf77c1
Merge branch 'main' into line-length-str-body
MichaelChirico Sep 15, 2025
a80c6e9
Merge branch 'main' into line-length-str-body
MichaelChirico Sep 16, 2025
b7ff1e3
try simpler version
MichaelChirico Oct 29, 2025
f925a78
Merge branch 'main' into line-length-str-body
MichaelChirico Oct 29, 2025
72f53bb
Revert "try simpler version"
MichaelChirico Oct 29, 2025
e8a50af
new test
MichaelChirico Oct 29, 2025
32d6266
example
MichaelChirico Oct 29, 2025
7f3a029
another example of an inline string
MichaelChirico Oct 29, 2025
69e1fd8
Merge branch 'main' into line-length-str-body
MichaelChirico Oct 29, 2025
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* `indentation_linter()` handles `for` un-braced for loops correctly (#2564, @MichaelChirico).
* Setting `exclusions` supports globs like `knitr*` to exclude files/directories with a pattern (#1554, @MichaelChirico).
* `object_name_linter()` and `object_length_linter()` apply to objects assigned with `assign()` or generics created with `setGeneric()` (#1665, @MichaelChirico).
* `line_length_linter()` has a new argument `ignore_string_bodies` (defaulting to `FALSE`) which governs whether the contents of multi-line string bodies should be linted (#856, @MichaelChirico). We think the biggest use case for this is writing SQL in R strings, especially in cases where the recommended string with for SQL & R differ.

### Lint accuracy fixes: removing false positives

Expand Down
51 changes: 48 additions & 3 deletions R/line_length_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#'
#' Check that the line length of both comments and code is less than `length`.
#'
#' @param length maximum line length allowed. Default is 80L (Hollerith limit).
#' @param length Maximum line length allowed. Default is `80L` (Hollerith limit).
#' @param ignore_string_bodies Logical, default `FALSE`. If `TRUE`, the contents
#' of string literals are ignored. The quotes themselves are included, so this
#' mainly affects wide multiline strings, e.g. SQL queries.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' mainly affects wide multiline strings, e.g. SQL queries.
#' only affects wide multiline strings, e.g. SQL queries.

?

Looking at the code, this seems clearer & more precise.

Copy link
Collaborator Author

@MichaelChirico MichaelChirico Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new example related to this -- what I'm trying to convey is mainly that the closing quote counts towards the line length where it's found.

1234567890
----------
string <- "
 too long!
"

The less common case is that the opening " here will also trigger a lint (with max width 10)

lint("string <- '\n  too long\n'", line_length_linter(10, ignore_string_bodies=TRUE))
# <text>:1:11: style: [line_length_linter] Lines should not be more than 10 characters. This line is 11 characters.
# string <- '
# ~~~~~~~~~~^

#'
#' @examples
#' # will produce lints
Expand All @@ -22,14 +25,20 @@
#' - [linters] for a complete list of linters available in lintr.
#' - <https://style.tidyverse.org/syntax.html#long-lines>
#' @export
line_length_linter <- function(length = 80L) {
line_length_linter <- function(length = 80L, ignore_string_bodies = FALSE) {
general_msg <- paste("Lines should not be more than", length, "characters.")

Linter(linter_level = "file", function(source_expression) {
# Only go over complete file
line_lengths <- nchar(source_expression$file_lines)
line_lengths <- as.integer(nchar(source_expression$file_lines))
long_lines <- which(line_lengths > length)

if (ignore_string_bodies) {
in_string_body_idx <-
is_in_string_body(source_expression$full_parsed_content, length, long_lines)
long_lines <- long_lines[!in_string_body_idx]
}

Map(
function(long_line, line_length) {
Lint(
Expand All @@ -47,3 +56,39 @@ line_length_linter <- function(length = 80L) {
)
})
}

is_in_string_body <- function(parse_data, max_length, long_idx) {
str_idx <- parse_data$token == "STR_CONST"
if (!any(str_idx)) {
return(rep(FALSE, length(long_idx)))
}
str_data <- parse_data[str_idx, ]
if (all(str_data$line1 == str_data$line2)) {
return(rep(FALSE, length(long_idx)))
}
# right delimiter just ends at 'col2', but 'col1' takes some sleuthing
str_data$line1_width <- nchar(vapply(
strsplit(str_data$text, "\n", fixed = TRUE),
function(x) x[1L],
FUN.VALUE = character(1L),
USE.NAMES = FALSE
))
str_data$col1_end <- str_data$col1 + str_data$line1_width
vapply(
long_idx,
function(line) {
# strictly inside a multi-line string body
if (any(str_data$line1 < line & str_data$line2 > line)) {
return(TRUE)
}
on_line1_idx <- str_data$line1 == line
if (any(on_line1_idx)) {
return(max(str_data$col1_end[on_line1_idx]) <= max_length)
}
# use parse data to capture possible trailing expressions on this line
on_line2_idx <- parse_data$line2 == line
any(on_line2_idx) && max(parse_data$col2[on_line2_idx]) <= max_length
},
logical(1L)
)
}
8 changes: 6 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,12 @@ get_r_string <- function(s, xpath = NULL) {
s <- xml_find_chr(s, sprintf("string(%s)", xpath))
}
}
# parse() skips "" elements --> offsets the length of the output,
# but NA in --> NA out
r_string_from_parse_text(s)
}

# parse() skips "" elements --> offsets the length of the output,
# but NA in --> NA out
r_string_from_parse_text <- function(s) {
is.na(s) <- !nzchar(s)
out <- as.character(parse(text = s, keep.source = FALSE))
is.na(out) <- is.na(s)
Expand Down
8 changes: 6 additions & 2 deletions man/line_length_linter.Rd

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

65 changes: 62 additions & 3 deletions tests/testthat/test-line_length_linter.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test_that("line_length_linter skips allowed usages", {
linter <- line_length_linter(80L)

expect_lint("blah", NULL, linter)
expect_lint(strrep("x", 80L), NULL, linter)
expect_no_lint("blah", linter)
expect_no_lint(strrep("x", 80L), linter)
})

test_that("line_length_linter blocks disallowed usages", {
Expand Down Expand Up @@ -37,7 +37,7 @@ test_that("line_length_linter blocks disallowed usages", {

linter <- line_length_linter(20L)
lint_msg <- rex::rex("Lines should not be more than 20 characters. This line is 22 characters.")
expect_lint(strrep("a", 20L), NULL, linter)
expect_no_lint(strrep("a", 20L), linter)
expect_lint(
strrep("a", 22L),
list(
Expand Down Expand Up @@ -71,3 +71,62 @@ test_that("Multiple lints give custom messages", {
line_length_linter(5L)
)
})

test_that("string bodies can be ignored", {
linter <- line_length_linter(10L, ignore_string_bodies = TRUE)
lint_msg <- rex::rex("Lines should not be more than 10 characters. This line is 15 characters.")

expect_no_lint(
trim_some("
1234567890
str <- '
123456789012345
'
"),
linter
)

expect_no_lint(
trim_some("
1234567890
str <- '90
123456789012345
123456789'
"),
linter
)

expect_lint(
trim_some("
1234567890
str <- '9012345
1234567890
123456789'
"),
lint_msg,
linter
)

expect_lint(
trim_some("
1234567890
str <- '90
1234567890
12345678'; 2345
"),
lint_msg,
linter
)

expect_lint(
"'1'; '2'; '345'",
lint_msg,
linter
)

expect_lint(
"123456789012345",
lint_msg,
linter
)
})
Loading