Skip to content

Commit

Permalink
Functions not objects (#557)
Browse files Browse the repository at this point in the history
* Option to control whether undesirable function names can appear as symbol

Closes #556

* Use inlined source

* Simplify

* Add expectation

* Remove examples

* Make test more concise

* operator spacing

Co-authored-by: AshesITR <alexander.rosenstock@web.de>
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
  • Loading branch information
3 people authored Nov 30, 2020
1 parent e8b7a00 commit b4aba4f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
22 changes: 16 additions & 6 deletions R/undesirable_function_linter.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#' @describeIn linters Report the use of undesirable functions, e.g. \code{return}, \code{options},
#' or \code{sapply} and suggest an alternative.
#' @param fun Named character vector, where the names are the names of the undesirable functions,
#' and the values are the text for the alternative function to use (or \code{NA}).
#' @describeIn linters Report the use of undesirable functions, e.g.
#' \code{return}, \code{options}, or \code{sapply} and suggest an alternative.
#' @param fun Named character vector, where the names are the names of the
#' undesirable functions, and the values are the text for the alternative
#' function to use (or \code{NA}).
#' @param symbol_is_undesirable Whether to consider the use of an undesirable
#' function name as a symbol undesirable or not.
#' @export
undesirable_function_linter <- function(fun=default_undesirable_functions) {
undesirable_function_linter <- function(fun = default_undesirable_functions,
symbol_is_undesirable = TRUE) {
stopifnot(is.logical(symbol_is_undesirable))
function(source_file) {
if (symbol_is_undesirable) {
vals <- c("SYMBOL_FUNCTION_CALL", "SYMBOL")
} else {
vals <- "SYMBOL_FUNCTION_CALL"
}
lapply(
ids_with_token(source_file, c("SYMBOL_FUNCTION_CALL", "SYMBOL"), fun=`%in%`),
ids_with_token(source_file, vals, fun = `%in%`),
function(id) {
token <- with_id(source_file, id)
fun_name <- token[["text"]]
Expand Down
17 changes: 12 additions & 5 deletions man/linters.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-undesirable_function_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ test_that("linter returns correct linting", {
),
linter)
})

test_that("it's possible to NOT lint symbols", {
linter <- undesirable_function_linter(
fun = c("dir" = NA, "log10" = "use log()"),
symbol_is_undesirable = FALSE
)
expect_lint("dir <- 'path/to/a/directory'", NULL, linter)
expect_lint("lapply(x, log10)", NULL, linter)
})

0 comments on commit b4aba4f

Please sign in to comment.