Skip to content

Commit

Permalink
remove all comments immediately after parsing to avoid issues where c…
Browse files Browse the repository at this point in the history
…omments are between items
  • Loading branch information
RobertASmith committed Feb 7, 2024
1 parent 7946c73 commit 16ab6fa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
27 changes: 18 additions & 9 deletions R/cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,36 @@ extract_function_name <- function(string){
#' @importFrom dplyr lead mutate
#'
#' @export
#'
#' @example
#' \dontrun{
#' find_function_definitions(filename = "tests/testthat/example_scripts/example_tricky_functions.R")

#' }
find_function_definitions <- function(filename){

df <- utils::getParseData(parse(filename, keep.source = TRUE), includeText = TRUE)

# Get the records of all the function and assign keywords

# remove comments before doing anything else
df <- df[df$token != "COMMENT",]

This comment has been minimized.

Copy link
@RobertASmith

RobertASmith Feb 7, 2024

Author Contributor

@Smit-tay I made one change here which removes all lines of the parseData that relate to comments.

Otherwise this code:

dplyr::lead(left_assign, n = 2, default = FALSE)
                      & dplyr::lead(fun_decs, n = 4, default = FALSE)

broke for this example

# Example function with lots of comments everywhere
# Messy function 10: add comment between
#' a comment
     lots_of_comments_foo <- # comment here
  # comment between equals and function call
     function( # weirdly, a commment here
    data # comment here
           ) {
    # Combine multiple steps, hard to follow
    result <- process_data(data) # comment here
    result <- transform_data(result) # comment here
    output <- generate_output(result) # comment here
    return(output) # another comment here
    # final comment
  }

Because left_assign and fun_decs have something between them.


# Get the records of all the function and assign keywords
left_assign <- (df$token == "EQ_ASSIGN" | df$token == "LEFT_ASSIGN")
fun_decs <- df$token == "FUNCTION"

# This indicates a the current index (type SYMBOL) is followed by
# This indicates a the current index (type SYMBOL) is followed by
# an XXX_ASSIGN, then the FUNCTION keyword, anything else isn't a named function.
#
# So, even though df is not directly referenced within which(),
# the logical vectors left_assign and fun_decs, which are derived
# from df$token, are used to determine the positions where the

# So, even though df is not directly referenced within which(),
# the logical vectors left_assign and fun_decs, which are derived
# from df$token, are used to determine the positions where the
# conditions are met. These positions are then used to subset df.
name_pos <- which( dplyr::lead(left_assign, n = 2, default = FALSE)
name_pos <- which( dplyr::lead(left_assign, n = 2, default = FALSE)
& dplyr::lead(fun_decs, n = 4, default = FALSE) )

# only return the pd rows matching name_pos IDs
funcs <- df[name_pos, ]
# Add in the source file name to the result set
# Add in the source file name to the result set
funcs <- dplyr::mutate(funcs, source = filename)
return(funcs)
}
Expand Down
26 changes: 23 additions & 3 deletions tests/testthat/example_scripts/example_tricky_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ generate_output <- function() {
return(output)
}

# Messy function 10: Overly complex, hard to maintain
do_everything <- function(data) {
# Messy function 10: add comment between
do_everything <-
# comment between equals and function call
function(data) {
# Combine multiple steps, hard to follow
result <- process_data(data)
result <- transform_data(result)
Expand All @@ -85,6 +87,24 @@ do_everything <- function(data) {

# Example Lambda (anonymous) function
# Tests that Lambdas are detected as named functions.
output <-
output <-
(function(x, y) x * y)(3, 4)
print(output)



# Example function with lots of comments everywhere
# Messy function 10: add comment between
#' a comment
lots_of_comments_foo <- # comment here
# comment between equals and function call
function( # weirdly, a commment here
data # comment here
) {
# Combine multiple steps, hard to follow
result <- process_data(data) # comment here
result <- transform_data(result) # comment here
output <- generate_output(result) # comment here
return(output) # another comment here
# final comment
}
24 changes: 13 additions & 11 deletions tests/testthat/test-cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,24 @@ test_that("get_folder_cheers_classifications works for an example project",
test_that("find_function_definitions works as intended",
{
expected = c(
"do_something_random"
, "calculate_something"
, "find_matches"
, "perform_task"
, "combine_strings"
, "process_data"
, "transform_data"
, "sort_values"
, "generate_output"
, "do_everything")
"do_something_random",
"calculate_something",
"find_matches",
"perform_task",
"combine_strings",
"process_data",
"transform_data",
"sort_values",
"generate_output",
"do_everything",
"lots_of_comments_foo"
)


object = find_function_definitions(
filename = testthat::test_path("example_scripts", "example_tricky_functions.R"))
object <- object$text
expect_equal(object, expected )
expect_equal(object, expected)
})


Expand Down

0 comments on commit 16ab6fa

Please sign in to comment.