-
Notifications
You must be signed in to change notification settings - Fork 192
object_name_linter: add regexes=
argument for custom style regexes
#1421
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ffb0f7c
refac: styles as named list of regexes
AshesITR aa08719
add regexes= argument and strip fewer symbols
AshesITR d626c2a
do not overwrite formal argument styles
AshesITR 2cf8ca7
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil d978a58
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil 6822533
grammar
MichaelChirico fde0134
Merge branch 'main' into feature/822-object_name-custom-style
AshesITR 8bc3e3c
change behaviour of missing(styles) && !missing(regexes), add tests f…
AshesITR 83209d2
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil c272434
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil faf8404
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil ba8a2ed
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil e981301
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil 58d47d3
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil 379e459
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil 2f22a3c
Update test-object_name_linter.R
IndrajeetPatil c6a6a20
Merge branch 'main' into feature/822-object_name-custom-style
AshesITR 6665985
split object_name_linters into object_length_linter.R and object_name…
AshesITR a2d445e
document(), add examples with regexes
AshesITR 395b7e4
un-escape quote for readability
MichaelChirico bccf595
again
MichaelChirico 246b4f1
fix documentation
AshesITR 1abdfdb
re-use nzchar output
MichaelChirico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.