diff --git a/NEWS.md b/NEWS.md index 3ca394548..f683d3a13 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # lintr 1.0.3.9000 # +* lintr now looks up its configuration in any parent directories as well as the package directory (#238, #345) * `object_usage_linter()` has been changed to better detect lexical scoping of global variables (#27, #336, #91, #382) * `object_usage_linter()` now respects `utils::globalVariables()`, so it can be used to avoid false positive warnings due to non-standard evaluation (#352) * `seq_linter` is now one of the default linters (#316). diff --git a/R/lint.R b/R/lint.R index be6f12eb4..87e83d49f 100644 --- a/R/lint.R +++ b/R/lint.R @@ -256,6 +256,27 @@ find_package <- function(path = getwd()) { normalizePath(prev_path) } +is_root <- function(path) { + identical(path, dirname(path)) +} + +has_config <- function(path, config) { + file.exists(file.path(path, config)) +} + +find_config2 <- function(path) { + config <- basename(getOption("lintr.linter_file")) + path <- normalizePath(path, mustWork = FALSE) + + while (!has_config(path, config)) { + path <- dirname(path) + if (is_root(path)) { + return(character()) + } + } + return(file.path(path, config)) +} + pkg_name <- function(path = find_package()) { if (is.null(path)) { return(NULL) diff --git a/R/settings.R b/R/settings.R index ca3182a21..01215bc1e 100644 --- a/R/settings.R +++ b/R/settings.R @@ -78,9 +78,8 @@ find_config <- function(filename) { return(linter_config) } - ## next check for a file in the project directory - project <- find_package(path) - linter_config <- file.path(project, linter_file) + ## next check for a file higher directories + linter_config <- find_config2(path) if (isTRUE(file.exists(linter_config))) { return(linter_config) }