Skip to content
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
21 changes: 10 additions & 11 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test-lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-unicode-path.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Test Unicode path")

test_that("Works with unicode path", {
skip_on_cran()
if (.Platform$OS.type == "windows") {
Expand Down