Skip to content

Commit

Permalink
remove lint
Browse files Browse the repository at this point in the history
  • Loading branch information
radbasa committed Jan 11, 2024
1 parent daf8a8a commit 976d3ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
30 changes: 15 additions & 15 deletions R/linter_box_trailing_commas_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
#' Checks that all `box:use` imports have a trailing comma. This applies to
#' package or module imports between `(` and `)`, and function imports between
#' `[` and `]`. Take note that `lintr::commas_linter()` may come into play.
#'
#'
#' @examples
#' # will produce lints
#' lintr::lint(
#' text = "box::use(base, rlang)",
#' linters = box_trailing_commas_linter()
#' )
#'
#'
#' lintr::lint(
#' text = "box::use(
#' dplyr[select, mutate],
#' ),
#' linters = box_trailing_commas_linter()
#' )
#'
#'
#' # okay
#' linter::lint(
#' text = "box::use(base, rlang,),
#' linters = box_trailing_commas_linter()
#' )
#'
#'
#' linter::lint(
#' text = "box::use(
#' dplyr[select, mutate,],
#' ),
#' linters = box_trailing_commas_linter()
#' )
#'
#'
#' @export
box_trailing_commas_linter <- function() {
base_xpath <- "//SYMBOL_PACKAGE[
Expand All @@ -41,16 +41,16 @@ box_trailing_commas_linter <- function() {
]
/parent::expr
"

right_paren_xpath <- paste(
base_xpath,
"/following-sibling::OP-RIGHT-PAREN[
preceding-sibling::*[1][not(self::OP-COMMA)]
]
"
)
right_bracket_xpath <-paste(

right_bracket_xpath <- paste(
base_xpath,
"/parent::expr
/descendant::OP-RIGHT-BRACKET[
Expand All @@ -63,31 +63,31 @@ box_trailing_commas_linter <- function() {
]
"
)

lintr::Linter(function(source_expression) {
if (!lintr::is_lint_level(source_expression, "file")) {
return(list())
}

xml <- source_expression$full_xml_parsed_content

bad_right_paren_expr <- xml2::xml_find_all(xml, right_paren_xpath)
bad_right_bracket_expr <- xml2::xml_find_all(xml, right_bracket_xpath)

paren_lints <- lintr::xml_nodes_to_lints(
bad_right_paren_expr,
source_expression = source_expression,
lint_message = "Always have a trailing comma at the end of imports, before a `)`.",
type = "style"
)

bracket_lints <- lintr::xml_nodes_to_lints(
bad_right_bracket_expr,
source_expression = source_expression,
lint_message = "Always have a trailing comma at the end of imports, before a `]`.",
type = "style"
)

c(paren_lints, bracket_lints)
})
}
}
20 changes: 10 additions & 10 deletions tests/testthat/test-linter_box_trailing_commas_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bad_package_function_commas <- "box::use(
],
)"

bad_package_function_commas_inline <- "box::use(stringr[select, mutate],)"
bad_pkg_function_commas_inline <- "box::use(stringr[select, mutate],)"

bad_module_commas <- "box::use(
path/to/file1,
Expand All @@ -51,14 +51,14 @@ bad_module_function_commas <- "box::use(

should_not_lint <- "x <- c(1, 2, 3)"

bad_module_function_commas_inline <- "box::use(path/to/file2[first_function, second_function], )"
bad_mod_function_commas_inline <- "box::use(path/to/file2[first_function, second_function], )"

paren_lint_msg <- rex::rex("Always have a trailing comma at the end of imports, before a `)`.")
bracket_lint_msg <- rex::rex("Always have a trailing comma at the end of imports, before a `]`.")

test_that("box_trailing_commas_linter skips allowed package import usage", {
linter <- box_trailing_commas_linter()

lintr::expect_lint(good_package_commas, NULL, linter)
lintr::expect_lint(good_package_commas_inline, NULL, linter)
lintr::expect_lint(good_module_commas, NULL, linter)
Expand All @@ -67,34 +67,34 @@ test_that("box_trailing_commas_linter skips allowed package import usage", {

test_that("box_trailing_commas_linter blocks no trailing commas in package imports", {
linter <- box_trailing_commas_linter()

lintr::expect_lint(bad_package_commas, list(message = paren_lint_msg), linter)
lintr::expect_lint(bad_package_commas_inline, list(message = paren_lint_msg), linter)
})

test_that("box_trailing_commas_linter blocks no trailing commas in package function imports", {
linter <- box_trailing_commas_linter()

lintr::expect_lint(bad_package_function_commas, list(message = bracket_lint_msg), linter)
lintr::expect_lint(bad_package_function_commas_inline, list(message = bracket_lint_msg), linter)
lintr::expect_lint(bad_pkg_function_commas_inline, list(message = bracket_lint_msg), linter)
})

test_that("box_trailing_comma_linter blocks no trailing commas in module imports", {
linter <- box_trailing_commas_linter()

lintr::expect_lint(bad_module_commas, list(message = paren_lint_msg), linter)
lintr::expect_lint(bad_module_commas_inline, list(message = paren_lint_msg), linter)
})

test_that("box_trailing_commas_linter blocks no trailing commas in module function imports", {
linter <- box_trailing_commas_linter()

lintr::expect_lint(bad_module_function_commas, list(message = bracket_lint_msg), linter)
lintr::expect_lint(bad_module_function_commas_inline, list(message = bracket_lint_msg), linter)
lintr::expect_lint(bad_mod_function_commas_inline, list(message = bracket_lint_msg), linter)
})

test_that("box_trailing_commas_linter should not lint outside of `box::use()`", {
linter <- box_trailing_commas_linter()

lintr::expect_lint(should_not_lint, NULL, linter)
})

0 comments on commit 976d3ac

Please sign in to comment.