Skip to content
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

rm internal 'seq_linter' for lintr version to fix #172 #176

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions R/my_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,74 +38,6 @@ trailing_semicolon_linter <- function() lintr::Linter(function(source_file) {
)
})

#' 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.