Skip to content

Commit 8e9ac82

Browse files
Merge pull request #1101 from r-lib/bump-to-r36
Bump minimum needed R version to `3.6`
2 parents 15a5f5b + 6045bee commit 8e9ac82

12 files changed

+7
-298
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ License: MIT + file LICENSE
2323
URL: https://github.com/r-lib/styler, https://styler.r-lib.org
2424
BugReports: https://github.com/r-lib/styler/issues
2525
Depends:
26-
R (>= 3.5.0)
26+
R (>= 3.6.0)
2727
Imports:
2828
cli (>= 3.1.1),
2929
magrittr (>= 2.0.0),

R/environments.R

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ parser_version_get <- function() {
4343

4444
#' @rdname parser_version_set
4545
parser_version_find <- function(pd) {
46-
ifelse(any(pd$token == "equal_assign"),
47-
2L,
48-
ifelse(any(pd$token == "expr_or_assign_or_help"),
49-
3L,
50-
1L
51-
)
52-
)
46+
if (any(pd$token == "equal_assign")) 2L else 3L
5347
}
5448

5549

R/nest.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ compute_parse_data_nested <- function(text,
2121
parse_data$child <- rep(list(NULL), length(parse_data$text))
2222
pd_nested <- parse_data %>%
2323
nest_parse_data() %>%
24-
flatten_operators()
24+
flatten_operators() %>%
25+
add_cache_block()
2526

26-
if (any(parse_data$token == "EQ_ASSIGN")) {
27-
pd_nested <- relocate_eq_assign(pd_nested)
28-
}
29-
30-
pd_nested %>% add_cache_block()
27+
pd_nested
3128
}
3229

3330
#' Creates a flat parse table with minimal initialization

R/parse.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
105105
pd <- pd %>%
106106
add_id_and_short()
107107

108-
parser_version_set(parser_version_find(pd))
109108
pd
110109
}
111110

R/relevel.R

Lines changed: 1 addition & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ flatten_operators <- function(pd_nested) {
2525
#' @keywords internal
2626
flatten_operators_one <- function(pd_nested) {
2727
pd_token_left <- c(special_token, "PIPE", math_token, "'$'")
28-
pd_token_right <- c(
29-
special_token, "PIPE", "LEFT_ASSIGN",
30-
if (parser_version_get() > 1L) "EQ_ASSIGN",
31-
"'+'", "'-'", "'~'"
32-
)
28+
pd_token_right <- c(special_token, "PIPE", "LEFT_ASSIGN", "EQ_ASSIGN", "'+'", "'-'", "'~'")
3329
pd_nested %>%
3430
flatten_pd(pd_token_left, left = TRUE) %>%
3531
flatten_pd(pd_token_right, left = FALSE)
@@ -106,143 +102,3 @@ wrap_expr_in_expr <- function(pd) {
106102
indents = pd$indent[1L]
107103
)
108104
}
109-
110-
111-
# ____________________________________________________________________________
112-
# Relocate EQ_ASSIGN ####
113-
114-
#' Relocate the expressions containing the token `EQ_ASSIGN` within the nested
115-
#' parse table
116-
#'
117-
#' Although syntactically identical, [utils::getParseData()] does not produce
118-
#' the same hierarchy of the parse table (parent and id relationship) for `<-`
119-
#' and `=` (See 'Examples').
120-
#' This is considered to be a bug and causes problems because the
121-
#' nested parse table constructed with [compute_parse_data_nested()] is not
122-
#' consistent if `EQ_ASSIGN` occurs in the expression to style. In particular,
123-
#' `EQ_ASSIGN` and the tokens to its left and right are located too high up in
124-
#' the hierarchy of the nested parse data. Hence, this function wraps the
125-
#' sub-expression into an expression, similar to [wrap_expr_in_curly()].
126-
#' Since `wrap_expr_in_curly()` is called from within a visitor
127-
#' (and `relocate_eq_assign()` not), we need to
128-
#' wrap the the implementation [relocate_eq_assign_nest()] that operates on
129-
#' *nests* into a visitor call.
130-
#' @param pd A parse table.
131-
#' @examples
132-
#' styler:::get_parse_data("a <- b <- 3")
133-
#' styler:::get_parse_data("a = b = 3")
134-
#' styler:::get_parse_data(
135-
#' "x = 5
136-
#' if(x >= 5)
137-
#' y = TRUE else
138-
#' y = FALSE",
139-
#' )
140-
#' styler:::get_parse_data(
141-
#' "x <- 5
142-
#' if(x >= 5)
143-
#' y <- TRUE else
144-
#' y <- FALSE",
145-
#' )
146-
#' @keywords internal
147-
relocate_eq_assign <- function(pd) {
148-
if (parser_version_get() < 2L) {
149-
post_visit_one(pd, relocate_eq_assign_nest)
150-
} else {
151-
pd
152-
}
153-
}
154-
155-
156-
#' Relocate all assignment expressions that contain `EQ_ASSIGN` within a *nest*
157-
#'
158-
#' Implements the relocation of an `EQ_ASSIGN` and associated tokens
159-
#' within a *nest* (nested parse table at one level of nesting).
160-
#' Note that one assignment expression (such as "a = b = c") can include
161-
#' multiple assignment operators, an assignment involves just one assignment
162-
#' operator.
163-
#' For the relocation of assignment expressions that contain `EQ_ASSIGN` within
164-
#' a *nest*, we need to first find the expressions that contain `=` and then
165-
#' split the *nest* into parse tables each containing one such assignment
166-
#' expression and then relocate each of them separately.
167-
#' We can't do all of them together because:
168-
#'
169-
#' * An assignment can contain more than just three tokens, e.g. (a <- b <- c).
170-
#' * Two assignments can be in the same nest although they don't belong to the
171-
#' same assignment (if-else statement).
172-
#'
173-
#' Please refer to the section 'Examples' in [relocate_eq_assign()] for details.
174-
#' @param pd A parse table.
175-
#'
176-
#' @keywords internal
177-
relocate_eq_assign_nest <- function(pd) {
178-
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
179-
if (length(idx_eq_assign) > 0L) {
180-
block_id <- find_block_id(pd)
181-
blocks <- vec_split(pd, block_id)
182-
pd <- map_dfr(blocks[[2L]], relocate_eq_assign_one)
183-
}
184-
pd
185-
}
186-
187-
#' Find the block to which a token belongs
188-
#'
189-
#' Two assignment tokens `EQ_ASSIGN` belong to the same block if they are not
190-
#' separated by more than one token. Token between `EQ_ASSIGN` tokens belong
191-
#' to the `EQ_ASSIGN` token occurring before them, except the token right before
192-
#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it. Note that this
193-
#' notion is unrelated to the column *block* in the parse table, which is used
194-
#' to [parse_transform_serialize_r()] code blocks and leave out the ones that
195-
#' are cached.
196-
#' @param pd A parse table.
197-
#' @keywords internal
198-
find_block_id <- function(pd) {
199-
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
200-
eq_belongs_to_block <- c(0L, diff(idx_eq_assign) > 2L)
201-
202-
empty_seq <- rep(0L, nrow(pd))
203-
empty_seq[idx_eq_assign - 1L] <- eq_belongs_to_block
204-
block_id <- cumsum(empty_seq)
205-
block_id
206-
}
207-
208-
#' Relocate an assignment expression
209-
#'
210-
#' Relocates an assignment expression within a parse table containing one
211-
#' assignment expression. Note that one assignment can include multiple
212-
#' assignment operators such as "a = b = c".
213-
#' @param pd A parse table with one assignment expression to relocate.
214-
#' @keywords internal
215-
relocate_eq_assign_one <- function(pd) {
216-
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
217-
eq_ind <- seq2(idx_eq_assign[1L] - 1L, last(idx_eq_assign) + 1L)
218-
# initialize because wrap_expr_in_expr -> create_tokens -> requires it
219-
pd$indent <- 0L
220-
eq_expr <- vec_slice(pd, eq_ind) %>%
221-
wrap_expr_in_expr() %>%
222-
add_line_col_to_wrapped_expr() %>%
223-
remove_attributes(c(
224-
"multi_line", "indention_ref_pos_id",
225-
"newlines", "indent", "spaces", "lag_newlines"
226-
))
227-
eq_expr$id <- NA
228-
eq_expr$parent <- NA
229-
pd$indent <- NULL
230-
non_eq_expr <- vec_slice(pd, -eq_ind)
231-
pd <- vec_rbind(eq_expr, non_eq_expr) %>%
232-
arrange_pos_id()
233-
pd
234-
}
235-
236-
#' Adds line and col information to an expression from its child
237-
#'
238-
#' @param pd A parse table.
239-
240-
#' @keywords internal
241-
add_line_col_to_wrapped_expr <- function(pd) {
242-
if (nrow(pd) > 1L) abort("pd must be a wrapped expression that has one row.")
243-
pd$line1 <- pd$child[[1L]]$line1[1L]
244-
pd$line2 <- last(pd$child[[1L]]$line2)
245-
pd$col1 <- pd$child[[1L]]$col1[1L]
246-
pd$col2 <- last(pd$child[[1L]]$col2)
247-
pd
248-
}

R/style-guides.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ tidyverse_style <- function(scope = "tokens",
237237
)
238238
)
239239

240-
if (getRversion() < "3.6") {
241-
transformers_drop$token$force_assignment_op <- NULL
242-
}
243-
244240
style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
245241
create_style_guide(
246242
# transformer functions

man/add_line_col_to_wrapped_expr.Rd

Lines changed: 0 additions & 15 deletions
This file was deleted.

man/find_block_id.Rd

Lines changed: 0 additions & 21 deletions
This file was deleted.

man/relocate_eq_assign.Rd

Lines changed: 0 additions & 44 deletions
This file was deleted.

man/relocate_eq_assign_nest.Rd

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)