From e48ace484d5af9a1f7a56fcc7a95c6fc80245fad Mon Sep 17 00:00:00 2001 From: AshesITR Date: Sun, 29 Nov 2020 19:11:08 +0100 Subject: [PATCH] Allow .onLoad and other special functions regardless of name style (#614) * Allow .onLoad and other special functions regardless of name style Also remove relevant # nolint in lintrs own .onLoad. fixes #500 * add a note about source for maintenance * Clarify NEWS Co-authored-by: Michael Chirico --- NEWS.md | 1 + R/object_name_linters.R | 18 +++++++++++++++++- R/zzz.R | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index f82d65310..ea5b36655 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,7 @@ * New `sprintf_linter()` (#544, #578, #624, #625, @renkun-ken, @AshesITR) * Exclusions specified in the `.lintr` file are now relative to the location of that file and support excluding entire directories (#158, #438, @AshesITR) +* `object_name_linter()` now excludes special R hook functions such as `.onLoad` (#500, #614, @AshesITR) # lintr 2.0.1 diff --git a/R/object_name_linters.R b/R/object_name_linters.R index 424605436..fabe3369e 100644 --- a/R/object_name_linters.R +++ b/R/object_name_linters.R @@ -129,7 +129,8 @@ make_object_linter <- function(fun) { keep_indices <- which( !is_operator(names) & !is_known_generic(names) & - !is_base_function(names) + !is_base_function(names) & + !is_special_function(names) ) lapply( @@ -239,6 +240,21 @@ is_base_function <- function(x) { x %in% base_funs } +# see ?".onLoad" and ?"Startup" +special_funs <- c( + ".onLoad", + ".onAttach", + ".onUnload", + ".onDetach", + ".Last.lib", + ".First", + ".Last" +) + +is_special_function <- function(x) { + x %in% special_funs +} + object_lint <- function(source_file, token, message, type) { Lint( filename = source_file$filename, diff --git a/R/zzz.R b/R/zzz.R index 39d962869..e67844d31 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -182,7 +182,7 @@ default_settings <- NULL settings <- NULL -.onLoad <- function(libname, pkgname) { # nolint +.onLoad <- function(libname, pkgname) { op <- options() op.lintr <- list( lintr.linter_file = ".lintr"