Skip to content

Commit

Permalink
Split comment_linters into two separate files for each linter (#2437)
Browse files Browse the repository at this point in the history
* split out todo

* split out commented_code

* remove todo_comment_linter

* remove commented_code_linter

* restore
  • Loading branch information
MichaelChirico authored Dec 14, 2023
1 parent 317429e commit fa92c47
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 95 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Collate:
'cache.R'
'class_equals_linter.R'
'commas_linter.R'
'comment_linters.R'
'commented_code_linter.R'
'comments.R'
'comparison_negation_linter.R'
'condition_call_linter.R'
Expand Down Expand Up @@ -184,6 +184,7 @@ Collate:
'strings_as_factors_linter.R'
'system_file_linter.R'
'terminal_close_linter.R'
'todo_comment_linter.R'
'trailing_blank_lines_linter.R'
'trailing_whitespace_linter.R'
'tree_utils.R'
Expand Down
118 changes: 26 additions & 92 deletions R/comment_linters.R → R/commented_code_linter.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
ops <- list(
"+",
# "-",
"=",
"==",
"!=",
"<=",
">=",
"<-",
"<<-",
"<",
">",
"->",
"->>",
"%%",
"/",
"^",
"*",
"**",
"|",
"||",
"&",
"&&",
rex("%", except_any_of("%"), "%")
)

#' Commented code linter
#'
#' Check that there is no commented code outside roxygen blocks.
Expand Down Expand Up @@ -60,6 +34,32 @@ ops <- list(
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
commented_code_linter <- function() {
ops <- list(
"+",
# "-",
"=",
"==",
"!=",
"<=",
">=",
"<-",
"<<-",
"<",
">",
"->",
"->>",
"%%",
"/",
"^",
"*",
"**",
"|",
"||",
"&",
"&&",
rex("%", except_any_of("%"), "%")
)

code_candidate_regex <- rex(
some_of("#"),
any_spaces,
Expand Down Expand Up @@ -117,69 +117,3 @@ parsable <- function(x) {
res <- try_silently(parse(text = x))
!inherits(res, "try-error")
}


#' TODO comment linter
#'
#' Check that the source contains no TODO comments (case-insensitive).
#'
#' @param todo Vector of strings that identify TODO comments.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "x + y # TODO",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "pi <- 1.0 # FIXME",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "x <- TRUE # hack",
#' linters = todo_comment_linter(todo = c("todo", "fixme", "hack"))
#' )
#'
#' # okay
#' lint(
#' text = "x + y # my informative comment",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "pi <- 3.14",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "x <- TRUE",
#' linters = todo_comment_linter()
#' )
#'
#' @evalRd rd_tags("todo_comment_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @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"]]))
)
}
)
})
}
64 changes: 64 additions & 0 deletions R/todo_comment_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' TODO comment linter
#'
#' Check that the source contains no TODO comments (case-insensitive).
#'
#' @param todo Vector of strings that identify TODO comments.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "x + y # TODO",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "pi <- 1.0 # FIXME",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "x <- TRUE # hack",
#' linters = todo_comment_linter(todo = c("todo", "fixme", "hack"))
#' )
#'
#' # okay
#' lint(
#' text = "x + y # my informative comment",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "pi <- 3.14",
#' linters = todo_comment_linter()
#' )
#'
#' lint(
#' text = "x <- TRUE",
#' linters = todo_comment_linter()
#' )
#'
#' @evalRd rd_tags("todo_comment_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @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"]]))
)
}
)
})
}
2 changes: 1 addition & 1 deletion man/commented_code_linter.Rd

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

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 fa92c47

Please sign in to comment.