Skip to content

Commit 39f7a59

Browse files
Make $xml_find_function_calls() land on parent::expr instead (#2715)
* Initial progress migrating style for $xml_find_function_calls * rest of first pass on linters, docs * document updated to man/ * vestigial debugging comment * ws change for consistency with other edits --------- Co-authored-by: AshesITR <[email protected]>
1 parent fe958e4 commit 39f7a59

File tree

66 files changed

+267
-333
lines changed

Some content is hidden

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

66 files changed

+267
-333
lines changed

R/any_duplicated_linter.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
#' @export
3636
any_duplicated_linter <- function() {
3737
any_duplicated_xpath <- "
38-
parent::expr
39-
/following-sibling::expr[1][expr[1][SYMBOL_FUNCTION_CALL[text() = 'duplicated']]]
38+
following-sibling::expr[1][expr[1][SYMBOL_FUNCTION_CALL[text() = 'duplicated']]]
4039
/parent::expr[
4140
count(expr) = 2
4241
or (count(expr) = 3 and SYMBOL_SUB[text() = 'na.rm'])

R/any_is_na_linter.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
#' @export
3838
any_is_na_linter <- function() {
3939
any_xpath <- "
40-
parent::expr
41-
/following-sibling::expr[1][expr[1][SYMBOL_FUNCTION_CALL[text() = 'is.na']]]
40+
following-sibling::expr[1][expr[1][SYMBOL_FUNCTION_CALL[text() = 'is.na']]]
4241
/parent::expr[
4342
count(expr) = 2
4443
or (count(expr) = 3 and SYMBOL_SUB[text() = 'na.rm'])

R/backport_linter.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ backport_linter <- function(r_version = getRversion(), except = character()) {
5656
used_symbols <- xml_find_all(xml, "//SYMBOL")
5757
used_symbols <- used_symbols[xml_text(used_symbols) %in% names(backport_index)]
5858

59+
used_calls <- source_expression$xml_find_function_calls(names(backport_index))
5960
all_names_nodes <- combine_nodesets(
60-
source_expression$xml_find_function_calls(names(backport_index)),
61+
xml_find_first(used_calls, "SYMBOL_FUNCTION_CALL"),
6162
used_symbols
6263
)
6364
all_names <- xml_text(all_names_nodes)

R/boolean_arithmetic_linter.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,19 @@ boolean_arithmetic_linter <- function() {
3636
one_expr <- "(LT or GE) and expr[NUM_CONST[text() = '1' or text() = '1L']]"
3737
length_xpath <- glue("
3838
parent::expr
39-
/parent::expr
4039
/parent::expr[
4140
expr[SYMBOL_FUNCTION_CALL[text() = 'length']]
4241
and parent::expr[ ({zero_expr}) or ({one_expr})]
4342
]
4443
")
4544
sum_xpath <- glue("
46-
parent::expr
47-
/parent::expr[
48-
expr[
49-
expr[SYMBOL_FUNCTION_CALL[text() = 'grepl']]
50-
or (EQ or NE or GT or LT or GE or LE)
51-
] and parent::expr[ ({zero_expr}) or ({one_expr})]
45+
parent::expr[
46+
expr[
47+
expr[SYMBOL_FUNCTION_CALL[text() = 'grepl']]
48+
or (EQ or NE or GT or LT or GE or LE)
5249
]
53-
")
50+
and parent::expr[ ({zero_expr}) or ({one_expr})]
51+
]")
5452

5553
Linter(linter_level = "expression", function(source_expression) {
5654
length_calls <- source_expression$xml_find_function_calls(c("which", "grep"))

R/class_equals_linter.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
class_equals_linter <- function() {
3737
xpath <- "
3838
parent::expr
39-
/parent::expr
4039
/parent::expr[
4140
not(preceding-sibling::OP-LEFT-BRACKET)
4241
and (EQ or NE or SPECIAL[text() = '%in%'])

R/condition_call_linter.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ condition_call_linter <- function(display_call = FALSE) {
7777
msg_fmt <- "Use %s(., call. = FALSE) not to display the call in an error message."
7878
}
7979

80-
xpath <- glue::glue("parent::expr[{call_cond}]/parent::expr")
80+
xpath <- glue::glue("./self::*[{call_cond}]/parent::expr")
8181

8282
Linter(linter_level = "expression", function(source_expression) {
8383
xml_calls <- source_expression$xml_find_function_calls(c("stop", "warning"))

R/condition_message_linter.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@
4444
condition_message_linter <- function() {
4545
translators <- c("packageStartupMessage", "message", "warning", "stop")
4646
xpath <- glue("
47-
self::SYMBOL_FUNCTION_CALL[
47+
self::*[SYMBOL_FUNCTION_CALL[
4848
not(preceding-sibling::OP-DOLLAR or preceding-sibling::OP-AT)
49-
]
50-
/parent::expr
49+
]]
5150
/following-sibling::expr[
5251
expr[1][SYMBOL_FUNCTION_CALL[text() = 'paste' or text() = 'paste0']]
5352
and not(SYMBOL_SUB[text() = 'collapse'])

R/conjunct_test_linter.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,16 @@ conjunct_test_linter <- function(allow_named_stopifnot = TRUE,
7979
allow_filter <- match.arg(allow_filter)
8080

8181
expect_true_assert_that_xpath <- "
82-
parent::expr
83-
/following-sibling::expr[1][AND2]
82+
following-sibling::expr[1][AND2]
8483
/parent::expr
8584
"
8685
named_stopifnot_condition <- if (allow_named_stopifnot) "and not(preceding-sibling::*[1][self::EQ_SUB])" else ""
8786
stopifnot_xpath <- glue("
88-
parent::expr
89-
/following-sibling::expr[1][AND2 {named_stopifnot_condition}]
87+
following-sibling::expr[1][AND2 {named_stopifnot_condition}]
9088
/parent::expr
9189
")
9290
expect_false_xpath <- "
93-
parent::expr
94-
/following-sibling::expr[1][OR2]
91+
following-sibling::expr[1][OR2]
9592
/parent::expr
9693
"
9794

@@ -101,7 +98,7 @@ conjunct_test_linter <- function(allow_named_stopifnot = TRUE,
10198
always = "true"
10299
)
103100
filter_xpath <- glue("
104-
parent::expr[{ filter_ns_cond }]
101+
self::*[{ filter_ns_cond }]
105102
/parent::expr
106103
/expr[AND]
107104
")

R/consecutive_assertion_linter.R

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ consecutive_assertion_linter <- function() {
3535
next_expr <- "following-sibling::*[self::expr or self::expr_or_assign_or_help or self::equal_assign][1]"
3636

3737
stopifnot_xpath <- glue("
38-
parent::expr
39-
/parent::expr[
40-
expr[1]/SYMBOL_FUNCTION_CALL = {next_expr}/expr[1]/SYMBOL_FUNCTION_CALL
41-
]
42-
")
38+
parent::expr[
39+
expr[1]/SYMBOL_FUNCTION_CALL = {next_expr}/expr[1]/SYMBOL_FUNCTION_CALL
40+
]")
4341
assert_that_xpath <- glue("
44-
parent::expr
45-
/parent::expr[
46-
not(SYMBOL_SUB[text() = 'msg'])
47-
and not(following-sibling::expr[1]/SYMBOL_SUB[text() = 'msg'])
48-
and expr[1]/SYMBOL_FUNCTION_CALL = {next_expr}/expr[1]/SYMBOL_FUNCTION_CALL
49-
]
50-
")
42+
parent::expr[
43+
not(SYMBOL_SUB[text() = 'msg'])
44+
and not(following-sibling::expr[1]/SYMBOL_SUB[text() = 'msg'])
45+
and expr[1]/SYMBOL_FUNCTION_CALL = {next_expr}/expr[1]/SYMBOL_FUNCTION_CALL
46+
]")
5147

5248
Linter(linter_level = "file", function(source_expression) {
5349
# need the full file to also catch usages at the top level

R/consecutive_mutate_linter.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
#' @export
3939
consecutive_mutate_linter <- function(invalid_backends = "dbplyr") {
4040
attach_pkg_xpath <- "
41-
parent::expr
42-
/following-sibling::expr
41+
following-sibling::expr
4342
/*[self::SYMBOL or self::STR_CONST]
4443
"
4544

0 commit comments

Comments
 (0)