Debian Codegraph @

R/call.R

raw
1
2 # given a call of the form e.g. 'pkg::foo()' or 'foo()',
3 # check that method 'foo()' is truly being called and
4 # strip off the 'pkg::' part for easier parsing.
5 renv_call_expect <- function(node, package, methods) {
6
7 result <- NULL
8
9 # check for call of the form 'pkg::foo(a, b, c)'
10 if (is.call(call <- node[[1L]]))
11 if (is.symbol(symbol <- call[[1L]]))
12 if (symbol == "::" || symbol == ":::")
13 if (call[[2L]] == package)
14 node[[1L]] <- call[[3L]]
15
16 # check for any method match
17 if (is.symbol(symbol <- node[[1L]]))
18 if (any(symbol == methods))
19 result <- node
20
21 result
22
23 }
24
25 renv_call_normalize <- function(node) {
26
27 # check for magrittr pipe -- if this part of the expression is
28 # being piped into, then we need to munge the call
29 ispipe <- renv_call_matches(node, names = c("%>%", "%T>%", "%<>%"))
30 if (!ispipe)
31 return(node)
32
33 # get lhs and rhs of piped expression
34 lhs <- node[[2L]]
35 rhs <- node[[3L]]
36
37 # handle rhs symbols
38 if (is.symbol(rhs))
39 rhs <- call(as.character(rhs))
40
41 # check for usage of '.'
42 # if it exists, replace each with lhs
43 hasdot <- FALSE
44 dot <- as.symbol(".")
45 for (i in seq_along(rhs)) {
46 if (identical(dot, rhs[[i]])) {
47 hasdot <- TRUE
48 rhs[[i]] <- lhs
49 }
50 }
51
52 if (hasdot)
53 return(rhs)
54
55 # otherwise, mutate rhs call with lhs passed as first argument
56 args <- as.list(rhs)
57 as.call(c(args[[1L]], lhs, args[-1L]))
58
59 }
60
61
62 renv_call_matches <- function(call, names, nargs = NULL) {
63
64 ok <- FALSE
65
66 if (is.call(call))
67 if (is.symbol(sym <- call[[1L]]))
68 if (any(names == sym))
69 ok <- is.null(nargs) || length(call) == nargs + 1L
70
71 ok
72
73 }