diff --git a/.lintr b/.lintr index 8244162db..75698f02c 100644 --- a/.lintr +++ b/.lintr @@ -1,27 +1,30 @@ -linters: linters_with_defaults( - commented_code_linter = NULL, - cyclocomp_linter = cyclocomp_linter(40), - fixed_regex_linter = NULL, - function_argument_linter = NULL, - indentation_linter = NULL, - line_length_linter(120), - namespace_linter = NULL, - nested_ifelse_linter = NULL, - object_name_linter = NULL, - object_length_linter(70), - object_usage_linter = NULL, - todo_comment_linter = NULL, - extraction_operator_linter = NULL, - nonportable_path_linter = NULL, - string_boundary_linter = NULL, - undesirable_function_linter = NULL, - undesirable_operator_linter = NULL, - defaults = linters_with_tags(tags = NULL) - ) +linters: linters_with_tags( + tags = NULL, + commented_code_linter = NULL, + cyclocomp_linter = cyclocomp_linter(40), + fixed_regex_linter = NULL, + function_argument_linter = NULL, + indentation_linter = NULL, + line_length_linter(120L), + namespace_linter = NULL, + nested_ifelse_linter = NULL, + # TODO: remove this once the lint message is fixed + nzchar_linter = NULL, + object_name_linter = NULL, + object_length_linter(70L), + object_overwrite_linter = NULL, + object_usage_linter = NULL, + todo_comment_linter = NULL, + extraction_operator_linter = NULL, + nonportable_path_linter = NULL, + string_boundary_linter = NULL, + undesirable_function_linter = NULL, + undesirable_operator_linter = NULL + ) exclusions: list( - "inst", - "man", - "tests", - "touchstone", - "vignettes" - ) + "inst", + "man", + "tests", + "touchstone", + "vignettes" + ) diff --git a/R/detect-alignment-utils.R b/R/detect-alignment-utils.R index ead1d041e..50e1b07f7 100644 --- a/R/detect-alignment-utils.R +++ b/R/detect-alignment-utils.R @@ -32,9 +32,8 @@ alignment_drop_comments <- function(pd_by_line) { out <- vec_slice(x, x$token != "COMMENT") if (nrow(out) < 1L) { return(NULL) - } else { - out } + out }) %>% compact() } @@ -82,22 +81,22 @@ alignment_ensure_trailing_comma <- function(pd_by_line) { last_pd$spaces[nrow(last_pd)] <- 0L if (last(last_pd$token) == "','") { return(pd_by_line) - } else { - tokens <- create_tokens( - tokens = "','", - texts = ",", - lag_newlines = 0L, - spaces = 0L, - pos_ids = NA, - stylerignore = last_pd$stylerignore[1L], - indents = last_pd$indent[1L] - ) - tokens$.lag_spaces <- 0L - - tokens$lag_newlines <- tokens$pos_id <- NULL - pd_by_line[[length(pd_by_line)]] <- rbind(last_pd, tokens) - pd_by_line } + + tokens <- create_tokens( + tokens = "','", + texts = ",", + lag_newlines = 0L, + spaces = 0L, + pos_ids = NA, + stylerignore = last_pd$stylerignore[1L], + indents = last_pd$indent[1L] + ) + tokens$.lag_spaces <- 0L + + tokens$lag_newlines <- tokens$pos_id <- NULL + pd_by_line[[length(pd_by_line)]] <- rbind(last_pd, tokens) + pd_by_line } #' Checks if all arguments of column 1 are named @@ -161,9 +160,8 @@ alignment_serialize <- function(pd_sub) { }, pd_sub$terminal, pd_sub$text, pd_sub$child, pd_sub$spaces, pd_sub$newlines) if (anyNA(out)) { return(NA) - } else { - paste0(out, collapse = "") } + paste0(out, collapse = "") } #' Check if spacing around comma is correct diff --git a/R/detect-alignment.R b/R/detect-alignment.R index 970ab6ffd..bf8ffbe7c 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -101,11 +101,11 @@ token_is_on_aligned_line <- function(pd_flat) { if (length(pd_by_line) < 1L) { return(TRUE) } - pd_by_line <- alignment_drop_last_expr(pd_by_line) %>% + pd_by_line <- pd_by_line %>% + alignment_drop_last_expr() %>% alignment_ensure_no_closing_brace(last_line_is_closing_brace_only) - pd_by_line <- pd_by_line %>% - alignment_ensure_trailing_comma() + pd_by_line <- alignment_ensure_trailing_comma(pd_by_line) # now, pd only contains arguments separated by values, ideal for iterating # over columns. n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','")) diff --git a/R/io.R b/R/io.R index 565afb400..26ad6d9fe 100644 --- a/R/io.R +++ b/R/io.R @@ -31,30 +31,30 @@ transform_utf8_one <- function(path, fun, dry) { identical_content <- identical(file_with_info$text, new) identical <- identical_content && !file_with_info$missing_EOF_line_break if (!identical) { - if (dry == "fail") { - rlang::abort( + switch(dry, + fail = rlang::abort( paste0( "File `", path, "` would be modified by styler and argument dry", " is set to 'fail'." ), class = "dryError" - ) - } else if (dry == "on") { - # don't do anything - } else if (dry == "off") { - write_utf8(new, path) - } else { - # not implemented - } + ), + on = { + # don't do anything + }, + off = write_utf8(new, path), + { + # not implemented + } + ) } !identical }, error = function(e) { if (inherits(e, "dryError")) { rlang::abort(conditionMessage(e)) - } else { - warn(paste0("When processing ", path, ": ", conditionMessage(e))) } + warn(paste0("When processing ", path, ": ", conditionMessage(e))) NA } ) @@ -103,7 +103,8 @@ read_utf8_bare <- function(con, warn = TRUE) { "The file ", con, " is not encoded in UTF-8. ", "These lines contain invalid UTF-8 characters: " ), - toString(c(utils::head(i), if (n > 6L) "...")) + toString(c(utils::head(i), if (n > 6L) "...")), + call. = FALSE ) } x diff --git a/R/nested-to-tree.R b/R/nested-to-tree.R index c51445140..90787fecd 100644 --- a/R/nested-to-tree.R +++ b/R/nested-to-tree.R @@ -66,13 +66,8 @@ create_node_from_nested <- function(pd_nested, parent, structure_only) { if (is.null(pd_nested)) { return() } - node_info <- create_node_info(pd_nested, structure_only) - - child_nodes <- - node_info %>% - map(parent$AddChild) - + child_nodes <- map(node_info, parent$AddChild) map2(pd_nested$child, child_nodes, create_node_from_nested, structure_only) } diff --git a/R/parse.R b/R/parse.R index 4ddbb72db..629203934 100644 --- a/R/parse.R +++ b/R/parse.R @@ -102,8 +102,7 @@ get_parse_data <- function(text, include_text = TRUE, ...) { )) } } - pd <- pd %>% - add_id_and_short() + pd <- add_id_and_short(pd) pd } diff --git a/R/roxygen-examples.R b/R/roxygen-examples.R index 242d422e1..588006df8 100644 --- a/R/roxygen-examples.R +++ b/R/roxygen-examples.R @@ -46,8 +46,7 @@ style_roxygen_code_example_one <- function(example_one, } ) } - unmasked %>% - add_roxygen_mask(example_one, bare$example_type) + add_roxygen_mask(unmasked, example_one, bare$example_type) } #' Style a roxygen code example segment @@ -118,8 +117,9 @@ style_roxygen_example_snippet <- function(code_snippet, ) ) if (!is_cached || !cache_is_active) { - code_snippet <- code_snippet %>% + code_snippet <- parse_transform_serialize_r( + code_snippet, transformers, base_indention = base_indention, warn_empty = FALSE, diff --git a/R/rules-spaces.R b/R/rules-spaces.R index f9dbb293f..580a0cdc7 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -58,14 +58,16 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) { # We remove spaces for zero (e.g., around ^ in the tidyverse style guide) # even for strict = FALSE to be consistent with the : operator if (any(pd_flat$token %in% zero)) { - pd_flat <- pd_flat %>% + pd_flat <- style_space_around_token( + pd_flat, strict = TRUE, tokens = zero, level_before = 0L, level_after = 0L ) } if (any(pd_flat$token %in% one)) { - pd_flat <- pd_flat %>% + pd_flat <- style_space_around_token( + pd_flat, strict = strict, tokens = one, level_before = 1L, level_after = 1L ) } diff --git a/R/rules-tokens.R b/R/rules-tokens.R index f36ba289a..1a2e857a4 100644 --- a/R/rules-tokens.R +++ b/R/rules-tokens.R @@ -81,8 +81,7 @@ wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2L) { ) } if (is_conditional_expr(pd)) { - pd <- pd %>% - wrap_else_multiline_curly(indent_by, space_after = 0L) + pd <- wrap_else_multiline_curly(pd, indent_by, space_after = 0L) } pd } diff --git a/R/transform-files.R b/R/transform-files.R index a79c4a61b..c49c54cc6 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -119,8 +119,9 @@ make_transformer <- function(transformers, if (use_cache) { text } else { - transformed_code <- text %>% + transformed_code <- parse_transform_serialize_r( + text, transformers, base_indention = base_indention, warn_empty = warn_empty diff --git a/R/utils-cache.R b/R/utils-cache.R index 2465a51a2..6b57fb788 100644 --- a/R/utils-cache.R +++ b/R/utils-cache.R @@ -102,8 +102,10 @@ cache_make_key <- function(text, transformers, more_specs) { text = hash_standardize(text), style_guide_name = transformers$style_guide_name, style_guide_version = transformers$style_guide_version, - more_specs_style_guide = as.character(transformers$more_specs_style_guide) %>% - set_names(names(transformers$more_specs_style_guide)), + more_specs_style_guide = set_names( + as.character(transformers$more_specs_style_guide), + names(transformers$more_specs_style_guide) + ), more_specs = more_specs ) } @@ -157,8 +159,7 @@ cache_by_expression <- function(text, expressions <- parse(text = text, keep.source = TRUE) %>% utils::getParseData(includeText = TRUE) if (env_current$any_stylerignore) { - expressions <- expressions %>% - add_stylerignore() + expressions <- add_stylerignore(expressions) } else { expressions$stylerignore <- rep(FALSE, length(expressions$text)) } @@ -169,8 +170,11 @@ cache_by_expression <- function(text, # which the indention # was removed via parse, same as it is in cache_by_expression) and add the # base indention. - expressions[expressions$parent == 0L & expressions$token != "COMMENT" & !expressions$stylerignore, "text"] %>% - map(cache_write, transformers = transformers, more_specs) + map( + expressions[expressions$parent == 0L & expressions$token != "COMMENT" & !expressions$stylerignore, "text"], + cache_write, + transformers = transformers, more_specs + ) } diff --git a/R/utils.R b/R/utils.R index e1a235469..6602efd75 100644 --- a/R/utils.R +++ b/R/utils.R @@ -117,11 +117,10 @@ calls_sys <- function(sys_call, ...) { #' option was not set. #' @keywords internal option_read <- function(x, default = NULL, error_if_not_found = TRUE) { - if (x %in% names(options()) || !error_if_not_found) { - getOption(x, default) - } else { + if (!(x %in% names(options())) && error_if_not_found) { rlang::abort(paste("R option", x, "must be set.")) } + getOption(x, default) } #' @keywords internal diff --git a/inst/WORDLIST b/inst/WORDLIST index 1e14ad162..9302ce870 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -145,6 +145,7 @@ NONINFRINGEMENT nonportable nph NUM +nzchar oldrel oneliner ORCID