-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
702 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#' Recommend usage of `call. = FALSE` in conditions | ||
#' | ||
#' This linter, with the default `display_call = FALSE`, enforces the | ||
#' recommendation of the tidyverse design guide regarding displaying error | ||
#' calls. | ||
#' | ||
#' @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 | ||
#' 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) | ||
#' | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "stop('test')", | ||
#' linters = condition_call_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "stop('test', call. = TRUE)", | ||
#' linters = condition_call_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "stop('test', call. = FALSE)", | ||
#' linters = condition_call_linter(display_call = TRUE) | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "stop('this is a', 'test', call. = FALSE)", | ||
#' linters = condition_call_linter(display_call = TRUE) | ||
#' ) | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = "stop('test', call. = FALSE)", | ||
#' linters = condition_call_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "stop('this is a', 'test', call. = FALSE)", | ||
#' linters = condition_call_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "stop('test', call. = TRUE)", | ||
#' linters = condition_call_linter(display_call = TRUE) | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("condition_call_linter") | ||
#' @seealso | ||
#' - [linters] for a complete list of linters available in lintr. | ||
#' - <https://design.tidyverse.org/err-call.html>> | ||
#' @export | ||
condition_call_linter <- function(display_call = FALSE) { | ||
call_xpath <- glue::glue(" | ||
following-sibling::SYMBOL_SUB[text() = 'call.'] | ||
/following-sibling::expr[1] | ||
/NUM_CONST[text() = '{!display_call}'] | ||
") | ||
no_call_xpath <- " | ||
parent::expr[ | ||
count(SYMBOL_SUB[text() = 'call.']) = 0 | ||
] | ||
" | ||
|
||
if (is.na(display_call)) { | ||
frag <- no_call_xpath | ||
} else if (display_call) { | ||
frag <- call_xpath | ||
} else { | ||
# call. = TRUE can be expressed in two way: | ||
# - either explicitly with call. = TRUE | ||
# - or by implicitly relying on the default | ||
frag <- xp_or(call_xpath, no_call_xpath) | ||
} | ||
|
||
xpath <- glue::glue(" | ||
//SYMBOL_FUNCTION_CALL[text() = 'stop' or text() = 'warning'] | ||
/parent::expr[{frag}] | ||
/parent::expr | ||
") | ||
|
||
Linter(linter_level = "expression", function(source_expression) { | ||
|
||
xml <- source_expression$xml_parsed_content | ||
if (is.null(xml)) return(list()) | ||
|
||
bad_expr <- xml_find_all(xml, xpath) | ||
|
||
if (is.na(display_call)) { | ||
msg <- glue::glue( | ||
"Provide an explicit value for call. in {xp_call_name(bad_expr)}()." | ||
) | ||
} else if (display_call) { | ||
msg <- glue::glue( | ||
"Use {xp_call_name(bad_expr)}(.) to display call in error message." | ||
) | ||
} else { | ||
msg <- glue::glue( | ||
"Use {xp_call_name(bad_expr)}(., call. = FALSE)", | ||
" to not display call in error message." | ||
) | ||
} | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = msg, | ||
type = "warning" | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.