From 82157a7fc3f6bd40bc4ceae81587668de7608201 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 30 Nov 2020 02:18:28 -0500 Subject: [PATCH 1/3] enable infix_spaces_linter, and apply it to the package --- .lintr | 1 - R/T_and_F_symbol_linter.R | 2 +- R/commas_linter.R | 4 ++-- R/expect_lint.R | 2 +- R/extraction_operator_linter.R | 2 +- R/namespace.R | 2 +- R/object_name_linters.R | 8 ++++---- R/path_linters.R | 4 ++-- R/undesirable_function_linter.R | 2 +- R/undesirable_operator_linter.R | 6 +++--- R/utils.R | 2 +- R/zzz.R | 4 ++-- 12 files changed, 19 insertions(+), 20 deletions(-) diff --git a/.lintr b/.lintr index 7632a93a7..25444447a 100644 --- a/.lintr +++ b/.lintr @@ -1,6 +1,5 @@ linters: with_defaults( # The following TODOs are part of an effort to have {lintr} lint-free (#584) line_length_linter = line_length_linter(120), - infix_spaces_linter = NULL, # TODO enable (#594) commented_code_linter = NULL, # TODO enable (#595) cyclocomp_linter = cyclocomp_linter(29), # TODO reduce to 15 object_name_linter = NULL, # TODO enable (#597) diff --git a/R/T_and_F_symbol_linter.R b/R/T_and_F_symbol_linter.R index 8a7401265..6858379a4 100644 --- a/R/T_and_F_symbol_linter.R +++ b/R/T_and_F_symbol_linter.R @@ -9,7 +9,7 @@ T_and_F_symbol_linter <- function(source_file) { token <- with_id(source_file, id) symbol <- re_matches(token[["text"]], rex(start, capture(or("T", "F")), end))[1L, 1L] if (!is.na(symbol)) { - replacement <- switch(symbol, "T"="TRUE", "F"="FALSE") + replacement <- switch(symbol, "T" = "TRUE", "F" = "FALSE") line_num <- token[["line2"]] start_col_num <- token[["col1"]] end_col_num <- token[["col2"]] diff --git a/R/commas_linter.R b/R/commas_linter.R index e56d4a491..5037a772c 100644 --- a/R/commas_linter.R +++ b/R/commas_linter.R @@ -38,7 +38,7 @@ commas_linter <- function(source_file) { empty_comma <- substr(line, comma_loc - 2L, comma_loc - 1L) %==% ", " parent <- source_file$parsed_content$parent - parent <- replace(parent, parent==0, NA) + parent <- replace(parent, parent == 0, NA) # a variable that is true for every node who has a grandchild that is switch, # i.e, any expression that starts with the function call to switch. @@ -50,7 +50,7 @@ commas_linter <- function(source_file) { is_blank_switch <- any(comma_loc_filter & (source_file$parsed_content$parent %in% switch_grandparents) & c(NA, head(source_file$parsed_content$token, -1)) == "EQ_SUB", - na.rm=TRUE + na.rm = TRUE ) if (has_token && !start_of_line && !empty_comma && !is_blank_switch) { diff --git a/R/expect_lint.R b/R/expect_lint.R index 328701315..53747bf1b 100644 --- a/R/expect_lint.R +++ b/R/expect_lint.R @@ -51,7 +51,7 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") { lints <- lint(file, ...) n_lints <- length(lints) - lint_str <- if (n_lints) paste0(c("", lints), collapse="\n") else "" + lint_str <- if (n_lints) paste0(c("", lints), collapse = "\n") else "" wrong_number_fmt <- "got %d lints instead of %d%s" if (is.null(checks)) { diff --git a/R/extraction_operator_linter.R b/R/extraction_operator_linter.R index 526b6ce6a..185007bc5 100644 --- a/R/extraction_operator_linter.R +++ b/R/extraction_operator_linter.R @@ -5,7 +5,7 @@ extraction_operator_linter <- function(source_file) { tokens <- source_file[["parsed_content"]] <- filter_out_token_type(source_file[["parsed_content"]], "expr") lapply( - ids_with_token(source_file, c("'$'", "'['"), fun=`%in%`), + ids_with_token(source_file, c("'$'", "'['"), fun = `%in%`), function(token_num) { if (is_dollar_extract(token_num, tokens) || is_bracket_extract(token_num, tokens)) { token <- with_id(source_file, token_num) diff --git a/R/namespace.R b/R/namespace.R index 503126539..20c92a2b3 100644 --- a/R/namespace.R +++ b/R/namespace.R @@ -2,7 +2,7 @@ namespace_imports <- function(path = find_package()) { imports <- tryCatch({ pkg_name <- suppressWarnings(pkg_name(path)) - data <- parseNamespaceFile(package=pkg_name, package.lib=file.path(path, "..")) + data <- parseNamespaceFile(package = pkg_name, package.lib = file.path(path, "..")) data$imports }, error = function(e) { list() diff --git a/R/object_name_linters.R b/R/object_name_linters.R index 79bc847fd..9db5264e8 100644 --- a/R/object_name_linters.R +++ b/R/object_name_linters.R @@ -121,7 +121,7 @@ make_object_linter <- function(fun) { function(source_file) { token_nums <- ids_with_token( - source_file, rex(start, "SYMBOL" %if_next_isnt% "_SUB"), fun=re_matches + source_file, rex(start, "SYMBOL" %if_next_isnt% "_SUB"), fun = re_matches ) if (length(token_nums) == 0) { return(list()) @@ -305,9 +305,9 @@ style_regexes <- list( regexes_rd <- paste0(collapse = ", ", paste0("\\sQuote{", names(style_regexes), "}")) matches_styles <- function(name, styles=names(style_regexes)) { - invalids <- paste(styles[!styles %in% names(style_regexes)], collapse=", ") + invalids <- paste(styles[!styles %in% names(style_regexes)], collapse = ", ") if (nzchar(invalids)) { - valids <- paste(names(style_regexes), collapse=", ") + valids <- paste(names(style_regexes), collapse = ", ") stop(sprintf("Invalid style(s) requested: %s\nValid styles are: %s\n", invalids, valids)) } name <- re_substitutes(name, rex(start, one_or_more(dot)), "") # remove leading dots @@ -315,7 +315,7 @@ matches_styles <- function(name, styles=names(style_regexes)) { style_regexes[styles], re_matches, logical(1L), - data=name + data = name ) } diff --git a/R/path_linters.R b/R/path_linters.R index 6d7ab3787..db6ce21ec 100644 --- a/R/path_linters.R +++ b/R/path_linters.R @@ -1,4 +1,4 @@ -control_char_regex <- rex(one_of(intToUtf8(seq.int(0L, 31L), multiple=TRUE))) +control_char_regex <- rex(one_of(intToUtf8(seq.int(0L, 31L), multiple = TRUE))) # control chars (non-printing) win32_bad_punct_regex <- rex(one_of("*", "?", "\"", "<", ">", "|", ":", "/", "\\")) @@ -121,7 +121,7 @@ split_path <- function(path, sep="/|\\\\") { } i <- i - 1L if (i > 0L) { - dirs <- c(paste0(rep(prefix, i), collapse=""), tail(dirs, -i)) + dirs <- c(paste0(rep(prefix, i), collapse = ""), tail(dirs, -i)) } # add // to protocols (like http, smb, ...) if (length(dirs)) { diff --git a/R/undesirable_function_linter.R b/R/undesirable_function_linter.R index 8ca4d6e08..a36731322 100644 --- a/R/undesirable_function_linter.R +++ b/R/undesirable_function_linter.R @@ -34,7 +34,7 @@ undesirable_function_linter <- function(fun = default_undesirable_functions, line_number = line_num, column_number = start_col_num, type = "warning", - message = paste0(msg, collapse=" "), + message = paste0(msg, collapse = " "), line = source_file[["lines"]][[as.character(line_num)]], ranges = list(c(start_col_num, end_col_num)), linter = "undesirable_function_linter" diff --git a/R/undesirable_operator_linter.R b/R/undesirable_operator_linter.R index cc4430ab1..39370d4a4 100644 --- a/R/undesirable_operator_linter.R +++ b/R/undesirable_operator_linter.R @@ -13,10 +13,10 @@ op_types <- c( #' @param op Named character vector, where the names are the names of the undesirable operators, #' and the values are the text for the alternative operator to use (or \code{NA}). #' @export -undesirable_operator_linter <- function(op=default_undesirable_operators) { +undesirable_operator_linter <- function(op = default_undesirable_operators) { function(source_file) { lapply( - ids_with_token(source_file, op_types, fun=`%in%`), + ids_with_token(source_file, op_types, fun = `%in%`), function(id) { token <- with_id(source_file, id) op_name <- token[["text"]] @@ -34,7 +34,7 @@ undesirable_operator_linter <- function(op=default_undesirable_operators) { line_number = line_num, column_number = start_col_num, type = "warning", - message = paste0(msg, collapse=" "), + message = paste0(msg, collapse = " "), line = source_file[["lines"]][[as.character(line_num)]], ranges = list(c(start_col_num, end_col_num)), linter = "undesirable_function_linter" diff --git a/R/utils.R b/R/utils.R index f95702758..b22e82287 100644 --- a/R/utils.R +++ b/R/utils.R @@ -202,7 +202,7 @@ escape_chars <- c( unescape <- function(str, q="`") { names(q) <- paste0("\\", q) my_escape_chars <- c(escape_chars, q) - res <- gregexpr(text=str, pattern=rex(or(names(my_escape_chars)))) + res <- gregexpr(text = str, pattern = rex(or(names(my_escape_chars)))) all_matches <- regmatches(str, res) regmatches(str, res) <- lapply( all_matches, diff --git a/R/zzz.R b/R/zzz.R index e67844d31..fb57b4c9c 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -137,7 +137,7 @@ all_undesirable_functions <- with_defaults(default = list(), #' @rdname default_undesirable_functions #' @export -default_undesirable_functions <- do.call(with_defaults, c(list(default=list()), +default_undesirable_functions <- do.call(with_defaults, c(list(default = list()), all_undesirable_functions[c( "attach", "detach", @@ -166,7 +166,7 @@ all_undesirable_operators <- with_defaults(default = list(), #' @rdname default_undesirable_functions #' @export -default_undesirable_operators <- do.call(with_defaults, c(list(default=list()), +default_undesirable_operators <- do.call(with_defaults, c(list(default = list()), all_undesirable_operators[c( ":::", "<<-", From d818de0fb6e652189730bc5f5b99bba306d4c2a2 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 30 Nov 2020 21:03:53 -0500 Subject: [PATCH 2/3] add rot test --- tests/testthat/test-settings.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-settings.R b/tests/testthat/test-settings.R index 5e731fa99..51c6acf0f 100644 --- a/tests/testthat/test-settings.R +++ b/tests/testthat/test-settings.R @@ -48,3 +48,8 @@ test_that("it errors if the config file does not end in a newline", { options("lintr.linter_file" = f) expect_error(read_settings("foo"), "Malformed config file") }) + +test_that("rot utility works as intended", { + rot <- lintr:::rot + expect_equal(rot(letters), c(letters[14:26], LETTERS[1:3])) +}) From ee81533e49d6532a9e1f3ff44833bb22119e5e00 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 30 Nov 2020 21:11:15 -0500 Subject: [PATCH 3/3] revert rot test [its in another PR] --- tests/testthat/test-settings.R | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/testthat/test-settings.R b/tests/testthat/test-settings.R index 51c6acf0f..5e731fa99 100644 --- a/tests/testthat/test-settings.R +++ b/tests/testthat/test-settings.R @@ -48,8 +48,3 @@ test_that("it errors if the config file does not end in a newline", { options("lintr.linter_file" = f) expect_error(read_settings("foo"), "Malformed config file") }) - -test_that("rot utility works as intended", { - rot <- lintr:::rot - expect_equal(rot(letters), c(letters[14:26], LETTERS[1:3])) -})