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

Convert todo_comment_linter to XPath approach #2438

Merged
merged 12 commits into from
Dec 15, 2023
31 changes: 13 additions & 18 deletions R/todo_comment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Check that the source contains no TODO comments (case-insensitive).
#'
#' @param todo Vector of strings that identify TODO comments.
#' @param todo Vector of case-insensitive strings that identify TODO comments.
#'
#' @examples
#' # will produce lints
Expand Down Expand Up @@ -42,23 +42,18 @@
#' @export
todo_comment_linter <- function(todo = c("todo", "fixme")) {
todo_comment_regex <- rex(one_or_more("#"), any_spaces, or(todo))
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
Linter(function(source_expression) {
tokens <- with_id(source_expression, ids_with_token(source_expression, "COMMENT"))
are_todo <- re_matches(tokens[["text"]], todo_comment_regex, ignore.case = TRUE)
tokens <- tokens[are_todo, ]
lapply(
split(tokens, seq_len(nrow(tokens))),
function(token) {
Lint(
filename = source_expression[["filename"]],
line_number = token[["line1"]],
column_number = token[["col1"]],
type = "style",
message = "Remove TODO comments.",
line = source_expression[["lines"]][[as.character(token[["line1"]])]],
ranges = list(c(token[["col1"]], token[["col2"]]))
)
}

Linter(linter_level = "expression", function(source_expression) {
xml <- source_expression$xml_parsed_content

comment_expr <- xml_find_all(xml, "//COMMENT")
are_todo <- re_matches(xml_text(comment_expr), todo_comment_regex, ignore.case = TRUE)

xml_nodes_to_lints(
comment_expr[are_todo],
source_expression = source_expression,
lint_message = "Remove TODO comments.",
type = "style"
)
})
}
2 changes: 1 addition & 1 deletion man/todo_comment_linter.Rd

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

Loading