diff --git a/DESCRIPTION b/DESCRIPTION index 73f28e0b..650b08ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,7 @@ Imports: desc (>= 1.2.0), fs (>= 1.3.1), jsonlite (>= 1.6), - lintr (>= 2.0.0), + lintr (>= 2.0.1.9000), parallel, R6 (>= 2.4.1), repr (>= 1.1.0), @@ -46,6 +46,8 @@ Suggests: testthat (>= 2.1.0), withr (>= 2.3.0), rmarkdown (>= 2.0) +Remotes: + jimhester/lintr ByteCompile: yes Encoding: UTF-8 NeedsCompilation: yes diff --git a/R/diagnostics.R b/R/diagnostics.R index 17c4f099..b45227c1 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -79,27 +79,26 @@ diagnose_file <- function(uri, content) { } path <- path_from_uri(uri) + + if (length(content) == 1) { + content <- c(content, "") + } + + linters <- NULL + linter_file <- find_config(path) if (is.null(linter_file)) { linters <- getOption("languageserver.default_linters", NULL) - } else { - linters <- NULL - op <- options(lintr.linter_file = linter_file) - on.exit(options(op)) } - if (length(content) == 1) { - content <- c(content, "") - } + lint_cache <- getOption("languageserver.lint_cache", TRUE) + lints <- lintr::lint(path, linters = linters, cache = lint_cache, text = content) - text <- paste0(content, collapse = "\n") - diagnostics <- lapply( - lintr::lint(text, linters = linters), diagnostic_from_lint, content = content) + diagnostics <- lapply(lints, diagnostic_from_lint, content = content) names(diagnostics) <- NULL diagnostics } - diagnostics_callback <- function(self, uri, version, diagnostics) { if (is.null(diagnostics) || !self$workspace$documents$has(uri)) return(NULL) logger$info("diagnostics_callback called:", list(uri = uri, version = version)) diff --git a/tests/testthat/test-lintr.R b/tests/testthat/test-lintr.R index d2262220..4f72d466 100644 --- a/tests/testthat/test-lintr.R +++ b/tests/testthat/test-lintr.R @@ -12,9 +12,65 @@ test_that("lintr works", { expect_equal(client$diagnostics$size(), 1) expect_equal(client$diagnostics$get(data$uri), data$diagnostics) + expect_equal(data$diagnostics[[1]]$source, "assignment_linter") expect_equal(data$diagnostics[[1]]$message, "Use <-, not =, for assignment.") }) +test_that("lintr config file works", { + skip_on_cran() + + dir <- tempdir() + lintr_file <- file.path(dir, ".lintr") + on.exit(unlink(lintr_file)) + + writeLines("linters: with_defaults()", lintr_file) + + client <- language_client(working_dir = dir, diagnostics = TRUE) + + temp_file <- withr::local_tempfile(tmpdir = dir, fileext = ".R") + writeLines("a=1", temp_file) + + client %>% did_open(temp_file) + data <- client %>% wait_for("textDocument/publishDiagnostics") + + expect_equal(client$diagnostics$size(), 1) + expect_equal(client$diagnostics$get(data$uri), data$diagnostics) + expect_length(data$diagnostics, 2) + expect_setequal(vapply(data$diagnostics, "[[", character(1), "source"), + c("assignment_linter", "infix_spaces_linter")) + + + writeLines("linters: with_defaults(assignment_linter=NULL)", lintr_file) + + client <- language_client(working_dir = dir, diagnostics = TRUE) + + temp_file <- withr::local_tempfile(tmpdir = dir, fileext = ".R") + writeLines("a=1", temp_file) + + client %>% did_open(temp_file) + data <- client %>% wait_for("textDocument/publishDiagnostics") + + expect_equal(client$diagnostics$size(), 1) + expect_equal(client$diagnostics$get(data$uri), data$diagnostics) + expect_length(data$diagnostics, 1) + expect_setequal(vapply(data$diagnostics, "[[", character(1), "source"), + c("infix_spaces_linter")) + + writeLines("linters: list()", lintr_file) + + client <- language_client(working_dir = dir, diagnostics = TRUE) + + temp_file <- withr::local_tempfile(tmpdir = dir, fileext = ".R") + writeLines("a=1", temp_file) + + client %>% did_open(temp_file) + data <- client %>% wait_for("textDocument/publishDiagnostics") + + expect_equal(client$diagnostics$size(), 1) + expect_equal(client$diagnostics$get(data$uri), data$diagnostics) + expect_length(data$diagnostics, 0) +}) + test_that("lintr is disabled", { skip_on_cran() client <- language_client(diagnostics = FALSE) diff --git a/tests/testthat/test-unicode-path.R b/tests/testthat/test-unicode-path.R index 9f90e8ad..9636abf9 100644 --- a/tests/testthat/test-unicode-path.R +++ b/tests/testthat/test-unicode-path.R @@ -1,5 +1,3 @@ -context("Test Unicode path") - test_that("Works with unicode path", { skip_on_cran() if (.Platform$OS.type == "windows") {