-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
object_name_linter: add
regexes=
argument for custom style regexes (#…
…1421) * refac: styles as named list of regexes * add regexes= argument and strip fewer symbols * do not overwrite formal argument styles * grammar * change behaviour of missing(styles) && !missing(regexes), add tests for auto-named regexes. fix test failure pertaining to is_linter not being a linter factory. * Update test-object_name_linter.R * split object_name_linters into object_length_linter.R and object_name_linter.R for consistency * document(), add examples with regexes * un-escape quote for readability * again * fix documentation * re-use nzchar output Co-authored-by: Indrajeet Patil <patilindrajeet.science@gmail.com> Co-authored-by: Michael Chirico <chiricom@google.com>
- Loading branch information
1 parent
6d8f446
commit 7a953f5
Showing
6 changed files
with
244 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#' Object length linter | ||
#' | ||
#' Check that object names are not too long. | ||
#' The length of an object name is defined as the length in characters, after removing extraneous parts: | ||
#' | ||
#' * generic prefixes for implementations of S3 generics, e.g. `as.data.frame.my_class` has length 8. | ||
#' * leading `.`, e.g. `.my_hidden_function` has length 18. | ||
#' * "%%" for infix operators, e.g. `%my_op%` has length 5. | ||
#' * trailing `<-` for assignment functions, e.g. `my_attr<-` has length 7. | ||
#' | ||
#' Note that this behavior relies in part on having packages in your Imports available; | ||
#' see the detailed note in [object_name_linter()] for more details. | ||
#' | ||
#' @param length maximum variable name length allowed. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "very_very_long_variable_name <- 1L", | ||
#' linters = object_length_linter(length = 10L) | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = "very_very_long_variable_name <- 1L", | ||
#' linters = object_length_linter(length = 30L) | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "var <- 1L", | ||
#' linters = object_length_linter(length = 10L) | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("object_length_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
object_length_linter <- function(length = 30L) { | ||
lint_message <- paste("Variable and function names should not be longer than", length, "characters.") | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "file")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$full_xml_parsed_content | ||
|
||
assignments <- xml2::xml_find_all(xml, object_name_xpath) | ||
|
||
# Retrieve assigned name | ||
nms <- strip_names( | ||
xml2::xml_text(assignments) | ||
) | ||
|
||
# run namespace_imports at run-time, not "compile" time to allow package structure to change | ||
ns_imports <- namespace_imports(find_package(source_expression$filename)) | ||
generics <- strip_names(c( | ||
declared_s3_generics(xml), | ||
imported_s3_generics(ns_imports)$fun, | ||
.base_s3_generics | ||
)) | ||
generics <- unique(generics[nzchar(generics)]) | ||
|
||
# Remove generic function names from generic implementations | ||
# This only lints S3 implementations if the class names are too long, still lints generics if they are too long. | ||
nms_stripped <- re_substitutes(nms, rex(start, or(generics), "."), "") | ||
|
||
too_long <- nchar(nms_stripped) > length | ||
|
||
xml_nodes_to_lints( | ||
assignments[too_long], | ||
source_expression = source_expression, | ||
lint_message = lint_message, | ||
type = "style" | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.