Skip to content

Commit

Permalink
Convert todo_comment_linter to XPath approach (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Dec 15, 2023
1 parent 6cbb24c commit f865f94
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
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))
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.

0 comments on commit f865f94

Please sign in to comment.