Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow .onLoad and other special functions regardless of name style #614

Merged
merged 9 commits into from
Nov 29, 2020
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 17 additions & 1 deletion R/object_name_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
)

lapply(
Expand Down Expand Up @@ -239,6 +240,21 @@ is_base_function <- function(x) {
x %in% base_funs
}

# see ?".onLoad" and ?"Startup"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, very good idea to note where they come from :-)

special_funs <- c(
".onLoad",
".onAttach",
".onUnload",
".onDetach",
".Last.lib",
".First",
".Last"
)

is_special_function <- function(x) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I guess we need a test for this 😅 I'm seeing this on current master when trying to do #597:

R/zzz.R:185:1: style: Variable and function name style should be snake_case.
.onLoad <- function(libname, pkgname) {
^~~~~~~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that strip_names removes the leading .. So actually we need to search for onLoad etc.

x %in% special_funs
}

object_lint <- function(source_file, token, message, type) {
Lint(
filename = source_file$filename,
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down