Skip to content

Commit

Permalink
Merge branch 'main' into anyna-in
Browse files Browse the repository at this point in the history
  • Loading branch information
AshesITR authored Dec 15, 2023
2 parents bf5ece3 + f865f94 commit c62d351
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 104 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"]]))
)
}
)
})
}
6 changes: 3 additions & 3 deletions R/condition_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#'
#' @param display_call Logical specifying expected behaviour regarding `call.`
#' argument in conditions.
#' - `NA` forces providing `call.=` but ignores its value (this can be used in
#' - `NA` forces providing `call. =` but ignores its value (this can be used in
#' cases where you expect a mix of `call. = FALSE` and `call. = TRUE`)
#' - lints `call. = FALSE`
#' - forces `call. = FALSE` (lints `call. = TRUE` or missing `call.=` value)
#' - `TRUE` lints `call. = FALSE`
#' - `FALSE` forces `call. = FALSE` (lints `call. = TRUE` or missing `call. =` value)
#'
#'
#' @examples
Expand Down
59 changes: 59 additions & 0 deletions R/todo_comment_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' TODO comment linter
#'
#' Check that the source contains no TODO comments (case-insensitive).
#'
#' @param todo Vector of case-insensitive 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(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 R/use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#'
#' * `tidyverse` creates a minimal lintr config, based on the default linters ([linters_with_defaults()]).
#' These are suitable for following [the tidyverse style guide](https://style.tidyverse.org/).
#' * `full` creates a lintr config using all available linters via [linters_with_tags()].
#' * `full` creates a lintr config using all available linters via [all_linters()].
#'
#' @return Path to the generated configuration, invisibly.
#'
Expand Down
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.

6 changes: 3 additions & 3 deletions man/condition_call_linter.Rd

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

4 changes: 2 additions & 2 deletions man/todo_comment_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/use_lintr.Rd

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

0 comments on commit c62d351

Please sign in to comment.