Skip to content

Benchmark study: Implicit imports #685

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Imports:
rematch2 (>= 2.0.1),
rlang (>= 0.1.1),
rprojroot (>= 1.1),
rstudioapi (>= 0.7),
tibble (>= 1.4.2),
tools,
withr (>= 1.0.0),
Expand All @@ -37,7 +38,6 @@ Suggests:
knitr,
prettycode,
rmarkdown,
rstudioapi (>= 0.7),
testthat (>= 2.1.0)
VignetteBuilder:
knitr
Expand Down
10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export(tidyverse_math_token_spacing)
export(tidyverse_reindention)
export(tidyverse_style)
import(tibble)
importFrom(R.cache,clearCache)
importFrom(R.cache,generateCache)
importFrom(R.cache,getCachePath)
importFrom(magrittr,"%>%")
importFrom(magrittr,set_names)
importFrom(purrr,as_mapper)
Expand All @@ -36,12 +39,19 @@ importFrom(purrr,partial)
importFrom(purrr,pmap)
importFrom(purrr,pwalk)
importFrom(purrr,when)
importFrom(rematch2,re_match)
importFrom(rlang,abort)
importFrom(rlang,arg_match)
importFrom(rlang,is_empty)
importFrom(rlang,is_installed)
importFrom(rlang,is_named)
importFrom(rlang,seq2)
importFrom(rlang,warn)
importFrom(rlang,with_handlers)
importFrom(rstudioapi,getActiveDocumentContext)
importFrom(tools,parse_Rd)
importFrom(utils,capture.output)
importFrom(utils,getParseData)
importFrom(utils,head)
importFrom(utils,tail)
importFrom(utils,write.table)
3 changes: 2 additions & 1 deletion R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ style_selection <- function() {
}
}

#' @importFrom rstudioapi getActiveDocumentContext
get_rstudio_context <- function() {
rstudioapi::getActiveDocumentContext()
getActiveDocumentContext()
}

#' Asks the user to supply a style
Expand Down
3 changes: 2 additions & 1 deletion R/compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ filter <- function(.data, ...) {
subset(.data, ...)
}

#' @importFrom rlang is_named
left_join <- function(x, y, by) {
if (rlang::is_named(by)) {
if (is_named(by)) {
by_x <- names(by)
by_y <- unname(by)
} else {
Expand Down
18 changes: 11 additions & 7 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ transform_utf8 <- function(path, fun, dry) {
#' latter returns an error if the input code is not identical to the result
#' of styling. "off", the default, writes back if the input and output of
#' styling are not identical.
#' @importFrom rlang with_handlers warn
#' @importFrom rlang with_handlers warn arg_match abort
#' @keywords internal
transform_utf8_one <- function(path, fun, dry) {
rlang::arg_match(dry, c("on", "off", "fail"))
arg_match(dry, c("on", "off", "fail"))
with_handlers(
{
file_with_info <- read_utf8(path)
Expand All @@ -32,7 +32,7 @@ transform_utf8_one <- function(path, fun, dry) {
identical <- identical_content && !file_with_info$missing_EOF_line_break
if (!identical) {
if (dry == "fail") {
rlang::abort(
abort(
paste0(
"File `", path, "` would be modified by styler and argument dry",
" is set to 'fail'."
Expand All @@ -51,7 +51,7 @@ transform_utf8_one <- function(path, fun, dry) {
},
error = function(e) {
if (inherits(e, "dryError")) {
rlang::abort(conditionMessage(e))
abort(conditionMessage(e))
} else {
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
}
Expand All @@ -69,8 +69,9 @@ transform_utf8_one <- function(path, fun, dry) {
#' with this implementation.
#' @param path A path to a file to read.
#' @keywords internal
#' @importFrom rlang with_handlers abort
read_utf8 <- function(path) {
out <- rlang::with_handlers(
out <- with_handlers(
read_utf8_bare(path),
warning = function(w) w,
error = function(e) e
Expand All @@ -81,7 +82,7 @@ read_utf8 <- function(path) {
missing_EOF_line_break = FALSE
)
} else if (inherits(out, "error")) {
rlang::abort(out$message)
abort(out$message)
} else if (inherits(out, "warning")) {
list(
text = read_utf8_bare(path, warn = FALSE),
Expand All @@ -93,7 +94,10 @@ read_utf8 <- function(path) {
#' Drop-in replacement for `xfun::read_utf8()`, with an optional `warn`
#' argument.
#' @keywords internal
#' @importFrom utils head
read_utf8_bare <- function(con, warn = TRUE) {
opts <- options(encoding = "native.enc")
on.exit(options(opts), add = TRUE)
x <- readLines(con, encoding = "UTF-8", warn = warn)
i <- invalid_utf8(x)
n <- length(i)
Expand All @@ -103,7 +107,7 @@ read_utf8_bare <- function(con, warn = TRUE) {
"The file ", con, " is not encoded in UTF-8. ",
"These lines contain invalid UTF-8 characters: "
),
paste(c(utils::head(i), if (n > 6) "..."), collapse = ", ")
paste(c(head(i), if (n > 6) "..."), collapse = ", ")
)
}
x
Expand Down
3 changes: 2 additions & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ tokenize <- function(text) {
#' @param include_text Passed to [utils::getParseData()] as `includeText`.
#' @param ... Other arguments passed to [utils::getParseData()].
#' @keywords internal
#' @importFrom utils getParseData
get_parse_data <- function(text, include_text = TRUE, ...) {
# avoid https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16041
parse_safely(text, keep.source = TRUE)
parsed <- parse_safely(text, keep.source = TRUE)
pd <- as_tibble(
utils::getParseData(parsed, includeText = include_text),
getParseData(parsed, includeText = include_text),
.name_repair = "minimal"
) %>%
add_id_and_short()
Expand Down
2 changes: 1 addition & 1 deletion R/roxygen-examples-find.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
}

identify_start_to_stop_of_roxygen_examples <- function(path) {
content <- xfun::read_utf8(path)
content <- read_utf8_bare(path, warn = FALSE)
identify_start_to_stop_of_roxygen_examples_from_text(content)
}

Expand Down
3 changes: 2 additions & 1 deletion R/roxygen-examples-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
#' "#'1+ 1"
#' ))
#' @keywords internal
#' @importFrom tools parse_Rd
parse_roxygen <- function(roxygen) {
connection <- remove_roxygen_mask(roxygen) %>%
textConnection()
parsed <- connection %>%
tools::parse_Rd(fragment = TRUE) %>%
parse_Rd(fragment = TRUE) %>%
as.character(deparse = TRUE)
is_line_break <- parsed[1] == "\n"
close(connection)
Expand Down
3 changes: 2 additions & 1 deletion R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ set_space_between_levels <- function(pd_flat) {
#' @param force_one Whether or not to force one space or allow multiple spaces
#' after the regex "^#+'*".
#' @importFrom purrr map_chr
#' @importFrom rematch2 re_match
#' @keywords internal
start_comments_with_space <- function(pd, force_one = FALSE) {
is_comment <- is_comment(pd)
Expand All @@ -305,7 +306,7 @@ start_comments_with_space <- function(pd, force_one = FALSE) {
return(pd)
}

comments <- rematch2::re_match(
comments <- re_match(
pd$text[is_comment],
"^(?<prefix>#+['\\*]*)(?<space_after_prefix> *)(?<text>.*)$"
)
Expand Down
4 changes: 2 additions & 2 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ tidyverse_style <- function(scope = "tokens",
style_space_around_tilde,
strict = strict
),
spacing_around_op = purrr::partial(set_space_around_op,
spacing_around_op = partial(set_space_around_op,
strict = strict
),
remove_space_after_opening_paren,
Expand Down Expand Up @@ -140,7 +140,7 @@ tidyverse_style <- function(scope = "tokens",
except_token_before = "COMMENT"
)
},
purrr::partial(remove_line_break_in_fun_call, strict = strict),
partial(remove_line_break_in_fun_call, strict = strict),
add_line_break_after_pipe = if (strict) add_line_break_after_pipe,
set_linebreak_after_ggplot2_plus = if (strict) set_linebreak_after_ggplot2_plus
)
Expand Down
6 changes: 4 additions & 2 deletions R/ui-caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#' potentially different with different versions of styler.
#' @param ask Whether or not to interactively ask the user again.
#' @family cache managers
#' @importFrom R.cache clearCache
#' @export
cache_clear <- function(cache_name = NULL, ask = TRUE) {
path_cache <- cache_find_path(cache_name)
R.cache::clearCache(path_cache, prompt = ask)
clearCache(path_cache, prompt = ask)
cache_deactivate(verbose = FALSE)
}

Expand Down Expand Up @@ -77,9 +78,10 @@ NULL
#' "tabular" for a tabular summary from [base::file.info()] or "both" for
#' both.
#' @family cache managers
#' @importFrom rlang arg_match
#' @export
cache_info <- function(cache_name = NULL, format = "both") {
rlang::arg_match(format, c("tabular", "lucid", "both"))
arg_match(format, c("tabular", "lucid", "both"))
path_cache <- cache_find_path(cache_name)
files <- list.files(path_cache, full.names = TRUE)
file_info <- file.info(files) %>%
Expand Down
14 changes: 9 additions & 5 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ hash_standardize <- function(text) {
#' @param cache_dir The caching directory relative to the `.Rcache` root to
#' look for a cached value.
#' @keywords internal
#' @importFrom R.cache generateCache
is_cached <- function(text,
transformers,
more_specs,
cache_dir = cache_dir_default()) {
R.cache::generateCache(
generateCache(
key = cache_make_key(text, transformers, more_specs),
dirs = cache_dir
) %>%
Expand Down Expand Up @@ -109,9 +110,10 @@ cache_make_key <- function(text, transformers, more_specs) {
#' Finds the path to the cache and creates it if it does not exist.
#' @inheritParams cache_clear
#' @keywords internal
#' @importFrom R.cache getCachePath
cache_find_path <- function(cache_name = NULL) {
cache_name <- cache_get_or_derive_name(cache_name)
R.cache::getCachePath(c("styler", cache_name))
getCachePath(c("styler", cache_name))
}

#' Check if a cache is activated
Expand Down Expand Up @@ -143,12 +145,13 @@ cache_is_activated <- function(cache_name = NULL) {
#' is.
#' @param text A character vector with one or more expressions.
#' @inheritParams cache_write
#' @importFrom utils getParseData
#' @keywords internal
cache_by_expression <- function(text,
cache_by_expression <- function(text,
transformers,
more_specs) {
expressions <- parse(text = text, keep.source = TRUE) %>%
utils::getParseData(includeText = TRUE)
getParseData(includeText = TRUE)
if (env_current$any_stylerignore) {
expressions <- expressions %>%
add_stylerignore()
Expand All @@ -164,8 +167,9 @@ cache_by_expression <- function(text,
#'
#' @inheritParams cache_make_key
#' @keywords internal
#' @importFrom R.cache generateCache
cache_write <- function(text, transformers, more_specs) {
R.cache::generateCache(
generateCache(
key = cache_make_key(text, transformers, more_specs),
dirs = cache_dir_default()
) %>%
Expand Down
3 changes: 2 additions & 1 deletion R/utils-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ map_filetype_to_pattern <- function(filetype) {
#' @param ... Passed to [base::dir()].
#' @seealso set_and_assert_arg_paths
#' @keywords internal
#' @importFrom purrr map
#' @examples
#' setdiff("./file.R", "file.R") # you want to standardize first.
dir_without_. <- function(path, ...) {
purrr::map(path, dir_without_._one, ...) %>%
map(path, dir_without_._one, ...) %>%
unlist()
}

Expand Down
3 changes: 2 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ calls_sys <- function(sys_call, ...) {
#' @param error_if_not_found Whether or not an error should be returned if the
#' option was not set.
#' @keywords internal
#' @importFrom rlang abort
option_read <- function(x, default = NULL, error_if_not_found = TRUE) {
if (x %in% names(options()) | !error_if_not_found) {
getOption(x, default)
} else {
rlang::abort(paste("R option", x, "most be set."))
abort(paste("R option", x, "most be set."))
}
}

Expand Down