Skip to content

Commit

Permalink
reset_settings() instead of clear_settings(), to ensure something is …
Browse files Browse the repository at this point in the history
…always set (#2180)
  • Loading branch information
MichaelChirico authored Oct 2, 2023
1 parent 5b41a9d commit d93437f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
27 changes: 14 additions & 13 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
#' Note that if files contain unparseable encoding problems, only the encoding problem will be linted to avoid
#' unintelligible error messages from other linters.
#'
#' @param filename either the filename for a file to lint, or a character string of inline R code for linting.
#' The latter (inline data) applies whenever `filename` has a newline character (\\n).
#' @param linters a named list of linter functions to apply. See [linters] for a full list of default and available
#' linters.
#' @param filename Either the filename for a file to lint, or a character string of inline R code for linting.
#' The latter (inline data) applies whenever `filename` has a newline character (\\n).
#' @param linters A named list of linter functions to apply. See [linters] for a full list of default and available
#' linters.
#' @param ... Provide additional arguments to be passed to:
#' - [exclude()] (in case of `lint()`; e.g. `lints` or `exclusions`)
#' - [lint()] (in case of `lint_dir()` and `lint_package()`; e.g. `linters` or `cache`)
#' @param cache given a logical, toggle caching of lint results. If passed a character string, store the cache in this
#' directory.
#' @param parse_settings whether to try and parse the settings.
#' - [exclude()] (in case of `lint()`; e.g. `lints` or `exclusions`)
#' - [lint()] (in case of `lint_dir()` and `lint_package()`; e.g. `linters` or `cache`)
#' @param cache When logical, toggle caching of lint results. I1f passed a character string, store the cache in this
#' directory.
#' @param parse_settings Logical, default `TRUE`. Whether to try and parse the settings;
#' otherwise, the [default_settings()] are used.
#' @param text Optional argument for supplying a string or lines directly, e.g. if the file is already in memory or
#' linting is being done ad hoc.
#' linting is being done ad hoc.
#'
#' @aliases lint_file
# TODO(next release after 3.0.0): remove the alias
Expand Down Expand Up @@ -58,7 +59,7 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings =

if (isTRUE(parse_settings)) {
read_settings(filename)
on.exit(clear_settings, add = TRUE)
on.exit(reset_settings(), add = TRUE)
}

linters <- define_linters(linters)
Expand Down Expand Up @@ -145,7 +146,7 @@ lint_dir <- function(path = ".", ...,

if (isTRUE(parse_settings)) {
read_settings(path)
on.exit(clear_settings, add = TRUE)
on.exit(reset_settings(), add = TRUE)

exclusions <- c(exclusions, settings$exclusions)
}
Expand Down Expand Up @@ -255,7 +256,7 @@ lint_package <- function(path = ".", ...,

if (parse_settings) {
read_settings(pkg_path)
on.exit(clear_settings, add = TRUE)
on.exit(reset_settings(), add = TRUE)
}

exclusions <- normalize_exclusions(
Expand Down
6 changes: 2 additions & 4 deletions R/settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' This file is a dcf file, see [base::read.dcf()] for details.
#' @param filename source file to be linted
read_settings <- function(filename) {
clear_settings()
reset_settings()

config_file <- find_config(filename)
default_encoding <- find_default_encoding(filename)
Expand Down Expand Up @@ -66,9 +66,7 @@ get_setting <- function(setting, config, defaults) {
}
}

clear_settings <- function() {
rm(list = ls(settings), envir = settings)
}
reset_settings <- function() list2env(default_settings, envir = settings)

find_default_encoding <- function(filename) {
if (is.null(filename)) {
Expand Down
4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ default_undesirable_operators <- all_undesirable_operators[names(all_undesirable
#' @export
default_settings <- NULL

settings <- NULL
settings <- new.env(parent = emptyenv())

# nocov start
.onLoad <- function(libname, pkgname) {
Expand Down Expand Up @@ -330,7 +330,7 @@ settings <- NULL
error_on_lint = logical_env("LINTR_ERROR_ON_LINT") %||% FALSE
))

utils::assignInMyNamespace("settings", list2env(default_settings, parent = emptyenv()))
reset_settings()

if (requireNamespace("tibble", quietly = TRUE)) {
registerS3method("as_tibble", "lints", as_tibble.lints, asNamespace("tibble"))
Expand Down
9 changes: 5 additions & 4 deletions man/lint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d93437f

Please sign in to comment.