Skip to content

Commit

Permalink
re_matches_logical helper ensures logical output of re_matches_logical
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Oct 27, 2024
1 parent 5bc4dbe commit 32a2229
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
8 changes: 1 addition & 7 deletions R/expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,10 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
)
# deparse ensures that NULL, list(), etc are handled gracefully
ok <- if (field == "message") {
re_matches(value, check)
re_matches_logical(value, check)
} else {
isTRUE(all.equal(value, check))
}
if (!is.logical(ok)) {
cli_abort(c(
x = "Invalid regex result. Did you mistakenly have a capture group in the regex?",
i = "You can match parentheses with a character class, i.e. inside `[]`."
))
}
testthat::expect(ok, msg)
})
},
Expand Down
2 changes: 1 addition & 1 deletion R/lintr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @importFrom cli cli_inform cli_abort cli_warn
#' @importFrom glue glue glue_collapse
#' @importFrom rex rex regex re_matches re_substitutes character_class
#' @importFrom stats na.omit
#' @importFrom stats complete.cases na.omit
#' @importFrom tools R_user_dir
#' @importFrom utils capture.output getParseData getTxtProgressBar globalVariables head relist
#' setTxtProgressBar tail txtProgressBar
Expand Down
7 changes: 1 addition & 6 deletions R/object_name_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,8 @@ object_name_linter <- function(styles = c("snake_case", "symbols"), regexes = ch
}

check_style <- function(nms, style, generics = character()) {
conforming <- re_matches(nms, style)
conforming <- re_matches_logical(nms, style)

# style has capture group(s)
if (is.data.frame(conforming)) {
# if any group is missing, all groups are missing, so just check the first column
conforming <- !is.na(conforming[[1L]])
}
# mark empty or NA names as conforming
conforming <- is.na(nms) | !nzchar(nms) | conforming

Expand Down
3 changes: 2 additions & 1 deletion R/return_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ return_linter <- function(
xml <- source_expression$xml_parsed_content
if (defer_except) {
assigned_functions <- xml_text(xml_find_all(xml, function_name_xpath))
except <- union(except, assigned_functions[re_matches(assigned_functions, except_regex)])
except <-
union(except, assigned_functions[re_matches_logical(assigned_functions, except_regex)])
except_xpath <- glue(except_xpath_fmt, except = except)
body_xpath <- glue(body_xpath_fmt, except_xpath = except_xpath)
}
Expand Down
4 changes: 2 additions & 2 deletions R/todo_comment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ todo_comment_linter <- function(todo = c("todo", "fixme"), except_regex = NULL)

comment_expr <- xml_find_all(xml, "//COMMENT")
comment_text <- xml_text(comment_expr)
invalid_todo <- re_matches(comment_text, todo_comment_regex, ignore.case = TRUE)
invalid_todo <- re_matches_logical(comment_text, todo_comment_regex, ignore.case = TRUE)
if (!is.null(valid_todo_regex)) {
invalid_todo <- invalid_todo & !re_matches(comment_text, valid_todo_regex)
invalid_todo <- invalid_todo & !re_matches_logical(comment_text, valid_todo_regex)
}

xml_nodes_to_lints(
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 @@ -130,7 +130,7 @@ unreachable_code_linter <- function(allow_comment_regex = getOption("covr.exclud

drop_valid_comments <- function(expr, valid_comment_re) {
is_valid_comment <- xml2::xml_name(expr) == "COMMENT" &
re_matches(xml_text(expr), valid_comment_re)
re_matches_logical(xml_text(expr), valid_comment_re)
expr[!is_valid_comment]
}

Expand Down
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ release_bullets <- function() {
platform_independent_order <- function(x) order(tolower(x), method = "radix")
platform_independent_sort <- function(x) x[platform_independent_order(x)]

#' re_matches with type-stable logical output
#' TODO(r-lib/rex#94): Use re_matches() option directly & deprecate this.
#' @noRd
re_matches_logical <- function(x, regex, ...) {
res <- re_matches(x, regex, ...)
if (is.data.frame(res)) {
res <- complete.cases(res)
}
res
}

#' Extract text from `STR_CONST` nodes
#'
#' Convert `STR_CONST` `text()` values into R strings. This is useful to account for arbitrary
Expand Down

0 comments on commit 32a2229

Please sign in to comment.