diff --git a/DESCRIPTION b/DESCRIPTION index 853729086..6c7b6ad93 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,7 @@ License: MIT + file LICENSE URL: https://github.com/r-lib/styler, https://styler.r-lib.org BugReports: https://github.com/r-lib/styler/issues Depends: - R (>= 3.5.0) + R (>= 3.6.0) Imports: cli (>= 3.1.1), magrittr (>= 2.0.0), diff --git a/R/environments.R b/R/environments.R index 0f895517c..01305ed1d 100755 --- a/R/environments.R +++ b/R/environments.R @@ -43,13 +43,11 @@ parser_version_get <- function() { #' @rdname parser_version_set parser_version_find <- function(pd) { - ifelse(any(pd$token == "equal_assign"), - 2, - ifelse(any(pd$token == "expr_or_assign_or_help"), - 3, - 1 - ) - ) + if (any(pd$token == "equal_assign")) { + 2L + } else { + 3L + } } diff --git a/R/nest.R b/R/nest.R index d9cb0ea5e..09f5876a3 100644 --- a/R/nest.R +++ b/R/nest.R @@ -16,7 +16,6 @@ compute_parse_data_nested <- function(text, pd_nested <- parse_data %>% nest_parse_data() %>% flatten_operators() %>% - when(any(parse_data$token == "EQ_ASSIGN") ~ relocate_eq_assign(.), ~.) %>% add_cache_block() pd_nested diff --git a/R/parse.R b/R/parse.R index 06bf64502..1b6e717f0 100644 --- a/R/parse.R +++ b/R/parse.R @@ -109,7 +109,6 @@ get_parse_data <- function(text, include_text = TRUE, ...) { pd <- pd %>% add_id_and_short() - parser_version_set(parser_version_find(pd)) pd } diff --git a/R/relevel.R b/R/relevel.R index 1b7208ee8..bfca0764c 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -25,11 +25,7 @@ flatten_operators <- function(pd_nested) { #' @keywords internal flatten_operators_one <- function(pd_nested) { pd_token_left <- c(special_token, "PIPE", math_token, "'$'") - pd_token_right <- c( - special_token, "PIPE", "LEFT_ASSIGN", - if (parser_version_get() > 1) "EQ_ASSIGN", - "'+'", "'-'", "'~'" - ) + pd_token_right <- c(special_token, "PIPE", "LEFT_ASSIGN", "EQ_ASSIGN", "'+'", "'-'", "'~'") pd_nested %>% flatten_pd(pd_token_left, left = TRUE) %>% flatten_pd(pd_token_right, left = FALSE) @@ -56,12 +52,14 @@ flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) { if (length(token_pos_candidates) == 0L) { return(pd_nested) } + token_pos_idx <- if (left) { 1L } else { length(token_pos_candidates) } token_pos <- token_pos_candidates[token_pos_idx] + if (left) { pos <- previous_non_comment(pd_nested, token_pos) } else { @@ -99,150 +97,10 @@ bind_with_child <- function(pd_nested, pos) { wrap_expr_in_expr <- function(pd) { create_tokens( "expr", "", - pos_ids = create_pos_ids(pd, 1, after = FALSE), + pos_ids = create_pos_ids(pd, 1L, after = FALSE), child = pd, terminal = FALSE, stylerignore = pd$stylerignore[1L], indents = pd$indent[1L] ) } - - -# ____________________________________________________________________________ -# Relocate EQ_ASSIGN #### - -#' Relocate the expressions containing the token `EQ_ASSIGN` within the nested -#' parse table -#' -#' Although syntactically identical, [utils::getParseData()] does not produce -#' the same hierarchy of the parse table (parent and id relationship) for `<-` -#' and `=` (See 'Examples'). -#' This is considered to be a bug and causes problems because the -#' nested parse table constructed with [compute_parse_data_nested()] is not -#' consistent if `EQ_ASSIGN` occurs in the expression to style. In particular, -#' `EQ_ASSIGN` and the tokens to its left and right are located too high up in -#' the hierarchy of the nested parse data. Hence, this function wraps the -#' sub-expression into an expression, similar to [wrap_expr_in_curly()]. -#' Since `wrap_expr_in_curly()` is called from within a visitor -#' (and `relocate_eq_assign()` not), we need to -#' wrap the the implementation [relocate_eq_assign_nest()] that operates on -#' *nests* into a visitor call. -#' @param pd A parse table. -#' @examples -#' styler:::get_parse_data("a <- b <- 3") -#' styler:::get_parse_data("a = b = 3") -#' styler:::get_parse_data( -#' "x = 5 -#' if(x >= 5) -#' y = TRUE else -#' y = FALSE", -#' ) -#' styler:::get_parse_data( -#' "x <- 5 -#' if(x >= 5) -#' y <- TRUE else -#' y <- FALSE", -#' ) -#' @keywords internal -relocate_eq_assign <- function(pd) { - if (parser_version_get() < 2) { - post_visit_one(pd, relocate_eq_assign_nest) - } else { - pd - } -} - - -#' Relocate all assignment expressions that contain `EQ_ASSIGN` within a *nest* -#' -#' Implements the relocation of an `EQ_ASSIGN` and associated tokens -#' within a *nest* (nested parse table at one level of nesting). -#' Note that one assignment expression (such as "a = b = c") can include -#' multiple assignment operators, an assignment involves just one assignment -#' operator. -#' For the relocation of assignment expressions that contain `EQ_ASSIGN` within -#' a *nest*, we need to first find the expressions that contain `=` and then -#' split the *nest* into parse tables each containing one such assignment -#' expression and then relocate each of them separately. -#' We can't do all of them together because: -#' -#' * An assignment can contain more than just three tokens, e.g. (a <- b <- c). -#' * Two assignments can be in the same nest although they don't belong to the -#' same assignment (if-else statement). -#' -#' Please refer to the section 'Examples' in [relocate_eq_assign()] for details. -#' @param pd A parse table. -#' @importFrom rlang seq2 -#' @keywords internal -relocate_eq_assign_nest <- function(pd) { - idx_eq_assign <- which(pd$token == "EQ_ASSIGN") - if (length(idx_eq_assign) > 0L) { - block_id <- find_block_id(pd) - blocks <- split(pd, block_id) - pd <- map_dfr(blocks, relocate_eq_assign_one) - } - pd -} - -#' Find the block to which a token belongs -#' -#' Two assignment tokens `EQ_ASSIGN` belong to the same block if they are not -#' separated by more than one token. Token between `EQ_ASSIGN` tokens belong -#' to the `EQ_ASSIGN` token occurring before them, except the token right before -#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it. Note that this -#' notion is unrelated to the column *block* in the parse table, which is used -#' to [parse_transform_serialize_r()] code blocks and leave out the ones that -#' are cached. -#' @param pd A parse table. -#' @keywords internal -find_block_id <- function(pd) { - idx_eq_assign <- which(pd$token == "EQ_ASSIGN") - eq_belongs_to_block <- c(0, diff(idx_eq_assign) > 2) - - empty_seq <- rep(0, nrow(pd)) - empty_seq[idx_eq_assign - 1] <- eq_belongs_to_block - block_id <- cumsum(empty_seq) - block_id -} - -#' Relocate an assignment expression -#' -#' Relocates an assignment expression within a parse table containing one -#' assignment expression. Note that one assignment can include multiple -#' assignment operators such as "a = b = c". -#' @param pd A parse table with one assignment expression to relocate. -#' @keywords internal -relocate_eq_assign_one <- function(pd) { - idx_eq_assign <- which(pd$token == "EQ_ASSIGN") - eq_ind <- seq2(idx_eq_assign[1L] - 1L, last(idx_eq_assign) + 1L) - # initialize because wrap_expr_in_expr -> create_tokens -> requires it - pd$indent <- 0 - eq_expr <- pd[eq_ind, ] %>% - wrap_expr_in_expr() %>% - add_line_col_to_wrapped_expr() %>% - remove_attributes(c( - "multi_line", "indention_ref_pos_id", - "newlines", "indent", "spaces", "lag_newlines" - )) - eq_expr$id <- NA - eq_expr$parent <- NA - pd$indent <- NULL - non_eq_expr <- pd[-eq_ind, ] - pd <- bind_rows(eq_expr, non_eq_expr) %>% - arrange_pos_id() - pd -} - -#' Adds line and col information to an expression from its child -#' -#' @param pd A parse table. -#' @importFrom rlang abort -#' @keywords internal -add_line_col_to_wrapped_expr <- function(pd) { - if (nrow(pd) > 1) abort("pd must be a wrapped expression that has one row.") - pd$line1 <- pd$child[[1L]]$line1[1L] - pd$line2 <- last(pd$child[[1L]]$line2) - pd$col1 <- pd$child[[1L]]$col1[1L] - pd$col2 <- last(pd$child[[1L]]$col2) - pd -} diff --git a/R/style-guides.R b/R/style-guides.R index 10f8fffb8..4abd92780 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -238,10 +238,6 @@ tidyverse_style <- function(scope = "tokens", ) ) - if (getRversion() < "3.6") { - transformers_drop$token$force_assignment_op <- NULL - } - style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib" create_style_guide( # transformer functions diff --git a/R/styler.R b/R/styler.R index a6e44e870..4409a837b 100644 --- a/R/styler.R +++ b/R/styler.R @@ -18,15 +18,13 @@ #' style_text("a%>%b; a", scope = "tokens") "_PACKAGE" -if (getRversion() >= "2.15.1") { - utils::globalVariables(c( - ".", - "pd", "pd_nested", "pd_flat", "flattened_pd", - "line1", "line2", "col1", "col2", "parent", - "terminal", "text", "short", - "spaces", "lag_spaces", - "newlines", "lag_newlines", - "pos_id", - NULL - )) -} +utils::globalVariables(c( + ".", + "pd", "pd_nested", "pd_flat", "flattened_pd", + "line1", "line2", "col1", "col2", "parent", + "terminal", "text", "short", + "spaces", "lag_spaces", + "newlines", "lag_newlines", + "pos_id", + NULL +)) diff --git a/R/utils-navigate-nest.R b/R/utils-navigate-nest.R index c4c5e7432..050152ae4 100644 --- a/R/utils-navigate-nest.R +++ b/R/utils-navigate-nest.R @@ -1,4 +1,3 @@ - #' Find the index of the next or previous non-comment in a parse table. #' @param pd A parse table. #' @param pos The position of the token to start the search from. diff --git a/man/add_line_col_to_wrapped_expr.Rd b/man/add_line_col_to_wrapped_expr.Rd deleted file mode 100644 index dc7b0d333..000000000 --- a/man/add_line_col_to_wrapped_expr.Rd +++ /dev/null @@ -1,15 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/relevel.R -\name{add_line_col_to_wrapped_expr} -\alias{add_line_col_to_wrapped_expr} -\title{Adds line and col information to an expression from its child} -\usage{ -add_line_col_to_wrapped_expr(pd) -} -\arguments{ -\item{pd}{A parse table.} -} -\description{ -Adds line and col information to an expression from its child -} -\keyword{internal} diff --git a/man/find_block_id.Rd b/man/find_block_id.Rd deleted file mode 100644 index 2635a8420..000000000 --- a/man/find_block_id.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/relevel.R -\name{find_block_id} -\alias{find_block_id} -\title{Find the block to which a token belongs} -\usage{ -find_block_id(pd) -} -\arguments{ -\item{pd}{A parse table.} -} -\description{ -Two assignment tokens \code{EQ_ASSIGN} belong to the same block if they are not -separated by more than one token. Token between \code{EQ_ASSIGN} tokens belong -to the \code{EQ_ASSIGN} token occurring before them, except the token right before -\code{EQ_ASSING} already belongs to the \code{EQ_ASSING} after it. Note that this -notion is unrelated to the column \emph{block} in the parse table, which is used -to \code{\link[=parse_transform_serialize_r]{parse_transform_serialize_r()}} code blocks and leave out the ones that -are cached. -} -\keyword{internal} diff --git a/man/relocate_eq_assign.Rd b/man/relocate_eq_assign.Rd deleted file mode 100644 index 5faf42e35..000000000 --- a/man/relocate_eq_assign.Rd +++ /dev/null @@ -1,44 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/relevel.R -\name{relocate_eq_assign} -\alias{relocate_eq_assign} -\title{Relocate the expressions containing the token \code{EQ_ASSIGN} within the nested -parse table} -\usage{ -relocate_eq_assign(pd) -} -\arguments{ -\item{pd}{A parse table.} -} -\description{ -Although syntactically identical, \code{\link[utils:getParseData]{utils::getParseData()}} does not produce -the same hierarchy of the parse table (parent and id relationship) for \verb{<-} -and \code{=} (See 'Examples'). -This is considered to be a bug and causes problems because the -nested parse table constructed with \code{\link[=compute_parse_data_nested]{compute_parse_data_nested()}} is not -consistent if \code{EQ_ASSIGN} occurs in the expression to style. In particular, -\code{EQ_ASSIGN} and the tokens to its left and right are located too high up in -the hierarchy of the nested parse data. Hence, this function wraps the -sub-expression into an expression, similar to \code{\link[=wrap_expr_in_curly]{wrap_expr_in_curly()}}. -Since \code{wrap_expr_in_curly()} is called from within a visitor -(and \code{relocate_eq_assign()} not), we need to -wrap the the implementation \code{\link[=relocate_eq_assign_nest]{relocate_eq_assign_nest()}} that operates on -\emph{nests} into a visitor call. -} -\examples{ -styler:::get_parse_data("a <- b <- 3") -styler:::get_parse_data("a = b = 3") -styler:::get_parse_data( - "x = 5 - if(x >= 5) - y = TRUE else - y = FALSE", -) -styler:::get_parse_data( - "x <- 5 - if(x >= 5) - y <- TRUE else - y <- FALSE", -) -} -\keyword{internal} diff --git a/man/relocate_eq_assign_nest.Rd b/man/relocate_eq_assign_nest.Rd deleted file mode 100644 index e66177d45..000000000 --- a/man/relocate_eq_assign_nest.Rd +++ /dev/null @@ -1,33 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/relevel.R -\name{relocate_eq_assign_nest} -\alias{relocate_eq_assign_nest} -\title{Relocate all assignment expressions that contain \code{EQ_ASSIGN} within a \emph{nest}} -\usage{ -relocate_eq_assign_nest(pd) -} -\arguments{ -\item{pd}{A parse table.} -} -\description{ -Implements the relocation of an \code{EQ_ASSIGN} and associated tokens -within a \emph{nest} (nested parse table at one level of nesting). -Note that one assignment expression (such as "a = b = c") can include -multiple assignment operators, an assignment involves just one assignment -operator. -For the relocation of assignment expressions that contain \code{EQ_ASSIGN} within -a \emph{nest}, we need to first find the expressions that contain \code{=} and then -split the \emph{nest} into parse tables each containing one such assignment -expression and then relocate each of them separately. -We can't do all of them together because: -} -\details{ -\itemize{ -\item An assignment can contain more than just three tokens, e.g. (a <- b <- c). -\item Two assignments can be in the same nest although they don't belong to the -same assignment (if-else statement). -} - -Please refer to the section 'Examples' in \code{\link[=relocate_eq_assign]{relocate_eq_assign()}} for details. -} -\keyword{internal} diff --git a/man/relocate_eq_assign_one.Rd b/man/relocate_eq_assign_one.Rd deleted file mode 100644 index 90318565e..000000000 --- a/man/relocate_eq_assign_one.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/relevel.R -\name{relocate_eq_assign_one} -\alias{relocate_eq_assign_one} -\title{Relocate an assignment expression} -\usage{ -relocate_eq_assign_one(pd) -} -\arguments{ -\item{pd}{A parse table with one assignment expression to relocate.} -} -\description{ -Relocates an assignment expression within a parse table containing one -assignment expression. Note that one assignment can include multiple -assignment operators such as "a = b = c". -} -\keyword{internal} diff --git a/tests/testthat/test-transformers-drop.R b/tests/testthat/test-transformers-drop.R index bad49dab1..2c126467d 100644 --- a/tests/testthat/test-transformers-drop.R +++ b/tests/testthat/test-transformers-drop.R @@ -94,10 +94,7 @@ test_that("tidyverse transformers are correctly dropped", { names_indention <- c("indent_braces", "indent_op", "indent_without_paren") expect_setequal(names(t_fun$indention), names_indention) - names_tokens <- c( - "fix_quotes", - if (getRversion() < "3.6") "force_assignment_op" - ) + names_tokens <- "fix_quotes" expect_setequal(names(t_fun$token), names_tokens) })