Skip to content

handle empty arguments in switch statements #499

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 5 commits into from
Jun 23, 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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ importFrom(cyclocomp,cyclocomp)
importFrom(stats,na.omit)
importFrom(utils,capture.output)
importFrom(utils,getParseData)
importFrom(utils,head)
importFrom(utils,relist)
importFrom(utils,tail)
importFrom(xml2,as_list)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* `object_usage_linter` has been changed to ensure lint-position is indicated
relative to the start of the file, rather than the start of a defining
function (#432, @russHyde).
* `commas_linter` now allows spaces to come before a comma when used to denote a
fall-through in a switch statement (#499, @MrMallIronmaker)

# lintr 2.0.0

Expand Down
26 changes: 23 additions & 3 deletions R/commas_linter.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @describeIn linters check that all commas are followed by spaces, but do not
#' have spaces before them.
#' @importFrom utils head
#' @export
commas_linter <- function(source_file) {

Expand All @@ -26,14 +27,33 @@ commas_linter <- function(source_file) {

if (space_before) {

has_token <- any(source_file$parsed_content$line1 == line_number &
source_file$parsed_content$col1 == comma_loc &
comma_loc_filter <- source_file$parsed_content$line1 == line_number &
source_file$parsed_content$col1 == comma_loc

has_token <- any(comma_loc_filter &
source_file$parsed_content$token == "','")

start_of_line <- re_matches(line, rex(start, spaces, ","))

empty_comma <- substr(line, comma_loc - 2L, comma_loc - 1L) %==% ", "
if (has_token && !start_of_line && !empty_comma) {

parent <- source_file$parsed_content$parent
parent <- replace(parent, parent==0, NA)

# a variable that is true for every node who has a grandchild that is switch,
# i.e, any expression that starts with the function call to switch.
switch_grandparents <- source_file$parsed_content[
# as.character allows interpretation as row indexes rather than row numbers
as.character(parent[source_file$parsed_content$text == "switch"]),
]$parent

is_blank_switch <- any(comma_loc_filter &
(source_file$parsed_content$parent %in% switch_grandparents) &
c(NA, head(source_file$parsed_content$token, -1)) == "EQ_SUB",
na.rm=TRUE
)

if (has_token && !start_of_line && !empty_comma && !is_blank_switch) {

lints[[length(lints) + 1L]] <-
Lint(
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-commas_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,41 @@ test_that("returns the correct linting", {
expect_lint("a[1, , 2, , 3]",
NULL,
commas_linter)

expect_lint("switch(op, x = foo, y = bar)",
NULL,
commas_linter)

expect_lint("switch(op, x = foo , y = bar)",
rex("Commas should never have a space before."),
commas_linter)

expect_lint("switch(op, x = , y = bar)",
NULL,
commas_linter)

expect_lint("switch(op, \"x\" = , y = bar)",
NULL,
commas_linter)

expect_lint("switch(op, x = foo , y = bar)",
rex("Commas should never have a space before."),
commas_linter)

expect_lint("switch(op , x = foo, y = bar)",
rex("Commas should never have a space before."),
commas_linter)

expect_lint("fun(op, x = foo , y = switch(bar, a = 4, b = 5))",
rex("Commas should never have a space before."),
commas_linter)

expect_lint("switch(op, x = foo, y = bar(a = 4 , b = 5))",
rex("Commas should never have a space before."),
commas_linter)

expect_lint("switch(op, x = ,\ny = bar)",
NULL,
commas_linter)

})