Skip to content

Commit af0e468

Browse files
Merge pull request #960 from r-lib/lintr
towards passing lintr checks
2 parents 9b0faa2 + d8220d1 commit af0e468

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+230
-154
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ editor_options:
4343
(#946).
4444
- turned off `styler.print.Vertical` in vignettes so ANSI output of {prettycode}
4545
not messing with {pkgdown} (\@IndrajeetPatil, #956, #957).
46+
- Improved code quality by fixing {lintr} warnings (#960).
47+
4648

4749
# styler 1.7.0
4850

R/addins.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
#' # save after styling when using the Addin
3838
#' options(styler.save_after_styling = TRUE)
3939
#' # only style with scope = "spaces" when using the Addin
40+
#' val <- "styler::tidyverse_style(scope = 'spaces')"
4041
#' options(
41-
#' styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')"
42+
#' styler.addins_style_transformer = val
4243
#' )
4344
#' }
4445
NULL
@@ -130,7 +131,11 @@ style_selection <- function() {
130131
base_indention = nchar(gsub("^( *).*", "\\1", text))
131132
)
132133
rstudioapi::modifyRange(
133-
context$selection[[1]]$range, paste0(c(out, if (context$selection[[1]]$range$end[2] == 1) ""), collapse = "\n"),
134+
context$selection[[1]]$range,
135+
paste0(c(
136+
out,
137+
if (context$selection[[1]]$range$end[2] == 1) ""
138+
), collapse = "\n"),
134139
id = context$id
135140
)
136141
if (save_after_styling_is_active() == TRUE && context$path != "") {
@@ -212,9 +217,9 @@ try_transform_as_r_file <- function(context, transformer) {
212217
transformer(context$contents),
213218
error = function(e) {
214219
preamble_for_unsaved <- paste(
215-
"Styling of unsaved files is only supported for R files with valid code.",
216-
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
217-
"can be parsed. Then, try to style again."
220+
"Styling of unsaved files is only supported for R files with valid ",
221+
"code. Please save the file (as .R or .Rmd) and make sure that the R ",
222+
"code in it can be parsed. Then, try to style again."
218223
)
219224

220225
if (context$path == "") {

R/communicate.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ communicate_summary <- function(changed, ruler_width) {
2525
if (!getOption("styler.quiet", FALSE)) {
2626
cli::cat_rule(width = max(40, ruler_width))
2727
cat("Status\tCount\tLegend \n")
28-
cli::cat_bullet("\t", sum(!changed, na.rm = TRUE), "\tFile unchanged.", bullet = "tick")
29-
cli::cat_bullet("\t", sum(changed, na.rm = TRUE), "\tFile changed.", bullet = "info")
30-
cli::cat_bullet(bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an error.")
28+
cli::cat_bullet(
29+
"\t", sum(!changed, na.rm = TRUE), "\tFile unchanged.",
30+
bullet = "tick"
31+
)
32+
cli::cat_bullet(
33+
"\t", sum(changed, na.rm = TRUE), "\tFile changed.",
34+
bullet = "info"
35+
)
36+
cli::cat_bullet(
37+
bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an error."
38+
)
3139
cli::cat_rule(width = max(40, ruler_width))
3240
}
3341
}

R/detect-alignment-utils.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ alignment_serialize_column <- function(relevant_pd_by_line, column) {
108108
#' @keywords internal
109109
alignment_serialize_line <- function(relevant_pd_by_line, column) {
110110
# TODO
111-
# better also add lover bound for column. If you already checked up to comma 2,
112-
# you don't need to re-construct text again, just check if text between comma 2
113-
# and 3 has the same length.
111+
# better also add lover bound for column. If you already checked up to
112+
# comma 2, you don't need to re-construct text again, just check if text
113+
# between comma 2 and 3 has the same length.
114114
comma_idx <- which(relevant_pd_by_line$token == "','")
115115
n_cols <- length(comma_idx)
116116
if (column > n_cols) {

R/detect-alignment.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ token_is_on_aligned_line <- function(pd_flat) {
116116

117117
# first col has no leading ,
118118
current_col <- nchar(by_line) - as.integer(column > 1)
119-
# Problem `by_line` counting from comma before column 3, previous_line counting 1 space before ~
119+
# Problem `by_line` counting from comma before column 3, previous_line
120+
# counting 1 space before ~
120121
if (column > 1) {
121122
previous_line <- previous_line[
122123
intersect(names(previous_line), names(by_line))

R/environments.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#' around. Examples are [#187](https://github.com/r-lib/styler/issues/187),
66
#' [#216](https://github.com/r-lib/styler/issues/216),
77
#' [#100](https://github.com/r-lib/styler/issues/100) and others. With
8-
#' [#419](https://github.com/r-lib/styler/issues/419), the structure of the parse
9-
#' data changes and we need to dispatch for older versions. As it is inconvenient
10-
#' to pass a parser version down in the call stack in various places, the
11-
#' environment `env_current` is used to store the current version *globally*
12-
#' but internally.
8+
#' [#419](https://github.com/r-lib/styler/issues/419), the structure of the
9+
#' parse data changes and we need to dispatch for older versions. As it is
10+
#' inconvenient to pass a parser version down in the call stack in various
11+
#' places, the environment `env_current` is used to store the current version
12+
#' *globally* but internally.
1313
#'
1414
#' We version the parser as follows:
1515
#'

R/indent.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#' @keywords internal
99
NULL
1010

11-
#' @describeIn update_indention Is used to indent for and statements and function
12-
#' definitions without parenthesis.
11+
#' @describeIn update_indention Is used to indent for and statements and
12+
#' function definitions without parenthesis.
1313
#' @keywords internal
1414
indent_without_paren_for_while_fun <- function(pd, indent_by) {
1515
tokens <- c("FOR", "WHILE", "FUNCTION")
@@ -39,7 +39,9 @@ indent_without_paren_if_else <- function(pd, indent_by) {
3939
if (!is_if) {
4040
return(pd)
4141
}
42-
needs_indention_now <- pd$lag_newlines[next_non_comment(pd, which(pd$token == "')'"))] > 0
42+
needs_indention_now <- pd$lag_newlines[
43+
next_non_comment(pd, which(pd$token == "')'"))
44+
] > 0
4345

4446
if (needs_indention_now) {
4547
pd$indent[expr_after_if] <- indent_by
@@ -57,7 +59,9 @@ indent_without_paren_if_else <- function(pd, indent_by) {
5759
pd$child[[expr_after_else_idx]]$token[1] != "'{'" &&
5860
pd$child[[expr_after_else_idx]]$token[1] != "IF"
5961

60-
needs_indention_now <- pd$lag_newlines[next_non_comment(pd, which(pd$token == "ELSE"))] > 0
62+
needs_indention_now <- pd$lag_newlines[
63+
next_non_comment(pd, which(pd$token == "ELSE"))
64+
] > 0
6165

6266
if (has_else_without_curly_or_else_chid && needs_indention_now) {
6367
pd$indent[seq(else_idx + 1, nrow(pd))] <- indent_by
@@ -231,7 +235,8 @@ pd_multi_line <- function(pd) {
231235
#' R/rules-spacing.R for tokens at the end of a line since this allows styling
232236
#' without touching indention.
233237
#' @param pd A parse table.
234-
#' @return A parse table with synchronized `lag_newlines` and `newlines` columns.
238+
#' @return A parse table with synchronized `lag_newlines` and `newlines`
239+
#' columns.
235240
#' @seealso choose_indention
236241
#' @keywords internal
237242
update_newlines <- function(pd) {

R/nest.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ add_cache_block <- function(pd_nested) {
109109
#' @keywords internal
110110
drop_cached_children <- function(pd) {
111111
if (cache_is_activated()) {
112-
pd_parent_first <- pd[order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)), ]
112+
order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal))
113+
pd_parent_first <- pd[order, ]
113114
pos_ids_to_keep <- pd_parent_first %>%
114115
split(cumsum(pd_parent_first$parent == 0)) %>%
115116
map(find_pos_id_to_keep) %>%
@@ -124,8 +125,9 @@ drop_cached_children <- function(pd) {
124125
#' Find the pos ids to keep
125126
#'
126127
#' To make a parse table shallow, we must know which ids to keep.
127-
#' `split(cumsum(pd_parent_first$parent == 0))` above puts comments with negative
128-
#' parents in the same block as proceeding expressions (but also with positive).
128+
#' `split(cumsum(pd_parent_first$parent == 0))` above puts comments with
129+
#' negative parents in the same block as proceeding expressions (but also with
130+
#' positive).
129131
#' `find_pos_id_to_keep()` must hence always keep negative comments. We did not
130132
#' use `split(cumsum(pd_parent_first$parent < 1))` because then every top-level
131133
#' comment is an expression on its own and processing takes much longer for
@@ -173,6 +175,7 @@ find_pos_id_to_keep <- function(pd) {
173175
#' option supports character vectors longer than one and the marker are not
174176
#' exactly matched, but using a regular expression, which means you can have
175177
#' multiple marker on one line, e.g. `# nolint start styler: off`.
178+
# nolint end
176179
#' @name stylerignore
177180
#' @examples
178181
#' # as long as the order of the markers is correct, the lines are ignored.

R/nested-to-tree.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ create_tree <- function(text, structure_only = FALSE) {
1212
create_tree_from_pd_with_default_style_attributes(structure_only)
1313
}
1414

15-
create_tree_from_pd_with_default_style_attributes <- function(pd, structure_only = FALSE) {
15+
create_tree_from_pd_with_default_style_attributes <- function(pd,
16+
structure_only = FALSE) {
1617
pd %>%
1718
create_node_from_nested_root(structure_only) %>%
1819
as.data.frame()
@@ -35,8 +36,12 @@ create_tree_from_pd_with_default_style_attributes <- function(pd, structure_only
3536
#' {
3637
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
3738
#' nested_pd <- styler:::compute_parse_data_nested(code)
38-
#' initialized <- styler:::pre_visit(nested_pd, c(default_style_guide_attributes))
39-
#' styler:::create_node_from_nested_root(initialized, structure_only = FALSE)
39+
#' initialized <- styler:::pre_visit(
40+
#' nested_pd, c(default_style_guide_attributes)
41+
#' )
42+
#' styler:::create_node_from_nested_root(initialized,
43+
#' structure_only = FALSE
44+
#' )
4045
#' }
4146
#' )
4247
#' }

R/parse.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ parse_safely <- function(text, ...) {
2727
abort(paste0(
2828
"The code to style seems to use Windows style line endings (CRLF). ",
2929
"styler currently only supports Unix style line endings (LF). ",
30-
"Please change the EOL character in your editor to Unix style and try again.",
31-
"\nThe parsing error was:\n", tried_parsing$message
30+
"Please change the EOL character in your editor to Unix style and try ",
31+
"again.\nThe parsing error was:\n", tried_parsing$message
3232
))
3333
} else {
3434
abort(tried_parsing$message)

0 commit comments

Comments
 (0)