Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ffb0f7c
refac: styles as named list of regexes
AshesITR Jun 19, 2022
aa08719
add regexes= argument and strip fewer symbols
AshesITR Jun 19, 2022
d626c2a
do not overwrite formal argument styles
AshesITR Jun 19, 2022
2cf8ca7
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Jun 29, 2022
d978a58
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Jul 2, 2022
6822533
grammar
MichaelChirico Jul 2, 2022
fde0134
Merge branch 'main' into feature/822-object_name-custom-style
AshesITR Jul 11, 2022
8bc3e3c
change behaviour of missing(styles) && !missing(regexes), add tests f…
AshesITR Jul 11, 2022
83209d2
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Jul 27, 2022
c272434
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Jul 27, 2022
faf8404
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Jul 27, 2022
ba8a2ed
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Aug 5, 2022
e981301
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Aug 9, 2022
58d47d3
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Aug 24, 2022
379e459
Merge branch 'main' into feature/822-object_name-custom-style
IndrajeetPatil Oct 8, 2022
2f22a3c
Update test-object_name_linter.R
IndrajeetPatil Oct 8, 2022
c6a6a20
Merge branch 'main' into feature/822-object_name-custom-style
AshesITR Oct 15, 2022
6665985
split object_name_linters into object_length_linter.R and object_name…
AshesITR Oct 15, 2022
a2d445e
document(), add examples with regexes
AshesITR Oct 15, 2022
395b7e4
un-escape quote for readability
MichaelChirico Oct 15, 2022
bccf595
again
MichaelChirico Oct 15, 2022
246b4f1
fix documentation
AshesITR Oct 15, 2022
1abdfdb
re-use nzchar output
MichaelChirico Oct 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions R/object_name_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ object_name_xpath <- local({
#' @param regexes A (possibly named) character vector specifying a custom naming convention.
#' If named, the names will be used in the lint message. Otherwise, "custom" will be used as a name for the style.
#' Quotes (`` `"' ``) and specials (`%` and trailing `<-`) are not considered part of the name to be matched.
#' Note that specifying `regexes` overrides the default `styles`. So if you want to combine `regexes` and `styles`,
#' both need to be explicitly specified.
#' @evalRd rd_tags("object_name_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
object_name_linter <- function(styles = c("snake_case", "symbols"), regexes = character()) {
if (length(styles) > 0L) {
if ((!missing(styles) || missing(regexes)) && length(styles) > 0L) {
# Allow `object_name_linter(NULL, "my_regex")`
styles <- match.arg(styles, names(style_regexes), several.ok = TRUE)
style_list <- style_regexes[styles]
Expand All @@ -62,9 +64,10 @@ object_name_linter <- function(styles = c("snake_case", "symbols"), regexes = ch
if (!is.character(regexes)) {
stop("`regexes` must be a character vector.")
}
if (is.null(names(regexes))) {
names(regexes) <- "custom"
}
rx_names <- names2(regexes)
rx_names[!nzchar(rx_names)] <- "custom"
names(regexes) <- rx_names

style_list <- c(style_list, as.list(regexes))
}
if (length(style_list) == 0L) {
Expand Down
4 changes: 3 additions & 1 deletion man/object_name_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 tests/testthat/test-linter_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test_that("available_linters matches the set of linters available from lintr", {
# ensure that the contents of inst/lintr/linters.csv covers all _linter objects in our namespace
expect_identical(sort(lintr_db$linter), sort(linters_in_namespace))
# ensure that all _linter objects in our namespace are also exported
exported_linters <- grep("_linter$", getNamespaceExports("lintr"), value = TRUE)
exported_linters <- setdiff(grep("_linter$", getNamespaceExports("lintr"), value = TRUE), "is_linter")
expect_identical(sort(linters_in_namespace), sort(exported_linters))
})

Expand Down
47 changes: 34 additions & 13 deletions tests/testthat/test-object_name_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ test_that("object_name_linter won't fail if an imported namespace is unavailable
})

test_that("object_name_linter supports custom regexes", {
# snake_case and symbols also allowed
# disables default styles
linter <- object_name_linter(
regexes = c("shinyModule" = rex(start, lower, zero_or_more(alnum), "UI" %or% "Server", end))
)
msg <- rex::rex("Variable and function name style should be snake_case, symbols or shinyModule.")
msg <- rex::rex("Variable and function name style should be shinyModule.")
linter2 <- object_name_linter(
styles = NULL,
styles = c("snake_case", "symbols"),
regexes = c("shinyModule" = rex(start, lower, zero_or_more(alnum), "UI" %or% "Server", end))
)
msg2 <- rex::rex("Variable and function name style should be shinyModule.")
msg2 <- rex::rex("Variable and function name style should be snake_case, symbols or shinyModule.")

# Can't allow 0 styles
expect_error(
Expand All @@ -184,7 +184,14 @@ test_that("object_name_linter supports custom regexes", {

myBadName <- 20L
"),
list(line_number = 12L, message = msg),
list(
list(line_number = 1L, message = msg),
list(line_number = 2L, message = msg),
# argument "id" is linted if we only allow shinyModule names
list(line_number = 4L, column_number = 24L, message = msg),
list(line_number = 8L, column_number = 28L, message = msg),
list(line_number = 12L, message = msg)
),
linter
)

Expand All @@ -203,14 +210,28 @@ test_that("object_name_linter supports custom regexes", {

myBadName <- 20L
"),
list(
list(line_number = 1L, message = msg2),
list(line_number = 2L, message = msg2),
# argument "id" is linted if we only allow shinyModule names
list(line_number = 4L, column_number = 24L, message = msg2),
list(line_number = 8L, column_number = 28L, message = msg2),
list(line_number = 12L, message = msg2)
),
list(line_number = 12L, message = msg2),
linter2
)

# Default regex naming works
expect_lint(
trim_some("
a <- 42L
b <- 1L
c <- 2L
"),
list(line_number = 3L, message = rex::rex("Variable and function name style should be custom.")),
object_name_linter(regexes = c("^a$", "^b$"))
)

expect_lint(
trim_some("
a <- 42L
b <- 1L
c <- 2L
"),
list(line_number = 3L, message = rex::rex("Variable and function name style should be a or custom.")),
object_name_linter(regexes = c(a = "^a$", "^b$"))
)
})