Skip to content

Commit

Permalink
Remove '::' from more frequent calls from {rex}, {utils} (#2079)
Browse files Browse the repository at this point in the history
* extend conjunct_test_linter for dplyr::filter()

* move commented code to PR

* another round of ubiquitously used functions without :: qualification

* forgot to branch from main

* one more vestigial

* restore utils::

---------

Co-authored-by: AshesITR <[email protected]>
  • Loading branch information
MichaelChirico and AshesITR authored Aug 11, 2023
1 parent 0e3eb9d commit 3d9e6d7
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 46 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ importFrom(rex,rex)
importFrom(stats,na.omit)
importFrom(utils,capture.output)
importFrom(utils,getParseData)
importFrom(utils,globalVariables)
importFrom(utils,head)
importFrom(utils,relist)
importFrom(utils,tail)
Expand Down
2 changes: 1 addition & 1 deletion R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ NULL
# need to register rex shortcuts as globals to avoid CRAN check errors
rex::register_shortcuts("lintr")

utils::globalVariables(
globalVariables(
c(
"line1", "col1", "line2", "col2", # columns of parsed_content
"id", "parent", "token", "terminal", "text" # ditto
Expand Down
4 changes: 2 additions & 2 deletions R/comment_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ commented_code_linter <- function() {
code_candidates <- re_matches(all_comments, code_candidate_regex, global = FALSE, locations = TRUE)
extracted_code <- code_candidates[, "code"]
# ignore trailing ',' when testing for parsability
extracted_code <- rex::re_substitutes(extracted_code, rex::rex(",", any_spaces, end), "")
extracted_code <- rex::re_substitutes(extracted_code, rex::rex(start, any_spaces, ","), "")
extracted_code <- re_substitutes(extracted_code, rex(",", any_spaces, end), "")
extracted_code <- re_substitutes(extracted_code, rex(start, any_spaces, ","), "")
is_parsable <- which(vapply(extracted_code, parsable, logical(1L)))

lint_list <- xml_nodes_to_lints(
Expand Down
10 changes: 5 additions & 5 deletions R/exclude.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ parse_exclusions <- function(file, exclude = settings$exclude,
return(list())
}

start_locations <- rex::re_matches(lines, exclude_start, locations = TRUE)[, "end"] + 1L
end_locations <- rex::re_matches(lines, exclude_end, locations = TRUE)[, "start"]
start_locations <- re_matches(lines, exclude_start, locations = TRUE)[, "end"] + 1L
end_locations <- re_matches(lines, exclude_end, locations = TRUE)[, "start"]
starts <- which(!is.na(start_locations))
ends <- which(!is.na(end_locations))

Expand All @@ -125,20 +125,20 @@ parse_exclusions <- function(file, exclude = settings$exclude,
for (i in seq_along(starts)) {
excluded_lines <- seq(starts[i], ends[i])
linters_string <- substring(lines[starts[i]], start_locations[starts[i]])
linters_string <- rex::re_matches(linters_string, exclude_linter)[, 1L]
linters_string <- re_matches(linters_string, exclude_linter)[, 1L]

exclusions <- add_exclusions(exclusions, excluded_lines, linters_string, exclude_linter_sep, linter_names)
}
}

nolint_locations <- rex::re_matches(lines, exclude, locations = TRUE)[, "end"] + 1L
nolint_locations <- re_matches(lines, exclude, locations = TRUE)[, "end"] + 1L
nolints <- which(!is.na(nolint_locations))
# Disregard nolint tags if they also match nolint start / end
nolints <- setdiff(nolints, c(starts, ends))

for (i in seq_along(nolints)) {
linters_string <- substring(lines[nolints[i]], nolint_locations[nolints[i]])
linters_string <- rex::re_matches(linters_string, exclude_linter)[, 1L]
linters_string <- re_matches(linters_string, exclude_linter)[, 1L]
exclusions <- add_exclusions(exclusions, nolints[i], linters_string, exclude_linter_sep, linter_names)
}

Expand Down
8 changes: 4 additions & 4 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ defines_knitr_engine <- function(start_lines) {
engines <- names(knitr::knit_engines$get())

# {some_engine}, {some_engine label, ...} or {some_engine, ...}
bare_engine_pattern <- rex::rex(
bare_engine_pattern <- rex(
"{", or(engines), one_of("}", " ", ",")
)
# {... engine = "some_engine" ...}
explicit_engine_pattern <- rex::rex(
explicit_engine_pattern <- rex(
boundary, "engine", any_spaces, "="
)

rex::re_matches(start_lines, explicit_engine_pattern) |
rex::re_matches(start_lines, bare_engine_pattern)
re_matches(start_lines, explicit_engine_pattern) |
re_matches(start_lines, bare_engine_pattern)
}

replace_prefix <- function(lines, prefix_pattern) {
Expand Down
4 changes: 2 additions & 2 deletions R/get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fixup_line <- function(line) {
#'
#' @noRd
lint_parse_error_r43 <- function(e, source_expression) {
msg <- rex::re_substitutes(e$message, rex::rex(" (", except_some_of(")"), ")", end), "")
msg <- re_substitutes(e$message, rex(" (", except_some_of(")"), ")", end), "")
line_number <- e$lineno
column <- e$colno
substr(msg, 1L, 1L) <- toupper(substr(msg, 1L, 1L))
Expand Down Expand Up @@ -621,7 +621,7 @@ fix_eq_assigns <- function(pc) {

eq_assign_locs <- which(pc$token == "EQ_ASSIGN")
# check whether the equal-assignment is the final entry
if (length(eq_assign_locs) == 0L || utils::tail(eq_assign_locs, 1L) == nrow(pc)) {
if (length(eq_assign_locs) == 0L || tail(eq_assign_locs, 1L) == nrow(pc)) {
return(pc)
}

Expand Down
10 changes: 5 additions & 5 deletions R/indentation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al
({xp_or(paste0('descendant::', paren_tokens_left, '[', xp_last_on_line, ']'))})
]/@line1
)]"),
glue::glue("({ global_nodes(infix_tokens) })[{xp_last_on_line}{infix_condition}]"),
glue::glue("({ global_nodes(no_paren_keywords) })[{xp_last_on_line}]"),
glue::glue("
glue("({ global_nodes(infix_tokens) })[{xp_last_on_line}{infix_condition}]"),
glue("({ global_nodes(no_paren_keywords) })[{xp_last_on_line}]"),
glue("
({ global_nodes(keyword_tokens) })
/following-sibling::OP-RIGHT-PAREN[
{xp_last_on_line} and
Expand Down Expand Up @@ -223,9 +223,9 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al
# + if there is no token following ( on the same line, a block indent is required until )
# - binary operators where the second arguments starts on a new line

indent_levels <- rex::re_matches(
indent_levels <- re_matches(
source_expression$file_lines,
rex::rex(start, any_spaces), locations = TRUE
rex(start, any_spaces), locations = TRUE
)[, "end"]
expected_indent_levels <- integer(length(indent_levels))
is_hanging <- logical(length(indent_levels))
Expand Down
6 changes: 3 additions & 3 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings =
stop("'cache' is no longer available as a positional argument; please supply 'cache' as a named argument instead.")
}

needs_tempfile <- missing(filename) || rex::re_matches(filename, rex::rex(newline))
needs_tempfile <- missing(filename) || re_matches(filename, rex(newline))
inline_data <- !is.null(text) || needs_tempfile
lines <- get_lines(filename, text)

Expand Down Expand Up @@ -125,7 +125,7 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings =
lint_dir <- function(path = ".", ...,
relative_path = TRUE,
exclusions = list("renv", "packrat"),
pattern = rex::rex(".", one_of("Rr"), or("", "html", "md", "nw", "rst", "tex", "txt"), end),
pattern = rex(".", one_of("Rr"), or("", "html", "md", "nw", "rst", "tex", "txt"), end),
parse_settings = TRUE) {
if (has_positional_logical(list(...))) {
stop(
Expand Down Expand Up @@ -738,7 +738,7 @@ maybe_append_error_lint <- function(lints, error, lint_cache, filename) {
get_lines <- function(filename, text) {
if (!is.null(text)) {
strsplit(paste(text, collapse = "\n"), "\n", fixed = TRUE)[[1L]]
} else if (rex::re_matches(filename, rex::rex(newline))) {
} else if (re_matches(filename, rex(newline))) {
strsplit(gsub("\n$", "", filename), "\n", fixed = TRUE)[[1L]]
} else {
read_lines(filename)
Expand Down
5 changes: 2 additions & 3 deletions R/lintr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
"_PACKAGE"

## lintr namespace: start
#' @importFrom cyclocomp cyclocomp
#' @importFrom glue glue glue_collapse
#' @importFrom rex rex regex re_matches re_substitutes character_class
#' @importFrom stats na.omit
#' @importFrom utils capture.output head getParseData relist
#' @importFrom utils capture.output getParseData globalVariables head relist tail
#' @importFrom xml2 xml_attr xml_find_all xml_find_chr xml_find_num xml_find_first xml_name xml_text as_list
#' @importFrom cyclocomp cyclocomp
#' @importFrom utils tail
#' @rawNamespace
#' if (getRversion() >= "4.0.0") {
#' importFrom(tools, R_user_dir)
Expand Down
4 changes: 2 additions & 2 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ trim_output <- function(x, max = 65535L) {
# otherwise trim x to the max, then search for the lint starts
x <- substr(x, 1L, max)

re <- rex::rex(
re <- rex(
"[", except_some_of(":"), ":", numbers, ":", numbers, ":", "]",
"(", except_some_of(")"), ")",
space,
Expand All @@ -133,7 +133,7 @@ trim_output <- function(x, max = 65535L) {
except_some_of("\r\n"), newline
)

lint_starts <- rex::re_matches(x, re, global = TRUE, locations = TRUE)[[1L]]
lint_starts <- re_matches(x, re, global = TRUE, locations = TRUE)[[1L]]

# if at least one lint ends before the cutoff, cutoff there, else just use
# the cutoff
Expand Down
4 changes: 2 additions & 2 deletions R/object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) {

pkg_name <- pkg_name(find_package(dirname(source_expression$filename)))

declared_globals <- try_silently(utils::globalVariables(package = pkg_name %||% globalenv()))
declared_globals <- try_silently(globalVariables(package = pkg_name %||% globalenv()))

xml <- source_expression$full_xml_parsed_content

Expand Down Expand Up @@ -93,7 +93,7 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) {

# TODO handle assignment functions properly
# e.g. `not_existing<-`(a, b)
res$name <- rex::re_substitutes(res$name, rex::rex("<-"), "")
res$name <- re_substitutes(res$name, rex("<-"), "")

lintable_symbols <- xml_find_all(fun_assignment, xpath_culprit_symbol)

Expand Down
16 changes: 8 additions & 8 deletions R/shared_constants.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rx_non_active_char <- rex::rex(none_of("^${(.*+?|[\\"))
rx_non_active_char <- rex(none_of("^${(.*+?|[\\"))
rx_static_escape <- local({
rx_char_escape <- rex::rex(or(
rx_char_escape <- rex(or(
group("\\", none_of(alnum)),
group("\\x", between(xdigit, 1L, 2L)),
group("\\", between("0":"7", 1L, 3L)),
Expand All @@ -9,7 +9,7 @@ rx_static_escape <- local({
group("\\U{", between(xdigit, 1L, 8L), "}"),
group("\\U", between(xdigit, 1L, 8L))
))
rx_trivial_char_group <- rex::rex(
rx_trivial_char_group <- rex(
"[",
or(
any,
Expand All @@ -18,21 +18,21 @@ rx_static_escape <- local({
),
"]"
)
rex::rex(or(
rex(or(
capture(rx_char_escape, name = "char_escape"),
capture(rx_trivial_char_group, name = "trivial_char_group")
))
})

rx_static_token <- local({
rex::rex(or(
rex(or(
rx_non_active_char,
rx_static_escape
))
})

rx_static_regex <- paste0("(?s)", rex::rex(start, zero_or_more(rx_static_token), end))
rx_first_static_token <- paste0("(?s)", rex::rex(start, zero_or_more(rx_non_active_char), rx_static_escape))
rx_static_regex <- paste0("(?s)", rex(start, zero_or_more(rx_static_token), end))
rx_first_static_token <- paste0("(?s)", rex(start, zero_or_more(rx_non_active_char), rx_static_escape))

#' Determine whether a regex pattern actually uses regex patterns
#'
Expand Down Expand Up @@ -98,7 +98,7 @@ get_token_replacement <- function(token_content, token_type) {
token_content
}
} else { # char_escape token
if (rex::re_matches(token_content, rex::rex("\\", one_of("^${}().*+?|[]\\<>=:;/_-!@#%&,~")))) {
if (re_matches(token_content, rex("\\", one_of("^${}().*+?|[]\\<>=:;/_-!@#%&,~")))) {
substr(token_content, start = 2L, stop = nchar(token_content))
} else {
eval(parse(text = paste0('"', token_content, '"')))
Expand Down
2 changes: 1 addition & 1 deletion R/unreachable_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ unreachable_code_linter <- function() {
bad_expr <- xml_find_all(xml, xpath)

is_nolint_end_comment <- xml2::xml_name(bad_expr) == "COMMENT" &
rex::re_matches(xml_text(bad_expr), settings$exclude_end)
re_matches(xml_text(bad_expr), settings$exclude_end)

xml_nodes_to_lints(
bad_expr[!is_nolint_end_comment],
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ linter_auto_name <- function(which = -3L) {
if (re_matches(nm, regex)) {
match <- re_matches(nm, regex, locations = TRUE)
nm <- substr(nm, start = 1L, stop = match[1L, "end"])
nm <- re_substitutes(nm, rex::rex(start, alnums, "::"), "")
nm <- re_substitutes(nm, rex(start, alnums, "::"), "")
}
nm
}
Expand Down
10 changes: 5 additions & 5 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,18 @@ settings <- NULL
default_settings <<- list(
linters = default_linters,
encoding = "UTF-8",
exclude = rex::rex("#", any_spaces, "nolint"),
exclude_start = rex::rex("#", any_spaces, "nolint start"),
exclude_end = rex::rex("#", any_spaces, "nolint end"),
exclude_linter = rex::rex(
exclude = rex("#", any_spaces, "nolint"),
exclude_start = rex("#", any_spaces, "nolint start"),
exclude_end = rex("#", any_spaces, "nolint end"),
exclude_linter = rex(
start, any_spaces, ":", any_spaces,
capture(
name = "linters",
zero_or_more(one_or_more(none_of(",.")), any_spaces, ",", any_spaces),
one_or_more(none_of(",."))
), "."
),
exclude_linter_sep = rex::rex(any_spaces, ",", any_spaces),
exclude_linter_sep = rex(any_spaces, ",", any_spaces),
exclusions = list(),
cache_directory = R_user_dir("lintr", "cache"),
comment_token = Sys.getenv("GITHUB_TOKEN", unset = NA) %||% rot(
Expand Down
3 changes: 1 addition & 2 deletions man/lint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d9e6d7

Please sign in to comment.