Skip to content

Commit

Permalink
Merge pull request #176 from ropensci-review-tools/seq-linter
Browse files Browse the repository at this point in the history
rm internal 'seq_linter' for lintr version to fix #172
  • Loading branch information
mpadge authored Sep 26, 2024
2 parents c3b60fb + 10553fb commit 80d6482
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 89 deletions.
71 changes: 0 additions & 71 deletions R/my_linters.R
Original file line number Diff line number Diff line change
@@ -1,74 +1,3 @@

#' @importFrom lintr Lint

#' Find dangerous 1:x expressions
#'
#' Find occurrences of \code{1:length(x)}, \code{1:nrow(x)},
#' \code{1:ncol(x)}, \code{1:NROW(x)}, \code{1:NCOL(x)} where
#' \code{x} is an R expression.
#'
#' @return Lint object.
#'
#' @importFrom xmlparsedata xml_parse_data
#' @importFrom xml2 read_xml xml_find_all xml_text xml_children xml_attr
#' @keywords internal

seq_linter <- function() lintr::Linter(function(source_file) {

if (!length(source_file$parsed_content)) return(list())

xml <- if ('xml_parsed_content' %in% names(source_file)) {
source_file$xml_parsed_content
} else {
read_xml(xml_parse_data(source_file$parsed_content))
}

bad_funcs <- c("length", "nrow", "ncol", "NROW", "NCOL")
text_clause <- paste0("text() = '", bad_funcs, "'", collapse = " or ")

xpath <- paste0(
"//expr",
"[expr[NUM_CONST[text()='1' or text()='1L']]]",
"[OP-COLON]",
"[expr[expr[SYMBOL_FUNCTION_CALL[", text_clause, "]]]]"
)

badx <- xml_find_all(xml, xpath)

## The actual order of the nodes is document order
## In practice we need to handle length(x):1
get_fun <- function(x, n) {
funcall <- xml_children(xml_children(x)[[n]])
if (!length(funcall)) return(NULL)
fun <- trim_ws(xml_text(funcall[[1]]))
if (! fun %in% bad_funcs) fun else paste0(fun, "(...)")
}

## Unfortunately the more natural lapply(badx, ...) does not work,
## because badx looses its class for length() and/or [[
lapply(
seq_along(badx),
function(i) {
x <- badx[[i]]
f1 <- get_fun(x, 1)
f2 <- get_fun(x, 3)
line1 <- xml_attr(x, "line1")
col1 <- xml_attr(x, "col1")
col2 <- xml_attr(x, "col1")
Lint(
filename = source_file$filename,
line_number = as.integer(line1),
column_number = as.integer(col1),
type = "warning",
message = paste0(
"Avoid ", f1, ":", f2, " expressions, use seq_len."),
line = source_file$lines[line1],
ranges = list(c(as.integer(col1), as.integer(col2)))
)
}
)
})

#' @importFrom lintr Lint

dangerous_functions_linter <- function(source_file, funcs, type,
Expand Down
2 changes: 1 addition & 1 deletion R/prep_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ linters_to_lint <- list(
setwd_linter = setwd_linter(),
sapply_linter = sapply_linter(),
library_require_linter = library_require_linter(),
seq_linter = seq_linter()
seq_linter = lintr::seq_linter()
)

#' @include lists.R
Expand Down
17 changes: 0 additions & 17 deletions man/seq_linter.Rd

This file was deleted.

0 comments on commit 80d6482

Please sign in to comment.