From bcc9c5f6f2a7ea0f796eeb774a443f9cf03f8920 Mon Sep 17 00:00:00 2001 From: m7pr Date: Wed, 6 Nov 2024 14:08:55 +0100 Subject: [PATCH] reorder and put more explanation to usage of extract_calls --- R/utils-get_code_dependency.R | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/R/utils-get_code_dependency.R b/R/utils-get_code_dependency.R index 8c2ff1ee..c9e0024a 100644 --- a/R/utils-get_code_dependency.R +++ b/R/utils-get_code_dependency.R @@ -318,6 +318,19 @@ extract_side_effects <- function(pd) { unlist(strsplit(sub("\\s*#\\s*@linksto\\s+", "", linksto), "\\s+")) } +#' @param parsed_code results of `parse(text = code, keep.source = TRUE` (parsed text) +#' @keywords internal +#' @noRd +extract_dependency <- function(parsed_code) { + pd <- normalize_pd(utils::getParseData(parsed_code)) + reordered_pd <- extract_calls(pd)[[1]] + # extract_calls is needed to reorder the pd so that assignment operator comes before symbol names + # extract_calls is needed also to substitute assignment operators into specific format with fix_arrows + # extract_calls is needed to omit empty calls that contain only one token `"';'"` + # This cleaning is needed as extract_occurrence assumes arrows are fixed, and order is different than in original pd. + c(extract_side_effects(reordered_pd), extract_occurrence(reordered_pd)) +} + # graph_parser ---- #' Return the indices of calls needed to reproduce an object @@ -384,6 +397,10 @@ detect_libraries <- function(graph) { ) } + +# utils ----------------------------------------------------------------------------------------------------------- + + #' Normalize parsed data removing backticks from symbols #' #' @param pd `data.frame` resulting from `utils::getParseData()` call. @@ -400,6 +417,10 @@ normalize_pd <- function(pd) { pd } + +# split_code ------------------------------------------------------------------------------------------------------ + + #' Get line/column in the source where the calls end #' #' @@ -458,11 +479,3 @@ split_code <- function(code) { gsub("^([[:space:]])*;(.+)$", "\\1\\2", new_code, perl = TRUE) } -#' @param parsed_code results of `parse(text = code, keep.source = TRUE` (parsed text) -#' @keywords internal -#' @noRd -extract_dependency <- function(parsed_code) { - pd <- normalize_pd(utils::getParseData(parsed_code)) - reordered_pd <- extract_calls(pd)[[1]] # because ... - c(extract_side_effects(reordered_pd), extract_occurrence(reordered_pd)) -}