Skip to content

Commit

Permalink
Bump minimum needed R version to 3.6
Browse files Browse the repository at this point in the history
R 4.3 is scheduled for release on April 21.

Follow-up on #985
  • Loading branch information
IndrajeetPatil committed Apr 1, 2023
1 parent 57bbf4e commit fc23728
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 354 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 1 addition & 7 deletions R/environments.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ parser_version_get <- function() {

#' @rdname parser_version_set
parser_version_find <- function(pd) {
ifelse(any(pd$token == "equal_assign"),
2L,
ifelse(any(pd$token == "expr_or_assign_or_help"),
3L,
1L
)
)
if (any(pd$token == "equal_assign")) 2L else 3L
}


Expand Down
1 change: 0 additions & 1 deletion R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
pd <- pd %>%
add_id_and_short()

parser_version_set(parser_version_find(pd))
pd
}

Expand Down
146 changes: 1 addition & 145 deletions R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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() > 1L) "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)
Expand Down Expand Up @@ -106,143 +102,3 @@ wrap_expr_in_expr <- function(pd) {
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() < 2L) {
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.
#'
#' @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(0L, diff(idx_eq_assign) > 2L)

empty_seq <- rep(0L, nrow(pd))
empty_seq[idx_eq_assign - 1L] <- 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 <- 0L
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.

#' @keywords internal
add_line_col_to_wrapped_expr <- function(pd) {
if (nrow(pd) > 1L) 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
}
4 changes: 0 additions & 4 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,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
Expand Down
15 changes: 0 additions & 15 deletions man/add_line_col_to_wrapped_expr.Rd

This file was deleted.

4 changes: 2 additions & 2 deletions man/combine_children.Rd

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

21 changes: 0 additions & 21 deletions man/find_block_id.Rd

This file was deleted.

44 changes: 0 additions & 44 deletions man/relocate_eq_assign.Rd

This file was deleted.

33 changes: 0 additions & 33 deletions man/relocate_eq_assign_nest.Rd

This file was deleted.

17 changes: 0 additions & 17 deletions man/relocate_eq_assign_one.Rd

This file was deleted.

Loading

0 comments on commit fc23728

Please sign in to comment.