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

include rlang functions in literal coercion #1437

Merged
merged 19 commits into from
Jul 2, 2022
Merged
Changes from 1 commit
Commits
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
28 changes: 10 additions & 18 deletions R/literal_coercion_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,24 @@ literal_coercion_linter <- function() {
base_coercers <- xp_text_in_table(
paste0("as.", c("logical", "integer", "numeric", "double", "character"))
)

# notes for clarification:
# - as.integer(1e6) is arguably easier to read than 1000000L
# - in x$"abc", the "abc" STR_CONST is at the top level, so exclude OP-DOLLAR
# - need condition against STR_CONST w/ EQ_SUB to skip quoted keyword arguments (see tests)
# - for {rlang} coercers, both `int(1)` and `int(1, )` need to be linted
xpath_e <- "expr[2][
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
not(OP-DOLLAR)
and (
NUM_CONST[not(contains(translate(text(), 'E', 'e'), 'e'))]
or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])]
)
]"
xpath <- glue::glue("//expr[
(
expr[1][SYMBOL_FUNCTION_CALL[ {base_coercers} ]]
and expr[2][
not(OP-DOLLAR)
and (
NUM_CONST[not(contains(translate(text(), 'E', 'e'), 'e'))]
or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])]
)
]
expr[1][SYMBOL_FUNCTION_CALL[ {base_coercers} ]] and {xpath_e}
) or (
(
expr[1][SYMBOL_FUNCTION_CALL[ {rlang_coercers} ]]
and expr[2][
not(OP-DOLLAR)
and (
NUM_CONST[not(contains(translate(text(), 'E', 'e'), 'e'))]
or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])]
)]
) and
count(expr) = 2
expr[1][SYMBOL_FUNCTION_CALL[ {rlang_coercers} ]] and {xpath_e} and count(expr) = 2
)
]")

Expand Down