diff --git a/.lintr b/.lintr index 7d89de3fd..89ba36d38 100644 --- a/.lintr +++ b/.lintr @@ -38,6 +38,7 @@ linters: all_linters( `<<-` = NULL )), unnecessary_concatenation_linter(allow_single_expression = FALSE), + cyclocomp_linter = if (requireNamespace("cyclocomp", quietly = TRUE)) cyclocomp_linter(), absolute_path_linter = NULL, library_call_linter = NULL, nonportable_path_linter = NULL, diff --git a/NEWS.md b/NEWS.md index 86e834f92..93bef360a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,7 +16,7 @@ ## Bug fixes * `Lint()`, and thus all linters, ensures that the returned object's `message` attribute is consistently a simple character string (and not, for example, an object of class `"glue"`; #2740, @MichaelChirico). -* Files with encoding inferred from settings read more robustly under `lint(parse_settings = TRUE)` (#2803, @MichaelChirico). +* Files with encoding inferred from settings read more robustly under `lint(parse_settings = TRUE)` (#2803, @MichaelChirico). Thanks also to @bastistician for detecting a regression caused by the initial change for users of Emacs (#2847). * `assignment_linter()` no longer errors if `"%<>%"` is an allowed operator (#2850, @AshesITR). ## Changes to default linters diff --git a/R/lint.R b/R/lint.R index 362336407..37c44621e 100644 --- a/R/lint.R +++ b/R/lint.R @@ -45,9 +45,8 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings = needs_tempfile <- missing(filename) || re_matches(filename, rex(newline)) inline_data <- !is.null(text) || needs_tempfile - parse_settings <- !inline_data && isTRUE(parse_settings) - if (parse_settings) { + if (isTRUE(parse_settings)) { read_settings(filename) on.exit(reset_settings(), add = TRUE) } diff --git a/R/settings.R b/R/settings.R index 48ffcf602..c106ffbd4 100644 --- a/R/settings.R +++ b/R/settings.R @@ -65,6 +65,8 @@ read_settings <- function(filename, call = parent.frame()) { reset_settings() + # doing lint(text=) should read settings from the current directory, required e.g. for Emacs, #2847 + if (missing(filename)) filename <- "./any_local_file" config_file <- find_config(filename) default_encoding <- find_default_encoding(filename) if (!is.null(default_encoding)) { diff --git a/tests/testthat/test-backport_linter.R b/tests/testthat/test-backport_linter.R index e445c4404..5404d39bb 100644 --- a/tests/testthat/test-backport_linter.R +++ b/tests/testthat/test-backport_linter.R @@ -1,6 +1,6 @@ test_that("backport_linter produces error when R version misspecified", { expect_error( - lint(text = "numToBits(2)", linters = backport_linter(420L)), + lint(text = "numToBits(2)", linters = backport_linter(420L), parse_settings = FALSE), "`r_version` must be an R version number" ) }) diff --git a/tests/testthat/test-cyclocomp_linter.R b/tests/testthat/test-cyclocomp_linter.R index 391973c2f..37f17bb0d 100644 --- a/tests/testthat/test-cyclocomp_linter.R +++ b/tests/testthat/test-cyclocomp_linter.R @@ -69,5 +69,9 @@ test_that("a null linter is returned, with warning, if cyclocomp is unavailable" linter <- cyclocomp_linter(1L) }) - expect_error(lint(text = "if (TRUE) 1 else 2", linters = linter), "disabled", fixed = TRUE) + expect_error( + lint(text = "if (TRUE) 1 else 2", linters = linter, parse_settings = FALSE), + "disabled", + fixed = TRUE + ) }) diff --git a/tests/testthat/test-lint.R b/tests/testthat/test-lint.R index 983c5e0c4..6b91af8f2 100644 --- a/tests/testthat/test-lint.R +++ b/tests/testthat/test-lint.R @@ -121,13 +121,13 @@ test_that("lint() results from file or text should be consistent", { file <- normalize_path(file) lint_from_file <- lint(file, linters = linters) - lint_from_lines <- lint(linters = linters, text = lines) - lint_from_text <- lint(linters = linters, text = text) + lint_from_lines <- lint(linters = linters, text = lines, parse_settings = FALSE) + lint_from_text <- lint(linters = linters, text = text, parse_settings = FALSE) # Remove file before linting to ensure that lint works and do not # assume that file exists when both filename and text are supplied. expect_identical(unlink(file), 0L) - lint_from_text2 <- lint(file, linters = linters, text = text) + lint_from_text2 <- lint(file, linters = linters, text = text, parse_settings = FALSE) expect_length(lint_from_file, 2L) expect_length(lint_from_lines, 2L) @@ -205,12 +205,12 @@ test_that("old compatibility usage errors", { ) expect_error( - lint("a <- 1\n", linters = function(two, arguments) NULL), + lint("a <- 1\n", linters = function(two, arguments) NULL, parse_settings = FALSE), error_msg ) expect_error( - lint("a <- 1\n", linters = "equals_na_linter"), + lint("a <- 1\n", linters = "equals_na_linter", parse_settings = FALSE), error_msg ) }) @@ -234,7 +234,6 @@ test_that("typo in argument name gives helpful error", { expect_error(lint("xxx", litners = identity), "Found unknown arguments in `...`: `litners`") }) - test_that("gitlab_output() writes expected report", { skip_if_not_installed("jsonlite") @@ -279,3 +278,11 @@ test_that("gitlab_output() writes expected report", { )) ) }) + +test_that("settings are picked up under lint(text=)", { + .lintr <- withr::local_tempfile(lines = "linters: list(assignment_linter())") + withr::local_options(lintr.linter_file = .lintr) + + # lint '=', but not the operator spacing + expect_length(lint(text = "a=1"), 1L) +}) diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index f8aef14e1..e7db32570 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -49,14 +49,14 @@ test_that("as.data.frame.lints", { }) test_that("summary.lints() works (no lints)", { - no_lints <- lint("x <- 1\n", linters = assignment_linter()) + no_lints <- lint("x <- 1\n", linters = assignment_linter(), parse_settings = FALSE) no_lint_summary <- summary(no_lints) expect_s3_class(no_lint_summary, "data.frame") expect_identical(nrow(no_lint_summary), 0L) }) test_that("summary.lints() works (lints found)", { - has_lints <- lint("x = 1\n", linters = assignment_linter()) + has_lints <- lint("x = 1\n", linters = assignment_linter(), parse_settings = FALSE) has_lint_summary <- summary(has_lints) expect_s3_class(has_lint_summary, "data.frame") diff --git a/tests/testthat/test-spaces_left_parentheses_linter.R b/tests/testthat/test-spaces_left_parentheses_linter.R index ce854828c..976bf0f00 100644 --- a/tests/testthat/test-spaces_left_parentheses_linter.R +++ b/tests/testthat/test-spaces_left_parentheses_linter.R @@ -93,7 +93,11 @@ test_that("doesn't produce a warning", { } ") - expect_no_warning(lint(text = complex_lines, linters = spaces_left_parentheses_linter())) + expect_no_warning(lint( + text = complex_lines, + linters = spaces_left_parentheses_linter(), + parse_settings = FALSE + )) }) test_that("lints vectorize", {