Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ubiquitously-called internal functions to NAMESPACE #2031

Merged
merged 22 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export(xml_nodes_to_lints)
export(yoda_test_linter)
importFrom(cyclocomp,cyclocomp)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(glue,glue_data)
importFrom(rex,character_class)
importFrom(rex,re_matches)
importFrom(rex,re_substitutes)
Expand All @@ -155,4 +157,10 @@ importFrom(utils,head)
importFrom(utils,relist)
importFrom(utils,tail)
importFrom(xml2,as_list)
importFrom(xml2,xml_attr)
importFrom(xml2,xml_attrs)
importFrom(xml2,xml_find_all)
importFrom(xml2,xml_find_chr)
importFrom(xml2,xml_find_first)
importFrom(xml2,xml_find_num)
importFrom(xml2,xml_text)
6 changes: 3 additions & 3 deletions R/T_and_F_symbol_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ T_and_F_symbol_linter <- function() { # nolint: object_name.
return(list())
}

bad_exprs <- xml2::xml_find_all(source_expression$xml_parsed_content, xpath)
bad_assigns <- xml2::xml_find_all(source_expression$xml_parsed_content, xpath_assignment)
bad_exprs <- xml_find_all(source_expression$xml_parsed_content, xpath)
bad_assigns <- xml_find_all(source_expression$xml_parsed_content, xpath_assignment)

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,
Expand Down
8 changes: 4 additions & 4 deletions R/any_duplicated_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down Expand Up @@ -92,17 +92,17 @@ 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,
lint_message = "anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).",
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)"
)
Expand Down
2 changes: 1 addition & 1 deletion R/any_is_na_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions R/assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ 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 <-)."
lint_message_fmt[operator == "%<>%"] <-
"Avoid the assignment pipe %s; prefer using <- and %%>%% separately."

if (!allow_trailing) {
bad_trailing_expr <- xml2::xml_find_all(xml, trailing_assign_xpath)
trailing_assignments <- xml2::xml_attrs(bad_expr) %in% xml2::xml_attrs(bad_trailing_expr)
bad_trailing_expr <- xml_find_all(xml, trailing_assign_xpath)
trailing_assignments <- xml_attrs(bad_expr) %in% xml_attrs(bad_trailing_expr)
lint_message_fmt[trailing_assignments] <- "Assignment %s should not be trailing at the end of a line."
}

Expand Down
4 changes: 2 additions & 2 deletions R/backport_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions R/boolean_arithmetic_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions R/brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
]")
Expand All @@ -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)
Expand All @@ -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])]"

Expand Down Expand Up @@ -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."
Expand All @@ -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."
)
Expand All @@ -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."
Expand All @@ -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 `}`."
)
Expand All @@ -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."
)
Expand All @@ -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."
)
Expand Down
4 changes: 2 additions & 2 deletions R/class_equals_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.<class> or is(x, 'class')",
operator
Expand Down
4 changes: 2 additions & 2 deletions R/commas_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ 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
range_end_xpath = "number(./@col1 - 1)" # end before comma
)

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
Expand Down
4 changes: 2 additions & 2 deletions R/comment_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/condition_message_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)} ]
/parent::expr
/following-sibling::expr[
Expand All @@ -59,7 +59,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("", " ")]
Expand Down
6 changes: 3 additions & 3 deletions R/conjunct_test_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand All @@ -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)",
Expand Down
2 changes: 1 addition & 1 deletion R/consecutive_assertion_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion R/declared_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions R/duplicate_argument_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion R/empty_assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions R/equals_na_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion R/exclude.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
}
Expand Down
Loading