Skip to content

fix symbols style #698

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

Merged
merged 2 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* `equals_na_linter()` now lints `x != NA` and `NA == x`, and skips usages in comments (#545, @michaelchirico)
* Malformed Rmd files now cause a lint instead of an error (#571, #575, @AshesITR)
* `object_name_linter()` gains a new default style, `"symbols"`, which won't lint all-symbol object names
(in particular, that means operator names like `%+%` are skipped; #615, #670, @michaelchirico and @AshesITR)
(in particular, that means operator names like `%+%` are skipped; #495, #615, #670, @michaelchirico and @AshesITR)
* `spaces_inside_linter` ignores spaces preceding trailing comments (#636, @michaelchirico)
* `T_and_F_symbol_linter` and `semicolon_terminator_linter` are now part of the default linters
(#517, #612, #683, #684, @AshesITR)
Expand Down
5 changes: 3 additions & 2 deletions R/object_name_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ object_name_linter <- function(styles = c("snake_case", "symbols")) {

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

generics <- strip_names(c(
declared_s3_generics(xml),
Expand Down Expand Up @@ -283,7 +284,7 @@ loweralnum <- rex(one_of(lower, digit))
upperalnum <- rex(one_of(upper, digit))

style_regexes <- list(
"symbols" = rex(start, maybe("."), zero_or_more(none_of(alnum)), end),
"symbols" = rex(start, zero_or_more(none_of(alnum)), end),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty name can occur here because symbols like <- are stripped before passing on to the regex.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels somewhat hacky... but OK for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is the other styles should allow e.g. snake_case<- as a function name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as applying those rules in the same order always works, we're OK, I'm just not 100% sure of that.

"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),
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-object_name_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ test_that("linter ignores some objects", {
expect_lint(".onLoad <- function(...) TRUE", NULL, object_name_linter("snake_case")) # namespace hooks, #500
expect_lint(".First <- function(...) TRUE", NULL, object_name_linter("snake_case")) # namespace hooks
expect_lint("`%++%` <- `+`", NULL, object_name_linter("symbols")) # all-symbol operator
expect_lint("`%<-%` <- `+`", NULL, object_name_linter("symbols")) # all-symbol operator #495
})

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