Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object name operator #621

Merged
merged 16 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
19 changes: 9 additions & 10 deletions R/object_name_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' \Sexpr[stage=render, results=rd]{lintr:::regexes_rd}. A name should
#' match at least one of these styles.
#' @export
object_name_linter <- function(styles = "snake_case") {
object_name_linter <- function(styles = c("snake_case", "symbols")) {

.or_string <- function(xs) {
# returns "<S> or <T>"
Expand Down Expand Up @@ -48,10 +48,8 @@ object_name_linter <- function(styles = "snake_case") {
assignments <- xml2::xml_find_all(xml, xpath)

# Retrieve assigned name
nms <- xml2::xml_text(assignments, "./text()")

# exclude operators if they're only symbols (e.g. %+%), #615
nms <- strip_names(grep("^[`'\"]%[^[:alnum:]]+%[`'\"]$", nms, invert = TRUE, value = TRUE))
nms <- strip_names(
as.character(xml2::xml_find_first(assignments, "./text()")))

generics <- c(
declared_s3_generics(xml),
Expand Down Expand Up @@ -275,11 +273,12 @@ loweralnum <- rex(one_of(lower, digit))
upperalnum <- rex(one_of(upper, digit))

style_regexes <- list(
"CamelCase" = rex(start, maybe("."), upper, zero_or_more(alnum), end),
"camelCase" = rex(start, maybe("."), lower, zero_or_more(alnum), end),
"snake_case" = rex(start, maybe("."), some_of(lower, digit), any_of("_", lower, digit), end),
"SNAKE_CASE" = rex(start, maybe("."), some_of(upper, digit), any_of("_", upper, digit), end),
"dotted.case" = rex(start, maybe("."), one_or_more(loweralnum), zero_or_more(dot, one_or_more(loweralnum)), end),
"symbols" = rex(start, maybe("."), zero_or_more(none_of(alnum)), end),
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
"CamelCase" = rex(start, maybe("."), upper, zero_or_more(alnum), end),
"camelCase" = rex(start, maybe("."), lower, zero_or_more(alnum), end),
"snake_case" = rex(start, maybe("."), some_of(lower, digit), any_of("_", lower, digit), end),
"SNAKE_CASE" = rex(start, maybe("."), some_of(upper, digit), any_of("_", upper, digit), end),
"dotted.case" = rex(start, maybe("."), one_or_more(loweralnum), zero_or_more(dot, one_or_more(loweralnum)), end),
"lowercase" = rex(start, maybe("."), one_or_more(loweralnum), end),
"UPPERCASE" = rex(start, maybe("."), one_or_more(upperalnum), end)
)
Expand Down
61 changes: 31 additions & 30 deletions tests/testthat/test-object_name_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ context("object_name_linter")

test_that("styles are correctly identified", {
styles <- names(style_regexes)
# UpC lowC snake SNAKE dot alllow ALLUP
expect_equivalent(lapply(styles, check_style, nms = "x" ), list(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = ".x" ), list(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X" ), list( TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "x." ), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X." ), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "x_" ), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X_" ), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "xy" ), list(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "xY" ), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "Xy" ), list( TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "XY" ), list( TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "x1" ), list(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X1" ), list( TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "x_y"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X_Y"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X.Y"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "x_2"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X_2"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "x.2"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
# symbl UpC lowC snake SNAKE dot alllow ALLUP
expect_equivalent(lapply(styles, check_style, nms = "x" ), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = ".x" ), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X" ), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "x." ), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X." ), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "x_" ), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X_" ), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "xy" ), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "xY" ), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "Xy" ), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "XY" ), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "x1" ), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X1" ), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "x_y"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X_Y"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X.Y"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "x_2"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X_2"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "x.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "X.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))


# UpC lowC snake SNAKE dot alllow ALLUP
expect_equivalent(lapply(styles, check_style, nms = "IHave1Cat" ), c( TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "iHave1Cat" ), c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "i_have_1_cat" ), c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "I_HAVE_1_CAT" ), c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "i.have.1.cat" ), c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "ihave1cat" ), c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "IHAVE1CAT" ), c( TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "I.HAVE_ONECAT"), c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
# symbl UpC lowC snake SNAKE dot alllow ALLUP
expect_equivalent(lapply(styles, check_style, nms = "IHave1Cat" ), c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "iHave1Cat" ), c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "i_have_1_cat" ), c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "I_HAVE_1_CAT" ), c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "i.have.1.cat" ), c(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "ihave1cat" ), c(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_equivalent(lapply(styles, check_style, nms = "IHAVE1CAT" ), c(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_equivalent(lapply(styles, check_style, nms = "I.HAVE_ONECAT"), c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
})
AshesITR marked this conversation as resolved.
Show resolved Hide resolved

test_that("linter ignores some objects", {
Expand All @@ -46,6 +46,7 @@ test_that("linter ignores some objects", {
expect_lint("print.foo <- t", NULL, object_name_linter("CamelCase")) # S3 generic
expect_lint("names.foo <- t", NULL, object_name_linter("CamelCase")) # int generic
expect_lint("sapply(x,f,USE.NAMES=T)", NULL, object_name_linter("snake_case")) # defined elsewhere
expect_lint("`%++%` <- `+`", NULL, object_name_linter("symbols")) # all-symbol operator
})

test_that("linter returns correct linting", {
Expand Down