-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New make_linter_from_xpath() (#2126)
* New make_linter_from_xpath * warning by default * more tests * delint * fix example * pkgdown entry * robust to old stopifnot --------- Co-authored-by: Indrajeet Patil <[email protected]>
- Loading branch information
1 parent
33335e8
commit 2a94dab
Showing
7 changed files
with
111 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#' Create a linter from an XPath | ||
#' | ||
#' @inheritParams xml_nodes_to_lints | ||
#' @inheritParams is_lint_level | ||
#' @param xpath Character string, an XPath identifying R code to lint. | ||
#' See [xmlparsedata::xml_parse_data()] and [get_source_expressions()]. | ||
#' | ||
#' @examples | ||
#' number_linter <- make_linter_from_xpath("//NUM_CONST", "This is a number.") | ||
#' lint(text = "1 + 2", linters = number_linter()) | ||
#' @export | ||
make_linter_from_xpath <- function(xpath, | ||
lint_message, | ||
type = c("warning", "style", "error"), | ||
level = c("expression", "file")) { | ||
type <- match.arg(type) | ||
level <- match.arg(level) | ||
|
||
stopifnot( | ||
"xpath should be a character string" = is.character(xpath) && length(xpath) == 1L && !is.na(xpath) | ||
) | ||
|
||
xml_key <- if (level == "expression") "xml_parsed_content" else "full_xml_parsed_content" | ||
|
||
function() { | ||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, level)) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression[[xml_key]] | ||
|
||
expr <- xml_find_all(xml, xpath) | ||
|
||
xml_nodes_to_lints( | ||
expr, | ||
source_expression = source_expression, | ||
lint_message = lint_message, | ||
type = type | ||
) | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,29 @@ | ||
test_that("basic usage works", { | ||
linter <- make_linter_from_xpath("//NUM_CONST", "Number") | ||
expect_type(linter, "closure") | ||
expect_lint("1", list("Number", type = "warning"), linter()) | ||
|
||
expect_lint("'a'", "Letter", make_linter_from_xpath("//STR_CONST", "Letter")()) | ||
expect_lint("'a'", "Letter", make_linter_from_xpath("//STR_CONST", "Letter", level = "file")()) | ||
expect_lint("'a'", list("Letter", type = "style"), make_linter_from_xpath("//STR_CONST", "Letter", type = "style")()) | ||
}) | ||
|
||
test_that("input validation works", { | ||
expect_error( | ||
make_linter_from_xpath("//NUM_CONST", "Number", type = "x"), | ||
'one of "warning", "style", "error"', | ||
fixed = TRUE | ||
) | ||
|
||
expect_error( | ||
make_linter_from_xpath("//NUM_CONST", "Number", level = "x"), | ||
'one of "expression", "file"', | ||
fixed = TRUE | ||
) | ||
|
||
err_msg <- if (getRversion() < "4.0.0") "is.character(xpath)" else "xpath should be a character string" | ||
expect_error(make_linter_from_xpath(FALSE), err_msg, fixed = TRUE) | ||
expect_error(make_linter_from_xpath(letters), err_msg, fixed = TRUE) | ||
expect_error(make_linter_from_xpath(NA_character_), err_msg, fixed = TRUE) | ||
expect_error(make_linter_from_xpath(character()), err_msg, fixed = TRUE) | ||
}) |