Skip to content

Commit

Permalink
Merge pull request #1158 from r-lib/clean-lints-dec23
Browse files Browse the repository at this point in the history
Update lintr config and address newly found lints
  • Loading branch information
IndrajeetPatil committed Dec 3, 2023
2 parents edf399c + f8a12b6 commit 2218546
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 86 deletions.
55 changes: 29 additions & 26 deletions .lintr
Original file line number Diff line number Diff line change
@@ -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"
)
36 changes: 17 additions & 19 deletions R/detect-alignment-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "','"))
Expand Down
27 changes: 14 additions & 13 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions R/nested-to-tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions R/roxygen-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
3 changes: 1 addition & 2 deletions R/rules-tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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))
}
Expand All @@ -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
)
}


Expand Down
5 changes: 2 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ NONINFRINGEMENT
nonportable
nph
NUM
nzchar
oldrel
oneliner
ORCID
Expand Down

0 comments on commit 2218546

Please sign in to comment.