From 6fc557085c7659431057499382ba165bde067c8a Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 7 Aug 2023 12:02:03 -0700 Subject: [PATCH] Add ubiquitously-called internal functions to NAMESPACE (#2031) * keyword_quote_linter for unnecessary quoting * trim_some * import glue * glue::glue -> glue * other glue functions * xml2:: replacement * more namespace importing * ue namespace imports for ubiquitous internal functions * remove test file too * delete from metadata * revert spurious edits to comments * fix whitespace in XPaths * revert inside string * restore :: for seldom-used calls * new :: usage * unimport rare calls --- NAMESPACE | 4 ++ R/T_and_F_symbol_linter.R | 6 +- R/any_duplicated_linter.R | 8 +-- R/any_is_na_linter.R | 2 +- R/assignment_linter.R | 6 +- R/backport_linter.R | 4 +- R/boolean_arithmetic_linter.R | 6 +- R/brace_linter.R | 20 +++---- R/class_equals_linter.R | 4 +- R/commas_linter.R | 4 +- R/comment_linters.R | 4 +- R/condition_message_linter.R | 4 +- R/conjunct_test_linter.R | 6 +- R/consecutive_assertion_linter.R | 2 +- R/declared_functions.R | 2 +- R/duplicate_argument_linter.R | 4 +- R/empty_assignment_linter.R | 2 +- R/equals_na_linter.R | 4 +- R/exclude.R | 2 +- R/expect_comparison_linter.R | 6 +- R/expect_identical_linter.R | 2 +- R/expect_length_linter.R | 2 +- R/expect_named_linter.R | 2 +- R/expect_not_linter.R | 2 +- R/expect_null_linter.R | 2 +- R/expect_s3_class_linter.R | 4 +- R/expect_s4_class_linter.R | 2 +- R/expect_true_false_linter.R | 4 +- R/expect_type_linter.R | 4 +- R/extraction_operator_linter.R | 6 +- R/fixed_regex_linter.R | 6 +- R/for_loop_index_linter.R | 2 +- R/function_argument_linter.R | 2 +- R/function_left_parentheses_linter.R | 8 +-- R/function_return_linter.R | 2 +- R/get_source_expressions.R | 2 +- R/ifelse_censor_linter.R | 8 +-- R/implicit_assignment_linter.R | 4 +- R/implicit_integer_linter.R | 4 +- R/indentation_linter.R | 82 ++++++++++++++++------------ R/infix_spaces_linter.R | 4 +- R/inner_combine_linter.R | 4 +- R/is_numeric_linter.R | 6 +- R/lengths_linter.R | 4 +- R/library_call_linter.R | 2 +- R/lintr-deprecated.R | 10 ++-- R/lintr-package.R | 4 +- R/literal_coercion_linter.R | 6 +- R/matrix_apply_linter.R | 28 +++++----- R/missing_argument_linter.R | 6 +- R/missing_package_linter.R | 4 +- R/namespace_linter.R | 8 +-- R/nested_ifelse_linter.R | 4 +- R/numeric_leading_zero_linter.R | 2 +- R/object_length_linter.R | 4 +- R/object_name_linter.R | 8 +-- R/object_usage_linter.R | 22 ++++---- R/outer_negation_linter.R | 2 +- R/package_hooks_linter.R | 22 ++++---- R/paren_body_linter.R | 2 +- R/paste_linter.R | 8 +-- R/pipe_call_linter.R | 2 +- R/pipe_continuation_linter.R | 2 +- R/redundant_equals_linter.R | 4 +- R/redundant_ifelse_linter.R | 16 +++--- R/regex_subset_linter.R | 8 +-- R/routine_registration_linter.R | 4 +- R/semicolon_linter.R | 4 +- R/seq_linter.R | 8 +-- R/sort_linter.R | 14 ++--- R/spaces_inside_linter.R | 12 ++-- R/spaces_left_parentheses_linter.R | 2 +- R/sprintf_linter.R | 2 +- R/string_boundary_linter.R | 12 ++-- R/strings_as_factors_linter.R | 4 +- R/system_file_linter.R | 4 +- R/trailing_whitespace_linter.R | 6 +- R/undesirable_function_linter.R | 6 +- R/undesirable_operator_linter.R | 4 +- R/unnecessary_concatenation_linter.R | 18 +++--- R/unnecessary_lambda_linter.R | 20 +++---- R/unnecessary_nested_if_linter.R | 2 +- R/unnecessary_placeholder_linter.R | 2 +- R/unreachable_code_linter.R | 4 +- R/unused_import_linter.R | 8 +-- R/utils.R | 24 ++++---- R/vector_logic_linter.R | 2 +- R/with.R | 4 +- R/xml_nodes_to_lints.R | 4 +- R/xp_utils.R | 8 +-- R/yoda_test_linter.R | 6 +- 91 files changed, 322 insertions(+), 304 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index e7b232bde..6a74b097c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -144,6 +144,7 @@ export(xml_nodes_to_lints) export(yoda_test_linter) importFrom(cyclocomp,cyclocomp) importFrom(glue,glue) +importFrom(glue,glue_collapse) importFrom(rex,character_class) importFrom(rex,re_matches) importFrom(rex,re_substitutes) @@ -156,6 +157,9 @@ importFrom(utils,head) importFrom(utils,relist) importFrom(utils,tail) importFrom(xml2,as_list) +importFrom(xml2,xml_attr) importFrom(xml2,xml_find_all) +importFrom(xml2,xml_find_chr) importFrom(xml2,xml_find_first) +importFrom(xml2,xml_find_num) importFrom(xml2,xml_text) diff --git a/R/T_and_F_symbol_linter.R b/R/T_and_F_symbol_linter.R index d5ccd9fd7..068fb46c3 100644 --- a/R/T_and_F_symbol_linter.R +++ b/R/T_and_F_symbol_linter.R @@ -48,11 +48,11 @@ T_and_F_symbol_linter <- function() { # nolint: object_name. return(list()) } - bad_usage <- xml2::xml_find_all(source_expression$xml_parsed_content, usage_xpath) - bad_assignment <- xml2::xml_find_all(source_expression$xml_parsed_content, assignment_xpath) + bad_usage <- xml_find_all(source_expression$xml_parsed_content, usage_xpath) + bad_assignment <- xml_find_all(source_expression$xml_parsed_content, assignment_xpath) make_lints <- function(expr, fmt) { - symbol <- xml2::xml_text(expr) + symbol <- xml_text(expr) lint_message <- sprintf(fmt, replacement_map[symbol], symbol) xml_nodes_to_lints( xml = expr, diff --git a/R/any_duplicated_linter.R b/R/any_duplicated_linter.R index 2c432df63..b7ad6af07 100644 --- a/R/any_duplicated_linter.R +++ b/R/any_duplicated_linter.R @@ -54,7 +54,7 @@ any_duplicated_linter <- function() { # the final parent::expr/expr gets us to the expr on the other side of EQ; # this lets us match on either side of EQ, where following-sibling # assumes we are before EQ, preceding-sibling assumes we are after EQ. - length_unique_xpath_parts <- glue::glue(" + length_unique_xpath_parts <- glue(" //{ c('EQ', 'NE', 'GT', 'LT') } /parent::expr /expr[ @@ -92,7 +92,7 @@ any_duplicated_linter <- function() { xml <- source_expression$xml_parsed_content - any_duplicated_expr <- xml2::xml_find_all(xml, any_duplicated_xpath) + any_duplicated_expr <- xml_find_all(xml, any_duplicated_xpath) any_duplicated_lints <- xml_nodes_to_lints( any_duplicated_expr, source_expression = source_expression, @@ -100,9 +100,9 @@ any_duplicated_linter <- function() { type = "warning" ) - length_unique_expr <- xml2::xml_find_all(xml, length_unique_xpath) + length_unique_expr <- xml_find_all(xml, length_unique_xpath) lint_message <- ifelse( - is.na(xml2::xml_find_first(length_unique_expr, uses_nrow_xpath)), + is.na(xml_find_first(length_unique_expr, uses_nrow_xpath)), "anyDuplicated(x) == 0L is better than length(unique(x)) == length(x).", "anyDuplicated(DF$col) == 0L is better than length(unique(DF$col)) == nrow(DF)" ) diff --git a/R/any_is_na_linter.R b/R/any_is_na_linter.R index 693514259..cd9178a85 100644 --- a/R/any_is_na_linter.R +++ b/R/any_is_na_linter.R @@ -53,7 +53,7 @@ any_is_na_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/assignment_linter.R b/R/assignment_linter.R index f97019d30..01b25ae11 100644 --- a/R/assignment_linter.R +++ b/R/assignment_linter.R @@ -106,12 +106,12 @@ assignment_linter <- function(allow_cascading_assign = TRUE, xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) if (length(bad_expr) == 0L) { return(list()) } - operator <- xml2::xml_text(bad_expr) + operator <- xml_text(bad_expr) lint_message_fmt <- rep("Use <-, not %s, for assignment.", length(operator)) lint_message_fmt[operator %in% c("<<-", "->>")] <- "%s can have hard-to-predict behavior; prefer assigning to a specific environment instead (with assign() or <-)." @@ -119,7 +119,7 @@ assignment_linter <- function(allow_cascading_assign = TRUE, "Avoid the assignment pipe %s; prefer using <- and %%>%% separately." if (!allow_trailing) { - bad_trailing_expr <- xml2::xml_find_all(xml, trailing_assign_xpath) + bad_trailing_expr <- xml_find_all(xml, trailing_assign_xpath) trailing_assignments <- xml2::xml_attrs(bad_expr) %in% xml2::xml_attrs(bad_trailing_expr) lint_message_fmt[trailing_assignments] <- "Assignment %s should not be trailing at the end of a line." } diff --git a/R/backport_linter.R b/R/backport_linter.R index 3aeb86a43..6ab9acbc8 100644 --- a/R/backport_linter.R +++ b/R/backport_linter.R @@ -50,8 +50,8 @@ backport_linter <- function(r_version = getRversion(), except = character()) { xml <- source_expression$xml_parsed_content - all_names_nodes <- xml2::xml_find_all(xml, names_xpath) - all_names <- xml2::xml_text(all_names_nodes) + all_names_nodes <- xml_find_all(xml, names_xpath) + all_names <- xml_text(all_names_nodes) # not sapply/vapply, which may over-simplify to vector # rbind makes sure we have a matrix with dimensions [n_versions x n_names] diff --git a/R/boolean_arithmetic_linter.R b/R/boolean_arithmetic_linter.R index acd80c290..0533c2f0c 100644 --- a/R/boolean_arithmetic_linter.R +++ b/R/boolean_arithmetic_linter.R @@ -34,7 +34,7 @@ boolean_arithmetic_linter <- function() { # TODO(#1581): extend to include all()-alike expressions zero_expr <- "(EQ or NE or GT or LE) and expr[NUM_CONST[text() = '0' or text() = '0L']]" one_expr <- "(LT or GE) and expr[NUM_CONST[text() = '1' or text() = '1L']]" - length_xpath <- glue::glue(" + length_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'which' or text() = 'grep'] /parent::expr /parent::expr @@ -43,7 +43,7 @@ boolean_arithmetic_linter <- function() { and parent::expr[ ({zero_expr}) or ({one_expr})] ] ") - sum_xpath <- glue::glue(" + sum_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'sum'] /parent::expr /parent::expr[ @@ -62,7 +62,7 @@ boolean_arithmetic_linter <- function() { xml <- source_expression$xml_parsed_content - any_expr <- xml2::xml_find_all(xml, any_xpath) + any_expr <- xml_find_all(xml, any_xpath) xml_nodes_to_lints( any_expr, diff --git a/R/brace_linter.R b/R/brace_linter.R index 95db1f649..3cb00eee7 100644 --- a/R/brace_linter.R +++ b/R/brace_linter.R @@ -75,7 +75,7 @@ brace_linter <- function(allow_single_line = FALSE) { )) # TODO (AshesITR): if c_style_braces is TRUE, invert the preceding-sibling condition - xp_open_curly <- glue::glue("//OP-LEFT-BRACE[ + xp_open_curly <- glue("//OP-LEFT-BRACE[ { xp_cond_open } and ( not(@line1 = parent::expr/preceding-sibling::*/@line2) @@ -85,7 +85,7 @@ brace_linter <- function(allow_single_line = FALSE) { xp_open_preceding <- "parent::expr/preceding-sibling::*[1][self::OP-RIGHT-PAREN or self::ELSE or self::REPEAT]" - xp_paren_brace <- glue::glue("//OP-LEFT-BRACE[ + xp_paren_brace <- glue("//OP-LEFT-BRACE[ @line1 = { xp_open_preceding }/@line1 and @col1 = { xp_open_preceding }/@col2 + 1 ]") @@ -110,7 +110,7 @@ brace_linter <- function(allow_single_line = FALSE) { )) # TODO (AshesITR): if c_style_braces is TRUE, skip the not(ELSE) condition - xp_closed_curly <- glue::glue("//OP-RIGHT-BRACE[ + xp_closed_curly <- glue("//OP-RIGHT-BRACE[ { xp_cond_closed } and ( (@line1 = preceding-sibling::*[1][not(self::OP-LEFT-BRACE)]/@line2) @@ -122,7 +122,7 @@ brace_linter <- function(allow_single_line = FALSE) { # need to (?) repeat previous_curly_path since != will return true if there is # no such node. ditto for approach with not(@line1 = ...). # TODO (AshesITR): if c_style_braces is TRUE, this needs to be @line2 + 1 - xp_else_same_line <- glue::glue("//ELSE[{xp_else_closed_curly} and @line1 != {xp_else_closed_curly}/@line2]") + xp_else_same_line <- glue("//ELSE[{xp_else_closed_curly} and @line1 != {xp_else_closed_curly}/@line2]") xp_function_brace <- "//FUNCTION/parent::expr[@line1 != @line2 and not(expr[OP-LEFT-BRACE])]" @@ -157,7 +157,7 @@ brace_linter <- function(allow_single_line = FALSE) { lints <- c( lints, xml_nodes_to_lints( - xml2::xml_find_all(xml, xp_open_curly), + xml_find_all(xml, xp_open_curly), source_expression = source_expression, lint_message = "Opening curly braces should never go on their own line and should always be followed by a new line." @@ -167,7 +167,7 @@ brace_linter <- function(allow_single_line = FALSE) { lints <- c( lints, xml_nodes_to_lints( - xml2::xml_find_all(xml, xp_paren_brace), + xml_find_all(xml, xp_paren_brace), source_expression = source_expression, lint_message = "There should be a space before an opening curly brace." ) @@ -176,7 +176,7 @@ brace_linter <- function(allow_single_line = FALSE) { lints <- c( lints, xml_nodes_to_lints( - xml2::xml_find_all(xml, xp_closed_curly), + xml_find_all(xml, xp_closed_curly), source_expression = source_expression, lint_message = "Closing curly-braces should always be on their own line, unless they are followed by an else." @@ -186,7 +186,7 @@ brace_linter <- function(allow_single_line = FALSE) { lints <- c( lints, xml_nodes_to_lints( - xml2::xml_find_all(xml, xp_else_same_line), + xml_find_all(xml, xp_else_same_line), source_expression = source_expression, lint_message = "`else` should come on the same line as the previous `}`." ) @@ -195,7 +195,7 @@ brace_linter <- function(allow_single_line = FALSE) { lints <- c( lints, xml_nodes_to_lints( - xml2::xml_find_all(xml, xp_function_brace), + xml_find_all(xml, xp_function_brace), source_expression = source_expression, lint_message = "Any function spanning multiple lines should use curly braces." ) @@ -204,7 +204,7 @@ brace_linter <- function(allow_single_line = FALSE) { lints <- c( lints, xml_nodes_to_lints( - xml2::xml_find_all(xml, xp_if_else_match_brace), + xml_find_all(xml, xp_if_else_match_brace), source_expression = source_expression, lint_message = "Either both or neither branch in `if`/`else` should use curly braces." ) diff --git a/R/class_equals_linter.R b/R/class_equals_linter.R index 33a4d4960..e5b795022 100644 --- a/R/class_equals_linter.R +++ b/R/class_equals_linter.R @@ -51,9 +51,9 @@ class_equals_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) - operator <- xml2::xml_find_chr(bad_expr, "string(*[2])") + operator <- xml_find_chr(bad_expr, "string(*[2])") lint_message <- sprintf( "Instead of comparing class(x) with %s, use inherits(x, 'class-name') or is. or is(x, 'class')", operator diff --git a/R/commas_linter.R b/R/commas_linter.R index bb9023688..d7ff5ac14 100644 --- a/R/commas_linter.R +++ b/R/commas_linter.R @@ -67,7 +67,7 @@ commas_linter <- function() { xml <- source_expression$xml_parsed_content before_lints <- xml_nodes_to_lints( - xml2::xml_find_all(xml, xpath_before), + xml_find_all(xml, xpath_before), source_expression = source_expression, lint_message = "Commas should never have a space before.", range_start_xpath = "number(./preceding-sibling::*[1]/@col2 + 1)", # start after preceding expression @@ -75,7 +75,7 @@ commas_linter <- function() { ) after_lints <- xml_nodes_to_lints( - xml2::xml_find_all(xml, xpath_after), + xml_find_all(xml, xpath_after), source_expression = source_expression, lint_message = "Commas should always have a space after.", range_start_xpath = "number(./@col2 + 1)", # start and end after comma diff --git a/R/comment_linters.R b/R/comment_linters.R index 007588171..01470fbd5 100644 --- a/R/comment_linters.R +++ b/R/comment_linters.R @@ -79,8 +79,8 @@ commented_code_linter <- function() { if (!is_lint_level(source_expression, "file")) { return(list()) } - all_comment_nodes <- xml2::xml_find_all(source_expression$full_xml_parsed_content, "//COMMENT") - all_comments <- xml2::xml_text(all_comment_nodes) + all_comment_nodes <- xml_find_all(source_expression$full_xml_parsed_content, "//COMMENT") + all_comments <- xml_text(all_comment_nodes) code_candidates <- re_matches(all_comments, code_candidate_regex, global = FALSE, locations = TRUE) extracted_code <- code_candidates[, "code"] # ignore trailing ',' when testing for parsability diff --git a/R/condition_message_linter.R b/R/condition_message_linter.R index 06e38ca56..8f5ea2987 100644 --- a/R/condition_message_linter.R +++ b/R/condition_message_linter.R @@ -42,7 +42,7 @@ #' @export condition_message_linter <- function() { translators <- c("packageStartupMessage", "message", "warning", "stop") - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ ({xp_text_in_table(translators)}) and not(preceding-sibling::OP-DOLLAR or preceding-sibling::OP-AT) @@ -62,7 +62,7 @@ condition_message_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) sep_value <- get_r_string(bad_expr, xpath = "./expr/SYMBOL_SUB[text() = 'sep']/following-sibling::expr/STR_CONST") bad_expr <- bad_expr[is.na(sep_value) | sep_value %in% c("", " ")] diff --git a/R/conjunct_test_linter.R b/R/conjunct_test_linter.R index 6c81f1ade..76ecd4653 100644 --- a/R/conjunct_test_linter.R +++ b/R/conjunct_test_linter.R @@ -48,7 +48,7 @@ conjunct_test_linter <- function(allow_named_stopifnot = TRUE) { /following-sibling::expr[1][AND2] " named_stopifnot_condition <- if (allow_named_stopifnot) "and not(preceding-sibling::*[1][self::EQ_SUB])" else "" - stopifnot_xpath <- glue::glue(" + stopifnot_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'stopifnot'] /parent::expr /following-sibling::expr[1][AND2 {named_stopifnot_condition}] @@ -72,14 +72,14 @@ conjunct_test_linter <- function(allow_named_stopifnot = TRUE) { xml <- source_expression$full_xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) if (length(bad_expr) == 0L) { return(list()) } matched_fun <- xp_call_name(bad_expr) - operator <- xml2::xml_find_chr(bad_expr, "string(expr/*[self::AND2 or self::OR2])") + operator <- xml_find_chr(bad_expr, "string(expr/*[self::AND2 or self::OR2])") replacement_fmt <- ifelse( matched_fun %in% c("expect_true", "expect_false"), "write multiple expectations like %1$s(A) and %1$s(B)", diff --git a/R/consecutive_assertion_linter.R b/R/consecutive_assertion_linter.R index 3a83584cf..22042f754 100644 --- a/R/consecutive_assertion_linter.R +++ b/R/consecutive_assertion_linter.R @@ -55,7 +55,7 @@ consecutive_assertion_linter <- function() { xml <- source_expression$full_xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_function <- xp_call_name(bad_expr) xml_nodes_to_lints( diff --git a/R/declared_functions.R b/R/declared_functions.R index 1e8849060..0096946b9 100644 --- a/R/declared_functions.R +++ b/R/declared_functions.R @@ -15,5 +15,5 @@ declared_s3_generics <- function(x) { "/expr/SYMBOL/text()" ) - as.character(xml2::xml_find_all(x, xpath)) + as.character(xml_find_all(x, xpath)) } diff --git a/R/duplicate_argument_linter.R b/R/duplicate_argument_linter.R index 61d166ef8..4c1bd32fe 100644 --- a/R/duplicate_argument_linter.R +++ b/R/duplicate_argument_linter.R @@ -47,14 +47,14 @@ duplicate_argument_linter <- function(except = c("mutate", "transmute")) { xml <- source_expression$full_xml_parsed_content - calls <- xml2::xml_find_all(xml, xpath_call_with_args) + calls <- xml_find_all(xml, xpath_call_with_args) if (length(except) > 0L) { calls_text <- get_r_string(xp_call_name(calls)) calls <- calls[!(calls_text %in% except)] } - all_arg_nodes <- lapply(calls, xml2::xml_find_all, xpath_arg_name) + all_arg_nodes <- lapply(calls, xml_find_all, xpath_arg_name) arg_names <- lapply(all_arg_nodes, get_r_string) is_duplicated <- lapply(arg_names, duplicated) diff --git a/R/empty_assignment_linter.R b/R/empty_assignment_linter.R index 36a340e67..fbb2338ef 100644 --- a/R/empty_assignment_linter.R +++ b/R/empty_assignment_linter.R @@ -49,7 +49,7 @@ empty_assignment_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/equals_na_linter.R b/R/equals_na_linter.R index fb36d3d94..16af7e1b9 100644 --- a/R/equals_na_linter.R +++ b/R/equals_na_linter.R @@ -32,7 +32,7 @@ equals_na_linter <- function() { na_table <- xp_text_in_table(c("NA", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_")) - xpath <- glue::glue(" + xpath <- glue(" //NUM_CONST[ {na_table} ] /parent::expr /parent::expr[EQ or NE] @@ -45,7 +45,7 @@ equals_na_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/exclude.R b/R/exclude.R index 0dc3d0605..cf41704b4 100644 --- a/R/exclude.R +++ b/R/exclude.R @@ -181,7 +181,7 @@ add_exclusions <- function(exclusions, lines, linters_string, exclude_linter_sep bad <- excluded_linters[!matched] warning( "Could not find linter", if (length(bad) > 1L) "s" else "", " named ", - glue::glue_collapse(sQuote(bad), sep = ", ", last = " and "), + glue_collapse(sQuote(bad), sep = ", ", last = " and "), " in the list of active linters. Make sure the linter is uniquely identified by the given name or prefix." ) } diff --git a/R/expect_comparison_linter.R b/R/expect_comparison_linter.R index e5ae53f11..e9801bf35 100644 --- a/R/expect_comparison_linter.R +++ b/R/expect_comparison_linter.R @@ -50,7 +50,7 @@ expect_comparison_linter <- function() { # != doesn't have a clean replacement comparator_nodes <- setdiff(as.list(infix_metadata$xml_tag[infix_metadata$comparator]), "NE") - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'expect_true'] /parent::expr /following-sibling::expr[ {xp_or(comparator_nodes)} ] @@ -70,9 +70,9 @@ expect_comparison_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) - comparator <- xml2::xml_find_chr(bad_expr, "string(expr[2]/*[2])") + comparator <- xml_find_chr(bad_expr, "string(expr[2]/*[2])") expectation <- comparator_expectation_map[comparator] lint_message <- sprintf("%s(x, y) is better than expect_true(x %s y).", expectation, comparator) xml_nodes_to_lints(bad_expr, source_expression, lint_message = lint_message, type = "warning") diff --git a/R/expect_identical_linter.R b/R/expect_identical_linter.R index 4e3146259..0202e08dd 100644 --- a/R/expect_identical_linter.R +++ b/R/expect_identical_linter.R @@ -88,7 +88,7 @@ expect_identical_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, source_expression = source_expression, diff --git a/R/expect_length_linter.R b/R/expect_length_linter.R index a4dd3ad58..bebac9484 100644 --- a/R/expect_length_linter.R +++ b/R/expect_length_linter.R @@ -39,7 +39,7 @@ expect_length_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_function <- xp_call_name(bad_expr) lint_message <- sprintf("expect_length(x, n) is better than %s(length(x), n)", matched_function) xml_nodes_to_lints(bad_expr, source_expression, lint_message, type = "warning") diff --git a/R/expect_named_linter.R b/R/expect_named_linter.R index 9bb82e254..19328aa2b 100644 --- a/R/expect_named_linter.R +++ b/R/expect_named_linter.R @@ -48,7 +48,7 @@ expect_named_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_function <- xp_call_name(bad_expr) lint_message <- sprintf("expect_named(x, n) is better than %s(names(x), n)", matched_function) diff --git a/R/expect_not_linter.R b/R/expect_not_linter.R index cd0099e38..22826ceb7 100644 --- a/R/expect_not_linter.R +++ b/R/expect_not_linter.R @@ -37,7 +37,7 @@ expect_not_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/expect_null_linter.R b/R/expect_null_linter.R index a82a7d648..dba609b2c 100644 --- a/R/expect_null_linter.R +++ b/R/expect_null_linter.R @@ -60,7 +60,7 @@ expect_null_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_function <- xp_call_name(bad_expr) msg <- ifelse( diff --git a/R/expect_s3_class_linter.R b/R/expect_s3_class_linter.R index 3a71a25e4..28e536e45 100644 --- a/R/expect_s3_class_linter.R +++ b/R/expect_s3_class_linter.R @@ -46,7 +46,7 @@ expect_s3_class_linter <- function() { /parent::expr[not(SYMBOL_SUB[text() = 'info' or text() = 'label' or text() = 'expected.label'])] " is_class_call <- xp_text_in_table(c(is_s3_class_calls, "inherits")) - expect_true_xpath <- glue::glue(" + expect_true_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'expect_true'] /parent::expr /following-sibling::expr[1][expr[1][SYMBOL_FUNCTION_CALL[ {is_class_call} ]]] @@ -61,7 +61,7 @@ expect_s3_class_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_function <- xp_call_name(bad_expr) msg <- ifelse( matched_function %in% c("expect_equal", "expect_identical"), diff --git a/R/expect_s4_class_linter.R b/R/expect_s4_class_linter.R index 6c4d16b8d..3b76ee64a 100644 --- a/R/expect_s4_class_linter.R +++ b/R/expect_s4_class_linter.R @@ -42,7 +42,7 @@ expect_s4_class_linter <- function() { # TODO(michaelchirico): also catch expect_{equal,identical}(methods::is(x), k). # this seems empirically rare, but didn't check many S4-heavy packages. - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, source_expression = source_expression, diff --git a/R/expect_true_false_linter.R b/R/expect_true_false_linter.R index 0712acc77..e01302176 100644 --- a/R/expect_true_false_linter.R +++ b/R/expect_true_false_linter.R @@ -46,11 +46,11 @@ expect_true_false_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) # NB: use expr/$node, not expr[$node], to exclude other things (especially ns:: parts of the call) call_name <- xp_call_name(bad_expr, condition = "starts-with(text(), 'expect_')") - truth_value <- xml2::xml_find_chr(bad_expr, "string(expr/NUM_CONST[text() = 'TRUE' or text() = 'FALSE'])") + truth_value <- xml_find_chr(bad_expr, "string(expr/NUM_CONST[text() = 'TRUE' or text() = 'FALSE'])") lint_message <- sprintf( "expect_%s(x) is better than %s(x, %s)", tolower(truth_value), call_name, truth_value diff --git a/R/expect_type_linter.R b/R/expect_type_linter.R index 028403b70..3f0bf9d1d 100644 --- a/R/expect_type_linter.R +++ b/R/expect_type_linter.R @@ -37,7 +37,7 @@ expect_type_linter <- function() { ] /parent::expr[not(SYMBOL_SUB[text() = 'info' or text() = 'label' or text() = 'expected.label'])] " - expect_true_xpath <- glue::glue(" + expect_true_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'expect_true'] /parent::expr /following-sibling::expr[1][expr[1][SYMBOL_FUNCTION_CALL[ {base_type_tests} ]]] @@ -52,7 +52,7 @@ expect_type_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_function <- xp_call_name(bad_expr) msg <- ifelse( matched_function %in% c("expect_equal", "expect_identical"), diff --git a/R/extraction_operator_linter.R b/R/extraction_operator_linter.R index d59112da0..0a59becae 100644 --- a/R/extraction_operator_linter.R +++ b/R/extraction_operator_linter.R @@ -52,7 +52,7 @@ #' @export extraction_operator_linter <- function() { constant_nodes_in_brackets <- paste0("self::", c("expr", "OP-PLUS", "NUM_CONST", "STR_CONST")) - xpath <- glue::glue(" + xpath <- glue(" //OP-DOLLAR[not(preceding-sibling::expr[1]/SYMBOL[text() = 'self' or text() = '.self'])] | //OP-LEFT-BRACKET[ @@ -67,8 +67,8 @@ extraction_operator_linter <- function() { } xml <- source_expression$xml_parsed_content - bad_exprs <- xml2::xml_find_all(xml, xpath) - msgs <- sprintf("Use `[[` instead of `%s` to extract an element.", xml2::xml_text(bad_exprs)) + bad_exprs <- xml_find_all(xml, xpath) + msgs <- sprintf("Use `[[` instead of `%s` to extract an element.", xml_text(bad_exprs)) xml_nodes_to_lints( bad_exprs, diff --git a/R/fixed_regex_linter.R b/R/fixed_regex_linter.R index 31edc57ea..7725c77c4 100644 --- a/R/fixed_regex_linter.R +++ b/R/fixed_regex_linter.R @@ -87,7 +87,7 @@ fixed_regex_linter <- function() { # NB: strsplit doesn't have an ignore.case argument # NB: we intentionally exclude cases like gsub(x, c("a" = "b")), where "b" is fixed - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {pos_1_regex_funs} ] /parent::expr[ not(following-sibling::SYMBOL_SUB[ @@ -114,12 +114,12 @@ fixed_regex_linter <- function() { xml <- source_expression$xml_parsed_content - patterns <- xml2::xml_find_all(xml, xpath) + patterns <- xml_find_all(xml, xpath) pattern_strings <- get_r_string(patterns) is_static <- is_not_regex(pattern_strings) fixed_equivalent <- encodeString(get_fixed_string(pattern_strings[is_static]), quote = '"', justify = "none") - call_name <- xml2::xml_find_chr(patterns[is_static], "string(preceding-sibling::expr[last()]/SYMBOL_FUNCTION_CALL)") + call_name <- xml_find_chr(patterns[is_static], "string(preceding-sibling::expr[last()]/SYMBOL_FUNCTION_CALL)") is_stringr <- startsWith(call_name, "str_") replacement <- ifelse( diff --git a/R/for_loop_index_linter.R b/R/for_loop_index_linter.R index c88d814c5..154a2cf9f 100644 --- a/R/for_loop_index_linter.R +++ b/R/for_loop_index_linter.R @@ -46,7 +46,7 @@ for_loop_index_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/function_argument_linter.R b/R/function_argument_linter.R index 63cb5da32..a0b92e303 100644 --- a/R/function_argument_linter.R +++ b/R/function_argument_linter.R @@ -62,7 +62,7 @@ function_argument_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/function_left_parentheses_linter.R b/R/function_left_parentheses_linter.R index 99f30f5c8..e19db0d8b 100644 --- a/R/function_left_parentheses_linter.R +++ b/R/function_left_parentheses_linter.R @@ -64,21 +64,21 @@ function_left_parentheses_linter <- function() { # nolint: object_length. xml <- source_expression$xml_parsed_content - bad_line_fun_exprs <- xml2::xml_find_all(xml, bad_line_fun_xpath) + bad_line_fun_exprs <- xml_find_all(xml, bad_line_fun_xpath) bad_line_fun_lints <- xml_nodes_to_lints( bad_line_fun_exprs, source_expression = source_expression, lint_message = "Left parenthesis should be on the same line as the 'function' symbol." ) - bad_line_call_exprs <- xml2::xml_find_all(xml, bad_line_call_xpath) + bad_line_call_exprs <- xml_find_all(xml, bad_line_call_xpath) bad_line_call_lints <- xml_nodes_to_lints( bad_line_call_exprs, source_expression = source_expression, lint_message = "Left parenthesis should be on the same line as the function's symbol." ) - bad_col_fun_exprs <- xml2::xml_find_all(xml, bad_col_fun_xpath) + bad_col_fun_exprs <- xml_find_all(xml, bad_col_fun_xpath) bad_col_fun_lints <- xml_nodes_to_lints( bad_col_fun_exprs, source_expression = source_expression, @@ -87,7 +87,7 @@ function_left_parentheses_linter <- function() { # nolint: object_length. range_end_xpath = "number(./following-sibling::OP-LEFT-PAREN/@col1 - 1)" # end before ( ) - bad_col_call_exprs <- xml2::xml_find_all(xml, bad_col_call_xpath) + bad_col_call_exprs <- xml_find_all(xml, bad_col_call_xpath) bad_col_call_lints <- xml_nodes_to_lints( bad_col_call_exprs, source_expression = source_expression, diff --git a/R/function_return_linter.R b/R/function_return_linter.R index 6d325db08..252cca808 100644 --- a/R/function_return_linter.R +++ b/R/function_return_linter.R @@ -68,7 +68,7 @@ function_return_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/get_source_expressions.R b/R/get_source_expressions.R index 736bdd65e..6030b3b19 100644 --- a/R/get_source_expressions.R +++ b/R/get_source_expressions.R @@ -101,7 +101,7 @@ get_source_expressions <- function(filename, lines = NULL) { if (!is.null(xml_parsed_content) && !is.na(xml_parsed_content)) { expression_xmls <- lapply( - xml2::xml_find_all(xml_parsed_content, "/exprlist/*"), + xml_find_all(xml_parsed_content, "/exprlist/*"), function(top_level_expr) xml2::xml_add_parent(xml2::xml_new_root(top_level_expr), "exprlist") ) for (i in seq_along(expressions)) { diff --git a/R/ifelse_censor_linter.R b/R/ifelse_censor_linter.R index 46488507d..b574303df 100644 --- a/R/ifelse_censor_linter.R +++ b/R/ifelse_censor_linter.R @@ -35,7 +35,7 @@ #' @seealso [linters] for a complete list of linters available in lintr. #' @export ifelse_censor_linter <- function() { - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(ifelse_funs)} ] /parent::expr /following-sibling::expr[ @@ -53,11 +53,11 @@ ifelse_censor_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_call <- xp_call_name(bad_expr) - operator <- xml2::xml_find_chr(bad_expr, "string(expr[2]/*[2])") - match_first <- !is.na(xml2::xml_find_first(bad_expr, "expr[2][expr[1] = following-sibling::expr[1]]")) + operator <- xml_find_chr(bad_expr, "string(expr[2]/*[2])") + match_first <- !is.na(xml_find_first(bad_expr, "expr[2][expr[1] = following-sibling::expr[1]]")) optimizer <- ifelse((operator %in% c("<", "<=")) == match_first, "pmin", "pmax") first_var <- rep_len("x", length(match_first)) second_var <- rep_len("y", length(match_first)) diff --git a/R/implicit_assignment_linter.R b/R/implicit_assignment_linter.R index b68fba71f..6978f52a4 100644 --- a/R/implicit_assignment_linter.R +++ b/R/implicit_assignment_linter.R @@ -41,7 +41,7 @@ implicit_assignment_linter <- function(except = c("bquote", "expression", "expr" if (length(except) > 0L) { exceptions <- xp_text_in_table(except) - xpath_exceptions <- glue::glue(" + xpath_exceptions <- glue(" //SYMBOL_FUNCTION_CALL[ not({exceptions}) ]") } else { xpath_exceptions <- " @@ -101,7 +101,7 @@ implicit_assignment_linter <- function(except = c("bquote", "expression", "expr" xml <- source_expression$full_xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) lint_message <- paste( "Avoid implicit assignments in function calls.", diff --git a/R/implicit_integer_linter.R b/R/implicit_integer_linter.R index df791528b..76a407cd1 100644 --- a/R/implicit_integer_linter.R +++ b/R/implicit_integer_linter.R @@ -58,10 +58,10 @@ implicit_integer_linter <- function(allow_colon = FALSE) { xml <- source_expression$full_xml_parsed_content - numbers <- xml2::xml_find_all(xml, xpath) + numbers <- xml_find_all(xml, xpath) xml_nodes_to_lints( - numbers[is_implicit_integer(xml2::xml_text(numbers))], + numbers[is_implicit_integer(xml_text(numbers))], source_expression = source_expression, lint_message = "Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.", type = "style", diff --git a/R/indentation_linter.R b/R/indentation_linter.R index 5d369f4ab..6c0fd8164 100644 --- a/R/indentation_linter.R +++ b/R/indentation_linter.R @@ -142,16 +142,16 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al if (isTRUE(assignment_as_infix)) { suppressing_tokens <- c("LEFT_ASSIGN", "EQ_ASSIGN", "EQ_SUB", "EQ_FORMALS") - xp_suppress <- glue::glue("preceding-sibling::{suppressing_tokens}[{xp_last_on_line}]") + xp_suppress <- glue("preceding-sibling::{suppressing_tokens}[{xp_last_on_line}]") restoring_tokens <- c("expr[SYMBOL_FUNCTION_CALL]", "OP-LEFT-BRACE") - xp_restore <- glue::glue("preceding-sibling::{restoring_tokens}") + xp_restore <- glue("preceding-sibling::{restoring_tokens}") # match the first ancestor expr that is either # * a suppressing token (<- or =) or # * a restoring token (braces or a function call) # suppress the indent if the matched ancestor is a suppressing token - infix_condition <- glue::glue(" + infix_condition <- glue(" and not(ancestor::expr[{xp_or(c(xp_suppress, xp_restore))}][1][{xp_or(xp_suppress)}]) ") } else { @@ -162,13 +162,22 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al "number(", paste( c( - glue::glue("self::{paren_tokens_left}/following-sibling::{paren_tokens_right}/preceding-sibling::*[1]/@line2"), - glue::glue("self::*[{xp_and(paste0('not(self::', paren_tokens_left, ')'))}] - /following-sibling::SYMBOL_FUNCTION_CALL/parent::expr/following-sibling::expr[1]/@line2"), - glue::glue("self::*[ - {xp_and(paste0('not(self::', paren_tokens_left, ')'))} and - not(following-sibling::SYMBOL_FUNCTION_CALL) - ]/following-sibling::*[not(self::COMMENT)][1]/@line2") + glue("self::{paren_tokens_left}/following-sibling::{paren_tokens_right}/preceding-sibling::*[1]/@line2"), + glue(" + self::*[{xp_and(paste0('not(self::', paren_tokens_left, ')'))}] + /following-sibling::SYMBOL_FUNCTION_CALL + /parent::expr + /following-sibling::expr[1] + /@line2 + "), + glue(" + self::*[ + {xp_and(paste0('not(self::', paren_tokens_left, ')'))} + and not(following-sibling::SYMBOL_FUNCTION_CALL) + ] + /following-sibling::*[not(self::COMMENT)][1] + /@line2 + ") ), collapse = " | " ), @@ -178,16 +187,21 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al global_nodes <- function(nodes) paste0("//", nodes, collapse = "|") xp_indent_changes <- paste( c( - glue::glue("//{paren_tokens_left}[not(@line1 = following-sibling::expr[ - @line2 > @line1 and - ({xp_or(paste0('descendant::', paren_tokens_left, '[', xp_last_on_line, ']'))}) - ]/@line1)]"), + glue("//{paren_tokens_left}[not( + @line1 = following-sibling::expr[ + @line2 > @line1 and + ({xp_or(paste0('descendant::', paren_tokens_left, '[', xp_last_on_line, ']'))}) + ]/@line1 + )]"), glue::glue("({ global_nodes(infix_tokens) })[{xp_last_on_line}{infix_condition}]"), glue::glue("({ global_nodes(no_paren_keywords) })[{xp_last_on_line}]"), - glue::glue("({ global_nodes(keyword_tokens) })/following-sibling::OP-RIGHT-PAREN[ - {xp_last_on_line} and - not(following-sibling::expr[1][OP-LEFT-BRACE]) - ]") + glue::glue(" + ({ global_nodes(keyword_tokens) }) + /following-sibling::OP-RIGHT-PAREN[ + {xp_last_on_line} and + not(following-sibling::expr[1][OP-LEFT-BRACE]) + ] + ") ), collapse = " | " ) @@ -220,29 +234,29 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al expected_indent_levels <- integer(length(indent_levels)) is_hanging <- logical(length(indent_levels)) - indent_changes <- xml2::xml_find_all(xml, xp_indent_changes) + indent_changes <- xml_find_all(xml, xp_indent_changes) for (change in indent_changes) { change_type <- find_indent_type(change) - change_begin <- as.integer(xml2::xml_attr(change, "line1")) + 1L - change_end <- xml2::xml_find_num(change, xp_block_ends) + change_begin <- as.integer(xml_attr(change, "line1")) + 1L + change_end <- xml_find_num(change, xp_block_ends) if (isTRUE(change_begin <= change_end)) { to_indent <- seq(from = change_begin, to = change_end) expected_indent_levels[to_indent] <- find_new_indent( current_indent = expected_indent_levels[to_indent], change_type = change_type, indent = indent, - hanging_indent = as.integer(xml2::xml_attr(change, "col2")) + hanging_indent = as.integer(xml_attr(change, "col2")) ) is_hanging[to_indent] <- change_type == "hanging" } } in_str_const <- logical(length(indent_levels)) - multiline_strings <- xml2::xml_find_all(xml, xp_multiline_string) + multiline_strings <- xml_find_all(xml, xp_multiline_string) for (string in multiline_strings) { is_in_str <- seq( - from = as.integer(xml2::xml_attr(string, "line1")) + 1L, - to = as.integer(xml2::xml_attr(string, "line2")) + from = as.integer(xml_attr(string, "line1")) + 1L, + to = as.integer(xml_attr(string, "line2")) ) in_str_const[is_in_str] <- TRUE } @@ -339,7 +353,7 @@ build_indentation_style_tidy <- function() { " xp_suppress <- paste( - glue::glue(" + glue(" self::{paren_tokens_left}[ @line1 = following-sibling::{paren_tokens_right}/{xp_inner_expr}[position() = 1]/@line1 ]/following-sibling::{paren_tokens_right}[ @@ -351,20 +365,20 @@ build_indentation_style_tidy <- function() { xp_is_not_hanging <- paste( c( - glue::glue( + glue( "self::{paren_tokens_left}/following-sibling::{paren_tokens_right}[@line1 > preceding-sibling::*[1]/@line2]" ), - glue::glue("self::*[{xp_and(paste0('not(self::', paren_tokens_left, ')'))} and {xp_last_on_line}]") + glue("self::*[{xp_and(paste0('not(self::', paren_tokens_left, ')'))} and {xp_last_on_line}]") ), collapse = " | " ) function(change) { - if (length(xml2::xml_find_first(change, xp_is_double_indent)) > 0L) { + if (length(xml_find_first(change, xp_is_double_indent)) > 0L) { "double" - } else if (length(xml2::xml_find_first(change, xp_suppress)) > 0L) { + } else if (length(xml_find_first(change, xp_suppress)) > 0L) { "suppress" - } else if (length(xml2::xml_find_first(change, xp_is_not_hanging)) == 0L) { + } else if (length(xml_find_first(change, xp_is_not_hanging)) == 0L) { "hanging" } else { "block" @@ -379,17 +393,17 @@ build_indentation_style_always <- function() { xp_is_not_hanging <- paste( c( - glue::glue(" + glue(" self::{paren_tokens_left}[{xp_last_on_line}]/ following-sibling::{paren_tokens_right}[@line1 > preceding-sibling::*[1]/@line2] "), - glue::glue("self::*[{xp_and(paste0('not(self::', paren_tokens_left, ')'))} and {xp_last_on_line}]") + glue("self::*[{xp_and(paste0('not(self::', paren_tokens_left, ')'))} and {xp_last_on_line}]") ), collapse = " | " ) function(change) { - if (length(xml2::xml_find_first(change, xp_is_not_hanging)) == 0L) { + if (length(xml_find_first(change, xp_is_not_hanging)) == 0L) { "hanging" } else { "block" diff --git a/R/infix_spaces_linter.R b/R/infix_spaces_linter.R index 2569a2f56..ed9590310 100644 --- a/R/infix_spaces_linter.R +++ b/R/infix_spaces_linter.R @@ -148,7 +148,7 @@ infix_spaces_linter <- function(exclude_operators = NULL, allow_multiple_spaces # SYMBOL_SUB for case with missing argument like alist(a =) # NB: the last not() disables lints inside box::use() declarations global_xpath <- paste0("//", infix_tokens, collapse = "|") - xpath <- glue::glue("({global_xpath})[ + xpath <- glue("({global_xpath})[ parent::*[count(expr | SYMBOL_SUB) > 1] and ( ( @@ -175,7 +175,7 @@ infix_spaces_linter <- function(exclude_operators = NULL, allow_multiple_spaces } xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/inner_combine_linter.R b/R/inner_combine_linter.R index 607e61bbe..2b1f22af8 100644 --- a/R/inner_combine_linter.R +++ b/R/inner_combine_linter.R @@ -76,7 +76,7 @@ inner_combine_linter <- function() { log_args_cond, lubridate_args_cond ) - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'c'] /parent::expr[count(following-sibling::expr) > 1] /following-sibling::expr[1][ {c_expr_cond} ] @@ -90,7 +90,7 @@ inner_combine_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_call <- xp_call_name(bad_expr, depth = 2L) lint_message <- paste( diff --git a/R/is_numeric_linter.R b/R/is_numeric_linter.R index 4db1b1cd6..dcfe670c0 100644 --- a/R/is_numeric_linter.R +++ b/R/is_numeric_linter.R @@ -43,7 +43,7 @@ is_numeric_linter <- function() { is_integer_expr <- "expr[1][SYMBOL_FUNCTION_CALL[text() = 'is.integer']]" # testing things like is.numeric(x) || is.integer(x) - or_xpath <- glue::glue(" + or_xpath <- glue(" //OR2 /parent::expr[ expr/{is_numeric_expr} @@ -76,7 +76,7 @@ is_numeric_linter <- function() { xml <- source_expression$xml_parsed_content - or_expr <- xml2::xml_find_all(xml, or_xpath) + or_expr <- xml_find_all(xml, or_xpath) or_lints <- xml_nodes_to_lints( or_expr, source_expression = source_expression, @@ -87,7 +87,7 @@ is_numeric_linter <- function() { type = "warning" ) - class_expr <- xml2::xml_find_all(xml, class_xpath) + class_expr <- xml_find_all(xml, class_xpath) if (length(class_expr) > 0L) { class_strings <- c( get_r_string(class_expr, "expr[2]/expr[2]/STR_CONST"), diff --git a/R/lengths_linter.R b/R/lengths_linter.R index 5f37bc3f2..48a04230a 100644 --- a/R/lengths_linter.R +++ b/R/lengths_linter.R @@ -32,7 +32,7 @@ #' @export lengths_linter <- function() { loop_funs <- c("sapply", "vapply", "map_int", "map_dbl") - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(loop_funs)} ] /parent::expr /parent::expr[expr/SYMBOL[text() = 'length']] @@ -45,7 +45,7 @@ lengths_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/library_call_linter.R b/R/library_call_linter.R index 52411f8fe..0fda85eee 100644 --- a/R/library_call_linter.R +++ b/R/library_call_linter.R @@ -59,7 +59,7 @@ library_call_linter <- function() { xml <- source_expression$full_xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) if (length(bad_expr) == 0L) { return(list()) diff --git a/R/lintr-deprecated.R b/R/lintr-deprecated.R index 27f3a4383..1037ad912 100644 --- a/R/lintr-deprecated.R +++ b/R/lintr-deprecated.R @@ -51,7 +51,7 @@ closed_curly_linter <- function(allow_single_line = FALSE) { )" )) - xpath <- glue::glue("//OP-RIGHT-BRACE[ + xpath <- glue("//OP-RIGHT-BRACE[ { xp_cond_closed } and ( (@line1 = preceding-sibling::*[1]/@line2) or (@line1 = parent::expr/following-sibling::*[1][not(self::ELSE)]/@line1) @@ -64,7 +64,7 @@ closed_curly_linter <- function(allow_single_line = FALSE) { } xml_nodes_to_lints( - xml2::xml_find_all(source_expression$xml_parsed_content, xpath), + xml_find_all(source_expression$xml_parsed_content, xpath), source_expression = source_expression, lint_message = "Closing curly-braces should always be on their own line, unless they are followed by an else." ) @@ -109,7 +109,7 @@ open_curly_linter <- function(allow_single_line = FALSE) { xml <- source_expression$xml_parsed_content - expr_before <- xml2::xml_find_all(xml, xpath_before) + expr_before <- xml_find_all(xml, xpath_before) lints_before <- xml_nodes_to_lints( expr_before, source_expression = source_expression, @@ -117,7 +117,7 @@ open_curly_linter <- function(allow_single_line = FALSE) { type = "style" ) - expr_after <- xml2::xml_find_all(xml, xpath_after) + expr_after <- xml_find_all(xml, xpath_after) lints_after <- xml_nodes_to_lints( expr_after, source_expression = source_expression, @@ -150,7 +150,7 @@ paren_brace_linter <- function() { xml <- source_expression$xml_parsed_content - match_exprs <- xml2::xml_find_all(xml, xpath) + match_exprs <- xml_find_all(xml, xpath) xml_nodes_to_lints( match_exprs, diff --git a/R/lintr-package.R b/R/lintr-package.R index 7961891ca..a351dc834 100644 --- a/R/lintr-package.R +++ b/R/lintr-package.R @@ -8,11 +8,11 @@ "_PACKAGE" ## lintr namespace: start -#' @importFrom glue glue +#' @importFrom glue glue glue_collapse #' @importFrom rex rex regex re_matches re_substitutes character_class #' @importFrom stats na.omit #' @importFrom utils capture.output head getParseData relist -#' @importFrom xml2 xml_find_all xml_find_first xml_text as_list +#' @importFrom xml2 xml_attr xml_find_all xml_find_chr xml_find_num xml_find_first xml_text as_list #' @importFrom cyclocomp cyclocomp #' @importFrom utils tail #' @rawNamespace diff --git a/R/literal_coercion_linter.R b/R/literal_coercion_linter.R index 4927ff7e6..3193bcc78 100644 --- a/R/literal_coercion_linter.R +++ b/R/literal_coercion_linter.R @@ -64,7 +64,7 @@ literal_coercion_linter <- function() { or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])] ) " - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {coercers} ] /parent::expr /parent::expr[ @@ -80,7 +80,7 @@ literal_coercion_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) coercer <- xp_call_name(bad_expr) # tiptoe around the fact that we don't require {rlang} @@ -96,7 +96,7 @@ literal_coercion_linter <- function() { ) } else { # duplicate, unless we add 'rlang::' and it wasn't there originally - coercion_str <- report_str <- xml2::xml_text(bad_expr) + coercion_str <- report_str <- xml_text(bad_expr) if (any(is_rlang_coercer) && !("package:rlang" %in% search())) { needs_prefix <- is_rlang_coercer & !startsWith(coercion_str, "rlang::") coercion_str[needs_prefix] <- paste0("rlang::", coercion_str[needs_prefix]) diff --git a/R/matrix_apply_linter.R b/R/matrix_apply_linter.R index e39c2082e..b0d7f390c 100644 --- a/R/matrix_apply_linter.R +++ b/R/matrix_apply_linter.R @@ -69,7 +69,7 @@ matrix_apply_linter <- function() { ] " - xpath <- glue::glue("{sums_xpath} | {means_xpath}") + xpath <- glue("{sums_xpath} | {means_xpath}") # This doesn't handle the case when MARGIN and FUN are named and in a different position # but this should be relatively rate @@ -83,17 +83,17 @@ matrix_apply_linter <- function() { } xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) - var <- xml2::xml_text(xml2::xml_find_all(bad_expr, var_xpath)) + var <- xml_text(xml_find_all(bad_expr, var_xpath)) - fun <- xml2::xml_text(xml2::xml_find_all(bad_expr, fun_xpath)) + fun <- xml_text(xml_find_all(bad_expr, fun_xpath)) fun <- tools::toTitleCase(fun) - margin <- xml2::xml_find_all(bad_expr, margin_xpath) + margin <- xml_find_all(bad_expr, margin_xpath) - narm_val <- xml2::xml_text( - xml2::xml_find_first(bad_expr, "SYMBOL_SUB[text() = 'na.rm']/following-sibling::expr") + narm_val <- xml_text( + xml_find_first(bad_expr, "SYMBOL_SUB[text() = 'na.rm']/following-sibling::expr") ) recos <- Map(craft_colsums_rowsums_msg, var, margin, fun, narm_val) @@ -109,12 +109,12 @@ matrix_apply_linter <- function() { craft_colsums_rowsums_msg <- function(var, margin, fun, narm_val) { - if (is.na(xml2::xml_find_first(margin, "OP-COLON"))) { - l1 <- xml2::xml_text(margin) + if (is.na(xml_find_first(margin, "OP-COLON"))) { + l1 <- xml_text(margin) l2 <- NULL } else { - l1 <- xml2::xml_text(xml2::xml_find_first(margin, "expr[1]")) - l2 <- xml2::xml_text(xml2::xml_find_first(margin, "expr[2]")) + l1 <- xml_text(xml_find_first(margin, "expr[1]")) + l2 <- xml_text(xml_find_first(margin, "expr[2]")) } # See #1764 for details about various cases. In short: @@ -131,15 +131,15 @@ craft_colsums_rowsums_msg <- function(var, margin, fun, narm_val) { l2 <- suppressWarnings(as.integer(re_substitutes(l2, "L$", ""))) if (!is.na(narm_val)) { - narm <- glue::glue(", na.rm = {narm_val}") + narm <- glue(", na.rm = {narm_val}") } else { narm <- "" } if (identical(l1, 1L)) { - reco <- glue::glue("row{fun}s({var}{narm}, dims = {l2})") + reco <- glue("row{fun}s({var}{narm}, dims = {l2})") } else { - reco <- glue::glue( + reco <- glue( "row{fun}s(col{fun}s({var}{narm}, dims = {l1 - 1}), dims = {l2 - l1 + 1})", " or ", "col{fun}s({var}{narm}, dims = {l1 - 1}) if {var} has {l2} dimensions" diff --git a/R/missing_argument_linter.R b/R/missing_argument_linter.R index 75fab6b84..ea025a068 100644 --- a/R/missing_argument_linter.R +++ b/R/missing_argument_linter.R @@ -40,7 +40,7 @@ missing_argument_linter <- function(except = c("alist", "quote", "switch"), allo conds <- c(conds, "self::OP-COMMA[following-sibling::*[not(self::COMMENT)][1][self::OP-RIGHT-PAREN]]") } - xpath <- glue::glue("//SYMBOL_FUNCTION_CALL/parent::expr/parent::expr/*[{xp_or(conds)}]") + xpath <- glue("//SYMBOL_FUNCTION_CALL/parent::expr/parent::expr/*[{xp_or(conds)}]") to_function_xpath <- "string(./preceding-sibling::expr[last()]/SYMBOL_FUNCTION_CALL)" Linter(function(source_expression) { @@ -50,8 +50,8 @@ missing_argument_linter <- function(except = c("alist", "quote", "switch"), allo xml <- source_expression$full_xml_parsed_content - missing_args <- xml2::xml_find_all(xml, xpath) - function_call_name <- get_r_string(xml2::xml_find_chr(missing_args, to_function_xpath)) + missing_args <- xml_find_all(xml, xpath) + function_call_name <- get_r_string(xml_find_chr(missing_args, to_function_xpath)) xml_nodes_to_lints( missing_args[!function_call_name %in% except], diff --git a/R/missing_package_linter.R b/R/missing_package_linter.R index 2ab0b9d63..7418a1abd 100644 --- a/R/missing_package_linter.R +++ b/R/missing_package_linter.R @@ -49,8 +49,8 @@ missing_package_linter <- function() { xml <- source_expression$full_xml_parsed_content - pkg_calls <- xml2::xml_find_all(xml, call_xpath) - pkg_names <- get_r_string(xml2::xml_find_all( + pkg_calls <- xml_find_all(xml, call_xpath) + pkg_names <- get_r_string(xml_find_all( pkg_calls, "OP-LEFT-PAREN[1]/following-sibling::expr[1][SYMBOL | STR_CONST]" )) diff --git a/R/namespace_linter.R b/R/namespace_linter.R index b301db9e9..a0ca682d5 100644 --- a/R/namespace_linter.R +++ b/R/namespace_linter.R @@ -46,7 +46,7 @@ namespace_linter <- function(check_exports = TRUE, check_nonexports = TRUE) { xml <- source_expression$full_xml_parsed_content - ns_nodes <- xml2::xml_find_all(xml, "//NS_GET | //NS_GET_INT") + ns_nodes <- xml_find_all(xml, "//NS_GET | //NS_GET_INT") if (length(ns_nodes) == 0L) { return(list()) @@ -54,7 +54,7 @@ namespace_linter <- function(check_exports = TRUE, check_nonexports = TRUE) { ## Case 1: pkg is uninstalled in pkg::foo - package_nodes <- xml2::xml_find_all(ns_nodes, "preceding-sibling::*[1]") + package_nodes <- xml_find_all(ns_nodes, "preceding-sibling::*[1]") packages <- get_r_string(package_nodes) lints <- list() @@ -95,8 +95,8 @@ namespace_linter <- function(check_exports = TRUE, check_nonexports = TRUE) { } # nocov end - ns_get <- xml2::xml_text(ns_nodes) == "::" - symbol_nodes <- xml2::xml_find_all(ns_nodes, "following-sibling::*[1]") + ns_get <- xml_text(ns_nodes) == "::" + symbol_nodes <- xml_find_all(ns_nodes, "following-sibling::*[1]") symbols <- get_r_string(symbol_nodes) if (check_nonexports) { diff --git a/R/nested_ifelse_linter.R b/R/nested_ifelse_linter.R index 7f6c0c02a..a70ed7a1f 100644 --- a/R/nested_ifelse_linter.R +++ b/R/nested_ifelse_linter.R @@ -36,7 +36,7 @@ #' @export nested_ifelse_linter <- function() { # NB: land on the nested (inner) call, not the outer call, and throw a lint with the inner call's name - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(ifelse_funs)}] /parent::expr /following-sibling::expr[expr[1][SYMBOL_FUNCTION_CALL[ {xp_text_in_table(ifelse_funs)} ]]] @@ -49,7 +49,7 @@ nested_ifelse_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_call <- xp_call_name(bad_expr) lint_message <- paste( diff --git a/R/numeric_leading_zero_linter.R b/R/numeric_leading_zero_linter.R index 57ffdb6e9..c7b755802 100644 --- a/R/numeric_leading_zero_linter.R +++ b/R/numeric_leading_zero_linter.R @@ -44,7 +44,7 @@ numeric_leading_zero_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/object_length_linter.R b/R/object_length_linter.R index 1610e6f01..98a227a69 100644 --- a/R/object_length_linter.R +++ b/R/object_length_linter.R @@ -44,11 +44,11 @@ object_length_linter <- function(length = 30L) { xml <- source_expression$full_xml_parsed_content - assignments <- xml2::xml_find_all(xml, object_name_xpath) + assignments <- xml_find_all(xml, object_name_xpath) # Retrieve assigned name nms <- strip_names( - xml2::xml_text(assignments) + xml_text(assignments) ) # run namespace_imports at run-time, not "compile" time to allow package structure to change diff --git a/R/object_name_linter.R b/R/object_name_linter.R index 716f72545..a5bd11995 100644 --- a/R/object_name_linter.R +++ b/R/object_name_linter.R @@ -22,7 +22,7 @@ object_name_xpath <- local({ "])" ) - glue::glue(" + glue(" //SYMBOL[ {sprintf(xp_assignment_target_fmt, 'ancestor')} ] | //STR_CONST[ {sprintf(xp_assignment_target_fmt, 'parent')} ] | //SYMBOL_FORMALS @@ -138,7 +138,7 @@ object_name_linter <- function(styles = c("snake_case", "symbols"), regexes = ch lint_message <- paste0( "Variable and function name style should match ", - glue::glue_collapse(unique(names(style_list)), sep = ", ", last = " or "), "." + glue_collapse(unique(names(style_list)), sep = ", ", last = " or "), "." ) Linter(function(source_expression) { @@ -148,11 +148,11 @@ object_name_linter <- function(styles = c("snake_case", "symbols"), regexes = ch xml <- source_expression$full_xml_parsed_content - assignments <- xml2::xml_find_all(xml, object_name_xpath) + assignments <- xml_find_all(xml, object_name_xpath) # Retrieve assigned name nms <- strip_names( - xml2::xml_text(assignments) + xml_text(assignments) ) # run namespace_imports at run-time, not "compile" time to allow package structure to change diff --git a/R/object_usage_linter.R b/R/object_usage_linter.R index 83949a879..48a391710 100644 --- a/R/object_usage_linter.R +++ b/R/object_usage_linter.R @@ -48,7 +48,7 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) { # not all instances of linted symbols are potential sources for the observed violations -- see #1914 symbol_exclude_cond <- "preceding-sibling::OP-DOLLAR or preceding-sibling::OP-AT or ancestor::expr[OP-TILDE]" - xpath_culprit_symbol <- glue::glue(" + xpath_culprit_symbol <- glue(" descendant::SYMBOL[not( {symbol_exclude_cond} )] | descendant::SYMBOL_FUNCTION_CALL[not( {symbol_exclude_cond} )] | descendant::SPECIAL @@ -78,7 +78,7 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) { assign(symbol, function(...) invisible(), envir = env) } - fun_assignments <- xml2::xml_find_all(xml, xpath_function_assignment) + fun_assignments <- xml_find_all(xml, xpath_function_assignment) lapply(fun_assignments, function(fun_assignment) { code <- get_content(lines = source_expression$content, fun_assignment) @@ -98,8 +98,8 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) { fun, known_used_symbols = known_used_symbols, declared_globals = declared_globals, - start_line = as.integer(xml2::xml_attr(fun_assignment, "line1")), - end_line = as.integer(xml2::xml_attr(fun_assignment, "line2")), + start_line = as.integer(xml_attr(fun_assignment, "line1")), + end_line = as.integer(xml_attr(fun_assignment, "line2")), skip_with = skip_with ) @@ -107,10 +107,10 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) { # e.g. `not_existing<-`(a, b) res$name <- rex::re_substitutes(res$name, rex::rex("<-"), "") - lintable_symbols <- xml2::xml_find_all(fun_assignment, xpath_culprit_symbol) + lintable_symbols <- xml_find_all(fun_assignment, xpath_culprit_symbol) - lintable_symbol_names <- gsub("^`|`$", "", xml2::xml_text(lintable_symbols)) - lintable_symbol_lines <- as.integer(xml2::xml_attr(lintable_symbols, "line1")) + lintable_symbol_names <- gsub("^`|`$", "", xml_text(lintable_symbols)) + lintable_symbol_lines <- as.integer(xml_attr(lintable_symbols, "line1")) matched_symbol <- vapply( seq_len(nrow(res)), @@ -129,7 +129,7 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) { # fallback to line based matching if no symbol is found missing_symbol <- is.na(matched_symbol) nodes[missing_symbol] <- lapply(which(missing_symbol), function(i) { - line_based_match <- xml2::xml_find_first( + line_based_match <- xml_find_first( fun_assignment, glue::glue_data(res[i, ], "descendant::expr[@line1 = {line1} and @line2 = {line2}]") ) @@ -166,7 +166,7 @@ extract_glued_symbols <- function(expr) { # # Package stringr: # - str_interp - glue_calls <- xml2::xml_find_all( + glue_calls <- xml_find_all( expr, xpath = paste0( "descendant::SYMBOL_FUNCTION_CALL[text() = 'glue']/", # a glue() call @@ -230,7 +230,7 @@ symbol_extractor <- function(text, envir, data) { } get_assignment_symbols <- function(xml) { - get_r_string(xml2::xml_find_all( + get_r_string(xml_find_all( xml, " expr[LEFT_ASSIGN]/expr[1]/SYMBOL[1] | @@ -332,7 +332,7 @@ get_imported_symbols <- function(xml) { ] /expr[STR_CONST or SYMBOL][1] " - import_exprs <- xml2::xml_find_all(xml, import_exprs_xpath) + import_exprs <- xml_find_all(xml, import_exprs_xpath) if (length(import_exprs) == 0L) { return(character()) } diff --git a/R/outer_negation_linter.R b/R/outer_negation_linter.R index 2429f6b09..e290005e2 100644 --- a/R/outer_negation_linter.R +++ b/R/outer_negation_linter.R @@ -57,7 +57,7 @@ outer_negation_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_call <- xp_call_name(bad_expr) inverse_call <- ifelse(matched_call == "any", "all", "any") diff --git a/R/package_hooks_linter.R b/R/package_hooks_linter.R index e6e4ddecd..1d28858ea 100644 --- a/R/package_hooks_linter.R +++ b/R/package_hooks_linter.R @@ -66,7 +66,7 @@ package_hooks_linter <- function() { names(bad_call_xpaths) <- names(bad_calls) make_bad_call_lint_message <- function(expr, hook) { - call_name <- xml2::xml_text(expr) + call_name <- xml_text(expr) lint_message <- sprintf("Don't use %s() in %s().", call_name, hook) lint_message[call_name == "packageStartupMessage"] <- "Put packageStartupMessage() calls in .onAttach(), not .onLoad()." @@ -129,7 +129,7 @@ package_hooks_linter <- function() { # inherits: source_expression, bad_call_xpaths bad_msg_call_lints <- function(xml, hook) { - bad_expr <- xml2::xml_find_all(xml, bad_call_xpaths[[hook]]) + bad_expr <- xml_find_all(xml, bad_call_xpaths[[hook]]) lint_message <- make_bad_call_lint_message(bad_expr, hook) xml_nodes_to_lints(bad_expr, source_expression, lint_message, type = "warning") } @@ -139,21 +139,21 @@ package_hooks_linter <- function() { onattach_bad_msg_call_lints <- bad_msg_call_lints(xml, ".onAttach") # (2) .onLoad() and .onAttach() should take two arguments, with names matching ^lib and ^pkg - load_arg_name_expr <- xml2::xml_find_all(xml, load_arg_name_xpath) + load_arg_name_expr <- xml_find_all(xml, load_arg_name_xpath) load_arg_name_message <- sprintf( "%s() should take two arguments, with the first starting with 'lib' and the second starting with 'pkg'.", - xml2::xml_find_chr(load_arg_name_expr, hook_xpath) + xml_find_chr(load_arg_name_expr, hook_xpath) ) load_arg_name_lints <- xml_nodes_to_lints(load_arg_name_expr, source_expression, load_arg_name_message, type = "warning") # (3) .onLoad() and .onAttach() shouldn't call require(), library(), or installed.packages() # NB: base only checks the SYMBOL_FUNCTION_CALL version, not SYMBOL. - library_require_expr <- xml2::xml_find_all(xml, library_require_xpath) + library_require_expr <- xml_find_all(xml, library_require_xpath) - library_require_bad_call <- xml2::xml_text(library_require_expr) - library_require_hook <- xml2::xml_find_chr(library_require_expr, hook_xpath) + library_require_bad_call <- xml_text(library_require_expr) + library_require_hook <- xml_find_chr(library_require_expr, hook_xpath) library_require_message <- character(length(library_require_bad_call)) is_installed_packages <- library_require_bad_call == "installed.packages" library_require_message[is_installed_packages] <- @@ -164,21 +164,21 @@ package_hooks_linter <- function() { xml_nodes_to_lints(library_require_expr, source_expression, library_require_message, type = "warning") # (4) .Last.lib() and .onDetach() shouldn't call library.dynam.unload() - bad_unload_call_expr <- xml2::xml_find_all(xml, bad_unload_call_xpath) + bad_unload_call_expr <- xml_find_all(xml, bad_unload_call_xpath) bad_unload_call_message <- sprintf( "Use library.dynam.unload() calls in .onUnload(), not %s().", - xml2::xml_find_chr(bad_unload_call_expr, hook_xpath) + xml_find_chr(bad_unload_call_expr, hook_xpath) ) bad_unload_call_lints <- xml_nodes_to_lints(bad_unload_call_expr, source_expression, bad_unload_call_message, type = "warning") # (5) .Last.lib() and .onDetach() should take one arguments with name matching ^lib - unload_arg_name_expr <- xml2::xml_find_all(xml, unload_arg_name_xpath) + unload_arg_name_expr <- xml_find_all(xml, unload_arg_name_xpath) unload_arg_name_message <- sprintf( "%s() should take one argument starting with 'lib'.", - xml2::xml_find_chr(unload_arg_name_expr, hook_xpath) + xml_find_chr(unload_arg_name_expr, hook_xpath) ) unload_arg_name_lints <- xml_nodes_to_lints(unload_arg_name_expr, source_expression, unload_arg_name_message, type = "warning") diff --git a/R/paren_body_linter.R b/R/paren_body_linter.R index 2704f2350..1fdccfdc1 100644 --- a/R/paren_body_linter.R +++ b/R/paren_body_linter.R @@ -53,7 +53,7 @@ paren_body_linter <- function() { } xml <- source_expression$xml_parsed_content - matched_expressions <- xml2::xml_find_all(xml, xpath) + matched_expressions <- xml_find_all(xml, xpath) xml_nodes_to_lints( matched_expressions, diff --git a/R/paste_linter.R b/R/paste_linter.R index b130a6f74..ed4f35424 100644 --- a/R/paste_linter.R +++ b/R/paste_linter.R @@ -120,7 +120,7 @@ paste_linter <- function(allow_empty_sep = FALSE, allow_to_string = FALSE) { optional_lints <- list() if (!allow_empty_sep) { - empty_sep_expr <- xml2::xml_find_all(xml, sep_xpath) + empty_sep_expr <- xml_find_all(xml, sep_xpath) sep_value <- get_r_string(empty_sep_expr, xpath = "./SYMBOL_SUB[text() = 'sep']/following-sibling::expr[1]") optional_lints <- c(optional_lints, xml_nodes_to_lints( @@ -133,7 +133,7 @@ paste_linter <- function(allow_empty_sep = FALSE, allow_to_string = FALSE) { if (!allow_to_string) { # 3 expr: the function call, the argument, and collapse= - to_string_expr <- xml2::xml_find_all(xml, to_string_xpath) + to_string_expr <- xml_find_all(xml, to_string_xpath) collapse_value <- get_r_string( to_string_expr, xpath = "./SYMBOL_SUB[text() = 'collapse']/following-sibling::expr[1]" @@ -151,7 +151,7 @@ paste_linter <- function(allow_empty_sep = FALSE, allow_to_string = FALSE) { )) } - paste0_sep_expr <- xml2::xml_find_all(xml, paste0_sep_xpath) + paste0_sep_expr <- xml_find_all(xml, paste0_sep_xpath) paste0_sep_lints <- xml_nodes_to_lints( paste0_sep_expr, source_expression = source_expression, @@ -159,7 +159,7 @@ paste_linter <- function(allow_empty_sep = FALSE, allow_to_string = FALSE) { type = "warning" ) - paste_strrep_expr <- xml2::xml_find_all(xml, paste_strrep_xpath) + paste_strrep_expr <- xml_find_all(xml, paste_strrep_xpath) collapse_arg <- get_r_string(paste_strrep_expr, "SYMBOL_SUB/following-sibling::expr[1]/STR_CONST") paste_strrep_expr <- paste_strrep_expr[!nzchar(collapse_arg)] paste_call <- xp_call_name(paste_strrep_expr) diff --git a/R/pipe_call_linter.R b/R/pipe_call_linter.R index b58c5c3e7..d37be2f66 100644 --- a/R/pipe_call_linter.R +++ b/R/pipe_call_linter.R @@ -33,7 +33,7 @@ pipe_call_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) pipe <- xml_text(xml_find_first(bad_expr, "preceding-sibling::SPECIAL[1]")) xml_nodes_to_lints( diff --git a/R/pipe_continuation_linter.R b/R/pipe_continuation_linter.R index e8a91674b..c395ff1c1 100644 --- a/R/pipe_continuation_linter.R +++ b/R/pipe_continuation_linter.R @@ -73,7 +73,7 @@ pipe_continuation_linter <- function() { } xml <- source_expression$full_xml_parsed_content - pipe_exprs <- xml2::xml_find_all(xml, xpath) + pipe_exprs <- xml_find_all(xml, xpath) pipe_text <- xml_text(pipe_exprs) xml_nodes_to_lints( diff --git a/R/redundant_equals_linter.R b/R/redundant_equals_linter.R index 274cde30a..daf910cb8 100644 --- a/R/redundant_equals_linter.R +++ b/R/redundant_equals_linter.R @@ -50,8 +50,8 @@ redundant_equals_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) - op <- xml2::xml_text(xml2::xml_find_first(bad_expr, "*[2]")) + bad_expr <- xml_find_all(xml, xpath) + op <- xml_text(xml_find_first(bad_expr, "*[2]")) xml_nodes_to_lints( bad_expr, diff --git a/R/redundant_ifelse_linter.R b/R/redundant_ifelse_linter.R index 0543a5c1c..00ce22938 100644 --- a/R/redundant_ifelse_linter.R +++ b/R/redundant_ifelse_linter.R @@ -44,7 +44,7 @@ #' @seealso [linters] for a complete list of linters available in lintr. #' @export redundant_ifelse_linter <- function(allow10 = FALSE) { - tf_xpath <- glue::glue(" + tf_xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(ifelse_funs)} ] /parent::expr /parent::expr[ @@ -53,7 +53,7 @@ redundant_ifelse_linter <- function(allow10 = FALSE) { ] ") - num_xpath <- glue::glue(" + num_xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(ifelse_funs)} ] /parent::expr /parent::expr[ @@ -70,11 +70,11 @@ redundant_ifelse_linter <- function(allow10 = FALSE) { xml <- source_expression$xml_parsed_content lints <- list() - tf_expr <- xml2::xml_find_all(xml, tf_xpath) + tf_expr <- xml_find_all(xml, tf_xpath) matched_call <- xp_call_name(tf_expr) # [1] call; [2] logical condition - first_arg <- xml2::xml_find_chr(tf_expr, "string(expr[3]/NUM_CONST)") - second_arg <- xml2::xml_find_chr(tf_expr, "string(expr[4]/NUM_CONST)") + first_arg <- xml_find_chr(tf_expr, "string(expr[3]/NUM_CONST)") + second_arg <- xml_find_chr(tf_expr, "string(expr[4]/NUM_CONST)") tf_message <- sprintf( "Just use the logical condition (or its negation) directly instead of calling %s(x, %s, %s)", matched_call, first_arg, second_arg @@ -82,11 +82,11 @@ redundant_ifelse_linter <- function(allow10 = FALSE) { lints <- c(lints, xml_nodes_to_lints(tf_expr, source_expression, tf_message, type = "warning")) if (!allow10) { - num_expr <- xml2::xml_find_all(xml, num_xpath) + num_expr <- xml_find_all(xml, num_xpath) matched_call <- xp_call_name(num_expr) # [1] call; [2] logical condition - first_arg <- xml2::xml_find_chr(num_expr, "string(expr[3]/NUM_CONST)") - second_arg <- xml2::xml_find_chr(num_expr, "string(expr[4]/NUM_CONST)") + first_arg <- xml_find_chr(num_expr, "string(expr[3]/NUM_CONST)") + second_arg <- xml_find_chr(num_expr, "string(expr[4]/NUM_CONST)") is_numeric_01 <- first_arg %in% c("0", "1") | second_arg %in% c("0", "1") coercion_function <- ifelse(is_numeric_01, "as.numeric", "as.integer") is_negated <- first_arg %in% c("0", "0L") diff --git a/R/regex_subset_linter.R b/R/regex_subset_linter.R index bd22de516..aaf026058 100644 --- a/R/regex_subset_linter.R +++ b/R/regex_subset_linter.R @@ -64,8 +64,8 @@ regex_subset_linter <- function() { and expr[position() = {arg_pos} ] = parent::expr/expr[1] ] " - grep_xpath <- glue::glue(xpath_fmt, calls = c("grepl", "grep"), arg_pos = 3L) - stringr_xpath <- glue::glue(xpath_fmt, calls = c("str_detect", "str_which"), arg_pos = 2L) + grep_xpath <- glue(xpath_fmt, calls = c("grepl", "grep"), arg_pos = 3L) + stringr_xpath <- glue(xpath_fmt, calls = c("str_detect", "str_which"), arg_pos = 2L) Linter(function(source_expression) { if (!is_lint_level(source_expression, "expression")) { @@ -74,7 +74,7 @@ regex_subset_linter <- function() { xml <- source_expression$xml_parsed_content - grep_expr <- xml2::xml_find_all(xml, grep_xpath) + grep_expr <- xml_find_all(xml, grep_xpath) grep_lints <- xml_nodes_to_lints( grep_expr, @@ -84,7 +84,7 @@ regex_subset_linter <- function() { type = "warning" ) - stringr_expr <- xml2::xml_find_all(xml, stringr_xpath) + stringr_expr <- xml_find_all(xml, stringr_xpath) stringr_lints <- xml_nodes_to_lints( stringr_expr, diff --git a/R/routine_registration_linter.R b/R/routine_registration_linter.R index 9948e8f25..6ff5769a6 100644 --- a/R/routine_registration_linter.R +++ b/R/routine_registration_linter.R @@ -33,7 +33,7 @@ #' @export routine_registration_linter <- function() { native_routine_callers <- c(".C", ".Call", ".Fortran", ".External") - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(native_routine_callers)} ] /parent::expr /following-sibling::expr[1]/STR_CONST @@ -47,7 +47,7 @@ routine_registration_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/semicolon_linter.R b/R/semicolon_linter.R index 68dd47185..dfa3af603 100644 --- a/R/semicolon_linter.R +++ b/R/semicolon_linter.R @@ -88,9 +88,9 @@ semicolon_linter <- function(allow_compound = FALSE, allow_trailing = FALSE) { } xml <- source_expression$full_xml_parsed_content - bad_exprs <- xml2::xml_find_all(xml, xpath) + bad_exprs <- xml_find_all(xml, xpath) if (need_detection) { - is_trailing <- is.na(xml2::xml_find_first(bad_exprs, compound_xpath)) + is_trailing <- is.na(xml_find_first(bad_exprs, compound_xpath)) msg <- ifelse(is_trailing, msg_trailing, msg_compound) } diff --git a/R/seq_linter.R b/R/seq_linter.R index 76536f173..d7833be82 100644 --- a/R/seq_linter.R +++ b/R/seq_linter.R @@ -49,14 +49,14 @@ seq_linter <- function() { bad_funcs <- xp_text_in_table(c("length", "n", "nrow", "ncol", "NROW", "NCOL", "dim")) # Exact `xpath` depends on whether bad function was used in conjunction with `seq()` - seq_xpath <- glue::glue(" + seq_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'seq'] /parent::expr /following-sibling::expr[1][expr/SYMBOL_FUNCTION_CALL[ {bad_funcs} ]] /parent::expr[count(expr) = 2] ") # `.N` from {data.table} is special since it's not a function but a symbol - colon_xpath <- glue::glue(" + colon_xpath <- glue(" //OP-COLON /parent::expr[ expr[NUM_CONST[text() = '1' or text() = '1L']] @@ -72,7 +72,7 @@ seq_linter <- function() { ## The actual order of the nodes is document order ## In practice we need to handle length(x):1 get_fun <- function(expr, n) { - funcall <- xml2::xml_find_chr(expr, sprintf("string(./expr[%d])", n)) + funcall <- xml_find_chr(expr, sprintf("string(./expr[%d])", n)) # `dplyr::n()` is special because it has no arguments, so the lint message # should mention `n()`, and not `n(...)` @@ -93,7 +93,7 @@ seq_linter <- function() { xml <- source_expression$xml_parsed_content - badx <- xml2::xml_find_all(xml, xpath) + badx <- xml_find_all(xml, xpath) # TODO: better message customization. For example, length(x):1 # would get rev(seq_along(x)) as the preferred replacement. diff --git a/R/sort_linter.R b/R/sort_linter.R index 568c84cd9..642ac3e14 100644 --- a/R/sort_linter.R +++ b/R/sort_linter.R @@ -61,7 +61,7 @@ sort_linter <- function() { text() = 'decreasing' or text() = 'na.last']" - arg_values_xpath <- glue::glue("{args_xpath}/following-sibling::expr[1]") + arg_values_xpath <- glue("{args_xpath}/following-sibling::expr[1]") Linter(function(source_expression) { if (!is_lint_level(source_expression, "expression")) { @@ -70,10 +70,10 @@ sort_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) - var <- xml2::xml_text( - xml2::xml_find_first( + var <- xml_text( + xml_find_first( bad_expr, ".//SYMBOL_FUNCTION_CALL[text() = 'order']/parent::expr[1]/following-sibling::expr[1]" ) @@ -87,9 +87,9 @@ sort_linter <- function() { # Reconstruct new argument call for each expression separately args <- vapply(bad_expr, function(e) { - arg_names <- xml2::xml_text(xml2::xml_find_all(e, args_xpath)) - arg_values <- xml2::xml_text( - xml2::xml_find_all(e, arg_values_xpath) + arg_names <- xml_text(xml_find_all(e, args_xpath)) + arg_values <- xml_text( + xml_find_all(e, arg_values_xpath) ) if (!"na.last" %in% arg_names) { arg_names <- c(arg_names, "na.last") diff --git a/R/spaces_inside_linter.R b/R/spaces_inside_linter.R index f891a9b57..79a871e71 100644 --- a/R/spaces_inside_linter.R +++ b/R/spaces_inside_linter.R @@ -38,7 +38,7 @@ spaces_inside_linter <- function() { and @end != following-sibling::*[1]/@start - 1 and @line1 = following-sibling::*[1]/@line1 " - left_xpath <- glue::glue(" + left_xpath <- glue(" //OP-LEFT-BRACKET[{left_xpath_condition}] | //LBB[{left_xpath_condition}] | //OP-LEFT-PAREN[{left_xpath_condition}]") @@ -48,7 +48,7 @@ spaces_inside_linter <- function() { and @start != preceding-sibling::*[1]/@end + 1 and @line1 = preceding-sibling::*[1]/@line2 " - right_xpath <- glue::glue(" + right_xpath <- glue(" //OP-RIGHT-BRACKET[{right_xpath_condition}] | //OP-RIGHT-PAREN[{right_xpath_condition} and not(preceding-sibling::*[1][self::EQ_SUB])]") @@ -59,9 +59,9 @@ spaces_inside_linter <- function() { xml <- source_expression$full_xml_parsed_content - left_expr <- xml2::xml_find_all(xml, left_xpath) + left_expr <- xml_find_all(xml, left_xpath) left_msg <- ifelse( - xml2::xml_text(left_expr) %in% c("[", "[["), + xml_text(left_expr) %in% c("[", "[["), "Do not place spaces after square brackets.", "Do not place spaces after parentheses." ) @@ -74,9 +74,9 @@ spaces_inside_linter <- function() { range_end_xpath = "number(./following-sibling::*[1]/@col1 - 1)" # end before following expr ) - right_expr <- xml2::xml_find_all(xml, right_xpath) + right_expr <- xml_find_all(xml, right_xpath) right_msg <- ifelse( - xml2::xml_text(right_expr) == "]", + xml_text(right_expr) == "]", "Do not place spaces before square brackets.", "Do not place spaces before parentheses." ) diff --git a/R/spaces_left_parentheses_linter.R b/R/spaces_left_parentheses_linter.R index 40de60c78..60d98e583 100644 --- a/R/spaces_left_parentheses_linter.R +++ b/R/spaces_left_parentheses_linter.R @@ -64,7 +64,7 @@ spaces_left_parentheses_linter <- function() { xpath <- expression_level_xpath } - bad_paren <- xml2::xml_find_all(xml, xpath) + bad_paren <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_paren, diff --git a/R/sprintf_linter.R b/R/sprintf_linter.R index 551e8fb05..adffba7b2 100644 --- a/R/sprintf_linter.R +++ b/R/sprintf_linter.R @@ -46,7 +46,7 @@ sprintf_linter <- function() { xml <- source_expression$full_xml_parsed_content - sprintf_calls <- xml2::xml_find_all(xml, xpath) + sprintf_calls <- xml_find_all(xml, xpath) message <- vapply(sprintf_calls, capture_sprintf_warning, character(1L)) diff --git a/R/string_boundary_linter.R b/R/string_boundary_linter.R index 86e643183..ac59004c3 100644 --- a/R/string_boundary_linter.R +++ b/R/string_boundary_linter.R @@ -60,7 +60,7 @@ string_boundary_linter <- function(allow_grepl = FALSE) { "string-length(text()) > 3", "contains(text(), '^') or contains(text(), '$')" ) - str_detect_xpath <- glue::glue(" + str_detect_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'str_detect'] /parent::expr /following-sibling::expr[2] @@ -68,7 +68,7 @@ string_boundary_linter <- function(allow_grepl = FALSE) { ") if (!allow_grepl) { - grepl_xpath <- glue::glue(" + grepl_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'grepl'] /parent::expr /parent::expr[ @@ -87,7 +87,7 @@ string_boundary_linter <- function(allow_grepl = FALSE) { } get_regex_lint_data <- function(xml, xpath) { - expr <- xml2::xml_find_all(xml, xpath) + expr <- xml_find_all(xml, xpath) patterns <- get_r_string(expr) initial_anchor <- startsWith(patterns, "^") search_start <- 1L + initial_anchor @@ -96,7 +96,7 @@ string_boundary_linter <- function(allow_grepl = FALSE) { list(lint_expr = expr[can_replace], initial_anchor = initial_anchor[can_replace]) } - substr_xpath_parts <- glue::glue(" + substr_xpath_parts <- glue(" //{ c('EQ', 'NE') } /parent::expr[ expr[STR_CONST] @@ -164,8 +164,8 @@ string_boundary_linter <- function(allow_grepl = FALSE) { )) } - substr_expr <- xml2::xml_find_all(xml, substr_xpath) - substr_one <- xml2::xml_find_chr(substr_expr, substr_arg2_xpath) %in% c("1", "1L") + substr_expr <- xml_find_all(xml, substr_xpath) + substr_one <- xml_find_chr(substr_expr, substr_arg2_xpath) %in% c("1", "1L") substr_lint_message <- paste( ifelse( substr_one, diff --git a/R/strings_as_factors_linter.R b/R/strings_as_factors_linter.R index e3820ed77..b580eca7f 100644 --- a/R/strings_as_factors_linter.R +++ b/R/strings_as_factors_linter.R @@ -62,7 +62,7 @@ strings_as_factors_linter <- function() { # two exclusions # (1) above argument is to row.names= # (2) stringsAsFactors is manually supplied (with any value) - xpath <- glue::glue(" + xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'data.frame'] /parent::expr /parent::expr[ @@ -89,7 +89,7 @@ strings_as_factors_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/system_file_linter.R b/R/system_file_linter.R index 6fd448d38..069764393 100644 --- a/R/system_file_linter.R +++ b/R/system_file_linter.R @@ -27,7 +27,7 @@ system_file_linter <- function() { funs <- c("system.file", "file.path") # either system.file(file.path(...)) or file.path(system.file(...)) - xpath_parts <- glue::glue(" + xpath_parts <- glue(" //SYMBOL_FUNCTION_CALL[text() = '{funs}'] /parent::expr[following-sibling::expr/expr/SYMBOL_FUNCTION_CALL[text() = '{rev(funs)}']] /parent::expr @@ -41,7 +41,7 @@ system_file_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) outer_call <- xp_call_name(bad_expr) lint_message <- paste( diff --git a/R/trailing_whitespace_linter.R b/R/trailing_whitespace_linter.R index 6a43d7c77..5e6d1636f 100644 --- a/R/trailing_whitespace_linter.R +++ b/R/trailing_whitespace_linter.R @@ -59,9 +59,9 @@ trailing_whitespace_linter <- function(allow_empty_lines = FALSE, allow_in_strin } if (isTRUE(allow_in_strings) && !is.null(source_expression$full_xml_parsed_content)) { - all_str_consts <- xml2::xml_find_all(source_expression$full_xml_parsed_content, "//STR_CONST") - start_lines <- as.integer(xml2::xml_attr(all_str_consts, "line1")) - end_lines <- as.integer(xml2::xml_attr(all_str_consts, "line2")) + all_str_consts <- xml_find_all(source_expression$full_xml_parsed_content, "//STR_CONST") + start_lines <- as.integer(xml_attr(all_str_consts, "line1")) + end_lines <- as.integer(xml_attr(all_str_consts, "line2")) is_in_str <- vapply(bad_lines, function(ln) any(start_lines <= ln & ln < end_lines), logical(1L)) bad_lines <- bad_lines[!is_in_str] diff --git a/R/undesirable_function_linter.R b/R/undesirable_function_linter.R index bbfc9b5b3..d6ef9ba1b 100644 --- a/R/undesirable_function_linter.R +++ b/R/undesirable_function_linter.R @@ -73,9 +73,9 @@ undesirable_function_linter <- function(fun = default_undesirable_functions, ) if (symbol_is_undesirable) { - xpath <- glue::glue("//SYMBOL_FUNCTION_CALL[{xp_condition}] | //SYMBOL[{xp_condition}]") + xpath <- glue("//SYMBOL_FUNCTION_CALL[{xp_condition}] | //SYMBOL[{xp_condition}]") } else { - xpath <- glue::glue("//SYMBOL_FUNCTION_CALL[{xp_condition}]") + xpath <- glue("//SYMBOL_FUNCTION_CALL[{xp_condition}]") } @@ -83,7 +83,7 @@ undesirable_function_linter <- function(fun = default_undesirable_functions, if (!is_lint_level(source_expression, "expression")) { return(list()) } - matched_nodes <- xml2::xml_find_all(source_expression$xml_parsed_content, xpath) + matched_nodes <- xml_find_all(source_expression$xml_parsed_content, xpath) fun_names <- get_r_string(matched_nodes) msgs <- vapply( diff --git a/R/undesirable_operator_linter.R b/R/undesirable_operator_linter.R index 20bcda35d..7fd594379 100644 --- a/R/undesirable_operator_linter.R +++ b/R/undesirable_operator_linter.R @@ -83,9 +83,9 @@ undesirable_operator_linter <- function(op = default_undesirable_operators) { xml <- source_expression$xml_parsed_content - bad_op <- xml2::xml_find_all(xml, xpath) + bad_op <- xml_find_all(xml, xpath) - operator <- xml2::xml_text(bad_op) + operator <- xml_text(bad_op) lint_message <- sprintf("Operator `%s` is undesirable.", operator) alternative <- op[operator] has_alternative <- !is.na(alternative) diff --git a/R/unnecessary_concatenation_linter.R b/R/unnecessary_concatenation_linter.R index 5ae6d44f3..ff9999a14 100644 --- a/R/unnecessary_concatenation_linter.R +++ b/R/unnecessary_concatenation_linter.R @@ -76,20 +76,20 @@ unnecessary_concatenation_linter <- function(allow_single_expression = TRUE) { # ") if (allow_single_expression) { zero_arg_cond <- - glue::glue("count(expr) = 1 and not( {to_pipe_xpath} / preceding-sibling::expr[ {non_constant_cond} ])") + glue("count(expr) = 1 and not( {to_pipe_xpath} / preceding-sibling::expr[ {non_constant_cond} ])") one_arg_cond <- - glue::glue("count(expr) = 2 and not(expr[2][ {non_constant_cond} ])") + glue("count(expr) = 2 and not(expr[2][ {non_constant_cond} ])") } else { - zero_arg_cond <- glue::glue("count(expr) = 1 and not( {to_pipe_xpath} )") + zero_arg_cond <- glue("count(expr) = 1 and not( {to_pipe_xpath} )") one_arg_cond <- "count(expr) = 2 and not(expr[2]/SYMBOL[text() = '...'])" - path_to_non_constant <- glue::glue("./expr[2][ {non_constant_cond} ]") + path_to_non_constant <- glue("./expr[2][ {non_constant_cond} ]") msg_const_expr <- paste( 'Unneeded concatenation of a simple expression. Remove the "c" call,', 'replacing with "as.vector" if using "c" to string attributes, e.g. in converting an array to a vector.' ) } - call_xpath <- glue::glue(" + call_xpath <- glue(" //SYMBOL_FUNCTION_CALL[text() = 'c'] /parent::expr /parent::expr[ @@ -105,11 +105,11 @@ unnecessary_concatenation_linter <- function(allow_single_expression = TRUE) { # } xml <- source_expression$xml_parsed_content - c_calls <- xml2::xml_find_all(xml, call_xpath) + c_calls <- xml_find_all(xml, call_xpath) # bump count(args) by 1 if inside a pipeline - num_args <- as.integer(xml2::xml_find_num(c_calls, num_args_xpath)) + - as.integer(!is.na(xml2::xml_find_first(c_calls, to_pipe_xpath))) + num_args <- as.integer(xml_find_num(c_calls, num_args_xpath)) + + as.integer(!is.na(xml_find_first(c_calls, to_pipe_xpath))) # NB: the xpath guarantees num_args is 0, 1, or 2. 2 comes # in "a" %>% c("b"). # TODO(michaelchirico): can we handle this all inside the XPath with reasonable concision? @@ -118,7 +118,7 @@ unnecessary_concatenation_linter <- function(allow_single_expression = TRUE) { # num_args <- num_args[is_unneeded] msg <- ifelse(num_args == 0L, msg_empty, msg_const) if (!allow_single_expression) { - is_single_expression <- !is.na(xml2::xml_find_first(c_calls, path_to_non_constant)) + is_single_expression <- !is.na(xml_find_first(c_calls, path_to_non_constant)) msg[is_single_expression] <- msg_const_expr } diff --git a/R/unnecessary_lambda_linter.R b/R/unnecessary_lambda_linter.R index a25ffae20..e4664ff14 100644 --- a/R/unnecessary_lambda_linter.R +++ b/R/unnecessary_lambda_linter.R @@ -73,8 +73,8 @@ unnecessary_lambda_linter <- function() { " default_fun_xpath <- paste( sep = "|", - glue::glue(default_fun_xpath_fmt, paren_path = "expr"), - glue::glue(default_fun_xpath_fmt, paren_path = "expr[OP-LEFT-BRACE and count(expr) = 1]/expr[1]") + glue(default_fun_xpath_fmt, paren_path = "expr"), + glue(default_fun_xpath_fmt, paren_path = "expr[OP-LEFT-BRACE and count(expr) = 1]/expr[1]") ) # purrr-style inline formulas-as-functions, e.g. ~foo(.x) @@ -82,7 +82,7 @@ unnecessary_lambda_linter <- function() { # 1. a formula (OP-TILDE) # 2. the lone argument marker `.x` or `.` purrr_symbol <- "SYMBOL[text() = '.x' or text() = '.']" - purrr_fun_xpath <- glue::glue(" + purrr_fun_xpath <- glue(" //SYMBOL_FUNCTION_CALL[ {xp_text_in_table(purrr_mappers)} ] /parent::expr /following-sibling::expr[ @@ -95,7 +95,7 @@ unnecessary_lambda_linter <- function() { # path to calling function symbol from the matched expressions fun_xpath <- "./parent::expr/expr/SYMBOL_FUNCTION_CALL" # path to the symbol of the simpler function that avoids a lambda - symbol_xpath <- glue::glue("(expr|expr[OP-LEFT-BRACE]/expr[1])/expr[SYMBOL_FUNCTION_CALL]") + symbol_xpath <- glue("(expr|expr[OP-LEFT-BRACE]/expr[1])/expr[SYMBOL_FUNCTION_CALL]") Linter(function(source_expression) { if (!is_lint_level(source_expression, "expression")) { @@ -104,15 +104,15 @@ unnecessary_lambda_linter <- function() { xml <- source_expression$xml_parsed_content - default_fun_expr <- xml2::xml_find_all(xml, default_fun_xpath) + default_fun_expr <- xml_find_all(xml, default_fun_xpath) # TODO(michaelchirico): further message customization is possible here, # e.g. don't always refer to 'lapply()' in the example, and customize to # whether arguments need to be subsumed in '...' or not. The trouble is in # keeping track of which argument the anonymous function is supplied (2nd # argument for many calls, but 3rd e.g. for apply()) - default_call_fun <- xml2::xml_text(xml2::xml_find_first(default_fun_expr, fun_xpath)) - default_symbol <- xml2::xml_text(xml2::xml_find_first(default_fun_expr, symbol_xpath)) + default_call_fun <- xml_text(xml_find_first(default_fun_expr, fun_xpath)) + default_symbol <- xml_text(xml_find_first(default_fun_expr, symbol_xpath)) default_fun_lints <- xml_nodes_to_lints( default_fun_expr, source_expression = source_expression, @@ -124,10 +124,10 @@ unnecessary_lambda_linter <- function() { type = "warning" ) - purrr_fun_expr <- xml2::xml_find_all(xml, purrr_fun_xpath) + purrr_fun_expr <- xml_find_all(xml, purrr_fun_xpath) - purrr_call_fun <- xml2::xml_text(xml2::xml_find_first(purrr_fun_expr, fun_xpath)) - purrr_symbol <- xml2::xml_text(xml2::xml_find_first(purrr_fun_expr, symbol_xpath)) + purrr_call_fun <- xml_text(xml_find_first(purrr_fun_expr, fun_xpath)) + purrr_symbol <- xml_text(xml_find_first(purrr_fun_expr, symbol_xpath)) purrr_fun_lints <- xml_nodes_to_lints( purrr_fun_expr, source_expression = source_expression, diff --git a/R/unnecessary_nested_if_linter.R b/R/unnecessary_nested_if_linter.R index dd078e897..354b167a3 100644 --- a/R/unnecessary_nested_if_linter.R +++ b/R/unnecessary_nested_if_linter.R @@ -43,7 +43,7 @@ unnecessary_nested_if_linter <- function() { xml <- source_expression$full_xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) lint_message <- paste( "Don't use nested `if` statements,", diff --git a/R/unnecessary_placeholder_linter.R b/R/unnecessary_placeholder_linter.R index 061821169..3cbcca0e3 100644 --- a/R/unnecessary_placeholder_linter.R +++ b/R/unnecessary_placeholder_linter.R @@ -56,7 +56,7 @@ unnecessary_placeholder_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/unreachable_code_linter.R b/R/unreachable_code_linter.R index 2d9aa2eb8..dc4acb400 100644 --- a/R/unreachable_code_linter.R +++ b/R/unreachable_code_linter.R @@ -51,10 +51,10 @@ unreachable_code_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) is_nolint_end_comment <- xml2::xml_name(bad_expr) == "COMMENT" & - rex::re_matches(xml2::xml_text(bad_expr), settings$exclude_end) + rex::re_matches(xml_text(bad_expr), settings$exclude_end) xml_nodes_to_lints( bad_expr[!is_nolint_end_comment], diff --git a/R/unused_import_linter.R b/R/unused_import_linter.R index c83f09b3b..d08ec16ec 100644 --- a/R/unused_import_linter.R +++ b/R/unused_import_linter.R @@ -70,15 +70,15 @@ unused_import_linter <- function(allow_ns_usage = FALSE, except_packages = c("bi xml <- source_expression$full_xml_parsed_content - import_exprs <- xml2::xml_find_all(xml, import_xpath) + import_exprs <- xml_find_all(xml, import_xpath) if (length(import_exprs) == 0L) { return(list()) } - imported_pkgs <- xml2::xml_find_chr(import_exprs, "string(expr[STR_CONST|SYMBOL])") + imported_pkgs <- xml_find_chr(import_exprs, "string(expr[STR_CONST|SYMBOL])") # as.character(parse(...)) returns one entry per expression imported_pkgs <- as.character(parse(text = imported_pkgs, keep.source = FALSE)) - used_symbols <- xml2::xml_text(xml2::xml_find_all(xml, xp_used_symbols)) + used_symbols <- xml_text(xml_find_all(xml, xp_used_symbols)) is_used <- vapply( imported_pkgs, @@ -101,7 +101,7 @@ unused_import_linter <- function(allow_ns_usage = FALSE, except_packages = c("bi is_ns_used <- vapply( imported_pkgs, function(pkg) { - ns_usage <- xml2::xml_find_first(xml, paste0("//SYMBOL_PACKAGE[text() = '", pkg, "']")) + ns_usage <- xml_find_first(xml, paste0("//SYMBOL_PACKAGE[text() = '", pkg, "']")) !identical(ns_usage, xml2::xml_missing()) }, logical(1L) diff --git a/R/utils.R b/R/utils.R index 05d010696..11b2d6ec6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -104,7 +104,7 @@ get_content <- function(lines, info) { if (!missing(info)) { if (inherits(info, "xml_node")) { info <- lapply(stats::setNames(nm = c("col1", "col2", "line1", "line2")), function(attr) { - as.integer(xml2::xml_attr(info, attr)) + as.integer(xml_attr(info, attr)) }) } @@ -231,9 +231,9 @@ platform_independent_sort <- function(x) x[platform_independent_order(x)] get_r_string <- function(s, xpath = NULL) { if (inherits(s, c("xml_node", "xml_nodeset"))) { if (is.null(xpath)) { - s <- xml2::xml_text(s) + s <- xml_text(s) } else { - s <- xml2::xml_find_chr(s, sprintf("string(%s)", xpath)) + s <- xml_find_chr(s, sprintf("string(%s)", xpath)) } } # parse() skips "" elements --> offsets the length of the output, @@ -246,7 +246,7 @@ get_r_string <- function(s, xpath = NULL) { #' Convert XML node to R code within #' -#' NB this is not equivalent to `xml2::xml_text(xml)` in the presence of line breaks +#' NB this is not equivalent to `xml_text(xml)` in the presence of line breaks #' #' @param xml An `xml_node`. #' @@ -256,22 +256,22 @@ get_r_string <- function(s, xpath = NULL) { get_r_code <- function(xml) { # shortcut if xml has line1 and line2 attrs and they are equal # if they are missing, xml_attr() returns NA, so we continue - if (isTRUE(xml2::xml_attr(xml, "line1") == xml2::xml_attr(xml, "line2"))) { - return(xml2::xml_text(xml)) + if (isTRUE(xml_attr(xml, "line1") == xml_attr(xml, "line2"))) { + return(xml_text(xml)) } # find all unique line numbers - line_numbers <- sort(unique(xml2::xml_find_num( - xml2::xml_find_all(xml, "./descendant-or-self::*[@line1]"), + line_numbers <- sort(unique(xml_find_num( + xml_find_all(xml, "./descendant-or-self::*[@line1]"), "number(./@line1)" ))) if (length(line_numbers) <= 1L) { # no line breaks necessary - return(xml2::xml_text(xml)) + return(xml_text(xml)) } lines <- vapply(line_numbers, function(line_num) { # all terminal nodes starting on line_num - paste(xml2::xml_text( - xml2::xml_find_all(xml, sprintf("./descendant-or-self::*[@line1 = %d and not(*)]", line_num)) + paste(xml_text( + xml_find_all(xml, sprintf("./descendant-or-self::*[@line1 = %d and not(*)]", line_num)) ), collapse = "") }, character(1L)) paste(lines, collapse = "\n") @@ -286,7 +286,7 @@ get_r_code <- function(xml) { #' @noRd xml2lang <- function(x) { x_strip_comments <- xml_find_all(x, ".//*[not(self::COMMENT or self::expr)]") - str2lang(paste(xml2::xml_text(x_strip_comments), collapse = "")) + str2lang(paste(xml_text(x_strip_comments), collapse = "")) } is_linter <- function(x) inherits(x, "linter") diff --git a/R/vector_logic_linter.R b/R/vector_logic_linter.R index f50149d1a..8fd8385a8 100644 --- a/R/vector_logic_linter.R +++ b/R/vector_logic_linter.R @@ -83,7 +83,7 @@ vector_logic_linter <- function() { } xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) xml_nodes_to_lints( bad_expr, diff --git a/R/with.R b/R/with.R index 400d36b95..295aabb39 100644 --- a/R/with.R +++ b/R/with.R @@ -46,7 +46,7 @@ modify_defaults <- function(defaults, ...) { bad_nms <- setdiff(nms[to_null], names(defaults)) is_are <- if (length(bad_nms) > 1L) "are" else "is" warning( - "Trying to remove ", glue::glue_collapse(sQuote(bad_nms), sep = ", ", last = " and "), + "Trying to remove ", glue_collapse(sQuote(bad_nms), sep = ", ", last = " and "), ", which ", is_are, " not in `defaults`." ) } @@ -104,7 +104,7 @@ linters_with_tags <- function(tags, ..., packages = "lintr", exclude_tags = "dep if (!all(available$linter %in% ns_exports)) { missing_linters <- setdiff(available$linter, ns_exports) stop( - "Linters ", glue::glue_collapse(sQuote(missing_linters), sep = ", ", last = " and "), + "Linters ", glue_collapse(sQuote(missing_linters), sep = ", ", last = " and "), " advertised by `available_linters()` but not exported by package ", package, "." ) } diff --git a/R/xml_nodes_to_lints.R b/R/xml_nodes_to_lints.R index 759cf7554..46407345a 100644 --- a/R/xml_nodes_to_lints.R +++ b/R/xml_nodes_to_lints.R @@ -59,7 +59,7 @@ xml_nodes_to_lints <- function(xml, source_expression, lint_message, ) } type <- match.arg(type, c("style", "warning", "error")) - line1 <- xml2::xml_attr(xml, "line1") + line1 <- xml_attr(xml, "line1") col1 <- xp_find_location(xml, range_start_xpath) if (is.na(col1)) { warning("Could not find range start for lint. Defaulting to start of line.") @@ -69,7 +69,7 @@ xml_nodes_to_lints <- function(xml, source_expression, lint_message, lines <- source_expression[["lines"]] if (is.null(lines)) lines <- source_expression[["file_lines"]] - if (xml2::xml_attr(xml, "line2") == line1) { + if (xml_attr(xml, "line2") == line1) { col2 <- xp_find_location(xml, range_end_xpath) if (is.na(col2)) { warning("Could not find range end for lint. Defaulting to width 1.") diff --git a/R/xp_utils.R b/R/xp_utils.R index 9bd136638..29d767137 100644 --- a/R/xp_utils.R +++ b/R/xp_utils.R @@ -53,15 +53,15 @@ xp_call_name <- function(expr, depth = 1L, condition = NULL) { xpath <- paste0("string(", strrep("expr/", depth), node, ")") - xml2::xml_find_chr(expr, xpath) + xml_find_chr(expr, xpath) } xp_find_location <- function(xml, xpath) { if (identical(xpath, "number(./@col1)")) { - as.integer(xml2::xml_attr(xml, "col1")) + as.integer(xml_attr(xml, "col1")) } else if (identical(xpath, "number(./@col2)")) { - as.integer(xml2::xml_attr(xml, "col2")) + as.integer(xml_attr(xml, "col2")) } else { - as.integer(xml2::xml_find_num(xml, xpath)) + as.integer(xml_find_num(xml, xpath)) } } diff --git a/R/yoda_test_linter.R b/R/yoda_test_linter.R index 9d90b7a5d..604232f3e 100644 --- a/R/yoda_test_linter.R +++ b/R/yoda_test_linter.R @@ -53,7 +53,7 @@ yoda_test_linter <- function() { /parent::expr[not(preceding-sibling::*[self::PIPE or self::SPECIAL[{ xp_text_in_table(pipes) }]])] ") - second_const_xpath <- glue::glue("expr[position() = 3 and ({const_condition})]") + second_const_xpath <- glue("expr[position() = 3 and ({const_condition})]") Linter(function(source_expression) { if (!is_lint_level(source_expression, "expression")) { @@ -62,10 +62,10 @@ yoda_test_linter <- function() { xml <- source_expression$xml_parsed_content - bad_expr <- xml2::xml_find_all(xml, xpath) + bad_expr <- xml_find_all(xml, xpath) matched_call <- xp_call_name(bad_expr) - second_const <- xml2::xml_find_first(bad_expr, second_const_xpath) + second_const <- xml_find_first(bad_expr, second_const_xpath) lint_message <- ifelse( is.na(second_const), paste(