-
Notifications
You must be signed in to change notification settings - Fork 118
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
Impute srcrefs for subexpressions #154
Merged
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
72ad35c
first stab at imputing srcrefs for subexpressions
70823d1
don't include text
f6d9d1c
use srcref()
4982d94
use devtools::load_all() instead of loadNamespace()
48fa2c6
fix coverage results
12cde4e
lintr
954437c
Revert "use devtools::load_all() instead of loadNamespace()"
3a8b454
repair parse data
1eab556
make use of get_parse_data()
b79cd73
move code
bf38a63
fix tests
4e15c61
how it works
8e75384
lint
6fb1120
test functionality
8c694ea
faster version of getParseData()
e1ce0c9
corrected values
c2984f9
lintr
e74d24c
don't treat repeat, usually needs compound statement anyway
da897b7
safeguard
8ce080c
NEWS
687d7a5
comment
6442dd8
add braces
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
repair_parse_data <- function(env) { | ||
srcref <- lapply(as.list(env), attr, "srcref") | ||
srcfile <- lapply(srcref, attr, "srcfile") | ||
parse_data <- compact(lapply(srcfile, "[[", "parseData")) | ||
if (length(parse_data) == 0L) { | ||
warning(paste("Parse data not found, coverage may be inaccurate. Try", | ||
"declaring a function in the last file of your R package."), | ||
call. = FALSE) | ||
return() | ||
} | ||
|
||
if (!all_identical(parse_data)) { | ||
warning("Ambiguous parse data, coverage may be inaccurate.", | ||
call. = FALSE) | ||
} | ||
|
||
original <- compact(lapply(srcfile, "[[", "original")) | ||
if (!all_identical(parse_data)) { | ||
warning("Ambiguous original file, coverage may be inaccurate.", | ||
call. = FALSE) | ||
} | ||
|
||
original[[1]][["parseData"]] <- parse_data[[1L]] | ||
} | ||
|
||
get_parse_data <- function(x) { | ||
if (inherits(x, "srcref")) | ||
get_parse_data(attr(x, "srcfile")) | ||
else if (exists("original", x)) | ||
get_parse_data(x$original) | ||
else if (exists("covr_parse_data", x)) | ||
x$covr_parse_data | ||
else if (!is.null(data <- x[["parseData"]])) { | ||
tokens <- attr(data, "tokens") | ||
data <- t(unclass(data)) | ||
colnames(data) <- c("line1", "col1", "line2", "col2", | ||
"terminal", "token.num", "id", "parent") | ||
x$covr_parse_data <- | ||
data.frame(data[, -c(5, 6), drop = FALSE], token = tokens, | ||
terminal = as.logical(data[, "terminal"]), | ||
stringsAsFactors = FALSE) | ||
x$covr_parse_data | ||
} | ||
} | ||
|
||
|
||
impute_srcref <- function(x, parent_ref) { | ||
if (length(as.character(x[[1L]])) != 1L) return() | ||
|
||
if (is.null(parent_ref)) return(NULL) | ||
pd <- get_parse_data(parent_ref) | ||
pd_expr <- | ||
pd$line1 == parent_ref[[7L]] & | ||
pd$col1 == parent_ref[[2L]] & | ||
pd$line2 == parent_ref[[8L]] & | ||
pd$col2 == parent_ref[[4L]] & | ||
pd$token == "expr" | ||
pd_expr_idx <- which(pd_expr) | ||
if (length(pd_expr_idx) == 0L) return(NULL) # srcref not found in parse data | ||
|
||
stopifnot(length(pd_expr_idx) == 1L) | ||
expr_id <- pd$id[pd_expr_idx] | ||
pd_child <- pd[pd$parent == expr_id, ] | ||
|
||
line_offset <- parent_ref[[7L]] - parent_ref[[1L]] | ||
|
||
make_srcref <- function(from, to = from) { | ||
srcref( | ||
attr(parent_ref, "srcfile"), | ||
c(pd_child$line1[from] - line_offset, | ||
pd_child$col1[from], | ||
pd_child$line2[to] - line_offset, | ||
pd_child$col2[to], | ||
pd_child$col1[from], | ||
pd_child$col2[to], | ||
pd_child$line1[from], | ||
pd_child$line2[to] | ||
)) | ||
} | ||
|
||
switch( | ||
as.character(x[[1L]]), | ||
"if" = { | ||
src_ref <- list( | ||
NULL, | ||
make_srcref(2, 4), | ||
make_srcref(5), | ||
make_srcref(6, 7) | ||
) | ||
src_ref[seq_along(x)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this to handle if's without else's right? Might be worth adding a comment so it is clear why the |
||
}, | ||
|
||
"for" = { | ||
list( | ||
NULL, | ||
NULL, | ||
make_srcref(2), | ||
make_srcref(3) | ||
) | ||
}, | ||
|
||
"while" = { | ||
list( | ||
NULL, | ||
make_srcref(3), | ||
make_srcref(5) | ||
) | ||
}, | ||
|
||
NULL | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
#' | ||
#' @export | ||
test_me <- function(x, y){ | ||
x + y | ||
if (TRUE) | ||
x + y | ||
else | ||
x - y | ||
} | ||
|
||
#' @export | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put braces around the bodies of these conditionals, I prefer to be explicit to avoid issues in the future when statements are added and someone forgets to add braces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah