Skip to contents

Check that the line length of both comments and code is less than length.

Usage

line_length_linter(
  length = 80L,
  ignore_string_bodies = FALSE,
  allow_alignment_calls = c("tribble", "rowwiseDT"),
  allow_long_test_names = TRUE
)

Arguments

length

Maximum line length allowed. Default is 80L (Hollerith limit).

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.

allow_alignment_calls

Character vector of function names whose calls are allowed to exceed length for tabular alignment (e.g. tribble(), rowwiseDT()).

allow_long_test_names

Logical, default TRUE. If TRUE, string literal test descriptions in testthat::test_that() calls are allowed to exceed length.

See also

Examples

# will produce lints
lint(
  text = strrep("x", 23L),
  linters = line_length_linter(length = 20L)
)
#> <text>:1:21: style: [line_length_linter] Lines should not be more than 20 characters. This line is 23 characters.
#> xxxxxxxxxxxxxxxxxxxxxxx
#> ~~~~~~~~~~~~~~~~~~~~^~~

# the trailing ' is counted towards line length, so this still lints
lint(
  text = "'a long single-line string'",
  linters = line_length_linter(length = 15L, ignore_string_bodies = TRUE)
)
#> <text>:1:16: style: [line_length_linter] Lines should not be more than 15 characters. This line is 27 characters.
#> 'a long single-line string'
#> ~~~~~~~~~~~~~~~^~~~~~~~~~~~

lines <- paste(
  "query <- '",
  "  SELECT *",
  "  FROM MyTable",
  "  WHERE profit > 0",
  "'",
  sep = "\n"
)
writeLines(lines)
#> query <- '
#>   SELECT *
#>   FROM MyTable
#>   WHERE profit > 0
#> '
lint(
  text = lines,
  linters = line_length_linter(length = 10L)
)
#> <text>:3:11: style: [line_length_linter] Lines should not be more than 10 characters. This line is 14 characters.
#>   FROM MyTable
#> ~~~~~~~~~~^~~~
#> <text>:4:11: style: [line_length_linter] Lines should not be more than 10 characters. This line is 18 characters.
#>   WHERE profit > 0
#> ~~~~~~~~~~^~~~~~~~

code_lines <- "tibble::tribble(\n  ~col_one, ~col_two,\n  'long_val_1', 'long_val_2'\n)"
writeLines(code_lines)
#> tibble::tribble(
#>   ~col_one, ~col_two,
#>   'long_val_1', 'long_val_2'
#> )
lint(
  text = code_lines,
  linters = line_length_linter(length = 20L, allow_alignment_calls = character())
)
#> <text>:2:21: style: [line_length_linter] Lines should not be more than 20 characters. This line is 21 characters.
#>   ~col_one, ~col_two,
#> ~~~~~~~~~~~~~~~~~~~~^
#> <text>:3:21: style: [line_length_linter] Lines should not be more than 20 characters. This line is 28 characters.
#>   'long_val_1', 'long_val_2'
#> ~~~~~~~~~~~~~~~~~~~~^~~~~~~~

code_lines <- "test_that('a very long test description', {\n  expect_true(TRUE)\n})"
writeLines(code_lines)
#> test_that('a very long test description', {
#>   expect_true(TRUE)
#> })
lint(
  text = code_lines,
  linters = line_length_linter(length = 20L, allow_long_test_names = FALSE)
)
#> <text>:1:21: style: [line_length_linter] Lines should not be more than 20 characters. This line is 43 characters.
#> test_that('a very long test description', {
#> ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

# okay
lint(
  text = strrep("x", 21L),
  linters = line_length_linter(length = 40L)
)
#> ℹ No lints found.

lines <- paste(
  "paste(",
  "  'a long',",
  "  'single-line',",
  "  'string'",
  ")",
  sep = "\n"
)
writeLines(lines)
#> paste(
#>   'a long',
#>   'single-line',
#>   'string'
#> )
lint(
  text = lines,
  linters = line_length_linter(length = 15L, ignore_string_bodies = TRUE)
)
#> <text>:3:16: style: [line_length_linter] Lines should not be more than 15 characters. This line is 16 characters.
#>   'single-line',
#> ~~~~~~~~~~~~~~~^

lines <- paste(
  "query <- '",
  "  SELECT *",
  "  FROM MyTable",
  "  WHERE profit > 0",
  "'",
  sep = "\n"
)
writeLines(lines)
#> query <- '
#>   SELECT *
#>   FROM MyTable
#>   WHERE profit > 0
#> '
lint(
  text = lines,
  linters = line_length_linter(length = 10L, ignore_string_bodies = TRUE)
)
#> ℹ No lints found.

code_lines <- "tibble::tribble(\n  ~col_one, ~col_two,\n  'long_val_1', 'long_val_2'\n)"
writeLines(code_lines)
#> tibble::tribble(
#>   ~col_one, ~col_two,
#>   'long_val_1', 'long_val_2'
#> )
lint(
  text = code_lines,
  linters = line_length_linter(length = 20L)
)
#> ℹ No lints found.

code_lines <- "test_that('a very long test description', {\n  expect_true(TRUE)\n})"
writeLines(code_lines)
#> test_that('a very long test description', {
#>   expect_true(TRUE)
#> })
lint(
  text = code_lines,
  linters = line_length_linter(length = 20L)
)
#> ℹ No lints found.