-
Notifications
You must be signed in to change notification settings - Fork 186
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
first pass at implementing a backport_linter #622
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dbff7ca
first pass at implementing a backport_linter
MichaelChirico 8662f64
need Collate order
MichaelChirico 7fcda6a
good job lintr bot
MichaelChirico 99f03f0
incorporate some feedback
MichaelChirico e3b7efe
Merge branch 'master' into backports-warning
MichaelChirico 2e29980
rename to devel per good suggestion
MichaelChirico 3bb6c3c
Merge branch 'master' into backports-warning
MichaelChirico 51173cc
update tests
MichaelChirico d6ca8d5
add test of really old version; remove test of r-devel
MichaelChirico 33c952b
Merge branch 'backports-warning' of github.com:jimhester/lintr into b…
MichaelChirico 038df29
Merge branch 'master' into backports-warning
MichaelChirico e9a54d4
Merge branch 'master' into backports-warning
MichaelChirico c653b51
Merge branch 'master' into backports-warning
MichaelChirico 3dad15a
Merge branch 'master' into backports-warning
AshesITR b141e3a
Merge branch 'master' into backports-warning
MichaelChirico f893126
Merge branch 'master' into backports-warning
MichaelChirico ca8c49a
Merge branch 'master' into backports-warning
MichaelChirico 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,86 @@ | ||
#' @describeIn linters that checks for usage of unavailable functions. Not reliable for testing r-devel dependencies. | ||
#' @export | ||
backport_linter <- function(r_version = getRversion()) { | ||
function(source_file) { | ||
if (inherits(r_version, "numeric_version")) r_version <- format(r_version) | ||
if (r_version < "3.0.0") { | ||
warning("It is not recommended to depend on an R version older than 3.0.0. Resetting 'r_version' to 3.0.0.") | ||
r_version <- "3.0.0" | ||
} | ||
if (is.null(source_file$xml_parsed_content)) return(list()) | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
names_xpath <- "//*[self::SYMBOL or self::SYMBOL_FUNCTION_CALL]" | ||
all_names_nodes <- xml2::xml_find_all(xml, names_xpath) | ||
all_names <- xml2::xml_text(all_names_nodes) | ||
|
||
# guaranteed to include 1 by early return above; which.min fails if all TRUE (handled by nomatch) | ||
needs_backport_names <- backports[1:(match(FALSE, r_version < names(backports), nomatch = length(backports)) - 1L)] | ||
|
||
# not sapply/vapply, which may over-simplify to vector -- cbind makes sure we have a matrix so rowSums works | ||
needs_backport <- do.call(cbind, lapply(needs_backport_names, function(nm) all_names %in% nm)) | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bad_idx <- rowSums(needs_backport) > 0L | ||
|
||
lapply(which(bad_idx), function(ii) { | ||
node <- all_names_nodes[[ii]] | ||
line1 <- xml2::xml_attr(node, "line1") | ||
col1 <- as.integer(xml2::xml_attr(node, "col1")) | ||
if (xml2::xml_attr(node, "line2") == line1) { | ||
col2 <- as.integer(xml2::xml_attr(node, "col2")) | ||
} else { | ||
col2 <- nchar(source_file$lines[line1]) | ||
} | ||
Lint( | ||
filename = source_file$filename, | ||
line_number = as.integer(line1), | ||
column_number = col1, | ||
type = "warning", | ||
message = sprintf( | ||
"%s (R %s) is not available for dependency R >= %s.", | ||
all_names[ii], names(needs_backport_names)[which(needs_backport[ii, ])], r_version | ||
), | ||
line = source_file$lines[[line1]], | ||
ranges = list(c(col1, col2)), | ||
linter = "backport_linter" | ||
) | ||
}) | ||
} | ||
} | ||
|
||
backports <- list( | ||
`devel` = c("...names", "checkRdContents", "numToBits", "numToInts", "packBits"), | ||
`4.0.0` = c( | ||
".class2", ".S3method", "activeBindingFunction", "deparse1", "globalCallingHandlers", | ||
"infoRDS", "list2DF", "marginSums", "proportions", "R_user_dir", "socketTimeout", "tryInvokeRestart" | ||
), | ||
`3.6.0` = c( | ||
"asplit", "hcl.colors", "hcl.pals", "mem.maxNsize", "mem.maxVsize", "nullfile", "str2lang", | ||
"str2expression", "update_PACKAGES" | ||
), | ||
`3.5.0` = c("...elt", "...length", "askYesNo", "getDefaultCluster", "isFALSE", "packageDate", "warnErrList"), | ||
`3.4.0` = c( | ||
"check_packages_in_dir_details", "CRAN_package_db", "debugcall", "hasName", | ||
"isS3stdgeneric", "strcapture", "Sys.setFileTime", "undebugcall" | ||
), | ||
`3.3.0` = c( | ||
".traceback", "chkDots", "curlGetHeaders", "endsWith", "grouping", "isS3method", | ||
"makevars_site", "makevars_user", "Rcmd", "sigma", "startsWith", "strrep", "validEnc", "validUTF8" | ||
), | ||
`3.2.0` = c( | ||
".getNamespaceInfo", "check_packages_in_dir_changes", "debuggingState", | ||
"dir.exists", "dynGet", "extSoftVersion", "get0", "grSoftVersion", "hsearch_db", | ||
"isNamespaceLoaded", "lengths", "libcurlVersion", "returnValue", "tclVersion", "toTitleCase", "trimws" | ||
), | ||
`3.1.3` = "pcre_config", | ||
`3.1.2` = "icuGetCollate", | ||
`3.1.1` = c(".nknots.smspl", "promptImport"), | ||
`3.1.0` = c("agrepl", "anyNA", "changedFiles", "cospi", "fileSnapshot", "find_gs_cmd", "sinpi", "tanpi"), | ||
`3.0.3` = "La_version", | ||
`3.0.2` = c("assertCondition", "assertError", "assertWarning", "getVignetteInfo"), | ||
`3.0.0` = c( | ||
".onDetach", "bitwAnd", "bitwNot", "bitwOr", "bitwShiftL", "bitwShiftR", "bitwXor", | ||
"check_packages_in_dir", "cite", "citeNatbib", "clearPushBack", "packageName", | ||
"process.events", "provideDimnames", "quartz.save", "rep_len" | ||
) | ||
) |
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,32 @@ | ||
test_that("backport_linter detects backwards-incompatibility", { | ||
# default should be current R version; all of these are included on our dependency | ||
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter()) | ||
|
||
# don't allow dependencies older than we've recorded | ||
writeLines("x <- x + 1", tmp <- tempfile()) | ||
on.exit(unlink(tmp)) | ||
|
||
expect_warning(l <- lint(tmp, backport_linter("2.0.0")), "version older than 3.0.0", fixed = TRUE) | ||
expect_identical(l, lint(tmp, backport_linter("3.0.0"))) | ||
|
||
expect_lint( | ||
"numToBits(2)", | ||
rex("numToBits (R devel) is not available for dependency R >= 4.0.0."), | ||
backport_linter("4.0.0") | ||
) | ||
# symbols as well as calls | ||
expect_lint( | ||
"lapply(1:10, numToBits)", | ||
rex("numToBits (R devel) is not available for dependency R >= 4.0.0."), | ||
backport_linter("4.0.0") | ||
) | ||
|
||
expect_lint( | ||
"trimws(...names())", | ||
list( | ||
rex("trimws (R 3.2.0) is not available for dependency R >= 3.0.0."), | ||
rex("...names (R devel) is not available for dependency R >= 3.0.0.") | ||
), | ||
backport_linter("3.0.0") | ||
) | ||
}) |
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.
We should use
numeric_version
comparison here, else it will bite us when R 10.0.0 rolls out ;)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.
haha that can be a problem for our future selves -- after 2050 at the current rate 😸
I prefer this way because the test below makes the same assumption by doing
r_version < names(backports)
So really this assumption is backed into
names(backports)
as well. Maybe by R 10.0 R will allow objects to have objects as names like python