Skip to content

Commit

Permalink
Add :: and ::: detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Oct 18, 2024
1 parent 47eaeaf commit 227aaa8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 56 deletions.
37 changes: 23 additions & 14 deletions R/scan-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ s_expr <- function(code) {
# we use these as fallbacks. If a call is not identified some other way
# we parse it with R and match the call.
q_library_0 <- function() {
'((call function: (identifier) @fn-name) @call-code
'((call function: (identifier) @fn-name) @dep-code
(#any-of? @fn-name "library" "require")
)'
}
Expand All @@ -53,19 +53,28 @@ q_library_1 <- function() {
(string (string_content) @pkg-name)
]) . )
(#any-of? @fn-name "library" "require")
) @call-code'
) @dep-code'
}

q_library <- function() {
c(q_library_0 = q_library_0(), q_library_1 = q_library_1())
q_colon <- function() {
'(namespace_operator lhs: (identifier) @pkg-name) @dep-code'
}

q_deps <- function() {
c(
q_library_0 = q_library_0(),
q_library_1 = q_library_1(),
q_colon = q_colon()
)
}

scan_path_deps <- function(path) {
code <- readBin(path, "raw", file.size(path))
has_library <- length(grepRaw("library", code, fixed = TRUE)) > 0
has_require <- length(grepRaw("require", code, fixed = TRUE)) > 0
has_colon <- length(grepRaw("::", code, fixed = TRUE)) > 0

deps <- if (has_library || has_require) {
deps <- if (has_library || has_require || has_colon) {
scan_path_deps_do(code, path)
} else {
scan_path_deps_empty()
Expand All @@ -86,7 +95,7 @@ scan_path_deps_empty <- function() {
}

scan_path_deps_do <- function(code, path) {
hits <- code_query(code, q_library())
hits <- code_query(code, q_deps())
# q_library_0 hits are generic ones, only use them if they are not hit
gen_pat <- hits$patterns$id[hits$patterns$name == "q_library_0"]
gen_hits <- hits$matched_captures[hits$matched_captures$pattern == gen_pat, ]
Expand All @@ -103,15 +112,15 @@ scan_path_deps_do_ok_hits <- function(hits, path) {
data_frame(
path = path,
package = hits$code[hits$name == "pkg-name"],
code = hits$code[hits$name == "call-code"],
start_row = hits$start_row[hits$name == "call-code"],
start_column = hits$start_column[hits$name == "call-code"],
start_byte = hits$start_byte[hits$name == "call-code"]
code = hits$code[hits$name == "dep-code"],
start_row = hits$start_row[hits$name == "dep-code"],
start_column = hits$start_column[hits$name == "dep-code"],
start_byte = hits$start_byte[hits$name == "dep-code"]
)
}

scan_path_deps_do_gen_hits <- function(hits, path) {
code <- hits$code[hits$name == "call-code"]
code <- hits$code[hits$name == "dep-code"]
fn <- hits$code[hits$name == "fn-name"]
pkgs <- vcapply(seq_along(code), function(i) {
parse_pkg_from_library_call(fn[i], code[i])
Expand All @@ -121,9 +130,9 @@ scan_path_deps_do_gen_hits <- function(hits, path) {
path = path,
package = pkgs[ok],
code = code[ok],
start_row = hits$start_row[hits$name == "call-code"][ok],
start_column = hits$start_column[hits$name == "call-code"][ok],
start_byte = hits$start_byte[hits$name == "call-code"][ok]
start_row = hits$start_row[hits$name == "dep-code"][ok],
start_column = hits$start_column[hits$name == "dep-code"][ok],
start_byte = hits$start_byte[hits$name == "dep-code"][ok]
)
}

Expand Down
84 changes: 42 additions & 42 deletions tests/testthat/_snaps/scan-dependencies-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,81 @@
do("library(pkg, lib.loc = path)")
Output
# A data frame: 2 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 call-code library(pkg, ~
2 1 1 1 1 1 1 fn-name library
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 dep-code library(pkg, l~
2 1 1 1 1 1 1 fn-name library
Code
do("library('pkg', lib.loc = path)")
Output
# A data frame: 2 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 call-code library('pkg'~
2 1 1 1 1 1 1 fn-name library
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 dep-code library('pkg',~
2 1 1 1 1 1 1 fn-name library
Code
do("library(lib.loc = path, pkg)")
Output
# A data frame: 2 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 call-code library(lib.l~
2 1 1 1 1 1 1 fn-name library
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 dep-code library(lib.lo~
2 1 1 1 1 1 1 fn-name library
Code
do("require(lib.loc = path, character.only = TRUE, 'pkg')")
Output
# A data frame: 2 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 call-code require(lib.l~
2 1 1 1 1 1 1 fn-name require
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 dep-code require(lib.lo~
2 1 1 1 1 1 1 fn-name require
Code
do("library(foo, require(bar))")
Output
# A data frame: 4 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 call-code library(foo, ~
2 1 1 1 1 1 1 fn-name library
3 2 1 2 14 1 14 call-code require(bar)
4 1 1 2 14 1 14 fn-name require
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 2 1 1 1 1 1 dep-code library(foo, r~
2 1 1 1 1 1 1 fn-name library
3 2 1 2 14 1 14 dep-code require(bar)
4 1 1 2 14 1 14 fn-name require

# q_library_1

Code
code_query("library(pkg)", q_library_1())[["matched_captures"]]
Output
# A data frame: 3 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 call-code library(pkg)
2 1 1 1 1 1 1 fn-name library
3 2 1 1 9 1 9 pkg-name pkg
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 dep-code library(pkg)
2 1 1 1 1 1 1 fn-name library
3 2 1 1 9 1 9 pkg-name pkg
Code
code_query("library('pkg')", q_library_1())[["matched_captures"]]
Output
# A data frame: 3 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 call-code library('pkg')
2 1 1 1 1 1 1 fn-name library
3 2 1 1 10 1 10 pkg-name pkg
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 dep-code library('pkg')
2 1 1 1 1 1 1 fn-name library
3 2 1 1 10 1 10 pkg-name pkg
Code
code_query("require(pkg)", q_library_1())[["matched_captures"]]
Output
# A data frame: 3 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 call-code require(pkg)
2 1 1 1 1 1 1 fn-name require
3 2 1 1 9 1 9 pkg-name pkg
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 dep-code require(pkg)
2 1 1 1 1 1 1 fn-name require
3 2 1 1 9 1 9 pkg-name pkg
Code
code_query("require('pkg')", q_library_1())[["matched_captures"]]
Output
# A data frame: 3 x 8
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 call-code require('pkg')
2 1 1 1 1 1 1 fn-name require
3 2 1 1 10 1 10 pkg-name pkg
id pattern match start_byte start_row start_column name code
<int> <int> <int> <int> <int> <int> <chr> <chr>
1 3 1 1 1 1 1 dep-code require('pkg')
2 1 1 1 1 1 1 fn-name require
3 2 1 1 10 1 10 pkg-name pkg

0 comments on commit 227aaa8

Please sign in to comment.